Commit fe529331 authored by Ossi Laine's avatar Ossi Laine
Browse files

Refactoring routes.py file to own blueprints (create, experiment, task)

parent 22437ea1
......@@ -9,4 +9,5 @@ scrap_script.py
config.py
*.pyc
*.db
/embody
[submodule "embody"]
path = embody
url = https://version.aalto.fi/gitlab/eglerean/embody.git
from flask import Flask
import pymysql
from flask import Flask, request, session, flash
from flask_bootstrap import Bootstrap
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_babel import Babel
from flask import request
from flask import session
from flask import flash
import pymysql
from config import Config
app = Flask(__name__)
#app.config['BABEL_DEFAULT_LOCALE'] = 'fin'
#app.config['BABEL_TRANSLATION_DIRECTORIES'] ='C:/Users/Timo/git/pet-rating/app/translations'
babel = Babel(app)
......@@ -38,13 +40,10 @@ def get_locale():
if session['lang'] == 'zh':
session['language'] = 'Chinese'
return session.get('lang', 'en')
"""
@babel.localeselector
def get_locale():
......@@ -73,6 +72,14 @@ migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
# Register blueprints
from .task.views import task_blueprint
from .experiment.views import experiment_blueprint
from .create.views import create_blueprint
app.register_blueprint(task_blueprint)
app.register_blueprint(experiment_blueprint)
app.register_blueprint(create_blueprint)
app.secret_key = 'random string'
"""app.secret_key = os.urandom(24)"""
......
import os
from datetime import datetime
from flask import (
Flask,
render_template,
request,
session,
flash,
redirect,
url_for,
Blueprint
)
from flask_login import login_required
from app.routes import APP_ROOT
from app import app, db
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
from app.models import user, trial_randomization
from app.forms import (
CreateExperimentForm, CreateBackgroundQuestionForm,
CreateQuestionForm, UploadStimuliForm
)
create_blueprint = Blueprint("create", __name__,
template_folder='templates',
#static_folder='static',
url_prefix='/create')
@create_blueprint.route('/experiment', methods=['GET', 'POST'])
@login_required
def create_experiment():
form = CreateExperimentForm(request.form)
if request.method == 'POST' and form.validate():
the_time = datetime.now()
the_time = the_time.replace(microsecond=0)
new_exp = experiment(name=request.form['name'], instruction=request.form['instruction'], language=request.form['language'], status='Hidden', randomization='Off', single_sentence_instruction=request.form['single_sentence_instruction'], short_instruction=request.form['short_instruction'], creator_name=request.form['creator_name'], is_archived='False', creation_time=the_time, stimulus_size='7', consent_text=request.form['consent_text'], use_forced_id='Off')
db.session.add(new_exp)
db.session.commit()
exp_id = new_exp.idexperiment
#data = request.form.to_dict()
#for key, value in data.items():
#tähän db insertit
#flash(key)
#flash(value)
#flash('{}'.format(form.name.data))
#Input registration page answers to database
# participant_background_question_answers = background_question_answer(answer_set_idanswer_set=session['answer_set'], answer=value, background_question_idbackground_question=key)
# db.session.add(participant_background_question_answers)
# db.session.commit()
return redirect(url_for('create.experiment_bgquestions', exp_id=exp_id))
return render_template('create_experiment.html', form=form)
@create_blueprint.route('/experiment_bgquestions', methods=['GET', 'POST'])
@login_required
def experiment_bgquestions():
exp_id = request.args.get('exp_id', None)
form = CreateBackgroundQuestionForm(request.form)
if request.method == 'POST' and form.validate():
str = form.bg_questions_and_options.data
#Split the form data into a list that separates questions followed by the corresponding options
str_list = str.split('/n')
#Iterate through the questions and options list
for a in range(len(str_list)):
#Split the list cells further into questions and options
list = str_list[a].split(';')
#Input the first item of the list as a question in db and the items followed by that as options for that question
for x in range(len(list)):
if x == 0:
add_bgquestion = background_question(background_question=list[x], experiment_idexperiment=exp_id)
db.session.add(add_bgquestion)
db.session.commit()
else:
add_bgq_option = background_question_option(background_question_idbackground_question=add_bgquestion.idbackground_question, option=list[x])
db.session.add(add_bgq_option)
db.session.commit()
return redirect(url_for('create.experiment_questions', exp_id=exp_id))
return render_template('create_experiment_bgquestions.html', form=form, exp_id=exp_id)
@create_blueprint.route('/experiment_questions', methods=['GET', 'POST'])
@login_required
def experiment_questions():
# TODO: add embody -type here
exp_id = request.args.get('exp_id', None)
form = CreateQuestionForm(request.form)
if request.method == 'POST' and form.validate():
str = form.questions_and_options.data
str_list = str.split('/n')
for a in range(len(str_list)):
list = str_list[a].split(';')
#If there are the wrong amount of values for any of the the slider input values
#redirect back to the form
if len(list) != 3:
flash("Error Each slider must have 3 parameters separated by ; Some slider has:")
flash(len(list))
return redirect(url_for('create.experiment_questions', exp_id=exp_id))
#If all the slider inputs were of length 3 items
#we can input them to db
for a in range(len(str_list)):
list = str_list[a].split(';')
add_question = question(experiment_idexperiment=exp_id, question=list[0], left=list[1], right=list[2])
db.session.add(add_question)
db.session.commit()
return redirect(url_for('create.experiment_upload_stimuli', exp_id=exp_id))
return render_template('create_experiment_questions.html', form=form)
@create_blueprint.route('/experiment_upload_stimuli', methods=['GET', 'POST'])
@login_required
def experiment_upload_stimuli():
exp_id = request.args.get('exp_id', None)
form = UploadStimuliForm(request.form)
if request.method == 'POST' and form.validate():
#If stimulus type is text lets parse the information and insert it to database
if form.type.data == 'text':
string = form.text.data
str_list = string.split('/n')
for a in range(len(str_list)):
add_text_stimulus = page(experiment_idexperiment=exp_id, type='text', text=str_list[a], media='none')
db.session.add(add_text_stimulus)
db.session.commit()
return redirect(url_for('experiment.view', exp_id=exp_id))
else:
#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)
path = 'static/experiment_stimuli/' + str(exp_id)
target = os.path.join(APP_ROOT, path)
if not os.path.isdir(target):
os.mkdir(target)
#This returns a list of filenames: request.files.getlist("file")
for file in request.files.getlist("file"):
#save files in the correct folder
filename = file.filename
destination = "/".join([target, filename])
file.save(destination)
#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)
db.session.add(new_page)
db.session.commit()
#flash("Succes!")
return redirect(url_for('experiment.view', exp_id=exp_id))
return redirect(url_for('create.experiment_upload_stimuli', exp_id=exp_id))
return render_template('create_experiment_upload_stimuli.html', form=form)
# EOF
\ No newline at end of file
......@@ -19,24 +19,15 @@
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" id="file" name="file" accept="audio/*,image/*, .mp4" multiple>
<!-- TODO add hidden type field { stimulus_type } else this will be None!! -->
<label class="custom-file-label" for="upload_file">Choose file</label>
</div>
</div>
{% endif %}
<br>
<button type="submit" class="btn btn-primary">Submit</button>
<a class="btn btn-primary" href="{{ request.referrer }}" role="button">Cancel</a>
......
......@@ -44,7 +44,7 @@
</div>
<br>
<button type="submit" class="btn btn-primary">Update</button>
<a class="btn btn-primary" href="{{ url_for('view_experiment', exp_id=exp_id) }}" role="button">Cancel</a>
<a class="btn btn-primary" href="{{ url_for('experiment.view', exp_id=exp_id) }}" role="button">Cancel</a>
</form>
......
......@@ -19,7 +19,7 @@
<input type="text" class="form-control" id="remove" name="remove" required>
<br>
<button type="submit" class="btn btn-primary">Remove</button>
<a class="btn btn-primary" href="{{ url_for('view_experiment', exp_id=exp_id) }}" role="button">Cancel</a>
<a class="btn btn-primary" href="{{ url_for('experiment.view', exp_id=exp_id) }}" role="button">Cancel</a>
</form>
......
......@@ -32,7 +32,7 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="{{ url_for('remove_experiment', exp_id=exp.idexperiment) }}" role="button">Yes, remove</a>
<a class="btn btn-primary" href="{{ url_for('experiment.remove', exp_id=exp.idexperiment) }}" role="button">Yes, remove</a>
</div>
</div>
</div>
......@@ -45,12 +45,12 @@
<td>{{ exp.status }}</td>
<td>
{% if exp.status == 'Hidden' %}
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('publish_experiment', exp_id=exp.idexperiment) }}" role="button">Publish (visible)</a>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('private_experiment', exp_id=exp.idexperiment) }}" role="button">Publish (private)</a>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.publish', exp_id=exp.idexperiment) }}" role="button">Publish (visible)</a>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.private', exp_id=exp.idexperiment) }}" role="button">Publish (private)</a>
</td>
{% endif %}
{% if exp.status != 'Hidden' %}
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('hide_experiment', exp_id=exp.idexperiment) }}" role="button">Unpublish</a></td>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.hide', exp_id=exp.idexperiment) }}" role="button">Unpublish</a></td>
{% endif %}
</tr>
......@@ -81,10 +81,10 @@
<td>{{ exp.randomization }}</td>
<td nowrap>
{% if exp.randomization == 'Off' %}
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('enable_randomization', exp_id=exp.idexperiment) }}" role="button">Enable</a></td>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.randomization', exp_id=exp.idexperiment, set='On') }}" role="button">Enable</a></td>
{% endif %}
{% if exp.randomization == 'On' %}
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('disable_randomization', exp_id=exp.idexperiment) }}" role="button">Disable</a></td>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.randomization', exp_id=exp.idexperiment, set='Off') }}" role="button">Disable</a></td>
{% endif %}
</td>
......@@ -93,13 +93,13 @@
<td>{{ exp.use_forced_id }}</td>
<td nowrap>
{% if exp.use_forced_id == 'Off' %}
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('enable_forced_id', exp_id=exp.idexperiment) }}" role="button">Enable</a>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.set_forced_id', exp_id=exp.idexperiment, set='On') }}" role="button">Enable</a>
{% endif %}
{% if exp.use_forced_id == 'On' %}
<a class="btn btn-primary w-50 btn-sm btn-info" href="{{ url_for('disable_forced_id', exp_id=exp.idexperiment) }}" role="button">Disable</a>
<a class="btn btn-primary w-50 btn-sm btn-info" href="{{ url_for('view_forced_id_list', exp_id=exp.idexperiment) }}" role="button">Check ID</a>
<a class="btn btn-primary w-50 btn-sm btn-info" href="{{ url_for('experiment.set_forced_id', exp_id=exp.idexperiment, set='Off') }}" role="button">Disable</a>
<a class="btn btn-primary w-50 btn-sm btn-info" href="{{ url_for('experiment.view_forced_id_list', exp_id=exp.idexperiment) }}" role="button">Check ID</a>
{% endif %}
</td>
......@@ -108,9 +108,9 @@
<td> Research bulletin:</td>
<td> {{ exp.research_notification_filename }}</td>
{% if exp.research_notification_filename %}
<td> <a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('remove_research_notification', exp_id=exp.idexperiment) }}" role="button">Remove</a></td>
<td> <a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.remove_research_notification', exp_id=exp.idexperiment) }}" role="button">Remove</a></td>
{% else %}
<td> <a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('upload_research_notification', exp_id=exp.idexperiment) }}" role="button">Upload</a></td>
<td> <a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.upload_research_notification', exp_id=exp.idexperiment) }}" role="button">Upload</a></td>
{% endif %}
</tr>
......@@ -121,7 +121,7 @@
<td class="text-justify">{{ exp.instruction }}</td>
<td nowrap>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('edit_experiment', exp_id=exp.idexperiment) }}" role="button">Edit properties</a>
<a class="btn btn-primary btn-block btn-sm btn-info" href="{{ url_for('experiment.edit', exp_id=exp.idexperiment) }}" role="button">Edit properties</a>
</td>
</tr>
<tr>
......@@ -190,12 +190,12 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="{{ url_for('remove_bg_question', idbackground_question=options[0], exp_id=exp_id) }}" role="button">Yes, remove</a>
<a class="btn btn-primary" href="{{ url_for('experiment.remove_bg_question', idbackground_question=options[0], exp_id=exp_id) }}" role="button">Yes, remove</a>
</div>
</div>
</div>
</div>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('edit_bg_question', idbackground_question=options[0]) }}" role="button">Edit</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.edit_bg_question', idbackground_question=options[0]) }}" role="button">Edit</a>
</td>
</tr>
......@@ -206,7 +206,7 @@
<table class="table">
<tbody>
<td class="text-nowrap align-bottom text-right col-8">
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('add_bg_question', exp_id=exp_id) }}" role="button">Add more</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.add_bg_question', exp_id=exp_id) }}" role="button">Add more</a>
</td>
</tbody>
</table>
......@@ -214,6 +214,9 @@
<h1 class="container mt-5 display-4 text-left"><br>Rating set:</h1>
<hr>
TODO: add embody tool
<hr>
<br>
{% for category in categories1 %}
......@@ -247,12 +250,12 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="{{ url_for('remove_question', idquestion=category[0], exp_id=exp_id) }}" role="button">Yes, remove</a>
<a class="btn btn-primary" href="{{ url_for('experiment.remove_question', idquestion=category[0], exp_id=exp_id) }}" role="button">Yes, remove</a>
</div>
</div>
</div>
</div>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('edit_question', idquestion=category[0]) }}" role="button">Edit</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.edit_question', idquestion=category[0]) }}" role="button">Edit</a>
</div>
......@@ -264,7 +267,7 @@
<table class="table">
<tbody>
<td class="text-nowrap align-bottom text-right col-8">
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('add_questions', exp_id=exp_id) }}" role="button">Add more</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.add_questions', exp_id=exp_id) }}" role="button">Add more</a>
</td>
</tbody>
</table>
......@@ -304,12 +307,12 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="{{ url_for('remove_page', idpage=page.idpage, exp_id=exp_id) }}" role="button">Yes, remove</a>
<a class="btn btn-primary" href="{{ url_for('experiment.remove_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Yes, remove</a>
</div>
</div>
</div>
</div>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('edit_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Edit</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.edit_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Edit</a>
</td>
</tr>
{% endfor %}
......@@ -317,7 +320,7 @@
<table class="table">
<tbody>
<td class="text-nowrap align-bottom text-right col-8">
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('add_stimuli', exp_id=exp_id, stimulus_type=mtype.type) }}" role="button">Add more</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.add_stimuli', exp_id=exp_id, stimulus_type=mtype.type) }}" role="button">Add more</a>
</td>
</tbody>
</table>
......@@ -346,12 +349,12 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" href="{{ url_for('remove_page', idpage=page.idpage, exp_id=exp_id) }}" role="button">Yes, remove</a>
<a class="btn btn-primary" href="{{ url_for('experiment.remove_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Yes, remove</a>
</div>
</div>
</div>
</div>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('edit_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Replace</a>
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.edit_stimuli', idpage=page.idpage, exp_id=exp_id) }}" role="button">Replace</a>
</td>
</tr>
......@@ -360,7 +363,8 @@
<table class="table">
<tbody>
<td class="text-nowrap align-bottom text-right col-8">
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('add_stimuli', exp_id=exp_id, stimulus_type=mtype.type) }}" role="button">Add more</a>
{{ mtype }}
<a class="btn btn-primary btn-sm btn-info" href="{{ url_for('experiment.add_stimuli', exp_id=exp_id, stimulus_type=mtype.type) }}" role="button">Add more</a>
</td>
</tbody>
</table>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment