19
|
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):
|
22
|
21
|
19
|
22 auth = web.ctx.env.get('HTTP_AUTHORIZATION')
|
|
23
|
|
24 authreq = False
|
22
|
25 if auth is None:#no authentification needed, SHOULD BE DONE VIA APACHE!!
|
|
26 #authreq = True
|
|
27 username = "internal user (Please replace with your username)"
|
19
|
28 else:
|
22
|
29
|
19
|
30 auth = re.sub('^Basic ','',auth)
|
|
31 username,password = base64.decodestring(auth).split(':')
|
|
32
|
|
33
|
|
34 if authreq:
|
|
35 web.header('WWW-Authenticate','Basic realm="Auth example"')
|
|
36 web.ctx.status = '401 Unauthorized'
|
|
37 return
|
|
38
|
|
39 return self.render.registerPurls(username)
|
|
40
|
|
41 def POST(self):
|
|
42 inp = web.input()
|
|
43
|
|
44 username=inp.get("userName",None)
|
|
45 amount=inp.get("amount",None)
|
|
46
|
|
47 if (username==None) or (amount==None):
|
|
48 raise web.badrequest("Username and amount have to be send!")
|
|
49
|
|
50 try:
|
|
51 amount=int(amount)
|
|
52 except:
|
|
53 raise web.badrequest("Amount is not an integer!")
|
|
54
|
|
55 if amount>10:
|
|
56 raise web.badrequest("Amount is to large (maximum 10)")
|
|
57
|
|
58 purls=[]
|
|
59 for i in range(amount):
|
|
60 purls.append(self.purlManager.register(user=username))
|
|
61
|
|
62
|
|
63 return self.render.registeredPurlsResponse(purls)
|
|
64
|