From 4ec55731aa039a4676b01501125b27f94a2f4d36 Mon Sep 17 00:00:00 2001 From: Ossi Laine <ossi.laine@utu.fi> Date: Wed, 13 Mar 2019 13:59:18 +0200 Subject: [PATCH] Breaked 'task/<int:page_num>/' GET and POST requests to own methods --- .../{ => templates}/edit_bg_question.html | 0 app/task/views.py | 150 ++++++++++-------- app/templates/base.html | 15 +- 3 files changed, 92 insertions(+), 73 deletions(-) rename app/experiment/{ => templates}/edit_bg_question.html (100%) diff --git a/app/experiment/edit_bg_question.html b/app/experiment/templates/edit_bg_question.html similarity index 100% rename from app/experiment/edit_bg_question.html rename to app/experiment/templates/edit_bg_question.html diff --git a/app/task/views.py b/app/task/views.py index 0c0ccf2..c92ee9d 100644 --- a/app/task/views.py +++ b/app/task/views.py @@ -30,105 +30,127 @@ task_blueprint = Blueprint("task", __name__, static_folder='static', url_prefix='/task') -@task_blueprint.route('/<int:page_num>', methods=['GET', 'POST']) -def task(page_num): - experiment_info = experiment.query.filter_by(idexperiment=session['exp_id']).first() - rating_instruction = experiment_info.single_sentence_instruction - stimulus_size = experiment_info.stimulus_size - - #for text stimuli the size needs to be calculated since the template element utilises h1-h6 tags. - #A value of stimulus size 12 gives h1 and value of 1 gives h6 - stimulus_size_text = 7-math.ceil((int(stimulus_size)/2)) - - print(stimulus_size_text) - - pages = page.query.filter_by(experiment_idexperiment=session['exp_id']).paginate(per_page=1, page=page_num, error_out=True) - progress_bar_percentage = round((pages.page/pages.pages)*100) +def get_randomized_page(pages): #this variable is feeded to the template as empty if trial randomization is set to "off" randomized_stimulus = "" #if trial randomization is on we will still use the same functionality that is used otherwise #but we will pass the randomized pair of the page_id from trial randomization table to the task.html - if session['randomization'] == 'On': - randomized_page_id = trial_randomization.query.filter(and_(trial_randomization.answer_set_idanswer_set==session['answer_set'], trial_randomization.page_idpage==pages.items[0].idpage)).first() - #answer.query.filter(and_(answer.answer_set_idanswer_set==session['answer_set'], answer.page_idpage==session['current_idpage'])).first() - #flash("randomized page:") - #flash(randomized_page_id.randomized_idpage) - #set the stimulus to be shown if randomization is on - randomized_stimulus = page.query.filter_by(idpage=randomized_page_id.randomized_idpage).first() - - for p in pages.items: - session['current_idpage'] = p.idpage - - #slider set + randomized_page_id = trial_randomization.query.filter(and_( + trial_randomization.answer_set_idanswer_set==session['answer_set'], + trial_randomization.page_idpage==pages.items[0].idpage + )).first() + + return randomized_page_id + + +def add_slider_answer(key, value, randomized_page_id): + '''Insert slider value to database. If trial randomization is set to 'Off' + the values are inputted for session['current_idpage']. Otherwise the values + are set for the corresponding id found in the trial randomization table''' + + page_idpage = session['current_idpage'] if session['randomization'] == 'Off' else randomized_page_id.randomized_idpage + participant_answer = answer(question_idquestion=key, answer_set_idanswer_set=session['answer_set'], answer=value, page_idpage=page_idpage) + db.session.add(participant_answer) + db.session.commit() + + +@task_blueprint.route('/<int:page_num>', methods=['POST']) +def task_answer(page_num): + form = TaskForm(request.form) - categories_and_scales = {} - categories = question.query.filter_by(experiment_idexperiment=session['exp_id']).all() + pages = page.query.filter_by(experiment_idexperiment=session['exp_id']).paginate(per_page=1, page=page_num, error_out=True) - for cat in categories: - scale_list = [(cat.left, cat.right)] - categories_and_scales[cat.idquestion, cat.question] = scale_list - - form.categories1 = categories_and_scales + # TODO: determine wheter handling POST data from slider or embody!!! - #slider set form handling - if request.method == 'POST'and form.validate(): + if form.validate(): #Lets check if there are answers in database already for this page_id (eg. if user returned to previous page and tried to answer again) #If so flash ("Page has been answered already. Answers discarded"), else insert values in to db #this has to be done separately for trial randomization "on" and "off" situations - + if session['randomization'] == 'On': + randomized_page_id = get_randomized_page(pages) check_answer = answer.query.filter(and_(answer.answer_set_idanswer_set==session['answer_set'], answer.page_idpage==randomized_page_id.randomized_idpage)).first() - - if session['randomization'] == 'Off': + else: check_answer = answer.query.filter(and_(answer.answer_set_idanswer_set==session['answer_set'], answer.page_idpage==session['current_idpage'])).first() if check_answer is None: - the_time = datetime.now() the_time = the_time.replace(microsecond=0) update_answer_counter = answer_set.query.filter_by(idanswer_set=session['answer_set']).first() update_answer_counter.answer_counter = int(update_answer_counter.answer_counter) + 1 update_answer_counter.last_answer_time = the_time - - #flash("vastauksia:") - #flash(update_answer_counter.answer_counter) db.session.commit() data = request.form.to_dict() for key, value in data.items(): - #flash(key) - #flash(value) - #flash(session['current_idpage']) - - #Insert slider values to database - - #If trial randomization is set to 'Off' the values are inputted for session['current_idpage'] - #Otherwise the values are set for the corresponding id found in the trial randomization table - - if session['randomization'] == 'Off': - participant_answer = answer(question_idquestion=key, answer_set_idanswer_set=session['answer_set'], answer=value, page_idpage=session['current_idpage']) - db.session.add(participant_answer) - db.session.commit() - else: - participant_answer = answer(question_idquestion=key, answer_set_idanswer_set=session['answer_set'], answer=value, page_idpage=randomized_page_id.randomized_idpage) - db.session.add(participant_answer) - db.session.commit() + add_slider_answer(key, value, randomized_page_id) else: flash("Page has been answered already. Answers discarded") page_num=pages.next_num - if pages.has_next: - return redirect( url_for('task.task', page_num=pages.next_num)) + if not pages.has_next: + return redirect ( url_for('task.completed')) + + return redirect( url_for('task.task', page_num=pages.next_num)) + + +@task_blueprint.route('/<int:page_num>', methods=['GET']) +def task(page_num): + + experiment_info = experiment.query.filter_by(idexperiment=session['exp_id']).first() + rating_instruction = experiment_info.single_sentence_instruction + stimulus_size = experiment_info.stimulus_size + + #for text stimuli the size needs to be calculated since the template element utilises h1-h6 tags. + #A value of stimulus size 12 gives h1 and value of 1 gives h6 + stimulus_size_text = 7-math.ceil((int(stimulus_size)/2)) + + print(stimulus_size_text) + + pages = page.query.filter_by(experiment_idexperiment=session['exp_id']).paginate(per_page=1, page=page_num, error_out=True) + progress_bar_percentage = round((pages.page/pages.pages)*100) + + #this variable is feeded to the template as empty if trial randomization is set to "off" + randomized_stimulus = "" + + # if trial randomization is on we will still use the same functionality that is used otherwise + # but we will pass the randomized pair of the page_id from trial randomization table to the task.html + if session['randomization'] == 'On': + + #set the stimulus to be shown if randomization is on + randomized_page_id = get_randomized_page(pages) + randomized_stimulus = page.query.filter_by(idpage=randomized_page_id.randomized_idpage).first() - return redirect ( url_for('task.completed')) + for p in pages.items: + session['current_idpage'] = p.idpage + + #slider set + form = TaskForm(request.form) + categories_and_scales = {} + categories = question.query.filter_by(experiment_idexperiment=session['exp_id']).all() + + for cat in categories: + scale_list = [(cat.left, cat.right)] + categories_and_scales[cat.idquestion, cat.question] = scale_list + + form.categories1 = categories_and_scales - return render_template('task.html', pages=pages, progress_bar_percentage=progress_bar_percentage, form=form, randomized_stimulus=randomized_stimulus, rating_instruction=rating_instruction, stimulus_size=stimulus_size, stimulus_size_text=stimulus_size_text) + return render_template( + 'task.html', + pages=pages, + progress_bar_percentage=progress_bar_percentage, + form=form, + randomized_stimulus=randomized_stimulus, + rating_instruction=rating_instruction, + stimulus_size=stimulus_size, + stimulus_size_text=stimulus_size_text + ) @task_blueprint.route('/completed') @@ -156,7 +178,7 @@ def continue_task(): participant = answer_set.query.filter(and_(answer_set.session==form.participant_id.data, answer_set.experiment_idexperiment==exp_id)).first() if participant is None: flash('Invalid ID') - return redirect(url_for('task.continue', exp_id=exp_id)) + return redirect(url_for('task.continue_task', exp_id=exp_id)) #flash('Login requested for participant {}'.format(form.participant_id.data)) diff --git a/app/templates/base.html b/app/templates/base.html index b6f0350..89bb793 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -8,14 +8,14 @@ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> - <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> - <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> - <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> - <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> + <link rel="stylesheet" href="{{ url_for('static', filename='lib/css/bootstrap.min.css') }}" ></link> + <script src="{{ url_for('static', filename='lib/js/jquery-3.3.1.slim.min.js') }}" ></script> + <script src="{{ url_for('static', filename='lib/js/popper.min.js') }}" ></script> + <script src="{{ url_for('static', filename='lib/js/bootstrap.min.js') }}" ></script> <title>Onni</title> <!-- Bootstrap core CSS --> - <link href="../../dist/css/bootstrap.min.css" rel="stylesheet"> + <link href="/lib/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> @@ -26,6 +26,7 @@ <header> <!-- Navigation --> <nav class="navbar navbar-expand bg-light fixed-top"> + {% if pages %} @@ -144,10 +145,6 @@ <!-- Bootstrap core JavaScript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> - <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> - <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-slim.min.js"><\/script>')</script> - <script src="../../assets/js/vendor/popper.min.js"></script> - <script src="../../dist/js/bootstrap.min.js"></script> </body> </html> -- GitLab