comparison restService/getPurls.py @ 19:cce127a28fc9

added getpurls
author dwinter
date Wed, 21 Nov 2012 15:39:08 +0100
parents
children f748e2b684c9
comparison
equal deleted inserted replaced
18:1eb5e3f6444b 19:cce127a28fc9
1 '''
2 Register purls at the server
3 Created on 22.11.2012
4
5 @author: dwinter
6 '''
7
8 import web
9 import base64
10 import re
11 import config
12 from managePurls.manageIndexMetaPURLs import IndexMetaPURLManager
13
14 class getPurls:
15
16 def __init__(self):
17 self.render = web.template.render(config.TEMPLATE_PATH)
18 self.purlManager = IndexMetaPURLManager()
19
20 def GET(self):
21
22 auth = web.ctx.env.get('HTTP_AUTHORIZATION')
23
24 authreq = False
25 if auth is None:
26 authreq = True
27 else:
28 print auth
29 auth = re.sub('^Basic ','',auth)
30 username,password = base64.decodestring(auth).split(':')
31
32
33 if authreq:
34 web.header('WWW-Authenticate','Basic realm="Auth example"')
35 web.ctx.status = '401 Unauthorized'
36 return
37
38 return self.render.registerPurls(username)
39
40 def POST(self):
41 inp = web.input()
42
43 username=inp.get("userName",None)
44 amount=inp.get("amount",None)
45
46 if (username==None) or (amount==None):
47 raise web.badrequest("Username and amount have to be send!")
48
49 try:
50 amount=int(amount)
51 except:
52 raise web.badrequest("Amount is not an integer!")
53
54 if amount>10:
55 raise web.badrequest("Amount is to large (maximum 10)")
56
57 purls=[]
58 for i in range(amount):
59 purls.append(self.purlManager.register(user=username))
60
61
62 return self.render.registeredPurlsResponse(purls)
63