admin.py 12.3 KB
Newer Older
1
from flask import Blueprint, redirect, url_for, request, flash, abort, send_file, Response
YSelf Tool's avatar
YSelf Tool committed
2
3
4
from flask.ext.login import login_required
from passlib.hash import pbkdf2_sha256

5
6
from datetime import datetime, timedelta

7
from models.database import User, Topic, Event, Speaker, Statement
8
from models.forms import AdminUserForm, NewUserForm, NewTopicForm, NewEventForm, AddStatementForm, EditSpeakerForm
YSelf Tool's avatar
YSelf Tool committed
9

10
11
from shared import db, admin_permission
from utils import render_layout, speaker_by_name_or_number
YSelf Tool's avatar
YSelf Tool committed
12
13
14
15
16
17
18
19
20

admin = Blueprint("admin", __name__)


@admin.route("/")
@login_required
@admin_permission.require()
def index():
    users = User.query.limit(10).all()
Robin Sonnabend's avatar
Robin Sonnabend committed
21
22
    events = Event.query.limit(10).all()
    return render_layout("admin_index.html", users=users, events=events)
YSelf Tool's avatar
YSelf Tool committed
23
24
25
26
27
28

@admin.route("/user/")
@login_required
@admin_permission.require()
def user():
    users = User.query.all()
29
    return render_layout("admin_user_index.html", users=users)
YSelf Tool's avatar
YSelf Tool committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43

@admin.route("/user/edit", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def user_edit():
    user_id = request.args.get("id", None)
    if user_id is not None:
        user = db.session.query(User).filter_by(id=user_id).first()
        form = AdminUserForm(obj=user)
        if form.validate_on_submit():
            form.populate_obj(user)
            db.session.commit()
            return redirect(url_for(".index"))
        else:
44
            return render_layout("admin_user_edit.html", form=form, id=user_id)
YSelf Tool's avatar
YSelf Tool committed
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
    else:
        return redirect(url_for(".index"))
            

@admin.route("/user/delete")
@login_required
@admin_permission.require()
def user_delete():
    user_id = request.args.get("id", None)
    if user_id is not None:
        user = User.query.filter_by(id=user_id).first()
        db.session.delete(user)
        db.session.commit()
        flash("User deleted.", "alert-success")
    return redirect(url_for(".user"))

@admin.route("/user/new", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def user_new():
    form = NewUserForm()
    if form.validate_on_submit():
        password_hash = pbkdf2_sha256.encrypt(form.password.data, rounds=200000, salt_size=16)
        user = User(form.fullname.data, form.username.data, password_hash)
        db.session.add(user)
        db.session.commit()
        return redirect(url_for(".user"))
72
    return render_layout("admin_user_new.html", form=form)
YSelf Tool's avatar
YSelf Tool committed
73

Hinrikus Wolf's avatar
Hinrikus Wolf committed
74
75
76
77
78
79
@admin.route("/event/")
@login_required
@admin_permission.require()
def event():
    events = Event.query.all()
    return render_layout("admin_event_index.html", events=events)
Robin Sonnabend's avatar
Robin Sonnabend committed
80
81
        

82
@admin.route("/event/show")
Robin Sonnabend's avatar
Robin Sonnabend committed
83
84
85
86
87
88
89
@login_required
@admin_permission.require()
def event_show():
    event_id = request.args.get("id", None)
    if event_id is not None:
        event = Event.query.filter_by(id=event_id).first()
        return render_layout("admin_event_show.html", event=event)
90
    return redirect(url_for(".index"))
Hinrikus Wolf's avatar
Hinrikus Wolf committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114


@admin.route("/event/new", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def event_new():
    form = NewEventForm()
    if form.validate_on_submit():
        if Topic.query.filter_by(name=form.name.data).count() > 0:
            flash("There already is an event with that name.", "alert-error")
            return render_layout("admin_event_new.html", form=form)
        event = Event(form.name.data)
        db.session.add(event)
        db.session.commit()
        return redirect(url_for(".event"))
    return render_layout("admin_event_new.html", form=form)


@admin.route("/event/delete")
@login_required
@admin_permission.require()
def event_delete():
    event_id = request.args.get("id", None)
    if event_id is not None:
Robin Sonnabend's avatar
Robin Sonnabend committed
115
        event = Event.query.filter_by(id=event_id).first()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
116
117
118
119
120
121
122
123
124
125
126
127
        db.session.delete(event)
        db.session.commit()
        flash("Event deleted.", "alert-success")
    return redirect(url_for(".event"))

@admin.route("/event/edit", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def event_edit():
    event_id = request.args.get("id", None)
    if event_id is not None:
        event = db.session.query(Event).filter_by(id=event_id).first()
Robin Sonnabend's avatar
Robin Sonnabend committed
128
        form = NewEventForm(obj=event)
Hinrikus Wolf's avatar
Hinrikus Wolf committed
129
        if form.validate_on_submit():
Robin Sonnabend's avatar
Robin Sonnabend committed
130
            form.populate_obj(event)
Hinrikus Wolf's avatar
Hinrikus Wolf committed
131
132
133
134
135
136
137
            db.session.commit()
            return redirect(url_for(".index"))
        else:
            return render_layout("admin_event_edit.html", form=form, id=event_id)
    else:
        return redirect(url_for(".index"))

YSelf Tool's avatar
YSelf Tool committed
138

139
140
141
142
143
144
145
@admin.route("/topic/show")
@login_required
@admin_permission.require()
def topic_show():
    topic_id = request.args.get("id", None)
    if topic_id is not None:
        topic = Topic.query.filter_by(id=topic_id).first()
Robin Sonnabend's avatar
Robin Sonnabend committed
146
147
        topic.event.current_topic_id = topic.id
        db.session.commit()
148
149
        form = AddStatementForm()
        form.topic.data = topic.id
Robin Sonnabend's avatar
Robin Sonnabend committed
150
        statements = topic.sorted_statements()
Robin Sonnabend's avatar
Robin Sonnabend committed
151
152
        topics = topic.event.sorted_topics()
        return render_layout("admin_topic_show.html", topic=topic, form=form, statements=statements, topics=topics)
153
154
155
    return redirect(url_for(".index"))
        

Hinrikus Wolf's avatar
Hinrikus Wolf committed
156
@admin.route("/topic/new", methods=["GET", "POST"])
YSelf Tool's avatar
YSelf Tool committed
157
158
@login_required
@admin_permission.require()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
159
160
def topic_new():
    form = NewTopicForm()
YSelf Tool's avatar
YSelf Tool committed
161
    if form.validate_on_submit():
Hinrikus Wolf's avatar
Hinrikus Wolf committed
162
163
164
        if Topic.query.filter_by(name=form.name.data).count() > 0:
            flash("There already is an topic with that name.", "alert-error")
            return render_layout("admin_topic_new.html", form=form)
Hinrikus Wolf's avatar
Hinrikus Wolf committed
165
        topic = Topic(form.name.data, form.mode.data, form.event_id.data)
Hinrikus Wolf's avatar
Hinrikus Wolf committed
166
        db.session.add(topic)
YSelf Tool's avatar
YSelf Tool committed
167
        db.session.commit()
Robin Sonnabend's avatar
Robin Sonnabend committed
168
        return redirect(url_for(".event", id=topic.event.id))
Robin Sonnabend's avatar
Robin Sonnabend committed
169
170
171
172
    event_id = request.args.get("event_id", None)
    if event_id is None:
        return redirect(url_for(".index"))
    form.event_id.data = event_id
Hinrikus Wolf's avatar
Hinrikus Wolf committed
173
    return render_layout("admin_topic_new.html", form=form)
YSelf Tool's avatar
YSelf Tool committed
174

Hinrikus Wolf's avatar
Hinrikus Wolf committed
175
@admin.route("/topic/delete")
YSelf Tool's avatar
YSelf Tool committed
176
177
@login_required
@admin_permission.require()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
178
def topic_delete():
Hinrikus Wolf's avatar
Hinrikus Wolf committed
179
180
181
182
    topic_id = request.args.get("id", None)
    if topic_id is not None:
        topic  = Topic.query.filter_by(id=topic_id).first()
        db.session.delete(topic)
YSelf Tool's avatar
YSelf Tool committed
183
        db.session.commit()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
184
185
        flash("Topic deleted.", "alert-success")
    return redirect(url_for(".topic"))
YSelf Tool's avatar
YSelf Tool committed
186

Hinrikus Wolf's avatar
Hinrikus Wolf committed
187
@admin.route("/topic/edit", methods=["GET", "POST"])
YSelf Tool's avatar
YSelf Tool committed
188
189
@login_required
@admin_permission.require()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
190
191
192
193
194
def topic_edit():
    topic_id = request.args.get("id", None)
    if topic_id is not None:
        topic = db.session.query(Topic).filter_by(id=topic_id).first()
        form = NewTopicForm(obj=topic)
YSelf Tool's avatar
YSelf Tool committed
195
        if form.validate_on_submit():
Hinrikus Wolf's avatar
Hinrikus Wolf committed
196
            form.populate_obj(topic)
YSelf Tool's avatar
YSelf Tool committed
197
            db.session.commit()
198
            return redirect(url_for(".topic_show", id=topic.id))
YSelf Tool's avatar
YSelf Tool committed
199
        else:
Hinrikus Wolf's avatar
Hinrikus Wolf committed
200
            return render_layout("admin_topic_edit.html", form=form, id=topic_id)
YSelf Tool's avatar
YSelf Tool committed
201
202
203
    else:
        return redirect(url_for(".index"))

Hinrikus Wolf's avatar
Hinrikus Wolf committed
204
@admin.route("/topic/")
YSelf Tool's avatar
YSelf Tool committed
205
206
@login_required
@admin_permission.require()
Hinrikus Wolf's avatar
Hinrikus Wolf committed
207
208
209
def topic():
    topics = Topic.query.all()
    return render_layout("admin_topic_index.html", topics=topics)
210

Robin Sonnabend's avatar
Robin Sonnabend committed
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
@admin.route("/topic/swap/up")
@login_required
@admin_permission.require()
def topic_swap_up():
    topic_id = request.args.get("id", None)
    original_id = request.args.get("original", None)
    if topic_id is not None:
        topic = Topic.query.filter_by(id=topic_id).first()
        topics = topic.event.sorted_topics()
        index = topics.index(topic)
        if index != 0:
            topic.swap_topics(topics[index-1])
            db.session.commit()
        return redirect(url_for(".topic_show", id=original_id))
    return redirect(url_for(".index"))

@admin.route("/topic/swap/down")
@login_required
@admin_permission.require()
def topic_swap_down():
    topic_id = request.args.get("id", None)
    original_id = request.args.get("original", None)
    if topic_id is not None:
        topic = Topic.query.filter_by(id=topic_id).first()
        topics = topic.event.sorted_topics()
        index = topics.index(topic)
        if index != len(topics) - 1:
            topic.swap_topics(topics[index+1])
            db.session.commit()
        return redirect(url_for(".topic_show", id=original_id))
    return redirect(url_for(".index"))
242

Hinrikus Wolf's avatar
Hinrikus Wolf committed
243
244
245
@admin.route("/speaker/rename", methods=["GET", "POST"])
@login_required
@admin_permission.require()
246
def speaker_edit():
Hinrikus Wolf's avatar
Hinrikus Wolf committed
247
248
    #speaker = Speaker.query.filter_by(number=number,event).first()
    #if speaker is not None:
249
250
251
252
253
254
    #id=statement.speaker.identifier(), topic_id=topic.id)
    speaker_id = request.args.get("id", None)
    topic_id = request.args.get("topic_id", None)
    speaker = Speaker.query.filter_by(id=speaker_id).first()
    form = EditSpeakerForm(obj=speaker)
    form.topic_id.data=topic_id
Robin Sonnabend's avatar
Robin Sonnabend committed
255
    
Hinrikus Wolf's avatar
Hinrikus Wolf committed
256
257
    if speaker is not None:
        if form.validate_on_submit():
258
            speaker.name = form.name.data
Robin Sonnabend's avatar
Robin Sonnabend committed
259
            speaker.number = form.number.data
Hinrikus Wolf's avatar
Hinrikus Wolf committed
260
261
            db.session.commit()
            return redirect(url_for(".topic_show",id=form.topic_id.data))
262
263
        else: 
            return render_layout("admin_speaker_edit.html", form=form, speaker=speaker, topic_id=topic_id)
Hinrikus Wolf's avatar
Hinrikus Wolf committed
264
265
266
267
268
    else:
        return redirect(url_for(".index"))
    


269
270
271
272
273
274
275
276
277
278
279
280
281
@admin.route("/statement/")
@login_required
@admin_permission.require()
def statement():
    statements = Statement.query.all()
    return render_layout("admin_statement_index.html", statement=statement)

@admin.route("/statement/new", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def statement_new():
    form = AddStatementForm()
    if form.validate_on_submit():
282
        statement = request.form.get("submit","add_statement")
283
284
285
286
287
288
289
        topic = Topic.query.filter_by(id=form.topic.data).first()
        speaker = speaker_by_name_or_number(form.speaker_name.data, topic.event.id)
        if topic is not None and speaker is not None:
            if speaker.count_active(topic) == 0 or (statement == "add_meta_statement" and speaker.count_active_meta(topic) == 0) :
                statement = Statement(speaker.id, topic.id, is_meta=(statement == "add_meta_statement"))
                db.session.add(statement)
                db.session.commit()
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
            return redirect(url_for(".topic_show", id=topic.id))
    return render_layout("admin_statement_new.html", form=form)

@admin.route("/statement/done")
@login_required
@admin_permission.require()
def statement_done():
    statement_id = request.args.get("id", None)
    if statement_id is not None:
        statement = Statement.query.filter_by(id=statement_id).first()
        if statement is not None:
            statement.done()
            db.session.commit()
    topic_id = request.args.get("topic_id", None)
    if topic_id is not None:
        return redirect(url_for(".topic_show", id=topic_id))
    return redirect(url_for(".index"))

@admin.route("/statement/delete")
@login_required
@admin_permission.require()
def statement_delete():
    statement_id = request.args.get("id", None)
    if statement_id is not None:
        statement = Statement.query.filter_by(id=statement_id).first()
        if statement is not None:
            db.session.delete(statement)
            db.session.commit()
    topic_id = request.args.get("topic_id", None)
    if topic_id is not None:
        return redirect(url_for(".topic_show", id=topic_id))
    return redirect(url_for(".index"))

323
324
325
326
@admin.route("/statement/undo")
@login_required
@admin_permission.require()
def statement_undo():
327
    topic_id = request.args.get("topic_id", None)
328
329
    if topic_id is not None:
        statement = Statement.query.filter_by(executed=True, topic_id=topic_id).order_by(db.desc(Statement.execution_time)).first()
330
331
332
        statement.undo()
        db.session.commit()
    return redirect(url_for(".topic_show", id=topic_id))
Robin Sonnabend's avatar
Robin Sonnabend committed
333

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
@admin.route("/pause", methods=["GET", "POST"])
@login_required
@admin_permission.require()
def pause():
    event_id = request.args.get("id", None)
    if event_id is not None:
        event = Event.query.filter_by(id=event_id).first()
        if event is not None:
            if event.paused_until == None:
                event.paused_until = datetime(1970, 1, 1)
            event.paused = not event.paused
            if event.paused:
                rawtime = float(request.form["timeslider"])
                delta = timedelta(seconds=rawtime)
                print(delta)
Robin Sonnabend's avatar
Robin Sonnabend committed
349
                event.paused_until = datetime.now() + delta    
350
351
352
            db.session.commit()
    topic_id = request.args.get("original", None)
    return redirect(url_for(".topic_show", id=topic_id))