view restService/getPurls.py @ 40:671dd1e4bd09 default tip

minor bug
author dwinter
date Wed, 05 Mar 2014 10:20:54 +0100
parents 6d0d7f1c11f2
children
line wrap: on
line source

'''
Register purls at the server
Created on 22.11.2012

@author: dwinter
'''

import web
import base64
import re
import config
from managePurls.manageIndexMetaPURLs import IndexMetaPURLManager
import json

class getPurls:
    
    def __init__(self):
        self.render = web.template.render(config.TEMPLATE_PATH)
        self.purlManager = IndexMetaPURLManager()
        
    def GET(self):
        
        auth = web.ctx.env.get('HTTP_AUTHORIZATION')
      
        params = web.input();
        
        authreq = False
        if auth is None:#no authentification needed, SHOULD BE DONE VIA APACHE!!
            #authreq = True
            
            
            if params.get("user",None) is not None:
                username = params.get("user")
            else:
                username = "internal user (Please replace with your username)"
            
            
            
        else:

            auth = re.sub('^Basic ','',auth)
            username,password = base64.decodestring(auth).split(':')
            
        
        if params.get("number",None) is not None:
            amount = params.get("number")
        else:
            amount =1
            
            
        if authreq:
            web.header('WWW-Authenticate','Basic realm="Auth example"')
            web.ctx.status = '401 Unauthorized'
            return
        
        return self.render.registerPurls(username,amount)
    
    def POST(self):
        inp = web.input()
        
        username=inp.get("userName",None)
        amount=inp.get("amount",None)
        
        outFormat=inp.get("format","html")

        if (username==None) or (amount==None):
            raise web.badrequest("UserName and amount have to be send!")
        
        try:
            amount=int(amount)
        except:
            raise web.badrequest("Amount is not an integer!")
        
        if amount>1000:
            raise web.badrequest("Amount is to large (maximum 10)")
        
        purls=[]
        for i in range(amount):
            purls.append(self.purlManager.register(user=username))
        
        if outFormat == "html":
            return self.render.registeredPurlsResponse(purls)
        elif outFormat == "plain":
            ret=""
            for p in purls:
                ret+="%s\n"%p[1]

            web.header('Content-Type', 'text/plain')
            return ret
        elif outFormat == "plain-noPrefix":
            ret=""
            for p in purls:
                ret+="%s\n"%p[1].replace("MPIWG:","")

            web.header('Content-Type', 'text/plain')
            return ret
        else:
            raise web.badrequest("Allowed parameters for format are only html, plain and plain-noPrefix. The default is html was %s.."%outFormat)
class randomSearch:
    def __init__(self):
      
        self.purlManager = IndexMetaPURLManager()
  
    def GET(self):
        lst = self.purlManager.getExistingRandom(3);
        
        return json.dumps(lst)
         

class lastEntries:
    def __init__(self):
      
        self.purlManager = IndexMetaPURLManager()
  
    def GET(self):
        lst = self.purlManager.getLastEntries(3)
        
        return json.dumps(lst)
    
class dris:
    def __init__(self):
        self.purlManager =IndexMetaPURLManager()
        
    def GET(self):
        
        params = web.input();
      
        limit = params.get("limit",None)
        offset = params.get("offset",None)
        format =params.get("format","json")
      
        lst = self.purlManager.getAllPurls(limit=limit,offset=offset,isDri=True)
        
        if format=="json":
            web.header('Content-Type', 'application/json')
            return json.dumps(lst)
        elif format =="csv":
            web.header('Content-Type', 'text/plain')
            str =""
            for entry in lst:
                str+="%s,%s\n"%(entry['path'],entry['dri'])
                
            return str
        else:
             raise web.badrequest("Allowed parameters for format are only  plain and json. The default is json. Parameter was %s.."%format)