Add a list of unhealthy questions to the admin homepage

This commit is contained in:
Matthew Welch 2021-04-15 16:47:12 -07:00
parent cf85a316e3
commit a0bac0ebf7
4 changed files with 30 additions and 5 deletions

View File

@ -11,7 +11,8 @@ Admin = Blueprint("admin", __name__, url_prefix="/admin", template_folder="templ
@Admin.route("/") @Admin.route("/")
@roles_required("admin") @roles_required("admin")
def index(): def index():
return render_template("admin/index.html", title="admin") questions = database.get_unhealthy_questions()
return render_template("admin/index.html", title="admin", questions=questions)
@Admin.route("/questions/") @Admin.route("/questions/")

View File

@ -1,5 +1,9 @@
{% extends "admin/base.html" %} {% extends "admin/base.html" %}
{% block body %} {% block body %}
<p>This is the admin home page</p> <ul>
{% for question in questions %}
<li><a href="{{ url_for("admin.edit_question", question_id=question.question_id) }}">{{ question.question_id }}</a></li>
{% endfor %}
</ul>
{% endblock %} {% endblock %}

View File

@ -1,16 +1,22 @@
import os
from flask import current_app, g from flask import current_app, g
from flask_security import UserMixin, RoleMixin from flask_security import UserMixin, RoleMixin
import sqlalchemy import sqlalchemy
from typing import Union, Optional, Literal, Type, List, Tuple from typing import Union, Optional, Literal, Type, List, Tuple
import random import random
from sqlalchemy import Column, JSON, String, Integer, create_engine, ForeignKey, func, Boolean, UnicodeText, DateTime from sqlalchemy import Column, JSON, String, Integer, create_engine, ForeignKey, func, Boolean, UnicodeText, DateTime
from sqlalchemy.sql.expression import or_, and_
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import sessionmaker, relationship, scoped_session from sqlalchemy.orm import sessionmaker, relationship, scoped_session
from werkzeug.utils import import_string
config = import_string(os.environ.get("CONFIGURATION_SETUP"))
engine = create_engine(config.DB_URL, pool_size=20)
def get_scoped_session(): def get_scoped_session():
engine = create_engine(current_app.config["DB_URL"])
session_factory = sessionmaker(bind=engine) session_factory = sessionmaker(bind=engine)
Base.query = scoped_session(session_factory).query_property() # This is for compatibility with Flask-Security-Too which assumes usage of Flask-Sqlalchemy Base.query = scoped_session(session_factory).query_property() # This is for compatibility with Flask-Security-Too which assumes usage of Flask-Sqlalchemy
return scoped_session(session_factory) return scoped_session(session_factory)
@ -226,7 +232,7 @@ def get_category_count(category: Union[Type[MultipleChoice], Type[HiddenAnswer],
def get_question(category: Union[Type[MultipleChoice], Type[HiddenAnswer], Type[AllQuestions]], question_id: int) -> \ def get_question(category: Union[Type[MultipleChoice], Type[HiddenAnswer], Type[AllQuestions]], question_id: int) -> \
Optional[Union[MultipleChoice, HiddenAnswer, AllQuestions]]: Optional[Union[MultipleChoice, HiddenAnswer, AllQuestions]]:
session = get_session() session = get_session()
return session.query(category).filter(category.question_id == question_id).one_or_none() return session.query(category).filter(category.question_id == question_id).one_or_none()
@ -281,6 +287,18 @@ def query_all_questions(offset, limit, query: dict = None, sort=None, order=None
return questions, count return questions, count
def get_unhealthy_questions() -> List[AllQuestions]:
session = get_session()
questions = []
missing_category_questions = session.query(AllQuestions).filter(and_(AllQuestions.hidden_answer == None, AllQuestions.multiple_choice == None)).all()
multiple_choice_difficulty = session.query(AllQuestions).filter(AllQuestions.multiple_choice.has(MultipleChoice.multiple_choice_difficulty == None)).all()
hidden_answer_difficulty = session.query(AllQuestions).filter(AllQuestions.hidden_answer.has(HiddenAnswer.hidden_answer_difficulty == None)).all()
questions.extend(missing_category_questions)
questions.extend(multiple_choice_difficulty)
questions.extend(hidden_answer_difficulty)
return questions
def update_question(question_id, data): def update_question(question_id, data):
session = get_session() session = get_session()
question: AllQuestions = session.query(AllQuestions).get(question_id) question: AllQuestions = session.query(AllQuestions).get(question_id)

View File

@ -1,2 +1,4 @@
runtime: python39 runtime: python39
entrypoint: gunicorn -b :$PORT QuizTheWord.app:app entrypoint: gunicorn -b :$PORT QuizTheWord.app:app
env_variables:
CONFIGURATION_SETUP: "QuizTheWord.config.Production"