-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenAPIKey.py
59 lines (50 loc) · 1.73 KB
/
OpenAPIKey.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import uuid
import OpenAPIConfig
from OpenAPI import APIHandler
from django import forms
from datetime import datetime
class RegisterForm(forms.Form):
apiKeyType = forms.ChoiceField(choices=OpenAPIConfig.API_NAMES, required=True)
name = forms.CharField(label="Your name", required=True)
email = forms.EmailField(label="E-Mail", required=True)
app = forms.CharField(label="Application name", required=True)
url = forms.URLField(label="Application URL", required=True)
class RegisterKey(APIHandler):
enforceAPIKey = False
def methodName(self):
methodName = os.path.basename(self.request.path).split(".")[0]
# The get name is reserved but if you want to have a methodName of get
# name your method _get. It will get translated here.
if methodName == 'key':
if self.request.method == 'GET':
methodName = '_get'
elif self.request.method == 'POST':
methodName = '_post'
return methodName
else:
return super(APIHandler, self).methodName()
def _get(self, args):
form = RegisterForm()
self.render("templates/RegisterKey.html", form=form, key=False)
def _post(self, args):
formArgs = {}
for k,v in args.items(): formArgs[k] = v[0]
form = RegisterForm(data=formArgs)
if form.is_valid():
# do whatever is required for registration
newKey = str(uuid.uuid4())
self.getAPIKeysCollection(formArgs['apiKeyType']).save( {
'apikey': newKey,
'name': formArgs['name'],
'email': formArgs['email'],
'app': formArgs['app'],
'url': formArgs['url'],
'issuedate': datetime.now(),
'timestamp': None,
'count': 0
} )
self.render("templates/RegisterKey.html", form=form, key=newKey)
else:
form = RegisterForm()
self.render("templates/RegisterKey.html", form=form, key=False)