This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathci_demo.py
345 lines (269 loc) · 10.5 KB
/
ci_demo.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import os
import random
import flask
import typing
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from functools import wraps
from passlib.apps import custom_app_context as pwd_context
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length
from xhtml2pdf import pisa
from hint import Hint, WorkshopHints
app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL', '')
app.config['SECRET_KEY'] = 'foo-bar'
app.config['CSRF_SESSION_KEY'] = 'foo-bar'
db = SQLAlchemy(app)
workshop_hints = WorkshopHints()
class User(db.Model):
"""
Represents a user in the database.
"""
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(32), unique=True)
password = db.Column(db.String(255), nullable=False)
workshop_step = db.Column(db.Integer, default=1, nullable=False)
hints = relationship("UserHints", back_populates="user")
def is_password_valid(self, password: str) -> bool:
"""
Checks if the entered password matches the stored hash.
:param password: The password to be validated.
:return : Validity of password.
"""
return pwd_context.verify(password, self.password)
def update_password(self, new_password: str) -> None:
"""
Updates the password to a new one.
:param new_password: The new password to be updated
"""
self.password = pwd_context.encrypt(new_password, category='admin')
class UserHints(db.Model):
"""
Keep track of taken hints by a user.
"""
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, ForeignKey("user.id"), primary_key=True)
user = relationship("User", back_populates="hints")
class LoginForm(FlaskForm):
"""
Represents the login form.
"""
name = StringField('Name', [DataRequired(), Length(max=10)])
password = PasswordField('Password', [DataRequired()])
submit = SubmitField('Log in')
class WorkshopForm(FlaskForm):
"""
Holds the buttons to navigate through the workshop.
"""
next = SubmitField('Proceed to next step')
previous = SubmitField('Return one step')
dashboard_images = [
'img/welcomeA.jpg',
'img/welcomeB.jpg',
'img/welcomeC.jpg'
]
workshop_steps = [
'workshop_github.html',
'workshop_codecov.html',
'workshop_heroku.html',
'workshop_travis.html',
'workshop_overview.html',
'workshop_final.html'
]
def login_required(wrapped_method: typing.Callable) -> typing.Callable:
"""
Decorator that redirects to the login page if a user is not logged in.
:param wrapped_method: The method to wrap.
:return:
"""
@wraps(wrapped_method)
def decorated_function(*args, **kwargs):
if flask.g.user is None:
return flask.redirect(flask.url_for('login', next=flask.request.endpoint))
return wrapped_method(*args, **kwargs)
return decorated_function
def get_valid_step(current_step: int, max_step: int) -> int:
"""
Checks if the current step is within boundaries and returns a corrected step.
:param current_step: The current step to check.
:param max_step: The maximum allowed step.
:return: A corrected step between 1 and the maximum step.
"""
if current_step < 1:
current_step = 1
elif current_step > max_step:
current_step = max_step
return current_step
def get_active_hints(user: User, hints: WorkshopHints) -> typing.List[Hint]:
"""
Retrieves the hints that were already activated for this user and workshop step.
:param user: The current user.
:param hints: All available hints.
:return: A list of hints.
"""
visible_hints = [hint.id for hint in user.hints]
return list(filter(lambda h: h.id in visible_hints, hints.get_hints_for_step(user.workshop_step)))
def retrieve_next_hint(user: User, current_step: int, hints: WorkshopHints) -> typing.Optional[Hint]:
"""
Retrieves the next hint (if available) for the user and the current step.
:param user: The current user.
:param current_step: The current step for the user
:param hints: All available hints.
:return: A hint, or None when not found.
"""
used_hints = [hint.id for hint in user.hints]
def filter_criterium(hint):
return hint.id not in used_hints
hints_for_step = list(filter(filter_criterium, hints.get_hints_for_step(current_step)))
hints_for_step.sort(key=lambda h: h.id)
return None if len(hints_for_step) == 0 else hints_for_step[0]
def unlock_all_hints_for_step(current_step: int, user: User, hints: WorkshopHints) -> None:
"""
Unlocks all hints for a user on a certain step.
:param current_step: The current step for the user
:param user: The current user.
:param hints: All available hints.
:return: void.
"""
active_hints_current_step = [h.id for h in get_active_hints(user, hints)]
for hint in hints.get_hints_for_step(current_step):
if hint.id not in active_hints_current_step:
user_hint = UserHints(id=hint.id, user_id=user.id)
db.session.add(user_hint)
db.session.commit()
def get_rendered_block_content(template: str, block: str = "content", **kwargs) -> str:
"""
Retrieves a given block from a given template, and renders it into html.
:param template: The template to retrieve the block from.
:param block: The block of content to retrieve and render.
:param kwargs: Optional arguments to be passed for rendering the block.
:return: The rendered block.
"""
goal_template = app.jinja_env.get_template(template)
goal_block = goal_template.blocks[block]
goal_context = goal_template.new_context(kwargs)
return ''.join(goal_block(goal_context))
@app.before_request
def before_request() -> None:
user_id = flask.session.get('user_id', 0)
flask.g.user = User.query.filter(User.id == user_id).first()
@app.route('/')
def dashboard() -> flask.Response:
"""
Shows the 'dashboard', or the 'index' of this application.
:return:
"""
return flask.render_template('dashboard.html', image=dashboard_images[random.randint(0, len(dashboard_images) - 1)])
@app.route('/login', methods=['GET', 'POST'])
def login() -> flask.Response:
"""
Shows a login page, and optionally processes the login attempt.
:return:
"""
form = LoginForm()
redirect_location = flask.request.args.get('next', '')
if form.validate_on_submit():
# Check if user exists
user = User.query.filter(User.name == form.name.data).first()
if user is None:
user = User(name=form.name.data)
user.update_password(form.password.data)
db.session.add(user)
db.session.commit()
logged_in = True
else:
logged_in = user.is_password_valid(form.password.data)
if logged_in:
flask.session['user_id'] = user.id
if len(redirect_location) == 0:
return flask.redirect("/")
return flask.redirect(flask.url_for(redirect_location))
else:
flask.flash('Wrong username or password', 'error-message')
return flask.render_template('login.html', form=form, next=redirect_location)
@app.route('/workshop')
def workshop() -> flask.Response:
"""
Shows the page that has the goal of the workshop.
:return:
"""
return flask.render_template('workshop.html')
@app.route('/my_workshop', methods=['GET', 'POST'])
@login_required
def my_workshop() -> flask.Response:
"""
Keeps track of the progress of the user throughout steps.
:return:
"""
current_step = flask.g.user.workshop_step
max_step = len(workshop_steps)
form = WorkshopForm()
if form.validate_on_submit():
# Store new step
if form.next.data:
unlock_all_hints_for_step(current_step, flask.g.user, workshop_hints)
current_step += 1
else:
current_step -= 1
current_step = get_valid_step(current_step, max_step)
flask.g.user.workshop_step = current_step
db.session.commit()
return flask.render_template(
workshop_steps[current_step - 1], form=form, current_step=current_step, max_step=max_step,
hints=get_active_hints(flask.g.user, workshop_hints),
maxHintsForStep=len(workshop_hints.get_hints_for_step(current_step))
)
@app.route('/my_workshop/hint', methods=['POST'])
@login_required
def get_hint() -> flask.Response:
current_step = get_valid_step(flask.g.user.workshop_step, len(workshop_steps))
hint = retrieve_next_hint(flask.g.user, current_step, workshop_hints)
if hint is not None:
sorted_hints = sorted(workshop_hints.get_hints_for_step(current_step), key=lambda h: h.id)
nr = sorted_hints.index(hint) + 1
user_hint = UserHints(id=hint.id, user_id=flask.g.user.id)
db.session.add(user_hint)
db.session.commit()
return flask.jsonify(
content=flask.render_template("hint.html", hint=hint, nr=nr),
top=flask.render_template("hint_top.html", hint=hint, nr=nr),
last=(len(sorted_hints) == nr)
)
return flask.jsonify(error="No hints available")
@app.route('/about')
def about() -> flask.Response:
"""
Shows an about page that lists all used libraries.
:return:
"""
return flask.render_template('about.html')
@app.route('/download_pdf')
def download_pdf() -> flask.Response:
"""
Triggers the download of a single page PDF of the worskhop.
:return:
"""
pdf_name = "workshop.pdf"
if not os.path.isfile(pdf_name):
rendered_template = flask.render_template(
"single_page_pdf.html",
goal_block=get_rendered_block_content("workshop.html", ignore=True),
about_block=get_rendered_block_content("about.html"),
steps=[get_rendered_block_content(
step,
block="step_content",
current_step=(workshop_steps.index(step) + 1),
ignore=True
) for step in workshop_steps],
hints=workshop_hints.get_all_hints()
)
with open(pdf_name, "w+b") as fh:
status = pisa.CreatePDF(rendered_template, dest=fh)
if status.err:
flask.abort(400)
return flask.send_file(pdf_name, as_attachment=True, cache_timeout=-1)
if __name__ == '__main__':
app.run()