Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed my mind, now even easier! #52

Merged
merged 1 commit into from
Nov 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions nlp4all/controllers/AdminController.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"""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
from nlp4all.forms.admin import AddOrgForm
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"""
Expand All @@ -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):
Expand All @@ -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)
5 changes: 4 additions & 1 deletion nlp4all/controllers/AnalysesController.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
17 changes: 17 additions & 0 deletions nlp4all/controllers/BaseController.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 10 additions & 7 deletions nlp4all/controllers/DataSourceController.py
Original file line number Diff line number Diff line change
@@ -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"))
16 changes: 10 additions & 6 deletions nlp4all/controllers/ProjectController.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions nlp4all/controllers/SiteController.py
Original file line number Diff line number Diff line change
@@ -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")
20 changes: 11 additions & 9 deletions nlp4all/controllers/UserController.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions nlp4all/views/analyses/analysis.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<p><span id='cat' type='hidden'></span></p>

<div class="pie-section" id="piechart" ondrop="drop(event)" ondragover="allowDrop(event)" >
{% include "pie_chart.html" %}
{% include "analyses/pie_chart.html" %}

<style>
.pie-section {
Expand Down Expand Up @@ -86,7 +86,7 @@
</div>
</div> -->

{% include "draggable.html" %}
{% include "analyses/draggable.html" %}

<!-- <div class="content-section">
<form method="POST" action="">
Expand Down Expand Up @@ -120,15 +120,15 @@
<div id="add-annotation" class="content-section" style="display: none;">
<h4>Add annotations to the tweet</h4>
{% set tag_list = tag_list %}
{% include "annotation2.html" %}
{% include "analyses/annotation2.html" %}
</div>
{% endif %}

{%if analysis.annotate == 3 %}
<div id="add-annotation" class="content-section" style="display: none;">
<h4>Add annotations to the tweet</h4>
{% set tag_list = tag_list %}
{% include "annotation.html" %}
{% include "analyses/annotation.html" %}
</div>
{% endif %}
</div>
Expand All @@ -148,7 +148,7 @@ <h4>Add annotations to the tweet</h4>
<div class="content-section">
<h4>Bayesian probabilities</h4>

{% include "bar_chart_jq.html" %}
{% include "analyses/bar_chart_jq.html" %}
<style type="text/css">
#chart {
margin-left:auto;margin-right:auto;width:50%;height:45%;
Expand Down
8 changes: 4 additions & 4 deletions nlp4all/views/analyses/annotation_summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h4 id="headline">Now showing the tag: {{ tag }} </h4>
<h4 id="headline">Table for tweets using the annotation tag <b>{{ tag }}</b> </h4>
{% set table_data = ann_table %}
{% set table_name = "tweet_annotations" %}
{% include "data_table.html" %}
{% include "analyses/data_table.html" %}
</div>

</div>
Expand All @@ -56,7 +56,7 @@ <h3>Overall use of tags</h3>
<div class="all-annotations" id="chart">
{% set chart_data = allann_table %}
{% set table_name = "all_annotations" %}
{% include "bar_chart.html" %}
{% include "analyses/bar_chart.html" %}
<style>
.all-annotations {
margin: auto;
Expand All @@ -73,7 +73,7 @@ <h3>Overall use of tags</h3>
</div>
<div class="col-md-6">
See all annotated tweets and annotations:
{% include "annotated_tweets.html" %}
{% include "analyses/annotated_tweets.html" %}
</div>

</div>
Expand All @@ -94,7 +94,7 @@ <h3>Annotations with the tag {{ tag }}</h3>
</div>
{% endfor %}
</table>
{% include "annotations_per_tweet_jq.html" %}
{% include "analyses/annotations_per_tweet_jq.html" %}

</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions nlp4all/views/analyses/annotations.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</table>
</div>

{% include "annotations_colors.html" %}
{% include "analyses/annotations_colors.html" %}

</div>
<div class="col-md-4">
Expand All @@ -47,7 +47,7 @@
<button id="hide-btn" onclick=hide() class="btn">Hide</button>
</div>
</div>
{% include "annotation_delete.html" %}
{% include "analyses/annotation_delete.html" %}
<style>
.table-delete {
font-size: 80%;
Expand Down
2 changes: 1 addition & 1 deletion nlp4all/views/analyses/annotations_colors.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h3>Your annotations</h3>
<h3>Annotations in this tweet:</h3>
{% set table_data = ann_dict[ann.id] %}
{% set table_name = "tweet_annotations" %}
{% include "data_table.html" %}
{% include "analyses/data_table.html" %}


{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion nlp4all/views/analyses/highscore.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

{% set table_name = 'HighScore' %}
{% set table_data = table_data %}
{% include "data_table.html" %}
{% include "analyses/data_table.html" %}


{{ include }}
Expand Down
2 changes: 1 addition & 1 deletion nlp4all/views/analyses/matrix.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h1> Confusion Matrix </h1>
{% set matrix = matrix %}
{% set cat_names = cat_names %}
{% set counts = counts %}
{% include "matrix_template.html" %}
{% include "analyses/matrix_template.html" %}

</div>

Expand Down
Loading