-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_pages.py
More file actions
275 lines (221 loc) · 8.2 KB
/
admin_pages.py
File metadata and controls
275 lines (221 loc) · 8.2 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from flask import Blueprint, render_template, make_response, redirect, url_for, request, flash
from flask_restful import Api, Resource, marshal_with, fields
from flask_jwt_extended import (create_access_token, create_refresh_token,
set_refresh_cookies, set_access_cookies,
jwt_required)
from datetime import datetime as dt
from datetime import timedelta
from admin.models.admin_model import Admin as AdminObject
from admin.models import Admin
from admin.admin_forms import *
from admin.resource_forms import *
from app import db
from passlib.handlers.sha2_crypt import sha512_crypt
from typing import Tuple, Dict
ALGORITHM = sha512_crypt
mod_frontend = Blueprint("frontend",
__name__,
template_folder="templates",
static_folder="static")
api_frontend = Api()
api_frontend.init_app(mod_frontend)
class Signup(Resource):
def get(self):
"""User sign-up form for account creation."""
form = SignupForm()
return render_template("signup.jinja2",
form=form,
template="form-template",
title="Signup Form")
def post(self):
"""
Defines responses for the `/admin/admin_adminprofile`
endpoint
It creates a Admin object from the request body it receives,
checking if all values satisfy the model constraints and then writing
data to the `amin` database.
Parameters:
----------
- name:
in: json body
type: string
required: true
description: Name of the Admin User
- email:
in: json body
type: string
format: email
required: true
description: E-mail id (unique) corresponding to the user
- password:
in : json body
type: string
format: length
required: true
Returns:
-------
json serializable dict
integer response code
responses:
- 200
description: Success registering admin
schema:
type: json
parameters:
- result
type: string
- id
type: integer
- email
type: string
format: email
- 400
description: Error message
schema:
type: json
parameters:
- result
type: string
description: Error message
- 403
description: Admin already registered
"""
"""TODO: delete unnecessary validation code
json_request = request.get_json()
if json_request is None:
return {"result": "Error json body cannot be None."}, 400
required_keys = {"name", "email", "password"}
missed_keys = required_keys.difference(json_request.keys())
if len(missed_keys) != 0:
return (
{
"result": "Values for fields cannot be null",
"required values": list(missed_keys),
},
400,
)"""
form = SignupForm(request.form)
try:
admin = AdminObject.from_dict({
"name": form.name.data,
"email": form.email.data,
"password": form.password.data
})
except ValueError as err:
flash("Error: ".join(err.args))
return render_template("signup.jinja2",
form=form,
template="form-template",
title="Signup Form")
admin_exists = Admin.query.filter_by(email=admin.email.lower()).first()
if admin_exists is None:
password_hash = ALGORITHM.hash(admin.password)
admin = Admin(
email=admin.email.lower(),
password=password_hash,
name=admin.name,
create_dt=dt.now(),
)
db.session.add(admin)
db.session.commit()
flash("Admin created successfully.")
return redirect(url_for("frontend.login"))
else:
#refresh signup and notification of error
flash("Admin already exists.")
return redirect(url_for("frontend.signup"))
class Login(Resource):
"""API to login admin."""
def get(self):
form = LoginForm(request.form)
return render_template("login.jinja2", form=form)
def post(self) -> Tuple[Dict[str, str], int]:
# def post(self) -> Tuple[Dict[str, str], int]:
"""
Defines responses for the `/admin/login/` endpoint.
It allows a registered Admin user to login and receive a jwt, that can
be used to access the other restricted endpoints.
Parameters:
-----------
- email:
in: json body
required: true
type: string
format: email
- password
in: json body
required: true
type: string
Returns:
-------
responses:
- 200
"""
form = LoginForm(request.form)
try:
admin = Admin.query.filter_by(
email=form.email.data.lower()).first()
if admin is None:
return {"result": "Admin does not exist."}, 404
else:
match = ALGORITHM.verify(form.password.data, admin.password)
expiry_time = timedelta(hours=4)
if not match:
return {"result": "Invalid password."}, 401
else:
filter_keys = {"email": form.email.data}
access_token = create_access_token(
identity=filter_keys, expires_delta=expiry_time)
refresh_token = create_refresh_token(identity=filter_keys)
response = make_response(
redirect(url_for("dashboard.admindashboardstats")))
set_access_cookies(response, access_token)
set_refresh_cookies(response, refresh_token)
return response
except KeyError as e:
return {"result": "Key error", "error": str(e)}, 500
class CreateApp(Resource):
@jwt_required
def get(self):
form = DatabaseCreation(request.form)
return render_template("create_app.jinja2", form=form)
class AddTables(Resource):
"""Add tables to the database / resources to the created app"""
@jwt_required
def get(self, app_name):
# check some app name so that you aren't just putting things
form = TableForm(request.form)
return render_template("create_table.jinja2",
form=form,
app_name=app_name)
class AddColumn(Resource):
"""Add table column"""
@jwt_required
def get(self, app_name, table_name):
form = ColumnForm(request.form)
if app_name in DB_DICT:
#todo: add and and condition locking the table name too
form.app_name.app_name = app_name
form.table_name.data = table_name
return render_template(
"create_column.jinja2",
app_name=app_name,
table_name=table_name,
form=form,
)
else:
flash("Please create an app first.")
return render_template("")
class Index(Resource):
def get(self):
return (render_template("landing.jinja2"))
api_frontend.add_resource(CreateApp, "/create_app")
api_frontend.add_resource(Signup, "/signup")
api_frontend.add_resource(Login, "/login")
api_frontend.add_resource(Index, "/")
api_frontend.add_resource(AddTables, "/add_tables/<string:app_name>")
api_frontend.add_resource(
AddColumn, "/add_data_content/<string:app_name>/<string:table_name>")
@api_frontend.representation("text/html")
def out_html(data, code, headers):
return make_response(data)