-
Notifications
You must be signed in to change notification settings - Fork 17
/
insert.py
95 lines (72 loc) · 3.33 KB
/
insert.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# MIT Licensed. Copyright (c) 2017
import falcon
from sqlalchemy.orm import Session
from sqlalchemy import exists, and_
from database import engine, Base
from tables import UsersTable, SignaturesTable
from time import ctime
from utilities import getSignatureQuery, getJson, to_bool
from auth import authenticate
def recordUser(session, valueDict):
userRow = None
requiredValues = ["Signature", "Name", "Email", "UserID"]
for value in requiredValues:
if value not in valueDict.keys():
return None
#resp.body = "Error, missing key {}".format(value)
#resp.status = falcon.HTTP_400
# User table values
signature = valueDict.get("Signature")
name = valueDict.get("Name")
email = valueDict.get("Email")
company = valueDict.get("Company")
userID = valueDict.get("UserID")
userExists = True
userRow = session.query(UsersTable).get((signature, userID))
if userRow is None:
userExists = False
userRow = UsersTable(Signature=signature, Name=name, Email=email, Company=company, UserID=userID, InstallDateTime=ctime())
return userRow, userExists
def createSignatureRow(session, valueDict):
signatureRow = None
signature = valueDict.get("Signature")
# Signature table values
installLimit = valueDict.get("InstallLimit")
unlimitedInstalls = to_bool(valueDict.get("UnlimitedInstalls"))
if unlimitedInstalls is True:
signatureRow = SignaturesTable(PrimaryKey=signature, InstallCount=0, InstallLimit=0, UnlimitedInstalls=unlimitedInstalls)
print("Created Signature: {}, with UnlimitedInstalls".format(signature))
elif installLimit is not None:
signatureRow = SignaturesTable(PrimaryKey=signature, InstallCount=0, InstallLimit=installLimit, UnlimitedInstalls=False)
print("Created Signature: {}, with InstallLimit of: {}".format(signature, installLimit))
else:
print("Error creating row with Signature: {}. No valid UnlimitedInstalls or InstallLimit provided".format(signature))
return signatureRow
class Resource(object):
def on_get(self, req, resp):
resp.body = "Accepted!"
resp.status = falcon.HTTP_200
def on_post(self, req, resp):
validRequest = authenticate(req)
if not validRequest:
resp.body = "Invalid username/password"
resp.status = falcon.HTTP_401
return
session = Session(engine)
valueDict = getJson(req)
signatureQuery = getSignatureQuery(req, session)
message = "Unable to add Signature"
resp.status = falcon.HTTP_400
if "Signature" in valueDict.keys() and signatureQuery is None:
signatureRow = createSignatureRow(session, valueDict)
message = "Unable to create signature row"
if signatureRow is not None:
session.add(signatureRow)
message = "Added signature to database: {}".format(signatureRow.PrimaryKey)
resp.status = falcon.HTTP_200
elif "Signature" in valueDict.keys():
message = "Unable to add Signature, already exists in database"
resp.body = message
print(message)
session.commit()
session.close()