-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
488 lines (407 loc) · 17.8 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import textwrap
import typing
from sqlalchemy import or_, and_, not_, asc, desc, func
from sqlalchemy.orm.exc import MultipleResultsFound
from datetime import datetime, timedelta
from flask import Flask, render_template, request, flash, redirect, session, abort, url_for, make_response, g, jsonify
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_login import LoginManager, login_required, login_user, logout_user, current_user
from flask_debugtoolbar import DebugToolbarExtension
import jinja2
from flaskext.markdown import Markdown
from wtforms import Form, BooleanField, TextField, TextAreaField, PasswordField, RadioField, SelectField, SelectMultipleField, BooleanField, IntegerField, HiddenField, SubmitField, validators, ValidationError, widgets
import diffhtml
import db
app = Flask('translator')
app.config.from_pyfile("config.py")
app.config['SQLALCHEMY_DATABASE_URI'] = app.config['DATABASE']
app.jinja_env.undefined = jinja2.StrictUndefined
db.db.init_app(app)
Markdown(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
login_manager.anonymous_user = db.AnonymousUser
@app.context_processor
def new_template_globals():
def datetime_format(value, format='%d. %m. %Y %H:%M:%S'):
if not value: return "-"
if isinstance(value, str): return value
return value.strftime(format)
return {
'type': type,
'db': db,
'datetime': datetime_format,
'round': round,
'diffhtml': diffhtml,
}
#app.jinja_env.globals['type'] = type
# XXX for some reason, doing this stops the Flask devserver from reloading
# templates. bug?
#app.jinja_env.globals['db'] = db
# inspired by http://flask.pocoo.org/snippets/12/
def flash_errors(form):
for field, errors in form.errors.items():
for error in errors:
flash("Chyba u {}: {}".format(
getattr(form, field).label.text,
error
), 'danger')
@app.before_request
def before_request():
g.sitename = app.config['SITENAME']
g.PRODUCTION = app.config['PRODUCTION']
g.db = db
g.now = datetime.now()
g.yesterday = g.now - timedelta(days=1)
g.tomorrow = g.now + timedelta(days=1)
if current_user.is_authenticated:
if current_user.active == False:
logout_user()
flash("Byli jste odhlášeni protože váš účet byl deaktivován.")
# flask-admin sucks
if request.path.startswith('/admin'):
app.jinja_env.undefined = jinja2.Undefined
else:
app.jinja_env.undefined = jinja2.StrictUndefined
@app.teardown_request
def shutdown_session(exception=None):
db.session.close()
db.session.remove()
@login_manager.user_loader
def load_user(user_id):
return db.session.query(db.User).get(user_id)
@app.route("/")
def index():
projects = db.session.query(db.Project).order_by(db.Project.position.asc()).all()
return render_template("index.html", projects=projects)
@app.route("/<project_identifier>/<category_identifier>/")
def category(project_identifier, category_identifier):
project = db.Project.from_identifier(project_identifier)
if not project: abort(404)
category = db.Category.from_identifier(category_identifier, project=project)
if not category: abort(404)
terms = db.session.query(db.Term).filter(db.Term.category == category,
db.Term.hidden == False).order_by(db.Term.number.asc())
return render_template("category.html", project=project, category=category,
terms=terms)
class SuggestionForm(Form):
text = TextField('Návrh', [validators.DataRequired()])
description = TextField('Popis')
submit = SubmitField('Přidat návrh')
class RevisionForm(Form):
text = TextAreaField('Revize', [validators.DataRequired()])
description = None
submit = SubmitField('Přidat revizi')
class CommentForm(Form):
comment_text = TextAreaField('Komentář', [validators.DataRequired()])
submit = SubmitField('Přidat komentář')
@app.route("/<project_identifier>/<category_identifier>/<term_identifier>/",
methods="GET POST".split())
def term(project_identifier, category_identifier, term_identifier):
# XXX this is a horrible train, make it more concise
project = db.Project.from_identifier(project_identifier)
if not project: abort(404)
category = db.Category.from_identifier(category_identifier, project=project)
if not category: abort(404)
try:
term = db.Term.from_identifier(term_identifier, category=category)
except MultipleResultsFound:
return "Tomuto identifikátoru odpovídá více termínů. Tohle je CHYBA a musí ji opravit administrátor. Můžete pomoci tím že nahlásíte URL."
if not term: abort(404)
if request.args.get("all"):
if current_user.admin:
suggestions_w_score = term.all_suggestions_w_score
else:
abort(403)
else:
suggestions_w_score = term.suggestions_w_score
comments = db.session.query(db.Comment).filter(
db.Comment.term == term, db.Comment.deleted == False) \
.order_by(db.Comment.created.desc()).all()
if term.dialogue:
comments_revs = comments + [r[0] for r in term.revisions_w_score]
comments_revs.sort(key=lambda x: x.created, reverse=True)
comments = comments_revs
if request.method == 'POST':
if 'action' in request.form:
if not current_user.admin: abort(403)
action = request.form['action']
if action == 'unlock':
term.locked = False
elif action == 'lock':
term.locked = True
else:
flash("Neznámá akce {}".format(action), "error")
db.session.commit()
suggestion_form = None
comment_form = None
if current_user.is_authenticated and (not term.locked or current_user.admin):
suggestion_form = (SuggestionForm if not term.dialogue else RevisionForm)(request.form)
comment_form = CommentForm(request.form)
if request.method == 'POST' and suggestion_form.validate():
suggestion_text = suggestion_form.text.data.strip()
if db.session.query(db.Suggestion).filter(
db.Suggestion.text == suggestion_text,
db.Suggestion.term == term,
(db.Suggestion.status == db.SuggestionStatus.approved) \
or (db.Suggestion.status == db.SuggestionStatus.final) ).all():
# TODO this should check for "final" as well, but
# for some reason adding `or (db.Suggestion.status == "final")`
# doesn't cut it
flash("Přesně tenhle návrh už existuje, mrkni se po něm!", 'info')
else:
suggestion = db.Suggestion(user=current_user, term=term,
created=datetime.now(), changed=datetime.now(),
text=suggestion_text,
description=suggestion_form.description.data if suggestion_form.description else None,
status=db.SuggestionStatus.approved)
if term.dialogue:
if term.latest_revision:
if term.latest_revision.revision is None:
suggestion.revision = 2
else:
suggestion.revision = term.latest_revision.revision + 1
else:
suggestion.revision = 1
db.session.add(suggestion)
db.session.commit()
return redirect(term.url)
else:
if term.dialogue and term.latest_revision:
suggestion_form.text.data = term.latest_revision.text
if request.method == 'POST' and comment_form.validate():
comment = db.Comment(user=current_user, term=term,
created=datetime.now(),
text=comment_form.comment_text.data,
deleted=False)
db.session.add(comment)
db.session.commit()
return redirect(term.url)
return render_template("term.html", project=project, category=category, term=term,
suggestion_form=suggestion_form, comment_form=comment_form,
vote_from_for = db.Vote.from_for, comments=comments,
suggestions_w_score=suggestions_w_score)
@app.route("/recent")
def recent():
skip_suggestions = request.args.get("sskip") or 0
skip_comments = request.args.get("cskip") or 0
skip_suggestions = int(skip_suggestions)
skip_comments = int(skip_comments)
suggestions = db.session.query(db.Suggestion) \
.filter(db.Suggestion.created != None) \
.order_by(db.Suggestion.created.desc()).offset(skip_suggestions).limit(100).all()
comments = db.session.query(db.Comment) \
.filter(db.Comment.deleted == False, db.Comment.created != None) \
.order_by(db.Comment.created.desc()).offset(skip_comments).limit(100).all()
changes = [c for c in suggestions + comments]
changes.sort(key=lambda x: x.created, reverse=True)
changes = changes[0:100]
num_suggestions = 0
num_comments = 0
for c in changes:
if isinstance(c, db.Suggestion):
num_suggestions += 1
elif isinstance(c, db.Comment):
num_comments += 1
return render_template("recent.html", changes=changes,
sskip=skip_suggestions, cskip=skip_comments,
num_suggestions=num_suggestions, num_comments=num_comments)
@app.route("/users")
def users():
users = db.session.query(db.User).filter(db.User.active == True).order_by(db.User.id).all()
projects = db.session.query(db.Project).order_by(db.Project.position.asc()).all()
return render_template("users.html", users=users, projects=projects)
@app.route("/vote", methods=["POST"])
@login_required
def vote():
suggestion = db.session.query(db.Suggestion).get(request.form['suggestion_id'])
if not suggestion: abort(404)
if suggestion.term.locked: abort(403)
if suggestion.status not in db.Suggestion.GOOD_STATUSES:
flash("Voting for a suggestion that isn't approved isn't possible.", 'danger')
abort(403)
return redirect(suggestion.url)
vote_num = request.form['vote']
if vote_num not in ["0", "1", "2"]: abort(400)
if suggestion.term.dialogue and vote_num == "2": abort(400)
vote = db.Vote.from_for(current_user, suggestion)
if vote:
db.session.delete(vote)
vote = db.Vote(suggestion=suggestion, user=current_user, vote=vote_num,
valid=True, changed=datetime.now())
db.session.add(vote)
db.session.commit()
if not request.is_xhr:
return redirect(suggestion.url)
else:
return "OK"
@app.route("/make_reference", methods=["POST"])
@login_required
def make_reference():
if not current_user.admin: abort(403)
term0 = db.session.query(db.Term).get(request.form['term0_id'])
term1 = db.session.query(db.Term).get(request.form['term1_id'])
valid = True if request.form['valid']=="1" else False
reference = db.Reference(suggestion=suggestion, user=current_user, vote=vote_num,
valid=True, changed=datetime.now())
db.session.add(vote)
db.session.commit()
@app.route("/suggestion", methods=["POST"])
@login_required
def suggestion():
suggestion = db.session.query(db.Suggestion).get(request.form['suggestion_id'])
if not suggestion: abort(404)
if suggestion.term.locked: abort(403)
action = request.form['action']
ALLOWED_ACTIONS = {
"delete": db.SuggestionStatus.deleted,
"approve": db.SuggestionStatus.approved,
"hide": db.SuggestionStatus.hidden,
"candidate": db.SuggestionStatus.candidate,
"finalize": db.SuggestionStatus.final
}
EXCLUSIVE_ACTIONS = ("finalize",)
EXCLUSIVE_STATUSES = ("final",)
if action in ALLOWED_ACTIONS:
if current_user.admin:
if action in EXCLUSIVE_ACTIONS:
# We need to make sure there's no other final
for s in suggestion.term.suggestions:
if s.status in EXCLUSIVE_STATUSES:
s.status = db.SuggestionStatus.approved
suggestion.status = ALLOWED_ACTIONS[action]
else:
abort(403)
elif action == 'withdraw' and suggestion.user == current_user:
if suggestion.score == 0:
suggestion.status = db.SuggestionStatus.withdrawn
flash("Návrh vzán zpět.", 'success')
else:
flash("Návrh lze vzít zpět pouze dokud má nulové skóre.", 'danger')
else:
flash("Neznámá či nepovolená operace", 'danger')
db.session.commit()
return redirect(suggestion.url)
class LoginForm(Form):
username = TextField('Username', [validators.DataRequired()])
password = PasswordField('Heslo', [validators.DataRequired()])
submit = SubmitField('Přihlásit se')
@app.route("/login", methods="GET POST".split())
def login():
form = LoginForm(request.form)
failed = False
if request.method == 'POST' and form.validate():
user: db.User = db.session.query(db.User).filter(db.User.username == form.username.data.lower()).scalar()
if not user: failed = True
else:
try:
password_matches = user.verify_password(form.password.data)
except ValueError:
flash("Vaše heslo je v nesprávném formátu, požádajete prosím Sankyho o opravu.", 'error')
return redirect(url_for('index'))
if password_matches:
if user.active:
login_user(user, remember=True)
flash("Jste přihlášeni.", 'success')
return redirect(url_for('index'))
else:
flash("Váš účet není aktivován administrátorem.")
else:
failed = True
return render_template("login.html", form=form, failed=failed)
class RegisterForm(Form):
username = TextField('Username', [validators.DataRequired()])
password = PasswordField('Heslo', [
validators.DataRequired(),
validators.EqualTo('confirm_password', message='Hesla se musí shodovat')
])
confirm_password = PasswordField('Heslo znovu', [validators.DataRequired()])
email = TextField('Email', [validators.DataRequired()])
key = TextField('Klíč', [validators.DataRequired()])
submit = SubmitField('Zaregistrovat se')
@app.route("/register", methods="GET POST".split())
def register():
form = RegisterForm(request.form)
failed = False
if request.method == 'POST' and form.validate():
if form.key.data != app.config["REGISTER_KEY"]:
flash("Zadali jste nesprávný registrační klíč.", "danger")
else:
user = db.session.query(db.User).filter(db.User.username == form.username.data.lower()).scalar()
if user:
flash("Toto uživatelské jméno je již zabrané, vyberte si, prosím, jiné.", "danger")
else:
user = db.User(username=form.username.data.lower(),
email=form.email.data,
active=True,
registered=datetime.now(),
seen=datetime.now())
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
if user.id == 1:
user.admin = True
db.session.commit()
login_user(user, remember=True)
flash("Registrace proběhla úspěšně.", 'success')
return redirect(url_for('index'))
else:
flash_errors(form)
return render_template("register.html", form=form)
@app.route("/generate/ladx")
def generate_ladx():
terms: typing.List[db.Term] = db.session.query(db.Term).join(db.Category, db.Project)\
.filter(
db.Project.identifier == "ladx",
db.Term.dialogue == True
)\
.order_by(db.Term.number)\
.all()
out = ""
for term in terms:
suggestion = term.latest_revision
out += f"Dialog{term.number:03X}::\n"
if suggestion:
text = suggestion.text
else:
text = term.text_en
LINE_LENGTH = 16
wrapped = textwrap.wrap(text, width=LINE_LENGTH)
last = wrapped.pop()
for line in wrapped:
out += f" db \"{line.ljust(LINE_LENGTH)}\"\n"
out += f" db \"{last}@\"\n"
out += "\n"
response = make_response(out)
response.mimetype = "text/plain"
return response
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
def create_admin():
admin = Admin(app)
class RestrictedModelView(ModelView):
def is_accessible(self):
return current_user.is_authenticated and current_user.admin
def inaccessible_callback(self, name, **kwargs):
return redirect(url_for('login', next=request.url))
class UserView(RestrictedModelView):
form_excluded_columns = ['password', 'suggestions', 'comments', 'votes']
form_extra_fields = {
'new_password': TextField('New password')
}
def on_model_change(self, form, model, is_created):
if form.new_password.data:
model.set_password(form.new_password.data)
admin.add_view(UserView(db.User, db.session))
for table in (db.Project, db.Category, db.Term, db.Reference, db.Suggestion, db.Comment, db.Outlink):
admin.add_view(RestrictedModelView(table, db.session))
create_admin()
if __name__ == "__main__":
app.debug = True
toolbar = DebugToolbarExtension(app)
app.run(host="0.0.0.0", port=8004, debug=True, threaded=True)