view restService/getPurls.py @ 19:cce127a28fc9

added getpurls
author dwinter
date Wed, 21 Nov 2012 15:39:08 +0100
parents
children f748e2b684c9
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

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')
      
        authreq = False
        if auth is None:
            authreq = True
        else:
            print auth
            auth = re.sub('^Basic ','',auth)
            username,password = base64.decodestring(auth).split(':')
            
            
        if authreq:
            web.header('WWW-Authenticate','Basic realm="Auth example"')
            web.ctx.status = '401 Unauthorized'
            return
        
        return self.render.registerPurls(username)
    
    def POST(self):
        inp = web.input()
        
        username=inp.get("userName",None)
        amount=inp.get("amount",None)
        
        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>10:
            raise web.badrequest("Amount is to large (maximum 10)")
        
        purls=[]
        for i in range(amount):
            purls.append(self.purlManager.register(user=username))
        
        
        return self.render.registeredPurlsResponse(purls)