-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
161 lines (145 loc) · 3.93 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import logging
import logging.config
from crypto import AES_Cipher
from database import Database
from flask import Flask, render_template, request
'''
loading the config
'''
def load_form(filename: str = "C:\\Users\\Sreenath\\Desktop\\n1\\pyjamas_forms\\pyjamas_config.json"):
with open(filename, "r") as f:
return json.load(f)
try:
pyjamas_config = load_form()
except Exception as e:
pyjamas_config = None
'''
create the database
'''
db = Database(pyjamas_config["db_name"] + ".csv") if pyjamas_config else None
app = Flask(__name__)
'''
create a logger instance for logging purposes
'''
@app.before_first_request
def handle_logger():
import os
root = os.path.dirname(os.path.abspath(__file__))
logdir = os.path.join(root, 'logs')
if not os.path.exists(logdir):
os.mkdir(logdir)
log_file = os.path.join(logdir, 'app.log')
handler = logging.FileHandler(log_file)
app.logger.addHandler(handler)
app.logger.setLevel(logging.DEBUG)
'''
logging each request
'''
@app.before_request
def log_to_file():
app.logger.debug("Request received: %s %s", request.method, request.url)
'''
load the configuration file and render the index template
'''
@app.route("/", methods=["GET"])
def index():
try:
pyjamas_config = load_form()
except:
pyjamas_config = None
if pyjamas_config is not None:
return render_template(
"index.html",
form_name=pyjamas_config["form_fields"],
fields=pyjamas_config["form_fields"].keys(),
)
else:
return render_template("index.html")
# @app.route("/home",methods=["GET"])
# def home():
# return render_template("home.html")
'''
get a form response and encrypts the fields that are to be encrypted
writes the processed data to the database
'''
@app.route("/submit", methods=["POST"])
def submit():
try:
pyjamas_config = load_form()
except:
pyjamas_config = None
row = {}
print(request.form)
for form_field, config in pyjamas_config["form_fields"].items():
data = request.form[form_field]
if config["isEncrypted"]:
data = AES_Cipher.encrypt(data)
row[form_field] = data
db.write(row)
return {"Submitted": True}
'''
gets a form response to be searched for in the database
decrypts the rows in database based on encryption key and matches to form response
returns the row if found
'''
@app.route("/find", methods=["POST"])
def find():
try:
pyjamas_config = load_form()
except:
pyjamas_config = None
try:
return db.find(
{
key: {
"data": data,
"isEncrypted": pyjamas_config["form_fields"][key]["isEncrypted"],
}
for key, data in request.form.items()
},pyjamas_config
)
except Exception as e:
print("error", e)
return "Not Found"
'''
renders a ui for searching in the database with the appropriate primary keys
'''
@app.route("/lookup", methods=["GET"])
def lookup():
try:
pyjamas_config = load_form()
except Exception as e:
print(e)
pyjamas_config = None
return render_template(
"lookup.html",
form_name=pyjamas_config["form_name"],
fields=[
key
for key, config in pyjamas_config["form_fields"].items()
if config["primaryKey"]
],
)
'''
renders a ui for inserting a row in database
'''
@app.route("/insert",methods=["GET"])
def insert():
try:
pyjamas_config = load_form()
except Exception as e:
print(e)
pyjamas_config = None
return render_template(
"submit.html",
form_name=pyjamas_config["form_name"],
fields=
list(dict(pyjamas_config["form_fields"].items()).keys())
,
)
'''
runs the application
'''
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80, debug=True)