views.py 39.6 KB
Newer Older
1
2
3

import os
import secrets
Ossi Laine's avatar
Ossi Laine committed
4
from datetime import date
5
from tempfile import mkstemp
6

Ossi Laine's avatar
Ossi Laine committed
7
8
9
10
from flask_socketio import emit
from sqlalchemy import and_
from flask_login import login_required
from werkzeug import secure_filename
11
from flask import (
Ossi Laine's avatar
Ossi Laine committed
12
13
14
15
16
    render_template,
    request,
    flash,
    redirect,
    url_for,
17
18
    Blueprint,
    send_file
19
20
)

Ossi Laine's avatar
Ossi Laine committed
21
from app import app, db, socketio
22
23
24
25
26
27
from app.routes import APP_ROOT
from app.models import background_question, experiment
from app.models import background_question_answer
from app.models import page, question
from app.models import background_question_option
from app.models import answer_set, answer, forced_id
Ossi Laine's avatar
Ossi Laine committed
28
from app.models import trial_randomization
Ossi Laine's avatar
Ossi Laine committed
29
from app.models import embody_answer, embody_question
30
from app.forms import (
Ossi Laine's avatar
Ossi Laine committed
31
32
33
34
    CreateBackgroundQuestionForm,
    CreateQuestionForm, UploadStimuliForm, EditBackgroundQuestionForm,
    EditQuestionForm, EditExperimentForm, UploadResearchBulletinForm,
    EditPageForm, RemoveExperimentForm, GenerateIdForm, CreateEmbodyForm
35
)
36
from app.utils import get_mean_from_slider_answers, map_answers_to_questions, \
Ossi Laine's avatar
Ossi Laine committed
37
    generate_csv
Ossi Laine's avatar
Ossi Laine committed
38
from app.embody_plot import get_coordinates
Ossi Laine's avatar
Ossi Laine committed
39

40

Ossi Laine's avatar
Ossi Laine committed
41
42
43
44
experiment_blueprint = Blueprint("experiment", __name__,
                                 template_folder='templates',
                                 # static_folder='static',
                                 url_prefix='/experiment')
45

Ossi Laine's avatar
Ossi Laine committed
46
47
48
# Set sliders/embody:
DEFAULT_EMBODY_PICTURE = '/static/img/dummy_600.png'
DEFAULT_EMBODY_QUESTION = 'Color the regions whose activity you feel increasing or getting stronger'
49

Ossi Laine's avatar
Ossi Laine committed
50

51
52
53
@experiment_blueprint.route('/view')
@login_required
def view():
Ossi Laine's avatar
Ossi Laine committed
54
55

    # crap:3lines
56
57
58
    exp_id = request.args.get('exp_id', None)
    media = page.query.filter_by(experiment_idexperiment=exp_id).all()

Ossi Laine's avatar
Ossi Laine committed
59
    # stimulus type
60
    mtype = page.query.filter_by(experiment_idexperiment=exp_id).first()
Ossi Laine's avatar
Ossi Laine committed
61

Ossi Laine's avatar
Ossi Laine committed
62
63
64
65
    # experiment info
    experiment_info = experiment.query.filter_by(idexperiment=exp_id).all()

    # background questions
66
    questions_and_options = {}
Ossi Laine's avatar
Ossi Laine committed
67
68
69
70
71
72
73
74
75
76
77
    questions = background_question.query.filter_by(
        experiment_idexperiment=exp_id).all()

    for q in questions:

        options = background_question_option.query.filter_by(
            background_question_idbackground_question=q.idbackground_question).all()
        options_list = [(o.option, o.idbackground_question_option)
                        for o in options]
        questions_and_options[q.idbackground_question,
                              q.background_question] = options_list
78
79

    questions1 = questions_and_options
Ossi Laine's avatar
Ossi Laine committed
80

Ossi Laine's avatar
Ossi Laine committed
81
    # sliderset
82
83
84
    categories_and_scales = {}
    categories = question.query.filter_by(experiment_idexperiment=exp_id).all()

Ossi Laine's avatar
Ossi Laine committed
85
86
    for cat in categories:

87
        scale_list = [(cat.left, cat.right)]
Ossi Laine's avatar
Ossi Laine committed
88
89
        categories_and_scales[cat.idquestion, cat.question] = scale_list

90
91
    categories1 = categories_and_scales

Ossi Laine's avatar
Ossi Laine committed
92
93
94
95
96
    # embody pictures
    embody_pictures = embody_question.query.filter_by(
        experiment_idexperiment=exp_id).all()

    return render_template('view_experiment.html', exp_id=exp_id, media=media, mtype=mtype, experiment_info=experiment_info, categories1=categories1, questions1=questions1, embody_pictures=embody_pictures)
97
98
99
100
101
102
103
104
105
106
107
108


# Experiment info:

@experiment_blueprint.route('/remove', methods=['GET', 'POST'])
@login_required
def remove():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
Ossi Laine's avatar
Ossi Laine committed
109

110
111
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
112

113
    else:
Ossi Laine's avatar
Ossi Laine committed
114

115
        form = RemoveExperimentForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
116

117
        if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
118

119
            if form.remove.data == 'DELETE':
Ossi Laine's avatar
Ossi Laine committed
120
121
122
123
124
125
126

                # This removes all experiment data from the database!

                # Remove research bulletin if it exists
                empty_filevariable = experiment.query.filter_by(
                    idexperiment=exp_id).first()

127
                if empty_filevariable.research_notification_filename is not None:
Ossi Laine's avatar
Ossi Laine committed
128
129
130
131
                    target = os.path.join(
                        APP_ROOT, empty_filevariable.research_notification_filename)

                    if os.path.exists(target):
132
                        os.remove(target)
Ossi Laine's avatar
Ossi Laine committed
133
134
135
136

                # Tables
                remove_forced_id = forced_id.query.filter_by(
                    experiment_idexperiment=exp_id).all()
Ossi Laine's avatar
Ossi Laine committed
137
                remove_rows(remove_forced_id)
138

Ossi Laine's avatar
Ossi Laine committed
139
140
141
142
143
                # background_question_option & background_question & background question answers:
                remove_background_question = background_question.query.filter_by(
                    experiment_idexperiment=exp_id).all()

                # Remove all background questions and all answers given to each bg question
144
                for a in range(len(remove_background_question)):
Ossi Laine's avatar
Ossi Laine committed
145
146
                    remove_background_question_option = background_question_option.query.filter_by(
                        background_question_idbackground_question=remove_background_question[a].idbackground_question).all()
Ossi Laine's avatar
Ossi Laine committed
147
148
                    remove_rows(remove_background_question_option)

Ossi Laine's avatar
Ossi Laine committed
149
150
                    remove_background_question_answers = background_question_answer.query.filter_by(
                        background_question_idbackground_question=remove_background_question[a].idbackground_question).all()
Ossi Laine's avatar
Ossi Laine committed
151
152
                    remove_rows(remove_background_question_answers)

153
154
                    db.session.delete(remove_background_question[a])
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
155
156
157
158

                # Remove all questions and answers
                remove_question = question.query.filter_by(
                    experiment_idexperiment=exp_id).all()
159
                for a in range(len(remove_question)):
Ossi Laine's avatar
Ossi Laine committed
160
161
                    remove_question_answers = answer.query.filter_by(
                        question_idquestion=remove_question[a].idquestion).all()
Ossi Laine's avatar
Ossi Laine committed
162
                    remove_rows(remove_question_answers)
163
164
                    db.session.delete(remove_question[a])
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
165
166
167
168
169

                # Remove experiment embody pictures, questions and answers
                remove_embody_answers = embody_answer.query.filter(embody_answer.page_idpage.in_(list(map(
                    lambda x: x[0], page.query.with_entities(page.idpage).filter_by(experiment_idexperiment=exp_id).all())))).all()
                remove_rows(remove_embody_answers)
Ossi Laine's avatar
Ossi Laine committed
170
171
                remove_embody_questions = embody_question.query.filter_by(
                    experiment_idexperiment=exp_id).all()
Ossi Laine's avatar
Ossi Laine committed
172
173
174

                for a in range(len(remove_embody_questions)):
                    target = APP_ROOT + remove_embody_questions[a].picture
Ossi Laine's avatar
Ossi Laine committed
175
                    if os.path.exists(target) and DEFAULT_EMBODY_PICTURE != remove_embody_questions[a].picture:
Ossi Laine's avatar
Ossi Laine committed
176
177
                        os.remove(target)

Ossi Laine's avatar
Ossi Laine committed
178
179
                remove_rows(remove_embody_questions)

Ossi Laine's avatar
Ossi Laine committed
180
181
182
183
                # Remove all pages and datafiles
                remove_pages = page.query.filter_by(
                    experiment_idexperiment=exp_id).all()

184
                for a in range(len(remove_pages)):
Ossi Laine's avatar
Ossi Laine committed
185
                    if remove_pages[a].type != 'text':
186
                        target = os.path.join(APP_ROOT, remove_pages[a].media)
Ossi Laine's avatar
Ossi Laine committed
187
                        if os.path.exists(target):
188
                            os.remove(target)
Ossi Laine's avatar
Ossi Laine committed
189
190

                    # Now that the files are removed we can delete the page
191
192
                    db.session.delete(remove_pages[a])
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
193

Ossi Laine's avatar
Ossi Laine committed
194
195
196
197
                # Remove all answer_sets and trial_randomization orders
                remove_answer_set = answer_set.query.filter_by(
                    experiment_idexperiment=exp_id).all()

198
                for a in range(len(remove_answer_set)):
Ossi Laine's avatar
Ossi Laine committed
199
200
                    remove_trial_randomizations = trial_randomization.query.filter_by(
                        answer_set_idanswer_set=remove_answer_set[a].idanswer_set).all()
Ossi Laine's avatar
Ossi Laine committed
201
                    remove_rows(remove_trial_randomizations)
202
203
                    db.session.delete(remove_answer_set[a])
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
204
205
206
207

                # Remove experiment table
                remove_experiment = experiment.query.filter_by(
                    idexperiment=exp_id).first()
208
209
                db.session.delete(remove_experiment)
                db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
210
211
212
213

                # Remove empty directories
                os.rmdir(APP_ROOT + '/static/embody_images/' + str(exp_id))
                os.rmdir(APP_ROOT + '/static/experiment_stimuli/' + str(exp_id))
Ossi Laine's avatar
Ossi Laine committed
214

215
216
                flash("Experiment was removed from database!")
                return redirect(url_for('index'))
Ossi Laine's avatar
Ossi Laine committed
217

218
219
220
            else:
                flash("Experiment was not removed!")
                return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
221

222
223
224
225
226
227
228
        return render_template('remove_experiment.html', form=form, exp_id=exp_id)


@experiment_blueprint.route('/publish')
@login_required
def publish():
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
229
230
    publish_experiment = experiment.query.filter_by(
        idexperiment=exp_id).first()
231
232
233
234
235
236
237
238
239
240
    publish_experiment.status = 'Public'
    flash("Changed status to Public")
    db.session.commit()
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/hide')
@login_required
def hide():
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
241
    hide_experiment = experiment.query.filter_by(idexperiment=exp_id).first()
242
243
244
245
246
247
248
249
250
251
    hide_experiment.status = 'Hidden'
    flash("Changed status to Hidden")
    db.session.commit()
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/private')
@login_required
def private():
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
252
253
    private_experiment = experiment.query.filter_by(
        idexperiment=exp_id).first()
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    private_experiment.status = 'Private'
    flash("Changed status to Private")
    db.session.commit()
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/randomization')
@login_required
def randomization():
    exp_id = request.args.get('exp_id', None)
    status = request.args.get('set')

    if status == 'On':
        flash("Enabled trial randomization")
    elif status == 'Off':
        flash("Disabled trial randomization")

Ossi Laine's avatar
Ossi Laine committed
271
272
    experiment.query.filter_by(
        idexperiment=exp_id).first().randomization = status
273
    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
274

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/set_forced_id')
@login_required
def set_forced_id():
    '''By using forced ID login subjects can only participate to a rating task
    by logging in with a pregenerated ID.'''

    exp_id = request.args.get('exp_id', None)
    status = request.args.get('set')

    if status == 'On':
        flash("Enabled forced ID login")
    elif status == 'Off':
        flash("Disabled forced ID login")

Ossi Laine's avatar
Ossi Laine committed
292
293
    experiment.query.filter_by(
        idexperiment=exp_id).first().use_forced_id = status
294
    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
295

296
297
298
299
300
301
302
303
304
305
306
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/view_forced_id_list', methods=['GET', 'POST'])
@login_required
def view_forced_id_list():
    '''Forced ID login for participants'''

    exp_id = request.args.get('exp_id', None)
    id_list = forced_id.query.filter_by(experiment_idexperiment=exp_id).all()
    form = GenerateIdForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
307

308
309
    if request.method == 'POST' and form.validate():

Ossi Laine's avatar
Ossi Laine committed
310
        for i in range(int(request.form['number'])):
311
            random_id = str(request.form['string']) + str(secrets.token_hex(3))
Ossi Laine's avatar
Ossi Laine committed
312
313
314
315
316
317
318
            check_answer_set = answer_set.query.filter_by(
                session=random_id).first()
            check_forced_id = forced_id.query.filter_by(
                pregenerated_id=random_id).first()

            # here we check if the generated id is found from given answers from the whole database in answer_set table
            # or from forced_id table. If so another id is generated instead to avoid a duplicate
319
            if check_answer_set is not None or check_forced_id is not None:
Ossi Laine's avatar
Ossi Laine committed
320

321
322
                #flash("ID already existed; generated a new one")
                random_id = secrets.token_hex(3)
Ossi Laine's avatar
Ossi Laine committed
323
324
325
326
327
328
329
                check_answer_set = answer_set.query.filter_by(
                    session=random_id).first()
                check_forced_id = forced_id.query.filter_by(
                    pregenerated_id=random_id).first()

            input_id = forced_id(
                experiment_idexperiment=exp_id, pregenerated_id=random_id)
330
331
            db.session.add(input_id)
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
332

333
        return redirect(url_for('experiment.view_forced_id_list', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
334

335
336
337
338
339
340
341
342
343
344
    return render_template('view_forced_id_list.html', exp_id=exp_id, id_list=id_list)


@experiment_blueprint.route('/upload_research_notification', methods=['GET', 'POST'])
@login_required
def upload_research_notification():
    '''Upload research bulletin'''

    exp_id = request.args.get('exp_id', None)
    form = UploadResearchBulletinForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
345

346
347
348
    if request.method == 'POST':
        path = 'static/experiment_stimuli/' + str(exp_id)
        target = os.path.join(APP_ROOT, path)
Ossi Laine's avatar
Ossi Laine committed
349

350
351
        if not os.path.isdir(target):
            os.mkdir(target)
Ossi Laine's avatar
Ossi Laine committed
352
353

        # This returns a list of filenames: request.files.getlist("file")
354
        for file in request.files.getlist("file"):
Ossi Laine's avatar
Ossi Laine committed
355
            # save files in the correct folder
356
357
358
            filename = file.filename
            destination = "/".join([target, filename])
            file.save(destination)
Ossi Laine's avatar
Ossi Laine committed
359
360
361

            # add pages to the db
            db_path = path + str('/') + str(filename)
362
363
364
            bulletin = experiment.query.filter_by(idexperiment=exp_id).first()
            bulletin.research_notification_filename = db_path
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
365

366
367
368
369
370
371
372
373
374
        return redirect(url_for('experiment.view', exp_id=exp_id))

    return render_template('upload_research_notification.html', exp_id=exp_id, form=form)


@experiment_blueprint.route('/remove_research_notification')
@login_required
def remove_research_notification():
    '''Remove research bulletin'''
Ossi Laine's avatar
Ossi Laine committed
375

376
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
377
378
379
380
381
382
    empty_filevariable = experiment.query.filter_by(
        idexperiment=exp_id).first()
    target = os.path.join(
        APP_ROOT, empty_filevariable.research_notification_filename)

    if os.path.exists(target):
383
        os.remove(target)
Ossi Laine's avatar
Ossi Laine committed
384

385
386
    empty_filevariable.research_notification_filename = None
    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
387

388
389
390
391
392
393
394
395
396
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/edit', methods=['GET', 'POST'])
@login_required
def edit():
    '''Edit experiment details'''

    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
397
398
399
    current_experiment = experiment.query.filter_by(
        idexperiment=exp_id).first()

400
401
    form = EditExperimentForm(request.form, obj=current_experiment)
    form.language.default = current_experiment.language
Ossi Laine's avatar
Ossi Laine committed
402

403
    if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
404

405
406
407
        form.populate_obj(current_experiment)
        db.session.commit()

Ossi Laine's avatar
Ossi Laine committed
408
        return redirect(url_for('experiment.view', exp_id=exp_id))
409

Ossi Laine's avatar
Ossi Laine committed
410
    return render_template('edit_experiment.html', form=form, exp_id=exp_id)
411
412
413
414
415
416
417


# Background questions:

@experiment_blueprint.route('/add_bg_question', methods=['GET', 'POST'])
@login_required
def add_bg_question():
Ossi Laine's avatar
Ossi Laine committed
418

419
420
421
422
423
424
    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
425

426
427
    else:
        form = CreateBackgroundQuestionForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
428

429
        if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
430
431

            # Split the form data into a list that separates questions followed by the corresponding options
432
            str = form.bg_questions_and_options.data
433
            str_list = str.split('/n')
Ossi Laine's avatar
Ossi Laine committed
434
435

            # Iterate through the questions and options list
436
            for a in range(len(str_list)):
Ossi Laine's avatar
Ossi Laine committed
437
                # Split the list cells further into questions and options
438
                list = str_list[a].split(';')
Ossi Laine's avatar
Ossi Laine committed
439
440

                # Input the first item of the list as a question in db and the items followed by that as options for that question
441
442
                for x in range(len(list)):
                    if x == 0:
Ossi Laine's avatar
Ossi Laine committed
443
444
                        add_bgquestion = background_question(
                            background_question=list[x], experiment_idexperiment=exp_id)
445
446
447
                        db.session.add(add_bgquestion)
                        db.session.commit()
                    else:
Ossi Laine's avatar
Ossi Laine committed
448
449
                        add_bgq_option = background_question_option(
                            background_question_idbackground_question=add_bgquestion.idbackground_question, option=list[x])
450
451
                        db.session.add(add_bgq_option)
                        db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
452
453
454

            return redirect(url_for('experiment.view', exp_id=exp_id))

455
456
457
458
459
460
        return render_template('add_bg_question.html', form=form)


@experiment_blueprint.route('/edit_bg_question', methods=['GET', 'POST'])
@login_required
def edit_bg_question():
Ossi Laine's avatar
Ossi Laine committed
461

462
463
    bg_question_id = request.args.get('idbackground_question', None)

Ossi Laine's avatar
Ossi Laine committed
464
465
466
467
468
    # Search for the right question and for the right options. Form a string of those separated with ";" and insert the
    # formed string into the edit form
    current_bg_question = background_question.query.filter_by(
        idbackground_question=bg_question_id).first()
    exp_id = current_bg_question.experiment_idexperiment
469
    question_string = current_bg_question.background_question
Ossi Laine's avatar
Ossi Laine committed
470
471
472
473
474
475
476
    options = background_question_option.query.filter_by(
        background_question_idbackground_question=bg_question_id).all()

    for o in range(len(options)):
        question_string = str(question_string) + \
            str("; ") + str(options[o].option)

477
478
479
    form = EditBackgroundQuestionForm(request.form)
    form.bg_questions_and_options.data = question_string

Ossi Laine's avatar
Ossi Laine committed
480
    # After user chooses to update the question and options lets replace the old question and options with the ones from the form
481
    if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
482
        # Explode the string with new values from the form
483
484
        form_values = form.new_values.data
        form_values_list = form_values.split(';')
Ossi Laine's avatar
Ossi Laine committed
485
486

        # Check and remove possible whitespaces from string beginnings with lstrip
487
488
        for x in range(len(form_values_list)):
            form_values_list[x] = form_values_list[x].lstrip()
Ossi Laine's avatar
Ossi Laine committed
489
490

        # Cycle through strings and update db
491
        for x in range(len(form_values_list)):
Ossi Laine's avatar
Ossi Laine committed
492
493

            # Replace question and update the object to database
494
            if x == 0:
Ossi Laine's avatar
Ossi Laine committed
495
                current_bg_question.background_question = form_values_list[x]
496
                db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
497
498

                # Delete old options from db
499
500
501
                for o in options:
                    db.session.delete(o)
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
502
503

            # Insert new options to db
504
            else:
Ossi Laine's avatar
Ossi Laine committed
505
506
                new_option = background_question_option(
                    background_question_idbackground_question=current_bg_question.idbackground_question, option=form_values_list[x])
507
508
                db.session.add(new_option)
                db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
509

510
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
511

512
513
514
515
516
517
518
519
520
521
522
523
524
    return render_template('edit_bg_question.html', form=form, exp_id=exp_id)


@experiment_blueprint.route('/remove_bg_question')
@login_required
def remove_bg_question():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
525

526
    else:
527

528
        remove_id = request.args.get('idbackground_question', None)
Ossi Laine's avatar
Ossi Laine committed
529
530
531
532
        remove_options = background_question_option.query.filter_by(
            background_question_idbackground_question=remove_id).all()

        for a in range(len(remove_options)):
533
534
            db.session.delete(remove_options[a])
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
535
536
537
538

        remove_question = background_question.query.filter_by(
            idbackground_question=remove_id).first()

539
540
541
        db.session.delete(remove_question)
        db.session.commit()

Ossi Laine's avatar
Ossi Laine committed
542
    return redirect(url_for('experiment.view', exp_id=exp_id))
543
544


545
546
547
548
549
@experiment_blueprint.route('/set_embody')
@login_required
def set_embody():
    '''Enable/disable embody tool'''
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
550
    exp = experiment.query.filter_by(idexperiment=exp_id).first()
551
552
553
554
    exp.embody_enabled = (True if exp.embody_enabled == False else False)
    return redirect(url_for('experiment.view', exp_id=exp_id))


Ossi Laine's avatar
Ossi Laine committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
@experiment_blueprint.route('/add_embody', methods=['GET', 'POST'])
@login_required
def add_embody():

    exp_id = request.args.get('exp_id', None)
    default = request.args.get('default', None)
    exp_info = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_info.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
    else:
        form = CreateEmbodyForm(request.form)

        if request.method == 'POST' and form.validate():
            picture = request.files.get("picture")
            question = request.form.get("question")

573
574
575
576
577
578
579
580
            if not picture:
                default_embody = embody_question(
                    experiment_idexperiment=exp_id, picture=DEFAULT_EMBODY_PICTURE, question=question)
                db.session.add(default_embody)
                exp_info.embody_enabled = 1
                db.session.commit()
                return redirect(url_for('experiment.view', exp_id=exp_id))

Ossi Laine's avatar
Ossi Laine committed
581
            # get filename
Ossi Laine's avatar
Ossi Laine committed
582
583
            filename = secure_filename(picture.filename)
            path = 'static/embody_images/' + str(exp_id)
Ossi Laine's avatar
Ossi Laine committed
584
            db_path = '/' + path + str('/') + str(filename)
Ossi Laine's avatar
Ossi Laine committed
585
586
587
588
589
590
591
592
593
594
            target = os.path.join(APP_ROOT, path)

            # create folder with experiment id (if it does not exist)
            if not os.path.isdir(target):
                os.mkdir(target)

            # save file to server
            destination = "/".join([target, filename])
            picture.save(destination)

Ossi Laine's avatar
Ossi Laine committed
595
596
597
            # add pages to the db
            new_embody = embody_question(
                experiment_idexperiment=exp_id, question=question, picture=db_path)
Ossi Laine's avatar
Ossi Laine committed
598
599
600
            db.session.add(new_embody)
            exp_info.embody_enabled = 1
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
601
            return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
602
603
604
605

        return render_template('add_embody.html', form=form)


606
607
608
@experiment_blueprint.route('/add_questions', methods=['GET', 'POST'])
@login_required
def add_questions():
Ossi Laine's avatar
Ossi Laine committed
609

610
611
612
613
614
615
616
617
    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
    else:
        form = CreateQuestionForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
618

619
        if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
620

621
622
            str = form.questions_and_options.data
            str_list = str.split('/n')
Ossi Laine's avatar
Ossi Laine committed
623
624

            for a in range(len(str_list)):
625
                list = str_list[a].split(';')
Ossi Laine's avatar
Ossi Laine committed
626
627

                # If there are the right amount of values for the slider input values
628
                if len(list) == 3:
Ossi Laine's avatar
Ossi Laine committed
629
630
                    add_question = question(
                        experiment_idexperiment=exp_id, question=list[0], left=list[1], right=list[2])
631
632
                    db.session.add(add_question)
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
633
634

                # If slider has too many or too litlle parameters give an error and redirect back to input form
635
                else:
Ossi Laine's avatar
Ossi Laine committed
636
637
                    flash(
                        "Error Each slider must have 3 parameters separated by ; Some slider has:")
638
639
                    flash(len(list))
                    return redirect(url_for('create.experiment_questions', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
640
641
642

            return redirect(url_for('experiment.view', exp_id=exp_id))

643
644
645
646
647
648
        return render_template('add_questions.html', form=form)


@experiment_blueprint.route('/edit_question', methods=['GET', 'POST'])
@login_required
def edit_question():
Ossi Laine's avatar
Ossi Laine committed
649

650
651
652
    question_id = request.args.get('idquestion', None)
    current_question = question.query.filter_by(idquestion=question_id).first()
    form = EditQuestionForm(request.form, obj=current_question)
Ossi Laine's avatar
Ossi Laine committed
653

654
    if request.method == 'POST' and form.validate():
Ossi Laine's avatar
Ossi Laine committed
655

656
657
        form.populate_obj(current_question)
        db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
658

659
        return redirect(url_for('experiment.view', exp_id=current_question.experiment_idexperiment))
Ossi Laine's avatar
Ossi Laine committed
660

661
662
663
664
665
666
667
668
669
670
671
672
673
    return render_template('edit_question.html', form=form)


@experiment_blueprint.route('/remove_question')
@login_required
def remove_question():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
674

675
676
677
    else:

        remove_id = request.args.get('idquestion', None)
Ossi Laine's avatar
Ossi Laine committed
678
679
        remove_question = question.query.filter_by(
            idquestion=remove_id).first()
Ossi Laine's avatar
Ossi Laine committed
680
681

        # remove answers before removing questions
Ossi Laine's avatar
Ossi Laine committed
682
683
        remove_answers = answer.query.filter_by(
            question_idquestion=remove_question.idquestion).all()
Ossi Laine's avatar
Ossi Laine committed
684
685
686
        for a in range(len(remove_answers)):
            db.session.delete(remove_answers[a])
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
687

Ossi Laine's avatar
Ossi Laine committed
688
689
        db.session.delete(remove_question)
        db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
690

Ossi Laine's avatar
Ossi Laine committed
691
692
693
694
695
696
697
698
699
700
701
702
703
    return redirect(url_for('experiment.view', exp_id=exp_id))


@experiment_blueprint.route('/remove_embody')
@login_required
def remove_embody():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
704

Ossi Laine's avatar
Ossi Laine committed
705
706
    else:
        remove_id = request.args.get('idembody', None)
Ossi Laine's avatar
Ossi Laine committed
707
708
        remove_question = embody_question.query.filter_by(
            idembody=remove_id).first()
Ossi Laine's avatar
Ossi Laine committed
709
710
711
712
713

        # remove embody image from server
        if DEFAULT_EMBODY_PICTURE != remove_question.picture:
            os.remove(APP_ROOT + remove_question.picture)

Ossi Laine's avatar
Ossi Laine committed
714
715
        remove_answers = embody_answer.query.filter_by(
            embody_question_idembody=remove_question.idembody).all()
Ossi Laine's avatar
Ossi Laine committed
716
        remove_rows(remove_answers)
717
718
        db.session.delete(remove_question)
        db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
719

Ossi Laine's avatar
Ossi Laine committed
720
721
    question_count = embody_question.query.filter_by(
        experiment_idexperiment=exp_id).count()
Ossi Laine's avatar
Ossi Laine committed
722
723
724
725

    if question_count == 0:
        exp_status.embody_enabled = 0
        db.session.commit()
726

Ossi Laine's avatar
Ossi Laine committed
727
    return redirect(url_for('experiment.view', exp_id=exp_id))
728
729
730
731
732
733
734
735
736


# Stimuli:
@experiment_blueprint.route('/add_stimuli', methods=['GET', 'POST'])
@login_required
def add_stimuli():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()
Ossi Laine's avatar
Ossi Laine committed
737

738
739
740
741
    if exp_status.status != 'Hidden':
        flash("Experiment is public. Cannot modify structure.")
        return redirect(url_for('experiment.view', exp_id=exp_id))
    else:
Ossi Laine's avatar
Ossi Laine committed
742
743
744
745
        # If there are no pages set for the experiment lets reroute user to create experiment stimuli upload instead
        is_there_any_stimuli = page.query.filter_by(
            experiment_idexperiment=exp_id).first()

746
747
        if is_there_any_stimuli is None:
            return redirect(url_for('create.experiment_upload_stimuli', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
748

749
750
        stimulus_type = request.args.get('stimulus_type', None)
        form = UploadStimuliForm(request.form)
Ossi Laine's avatar
Ossi Laine committed
751

752
753
754
755
        if request.method == 'POST':
            if stimulus_type == 'text':
                string = form.text.data
                str_list = string.split('/n')
Ossi Laine's avatar
Ossi Laine committed
756

757
                for a in range(len(str_list)):
Ossi Laine's avatar
Ossi Laine committed
758
759
                    add_text_stimulus = page(
                        experiment_idexperiment=exp_id, type='text', text=str_list[a], media='none')
760
761
                    db.session.add(add_text_stimulus)
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
762
763
764

                return redirect(url_for('experiment.view', exp_id=exp_id))

765
            else:
Ossi Laine's avatar
Ossi Laine committed
766
767
                # Upload stimuli into /static/experiment_stimuli/exp_id folder
                # Create the pages for the stimuli by inserting experiment_id, stimulus type, text and names of the stimulus files (as a path to the folder)
768
769
                path = 'static/experiment_stimuli/' + str(exp_id)
                target = os.path.join(APP_ROOT, path)
Ossi Laine's avatar
Ossi Laine committed
770

771
772
                if not os.path.isdir(target):
                    os.mkdir(target)
Ossi Laine's avatar
Ossi Laine committed
773
774

                # This returns a list of filenames: request.files.getlist("file")
775
                for file in request.files.getlist("file"):
Ossi Laine's avatar
Ossi Laine committed
776
                    # save files in the correct folder
777
778
779
                    filename = file.filename
                    destination = "/".join([target, filename])
                    file.save(destination)
Ossi Laine's avatar
Ossi Laine committed
780

Ossi Laine's avatar
Ossi Laine committed
781
782
783
784
                    # add pages to the db
                    db_path = path + str('/') + str(filename)
                    new_page = page(experiment_idexperiment=exp_id,
                                    type=form.type.data, media=db_path)
785
786
                    db.session.add(new_page)
                    db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
787

788
                return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
789

790
791
792
793
794
795
796
797
798
799
800
801
            return redirect(url_for('experiment.view', exp_id=exp_id))

        return render_template('add_stimuli.html', form=form, stimulus_type=stimulus_type)


@experiment_blueprint.route('/edit_stimuli', methods=['GET', 'POST'])
@login_required
def edit_stimuli():

    exp_id = request.args.get('exp_id', None)
    page_id = request.args.get('idpage', None)
    edit_page = page.query.filter_by(idpage=page_id).first()
802
803
804
    form = EditPageForm(request.form)
    #form = EditPageForm(request.form, obj=edit_page)

Ossi Laine's avatar
Ossi Laine committed
805
    # TODO: replacing image not working -> form not getting validated for some reaseon !!
806

807
    if request.method == 'POST' and form.validate():
808
        print("POST IMAGE")
Ossi Laine's avatar
Ossi Laine committed
809
        # If the stimulus type is not text, then the old stimulus file is deleted from os and replaced
810
        if edit_page.type != 'text':
Ossi Laine's avatar
Ossi Laine committed
811
            # remove old file
812
813
814
            target = os.path.join(APP_ROOT, edit_page.media)
            os.remove(target)

Ossi Laine's avatar
Ossi Laine committed
815
            # upload new file
816
817
            path = 'static/experiment_stimuli/' + str(exp_id)
            target = os.path.join(APP_ROOT, path)
Ossi Laine's avatar
Ossi Laine committed
818

819
820
            if not os.path.isdir(target):
                os.mkdir(target)
Ossi Laine's avatar
Ossi Laine committed
821
822

            # This returns a list of filenames: request.files.getlist("file")
823
            for file in request.files.getlist("file"):
Ossi Laine's avatar
Ossi Laine committed
824
                # save files in the correct folder
825
826
827
                filename = file.filename
                destination = "/".join([target, filename])
                file.save(destination)
Ossi Laine's avatar
Ossi Laine committed
828
829
830
831

                # update db object
                db_path = path + str('/') + str(filename)
                edit_page.media = db_path
832
833
                db.session.commit()

Ossi Laine's avatar
Ossi Laine committed
834
        # If editing text stimulus no need for filehandling
835
        else:
Ossi Laine's avatar
Ossi Laine committed
836
837
838
            form.populate_obj(edit_page)
            db.session.commit()

839
        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
840

841
842
843
844
845
846
847
848
849
850
851
    return render_template('edit_stimuli.html', form=form, edit_page=edit_page)


@experiment_blueprint.route('/remove_stimuli')
@login_required
def remove_stimuli():

    exp_id = request.args.get('exp_id', None)
    exp_status = experiment.query.filter_by(idexperiment=exp_id).first()

    if exp_status.status != 'Hidden':
Ossi Laine's avatar
Ossi Laine committed
852

853
854
855
        flash("Experiment is public. Cannot modify structure.")

        return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
856

857
    else:
Ossi Laine's avatar
Ossi Laine committed
858

859
860
        remove_id = request.args.get('idpage', None)
        remove_page = page.query.filter_by(idpage=remove_id).first()
Ossi Laine's avatar
Ossi Laine committed
861
862
863
864
865
        experiment_pages = page.query.filter_by(
            experiment_idexperiment=exp_id).all()

        # if stimulustype is text, the stimulus itself is text on the database, other stimulus types are real files
        # on the server and need to be deleted
866
        if remove_page.type != 'text':
Ossi Laine's avatar
Ossi Laine committed
867
868

            # helper variable
869
            do_not_delete_file = 'False'
Ossi Laine's avatar
Ossi Laine committed
870
871

            # if the file to be deleted is in duplicate use of another page then we won't delete the file
872
873
874
            for a in range(len(experiment_pages)):
                if experiment_pages[a].media == remove_page.media and experiment_pages[a].idpage != remove_page.idpage:
                    do_not_delete_file = 'True'
Ossi Laine's avatar
Ossi Laine committed
875
876

            # If no other page is using the file then lets remove it
877
878
879
            if do_not_delete_file == 'False':
                target = os.path.join(APP_ROOT, remove_page.media)
                os.remove(target)
Ossi Laine's avatar
Ossi Laine committed
880

Ossi Laine's avatar
Ossi Laine committed
881
            # remove slider answer from this page
Ossi Laine's avatar
Ossi Laine committed
882
883
            remove_question_answers = answer.query.filter_by(
                page_idpage=remove_page.idpage).all()
Ossi Laine's avatar
Ossi Laine committed
884
885
886
            remove_rows(remove_question_answers)

            # remove embody answer from this page
Ossi Laine's avatar
Ossi Laine committed
887
888
            remove_embody_answers = embody_answer.query.filter_by(
                page_idpage=remove_page.idpage).all()
Ossi Laine's avatar
Ossi Laine committed
889
890
            remove_rows(remove_embody_answers)

891
892
            db.session.delete(remove_page)
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
893

894
            return redirect(url_for('experiment.view', exp_id=exp_id))
Ossi Laine's avatar
Ossi Laine committed
895

896
        if remove_page.type == 'text':
Ossi Laine's avatar
Ossi Laine committed
897

Ossi Laine's avatar
Ossi Laine committed
898
            # remove slider answer from this page
Ossi Laine's avatar
Ossi Laine committed
899
900
            remove_question_answers = answer.query.filter_by(
                page_idpage=remove_page.idpage).all()
Ossi Laine's avatar
Ossi Laine committed
901
902
903
            remove_rows(remove_question_answers)

            # remove embody answer from this page
Ossi Laine's avatar
Ossi Laine committed
904
905
            remove_embody_answers = embody_answer.query.filter_by(
                page_idpage=remove_page.idpage).all()
Ossi Laine's avatar
Ossi Laine committed
906
907
            remove_rows(remove_embody_answers)

908
909
            db.session.delete(remove_page)
            db.session.commit()
Ossi Laine's avatar
Ossi Laine committed
910

911
912
            return redirect(url_for('experiment.view', exp_id=exp_id))

Ossi Laine's avatar
Ossi Laine committed
913
        return redirect(url_for('experiment.view', exp_id=exp_id))
914
915
916
917
918
919
920


# Misc:

@experiment_blueprint.route('/statistics')
@login_required
def statistics():
921
922

    # TODO: Answers are in normal order although questions might be in randomized order
Ossi Laine's avatar
Ossi Laine committed
923

924
    exp_id = request.args.get('exp_id', None)
Ossi Laine's avatar
Ossi Laine committed
925
926
927
928
929
930

    experiment_info = experiment.query.filter_by(idexperiment=exp_id).all()
    participants = answer_set.query.filter_by(
        experiment_idexperiment=exp_id).all()

    # started and finished ratings counters
931
932
933
934
935
936
937
    started_ratings = answer_set.query.filter_by(
        experiment_idexperiment=exp_id).count()
    experiment_page_count = page.query.filter_by(
        experiment_idexperiment=exp_id).count()
    finished_ratings = answer_set.query.filter(and_(
        answer_set.answer_counter == experiment_page_count, answer_set.experiment_idexperiment == exp_id)).count()

Ossi Laine's avatar
Ossi Laine committed
938
939
940
941
942
943
    # Rating task headers
    question_headers = question.query.filter_by(
        experiment_idexperiment=exp_id).all()
    stimulus_headers = page.query.filter_by(
        experiment_idexperiment=exp_id).all()

944
945
946
947
    pages = page.query.filter_by(experiment_idexperiment=exp_id).all()
    questions = question.query.filter_by(experiment_idexperiment=exp_id).all()
    pages_and_questions = {}

948
949
    '''

950
951
    for p in pages:
        questions_list = [(p.idpage, a.idquestion) for a in questions]
Ossi Laine's avatar
Ossi Laine committed
952
953
954
955
        pages_and_questions[p.idpage] = questions_list

    # List of answers per participant in format question Stimulus ID/Question ID
    # those are in answer table as page_idpage and question_idquestion respectively
956
    slider_answers = {}
957
    for participant in participants:
Ossi Laine's avatar
Ossi Laine committed
958

959
960
        if int(participant.answer_counter) == 0:
            continue
Ossi Laine's avatar
Ossi Laine committed
961

962
963
964
965
        answers = answer.query.filter_by(
            answer_set_idanswer_set=participant.idanswer_set)\
            .order_by(answer.page_idpage)\
            .all()
966

967
968
969
970
971
972
973
        # flatten pages and questions to list of tuples (page_id, question_id)
        _questions = [
            item for sublist in pages_and_questions.values() for item in sublist]


        slider_answers[participant.session] = map_answers_to_questions(
            answers, _questions)
974
975
976
977
978

    mean = get_mean_from_slider_answers(slider_answers)
    # slider_answers['mean'] = get_mean_from_slider_answers(slider_answers)

    slider_answers = {
979
        'mean': mean
980
    }
981

982
983
984
985
    '''

    slider_answers = {}

Ossi Laine's avatar
Ossi Laine committed
986
    # Background question answers
Ossi Laine's avatar
Ossi Laine committed
987
988
    bg_questions = background_question.query.filter_by(
        experiment_idexperiment=exp_id).all()
989
990
991
    bg_answers_for_participants = {}

    for participant in participants:
992
        if participant.answer_counter > 0:
993
994
995
996
            bg_answers = background_question_answer.query.filter_by(
                answer_set_idanswer_set=participant.idanswer_set).all()
            bg_answers_list = [(a.answer) for a in bg_answers]
            bg_answers_for_participants[participant.session] = bg_answers_list
Ossi Laine's avatar
Ossi Laine committed
997
998
999
1000
1001

    # embody questions
    embody_questions = embody_question.query.filter_by(
        experiment_idexperiment=exp_id).all()

Ossi Laine's avatar
Ossi Laine committed
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
    return render_template('experiment_statistics.html',
                           experiment_info=experiment_info,
                           participants_and_answers=slider_answers,
                           pages_and_questions=pages_and_questions,
                           bg_questions=bg_questions,
                           bg_answers_for_participants=bg_answers_for_participants,
                           started_ratings=started_ratings,
                           finished_ratings=finished_ratings,
                           question_headers=question_headers,
                           stimulus_headers=stimulus_headers,
Ossi Laine's avatar
Ossi Laine committed
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
                           embody_questions=embody_questions)


@experiment_blueprint.route('/download_csv')
def download_csv():
    exp_id = request.args.get('exp_id', None)
    path = request.args.get('path', None)

    filename = "experiment_{}_{}.csv".format(
        exp_id, date.today().strftime("%Y-%m-%d"))

    path = '/tmp/' + path

    try:
        return send_file(path,
                         mimetype='text/csv',
                         as_attachment=True,
                         attachment_filename=filename)

    finally:
        os.remove(path)
1033
1034


root's avatar
root committed
1035
1036
1037
1038
1039
1040
1041
def remove_rows(rows):
    """Remove list of rows from database"""
    for a in range(len(rows)):
        db.session.delete(rows[a])
        db.session.commit()


1042
@socketio.on('connect', namespace="/create_embody")
1043
def start_create_embody():
1044
1045
    emit('success', {'connection': 'on'})

Ossi Laine's avatar
Ossi Laine committed
1046

1047
@socketio.on('draw', namespace="/create_embody")
1048
1049
1050
def create_embody(meta):
    page = meta["page"]
    embody = meta["embody"]
Ossi Laine's avatar
Ossi Laine committed
1051
    img_path = get_coordinates(page, embody)
root's avatar
root committed
1052
    app.logger.info(img_path)
Ossi Laine's avatar
Ossi Laine committed
1053
1054
    emit('end', {'path': img_path})

1055

1056
@socketio.on('connect', namespace="/download_csv")
1057
def start_download_csv():
1058
1059
1060
1061
    emit('success', {'connection': 'Start generating CSV file'})


@socketio.on('generate_csv', namespace="/download_csv")