view restService/getPurls.py @ 35:d3ecbfd21e06

Merge with a25bfc49a068371c555ac034df1a24e349850163
author dwinter
date Wed, 23 Oct 2013 12:28:22 +0200
parents a25bfc49a068
children 6d0d7f1c11f2
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)