view restService/getPurls.py @ 30:bcd8076ff7ec

random selection of entries bug fixes
author dwinter
date Wed, 05 Jun 2013 17:37:09 +0200
parents f748e2b684c9
children ea7017439ab9
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')
      
        authreq = False
        if auth is None:#no authentification needed, SHOULD BE DONE VIA APACHE!!
            #authreq = True
            username = "internal user (Please replace with your username)"
        else:

            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)
        

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)