-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
155 lines (112 loc) · 5.41 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
from flask import Flask
from flask_bootstrap import Bootstrap
from flask import render_template, redirect, url_for, request, flash, abort
from flask_sqlalchemy import SQLAlchemy
from markupsafe import Markup
from itsdangerous import URLSafeTimedSerializer
from flask_wtf import FlaskForm
from wtforms import StringField, SelectField, PasswordField, BooleanField, IntegerField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from wtforms.validators import InputRequired, Email, Length, EqualTo, NumberRange
from scipy.stats import gmean, hmean
from numpy import mean
from flask import redirect, url_for, request
from flask_admin import Admin, AdminIndexView
from flask_admin.contrib.sqla import ModelView
from wtforms.validators import InputRequired
from sqlalchemy.exc import IntegrityError
import math
#########################Config
#Here, configuration settings are made, including the app configuration, email and database config, etc.
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Thisissupposedtobesecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
app.config['FLASK_ADMIN_SWATCH'] = 'flatly'
#Flask Extension Area
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
from uuid import uuid4
#########################Database system
#This file holds the database table creation using the sqlalchemy module.
def IdColumn(*args, **kwargs):
return db.Column(db.String, *args, nullable=False,
default=lambda: str(uuid4())
if 'primary_key' in kwargs else None,
**kwargs)
class Meeting(db.Model):
__tablename__ = 'meeting'
meeting_id = IdColumn(primary_key= True)
meeting_name = db.Column(db.String(50))
meeting_date = db.Column(db.DateTime, nullable=True)
def __str__(self):
return self.meeting_name
def __repr__(self):
return self.meeting_name
class Review(db.Model):
__tablename__ = 'review'
review_id = IdColumn(primary_key= True)
meetingreviewed = db.Column(db.String(50), db.ForeignKey(Meeting.meeting_id), nullable=False)
#general
firstname = db.Column(db.String(35))
lastname = db.Column(db.String(35))
email = db.Column(db.String(60), unique=True)
postmeeting_feeling = db.Column(db.Integer, default= 50)
participation_score = db.Column(db.Integer, default= 50)
meeting_id = db.relationship("Meeting")
###########################Admin Dashboard system
#Set up the admin system
class MyView(ModelView):
page_size = 50
column_searchable_list = ['firstname', 'email']
column_filters = ['lastname', 'email']
column_editable_list = ['firstname', 'lastname']
column_list = ['email', "firstname", "lastname", "participation_score", "postmeeting_feeling"]
column_display_pk = False
can_create = False
can_edit = False
class MeetingView(ModelView):
page_size = 50
column_display_pk = False
create_modal = True
edit_modal = True
admin = Admin(app, template_mode = 'bootstrap3', url= '/admin', )
admin.add_view(MyView(Review, db.session))
admin.add_view(MeetingView(Meeting, db.session))
########################Form creation
#Create the forms
class FeedbackForm(FlaskForm):
firstname = StringField('First name', validators=[InputRequired(), Length(min=2, max=35)])
lastname = StringField('Last name', validators=[InputRequired(), Length(min=2, max=35)])
email = StringField('Email address', validators=[InputRequired(), Length(min=4, max=50)])
postmeeting_feeling = IntegerField('How did you feel about this meeting', validators=[InputRequired(), NumberRange(min=0, max=100, message="Please select a number between 1 and 100")])
participation_score = IntegerField("What is your perceived level of participation?", validators=[InputRequired(), NumberRange(min=1, max=100, message="Please select a number between 1 and 100")])
meeting = SelectField()
def __init__(self):
super(FeedbackForm, self).__init__()
self.meeting.choices = [(c.meeting_id, c.meeting_name) for c in Meeting.query.all()]
#Create the routes
def themeans(dlist):
arithmetic_mean = mean(dlist)
geometric_mean = gmean(dlist)
harmonic_mean = hmean(dlist)
return arithmetic_mean, geometric_mean, harmonic_mean
@app.route('/', methods=["GET", "POST"])
@app.route("/feedback", methods = ['GET', "POST"])
def feedback():
form = FeedbackForm()
if form.validate_on_submit():
try:
meetname = Meeting.query.filter_by(meeting_id = form.meeting.data).first()
feedback = Review(firstname=form.firstname.data, lastname=form.lastname.data, email=form.email.data + "+" + meetname.meeting_name, postmeeting_feeling= form.postmeeting_feeling.data, participation_score = form.participation_score.data, meetingreviewed = form.meeting.data)
db.session.add(feedback)
db.session.commit()
flash(Markup("Feedback has been noted. Thanks for reaching out."), "success")
return redirect(url_for('feedback'))
except IntegrityError:
db.session.rollback()
flash("ERROR! Feedback for Email({}) already exists for that particular meeting session.".format(form.email.data), 'error')
return render_template("feedback.html", form = form)
###############################initiate
#This file simply launches the whole app, allowing all the python files to call on themselves where necessary, hence making the app run.
if __name__ =="__main__":
app.run(debug=True)