diff --git a/nlp4all/controllers/AdminController.py b/nlp4all/controllers/AdminController.py index 483b17f..802e0f7 100644 --- a/nlp4all/controllers/AdminController.py +++ b/nlp4all/controllers/AdminController.py @@ -1,6 +1,6 @@ """Admin controller.""" # pylint: disable=invalid-name -from flask import flash, redirect, render_template, url_for +from flask import flash, redirect, url_for from nlp4all.models.database import db_session from nlp4all.models import Organization, TweetTagCategory @@ -8,10 +8,14 @@ from nlp4all.forms.analyses import AddTweetCategoryForm from nlp4all.helpers.tweets import add_tweets_from_account +from .BaseController import BaseController -class AdminController: + +class AdminController(BaseController): """Admin Controller""" + view_subdir = "admin" + @classmethod def manage_categories(cls): """Manage categories page""" @@ -21,7 +25,7 @@ def manage_categories(cls): add_tweets_from_account(form.twitter_handle.data) flash("Added tweets from the twitter handle", "success") return redirect(url_for("admin_controller.manage_categories")) - return render_template("admin/manage_categories.html", form=form, categories=categories) + return cls.render_template("manage_categories.html", form=form, categories=categories) @classmethod def add_org(cls): @@ -34,4 +38,4 @@ def add_org(cls): db_session.commit() flash("Your organization has been created!", "success") return redirect(url_for("admin_controller.add_org")) - return render_template("admin/add_org.html", form=form, orgs=orgs) + return cls.render_template("add_org.html", form=form, orgs=orgs) diff --git a/nlp4all/controllers/AnalysesController.py b/nlp4all/controllers/AnalysesController.py index 3aae0fa..8645c58 100644 --- a/nlp4all/controllers/AnalysesController.py +++ b/nlp4all/controllers/AnalysesController.py @@ -39,12 +39,15 @@ from nlp4all.forms.analyses import BayesianRobotForms, CreateMatrixForm, TaggingForm, ThresholdForm +from .BaseController import BaseController -class AnalysesController: # pylint: disable=too-many-public-methods +class AnalysesController(BaseController): # pylint: disable=too-many-public-methods """Analyses Controller""" # @TODO break out (probably) + view_subdir = "analyses" + @classmethod def robot_summary(cls): """Robot summary page""" diff --git a/nlp4all/controllers/BaseController.py b/nlp4all/controllers/BaseController.py new file mode 100644 index 0000000..39905ab --- /dev/null +++ b/nlp4all/controllers/BaseController.py @@ -0,0 +1,17 @@ +"""For providing a base controller for all controllers to inherit from.""" # pylint: disable=invalid-name + +import typing as t +from flask import Blueprint, render_template + +class BaseController: # pylint: disable=too-few-public-methods + """Base controller for all controllers to inherit from.""" + + view_subdir = None + blueprint: Blueprint + + @classmethod + def render_template(cls, template_name: str, **context: t.Any) -> str: + """Render a template, wraps flask render to easily organize views.""" + if cls.view_subdir: + return render_template(cls.view_subdir + '/' + template_name, **context) + raise TypeError("No view_subdir set for this controller") diff --git a/nlp4all/controllers/DataSourceController.py b/nlp4all/controllers/DataSourceController.py index 7ebf499..53b8771 100644 --- a/nlp4all/controllers/DataSourceController.py +++ b/nlp4all/controllers/DataSourceController.py @@ -1,30 +1,33 @@ """Data sources.""" # pylint: disable=invalid-name # @todo: SKELETON, NOT IMPLEMENTED -from flask import redirect, render_template, url_for +from flask import redirect, url_for +from .BaseController import BaseController -class DataSourceController: # pylint: disable=too-few-public-methods +class DataSourceController(BaseController): # pylint: disable=too-few-public-methods """Data Source Controller""" + view_subdir = "datasource" + @classmethod def home(cls): """List of prepared data source""" - return render_template("datasource/data_source_list.html", title="My Data Sources") + return cls.render_template("data_source_list.html", title="My Data Sources") @classmethod def create(cls): """Upload / create page""" - return render_template("datasource/create_data_source.html", title="New Data Source") + return cls.render_template("create_data_source.html", title="New Data Source") @classmethod def configure(cls): """Specify data fields""" - return render_template( - "datasource/configure_data_source.html", + return cls.render_template( + "configure_data_source.html", title="Configure Data Source" ) @classmethod def save(cls): """Save data source""" - return redirect(url_for("datasource/data_source_controller.home")) + return redirect(url_for("data_source_controller.home")) diff --git a/nlp4all/controllers/ProjectController.py b/nlp4all/controllers/ProjectController.py index 37bdfb1..74fde44 100644 --- a/nlp4all/controllers/ProjectController.py +++ b/nlp4all/controllers/ProjectController.py @@ -1,7 +1,7 @@ """Project controller""" # pylint: disable=invalid-name from random import sample, shuffle -from flask import redirect, render_template, request, url_for +from flask import redirect, request, url_for from flask_login import current_user from nlp4all.models.database import db_session @@ -15,17 +15,21 @@ get_user_projects, ) # @TODO: this add_project CLEARLY belongs on the model, not in helpers +from .BaseController import BaseController + # from flask_mail import Message -class ProjectController: +class ProjectController(BaseController): """Project controller""" + view_subdir = "project" + @classmethod def home(cls): """Project list page""" my_projects = get_user_projects(current_user) - return render_template("project/projects.html", projects=my_projects) + return cls.render_template("projects.html", projects=my_projects) @classmethod def add_project(cls): @@ -44,7 +48,7 @@ def add_project(cls): ) project_id = a_project.id return redirect(url_for("project_controller.home", project=project_id)) - return render_template("project/add_project.html", title="Add New Project", form=form) + return cls.render_template("add_project.html", title="Add New Project", form=form) # @TODO: refactor this, i moved this function here # because it was in utils, which imported models which imported utils @@ -111,8 +115,8 @@ def project(cls): # pylint: disable=too-many-locals db_session.flush() db_session.commit() # return(redirect(url_for('project', project=project_id))) - return render_template( - "project/project.html", + return cls.render_template( + "project.html", title="About", project=a_project, analyses=analyses, diff --git a/nlp4all/controllers/SiteController.py b/nlp4all/controllers/SiteController.py index 02881bf..593e02a 100644 --- a/nlp4all/controllers/SiteController.py +++ b/nlp4all/controllers/SiteController.py @@ -1,17 +1,18 @@ """Site controller, general pages, about us, etc.""" # pylint: disable=invalid-name -from flask import render_template +from .BaseController import BaseController - -class SiteController: # pylint: disable=too-few-public-methods +class SiteController(BaseController): # pylint: disable=too-few-public-methods """Site Controller""" + view_subdir = "site" + @classmethod def home(cls): """Home page""" - return render_template("site/home.html", title="Home") + return cls.render_template("home.html", title="Home") @classmethod def about(cls): """About page""" - return render_template("site/about.html", title="About") + return cls.render_template("about.html", title="About") diff --git a/nlp4all/controllers/UserController.py b/nlp4all/controllers/UserController.py index 662a243..d2248c9 100644 --- a/nlp4all/controllers/UserController.py +++ b/nlp4all/controllers/UserController.py @@ -6,7 +6,7 @@ from PIL import Image -from flask import flash, redirect, render_template, request, url_for, Blueprint, abort +from flask import flash, redirect, request, url_for, abort from flask_login import current_user, login_user, logout_user from flask_mail import Message from flask_bcrypt import check_password_hash, generate_password_hash @@ -24,10 +24,12 @@ IMCRegistrationForm, ) -class UserController: +from .BaseController import BaseController + +class UserController(BaseController): """User Controller""" - blueprint: Blueprint + view_subdir = "user" @classmethod def account(cls): @@ -46,7 +48,7 @@ def account(cls): form.username.data = current_user.username form.email.data = current_user.email image_file = url_for("static", filename="profile_pics/" + current_user.image_file) - return render_template("user/account.html", title="Account", + return cls.render_template("account.html", title="Account", image_file=image_file, form=form) @classmethod @@ -81,7 +83,7 @@ def login(cls): return redirect(next_page) return redirect(url_for("project_controller.home")) flash("Login Unsuccessful. Please check email and password", "danger") - return render_template("user/login.html", title="Login", form=form) + return cls.render_template("login.html", title="Login", form=form) @classmethod def logout(cls): @@ -110,7 +112,7 @@ def register(cls): db_session.commit() flash("Your account has been created! You are now able to log in", "success") return redirect(url_for("login")) - return render_template("user/register.html", title="Register", form=form) + return cls.render_template("register.html", title="Register", form=form) @classmethod def reset_request(cls): @@ -123,7 +125,7 @@ def reset_request(cls): cls.send_reset_email(user) flash("An email has been sent with instructions to reset your password.", "info") return redirect(url_for("login")) - return render_template("user/reset_request.html", title="Reset Password", form=form) + return cls.render_template("reset_request.html", title="Reset Password", form=form) @classmethod def reset_token(cls, token): @@ -141,7 +143,7 @@ def reset_token(cls, token): db_session.commit() flash("Your password has been updated! You are now able to log in", "success") return redirect(url_for("login")) - return render_template("user/reset_token.html", title="Reset Password", form=form) + return cls.render_template("reset_token.html", title="Reset Password", form=form) @classmethod def register_imc(cls): @@ -179,7 +181,7 @@ def register_imc(cls): db_session.add(bayes_analysis) db_session.commit() return redirect(url_for("project_controller.home")) - return render_template("user/register_imc.html", form=form) + return cls.render_template("register_imc.html", form=form) @classmethod def send_reset_email(cls, user): diff --git a/nlp4all/views/analyses/analysis.html b/nlp4all/views/analyses/analysis.html index fba9936..1c9d24a 100644 --- a/nlp4all/views/analyses/analysis.html +++ b/nlp4all/views/analyses/analysis.html @@ -57,7 +57,7 @@

- {% include "pie_chart.html" %} + {% include "analyses/pie_chart.html" %}