-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.py
69 lines (51 loc) · 1.84 KB
/
admin.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
"""Handles the http requests and interacts with the database"""
import os
from flask import Flask
import jinja2
from flask import render_template, redirect
import initialize_bd
import tesis_bd
from flask import request
# jinja2 configuration.
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
autoescape=True)
app = Flask(__name__)
@app.route('/admin/', methods=['GET'])
def admin_view():
"""User interface (only shows the token).
:return: An http response with the submitted information.
"""
# Check if the database is empty
doctors = tesis_bd.Doctor.query().fetch()
is_empty = False
if len(doctors) == 0:
is_empty = True
template_dict = {
'bd_is_empty': is_empty,
'doctors': doctors
}
return render_template('admin.html', **template_dict)
@app.route('/admin/dummy/', methods=['GET'])
def create_dummy_database():
"""Creates dummy database (only for testing)"""
# Check if the database is empty
doctors = tesis_bd.Doctor.query().fetch()
if len(doctors) > 0:
return render_template(
'error.html', "The database must be empty in order to create dummy data")
initialize_bd.initialize_bd()
return redirect("/admin/")
@app.route('/admin/adduser/', methods=['POST'])
def add_user():
name = request.form.get('name', '')
email = request.form.get('email', '')
if not name or not email:
return render_template(
'error.html', 'You must provide an email and name in order to create a new user.')
if not initialize_bd.add_user(name, email):
return render_template(
'error.html', 'The user cannot be created.')
return redirect('/admin/')
if __name__ == '__main__':
app.run()