-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_generator.py
More file actions
269 lines (242 loc) · 12.8 KB
/
comment_generator.py
File metadata and controls
269 lines (242 loc) · 12.8 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
"""
Copyright prolog and class level copyright are included in this utility.
This file is intended for the development of comments.
The user can make changes to the text/prolog-text as appropriate.
This work is licensed under
a Creative Commons Attribution-ShareAlike 3.0 Unported License.
©Thorben, 2021
email: thorbendhaenenstd@gmail.com
This file will create a flask application that displays a web application with the
use of a local database to store data. While the website doesn't require a login,
the login structure is used to let the user agree with the terms before using the tool.
The purpose of the website is to generate comments fast for evaluating other students.
The user has some example comments and requirements but needs to build his own repository
by adding his own comments and requirements.
If the user is going to use this for a university course, then the student needs to be aware
that using your own previous work is considered plagiarism. The user is recommended to
make appropriate changes after the comment is generated.
Using comments from someone else without proper citing is also considered plagiarism.
"""
from flask import Flask, render_template, request, redirect, url_for, session
import dbHelper
import gunicorn
app = Flask(__name__)
# The session object needs a secret key
app.secret_key = "HopKIdf78/*9*PO72xQ89Fg??"
@app.route('/intro', methods=['GET', 'POST'])
def intro():
"""
This function will create the view of the first page.
The user is required to agree with all the 'commandments'
before he can enter the other pages.
It uses the same structure as a login page.
"""
if request.method == 'GET':
return render_template('intro.html')
elif request.method == 'POST':
session['sworn'] = True
return redirect(url_for('main'))
@app.route("/swear_again")
def swear_again():
"""
This function will bring the user back to the terms.
It's not really necessary but more for fun.
"""
session['sworn'] = False
return redirect(url_for('intro'))
@app.route("/", methods=['GET', 'POST'])
def main():
"""
The view of the main page is to generate predefined comments.
It uses the class dbHelper to read all the comments and requirements.
"""
if not session.get('sworn'):
# The conditional will check if the user agreed with the commandments.
return render_template('intro.html')
else:
db_object = dbHelper.Main()
requirements_var = db_object.read_database('requirements')
return render_template('main.html', requirements=requirements_var)
@app.route("/comments", methods=['GET', 'POST'])
def comments():
"""
The view is to display all the comments and the possibility
to remove or update them.
"""
if not session.get('sworn'):
# The conditional will check if the user agreed with the commandments.
return render_template('intro.html')
else:
if request.method == 'POST':
for dic in request.form:
if dic[0:6] == 'update':
request_info = {'template_comment': request.form['template_comment'],
'template_type': request.form['template_type'],
'grade_of_excellence': request.form['grade_of_excellence']}
if 'written_assignment' in request.form:
request_info['written_assignment'] = True
else:
request_info['written_assignment'] = False
if 'programming_assignment' in request.form:
request_info['programming_assignment'] = True
else:
request_info['programming_assignment'] = False
if 'math_assignment' in request.form:
request_info['math_assignment'] = True
else:
request_info['math_assignment'] = False
db_obj = dbHelper.Comment(comment_arg=request_info['template_comment'],
type_arg=request_info['template_type'],
written_arg=request_info['written_assignment'],
math_arg=request_info['math_assignment'],
programming_arg=request_info['programming_assignment'],
grade_of_excellence_arg=request_info['grade_of_excellence'])
db_obj.update_comment(dic[6:])
elif dic[0:6] == 'delete':
comment_obj = dbHelper.Main()
comment_obj.del_comment(id=dic[6:], db_name='comments')
data = dbHelper.Main().read_database(db_name='comments')
return render_template('comments.html', data=data)
@app.route("/requirements", methods=['GET', 'POST'])
def requirements():
"""
The view is to display all the requirements and the possibility
to remove or update them.
"""
if not session.get('sworn'):
# The conditional will check if the user agreed with the commandments.
return render_template('intro.html')
else:
if request.method == 'POST':
for dic in request.form:
if dic[0:6] == 'update':
request_info = {'requirement': request.form['requirement'], 'best': request.form['best'],
'good': request.form['good'], 'bad': request.form['bad'],
'worst': request.form['worst'],
'type': request.form['type']}
if 'written_assignment' in request.form:
request_info['written_assignment'] = True
else:
request_info['written_assignment'] = False
if 'programming_assignment' in request.form:
request_info['programming_assignment'] = True
else:
request_info['programming_assignment'] = False
if 'math_assignment' in request.form:
request_info['math_assignment'] = True
else:
request_info['math_assignment'] = False
db_obj = dbHelper.Requirements(requirement_arg=request_info['requirement'],
best_arg=request_info['best'],
good_arg=request_info['good'], bad_arg=request_info['bad'],
worst_arg=request_info['worst'],
type_arg=request_info['type'],
written_arg=request_info['written_assignment'],
math_arg=request_info['math_assignment'],
programming_arg=request_info['programming_assignment'])
db_obj.update_requirement(dic[6:])
elif dic[0:6] == 'delete':
comment_obj = dbHelper.Main()
comment_obj.del_comment(id=dic[6:], db_name='requirements')
data = dbHelper.Main().read_database(db_name='requirements')
return render_template('requirements.html', data=data)
@app.route("/templates", methods=['GET', 'POST'])
def templates():
"""
This view will show the template page. The function will receive data
and store it in the database.
The comment form will be stored in the comments table
The requirement form will be stored in the requirements table.
"""
if not session.get('sworn'):
# The conditional will check if the user agreed with the commandments.
return render_template('intro.html')
else:
if request.method == "POST":
if 'template_comment' in request.form:
request_info = {'template_comment': request.form['template_comment'],
'template_type': request.form['template_type'],
'grade_of_excellence': request.form['grade_of_excellence']}
if 'written_assignment' in request.form:
request_info['written_assignment'] = True
else:
request_info['written_assignment'] = False
if 'programming_assignment' in request.form:
request_info['programming_assignment'] = True
else:
request_info['programming_assignment'] = False
if 'math_assignment' in request.form:
request_info['math_assignment'] = True
else:
request_info['math_assignment'] = False
db_obj = dbHelper.Comment(comment_arg=request_info['template_comment'],
type_arg=request_info['template_type'],
written_arg=request_info['written_assignment'],
math_arg=request_info['math_assignment'],
programming_arg=request_info['programming_assignment'],
grade_of_excellence_arg=request_info['grade_of_excellence'])
db_obj.add_comment()
elif 'requirement' in request.form:
request_info = {'requirement': request.form['requirement'], 'best': request.form['best'],
'good': request.form['good'], 'bad': request.form['bad'],
'worst': request.form['worst'],
'type': request.form['type']}
if 'written_assignment' in request.form:
request_info['written_assignment'] = True
else:
request_info['written_assignment'] = False
if 'programming_assignment' in request.form:
request_info['programming_assignment'] = True
else:
request_info['programming_assignment'] = False
if 'math_assignment' in request.form:
request_info['math_assignment'] = True
else:
request_info['math_assignment'] = False
db_obj = dbHelper.Requirements(requirement_arg=request_info['requirement'], best_arg=request_info['best'],
good_arg=request_info['good'], bad_arg=request_info['bad'],
worst_arg=request_info['worst'],
type_arg=request_info['type'],
written_arg=request_info['written_assignment'],
math_arg=request_info['math_assignment'],
programming_arg=request_info['programming_assignment'])
db_obj.add_requirement()
return render_template('template_comment.html')
@app.route('/script.js')
def main_script():
"""
This function will render the script that is used for the main and intro view.
This file needs to be rendered because it contains Jinja2 tags.
"""
db_object = dbHelper.Main()
requirements_var = db_object.read_database('requirements')
comments_var = db_object.read_database('comments')
# For this page we only need the 'comment', the 'grade of excellence' and the 'type' column.
comments_dict = {}
for element in comments_var:
if element[2] in comments_dict:
comments_dict[element[2]].append(f"{element[6]}:{element[1]}")
else:
comments_dict[element[2]] = [f"{element[6]}:{element[1]}"]
return render_template('main.js', requirements=requirements_var,
comments_dict=comments_dict)
@app.errorhandler(403)
def forbidden(e):
e_friendly = "a forbidden resource"
return render_template('error.html', e=e, e_friendly=e_friendly), 403
@app.errorhandler(404)
def not_found(e):
e_friendly = "chap, you made a mistake typing that URL"
print(type(e))
return render_template('error.html', e=e, e_friendly=e_friendly), 404
@app.errorhandler(410)
def gone(e):
e_friendly = "The page existed but is deleted and sent to Valhalla for all eternity."
return render_template('error.html', e=e, e_friendly=e_friendly), 410
@app.errorhandler(500)
def internal_server_error(e):
e_friendly = "'server problems' To be overloaded or not to be overloaded. That's the question."
return render_template('error.html', e=e, e_friendly=e_friendly), 500
if __name__ == '__main__':
# Turn debug mode on if you want to troubleshoot.
app.run(debug=False, port="5555")