2022-06-22 01:57:45 +01:00
|
|
|
from ..forms.admin import EditDataset
|
|
|
|
from ..models import Dataset, User
|
2022-08-17 16:37:03 +01:00
|
|
|
from ..tools.data import check_dataset_exists
|
2022-08-20 10:56:43 +01:00
|
|
|
from ..tools.forms import get_dataset_choices, send_errors_to_client
|
|
|
|
from ..tools.logs import write
|
2022-06-22 01:57:45 +01:00
|
|
|
|
2022-08-20 10:56:43 +01:00
|
|
|
from flask import Blueprint, jsonify, render_template
|
|
|
|
from flask.helpers import abort, flash, redirect, request, url_for
|
2022-06-22 01:57:45 +01:00
|
|
|
from flask_login import login_required
|
2022-08-20 10:56:43 +01:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2022-06-18 09:39:31 +01:00
|
|
|
|
|
|
|
editor = Blueprint(
|
|
|
|
name='editor',
|
|
|
|
import_name=__name__,
|
|
|
|
template_folder='templates',
|
|
|
|
static_folder='static'
|
|
|
|
)
|
|
|
|
|
2022-06-22 01:57:45 +01:00
|
|
|
@editor.route('/', methods=['GET','POST'])
|
|
|
|
@login_required
|
2022-06-18 09:39:31 +01:00
|
|
|
def _editor():
|
2022-06-22 01:57:45 +01:00
|
|
|
form = EditDataset()
|
|
|
|
form.dataset.choices = get_dataset_choices()
|
|
|
|
if request.method == 'POST':
|
|
|
|
if form.validate_on_submit():
|
|
|
|
id = request.form.get('dataset')
|
|
|
|
return jsonify({'success': 'Selected dataset', 'redirect_to': url_for('editor._editor_console', id=id)}),200
|
|
|
|
return send_errors_to_client(form=form)
|
|
|
|
form.process()
|
|
|
|
return render_template('/editor/index.html', form=form)
|
|
|
|
|
|
|
|
@editor.route('/<string:id>/')
|
2022-08-17 16:37:03 +01:00
|
|
|
@check_dataset_exists
|
2022-06-22 01:57:45 +01:00
|
|
|
@login_required
|
|
|
|
def _editor_console(id:str=None):
|
2022-08-20 10:56:43 +01:00
|
|
|
try:
|
|
|
|
dataset = Dataset.query.filter_by(id=id).first()
|
|
|
|
datasets = Dataset.query.count()
|
|
|
|
users = User.query.all()
|
2022-08-20 14:47:46 +01:00
|
|
|
except Exception as exception:
|
2022-08-20 10:56:43 +01:00
|
|
|
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
|
|
|
|
return abort(500)
|
2022-06-22 01:57:45 +01:00
|
|
|
if not dataset:
|
|
|
|
flash('Invalid dataset ID.', 'error')
|
|
|
|
return redirect(url_for('admin._questions'))
|
|
|
|
return render_template('/editor/console.html', dataset=dataset, datasets=datasets, users=users)
|