You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.3 KiB
95 lines
3.3 KiB
from flask import Blueprint, render_template, request, redirect, url_for
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from models import db, Question, Choice
|
|
from forms import ChoiceForm, QuestionForm
|
|
|
|
# Maak de blueprint aan
|
|
admin_bp = Blueprint('admin', __name__)
|
|
|
|
# Route voor het admin-dashboard
|
|
|
|
|
|
@admin_bp.route('/')
|
|
def admin_dashboard():
|
|
# Haal alle vragen en keuzes op uit de database
|
|
questions = Question.query.all()
|
|
return render_template('admin_dashboard.html', questions=questions)
|
|
|
|
# Route voor het toevoegen van een vraag
|
|
|
|
|
|
@admin_bp.route('/add_question', methods=['GET', 'POST'])
|
|
def add_question():
|
|
form = QuestionForm()
|
|
if form.validate_on_submit():
|
|
question_text = form.text.data
|
|
new_question = Question(text=question_text)
|
|
db.session.add(new_question)
|
|
db.session.commit()
|
|
return redirect(url_for('admin.admin_dashboard'))
|
|
return render_template('add_question.html', form=form)
|
|
|
|
# Route voor het bewerken van een vraag
|
|
|
|
|
|
@admin_bp.route('/edit_question/<int:question_id>', methods=['GET', 'POST'])
|
|
def edit_question(question_id):
|
|
question = Question.query.get_or_404(question_id)
|
|
form = QuestionForm(obj=question)
|
|
if form.validate_on_submit():
|
|
question.text = form.text.data
|
|
db.session.commit()
|
|
return redirect(url_for('admin.admin_dashboard'))
|
|
return render_template('edit_question.html', form=form, question=question)
|
|
|
|
# Route voor het verwijderen van een vraag
|
|
|
|
|
|
@admin_bp.route('/delete_question/<int:question_id>', methods=['POST'])
|
|
def delete_question(question_id):
|
|
question = Question.query.get_or_404(question_id)
|
|
db.session.delete(question)
|
|
db.session.commit()
|
|
return redirect(url_for('admin.admin_dashboard'))
|
|
|
|
# Route voor het toevoegen van een keuze aan een vraag
|
|
|
|
|
|
@admin_bp.route('/add_choice/<int:question_id>', methods=['GET', 'POST'])
|
|
def add_choice(question_id):
|
|
form = ChoiceForm()
|
|
question = Question.query.get_or_404(question_id)
|
|
if form.validate_on_submit():
|
|
choice_text = form.text.data
|
|
next_question_id = form.next_question.data if form.next_question.data else None
|
|
new_choice = Choice(
|
|
text=choice_text, question_id=question.id, next_question_id=next_question_id)
|
|
db.session.add(new_choice)
|
|
db.session.commit()
|
|
return redirect(url_for('admin.edit_question', question_id=question.id))
|
|
return render_template('add_choice.html', form=form, question=question)
|
|
|
|
# Route voor het bewerken van een keuze
|
|
|
|
|
|
@admin_bp.route('/edit_choice/<int:choice_id>', methods=['GET', 'POST'])
|
|
def edit_choice(choice_id):
|
|
choice = Choice.query.get_or_404(choice_id)
|
|
form = ChoiceForm(obj=choice)
|
|
if form.validate_on_submit():
|
|
choice.text = form.text.data
|
|
choice.next_question_id = form.next_question.data if form.next_question.data else None
|
|
db.session.commit()
|
|
return redirect(url_for('admin.edit_question', question_id=choice.question_id))
|
|
return render_template('edit_choice.html', form=form, choice=choice)
|
|
|
|
# Route voor het verwijderen van een keuze
|
|
|
|
|
|
@admin_bp.route('/delete_choice/<int:choice_id>', methods=['POST'])
|
|
def delete_choice(choice_id):
|
|
choice = Choice.query.get_or_404(choice_id)
|
|
db.session.delete(choice)
|
|
db.session.commit()
|
|
return redirect(url_for('admin.edit_question', question_id=choice.question_id))
|