diff --git a/app/controller/core.py b/app/controller/core.py index 9412d6f..fefda33 100644 --- a/app/controller/core.py +++ b/app/controller/core.py @@ -6,11 +6,7 @@ import random from flask import g, render_template from app.controller.controller import Controller -from app.model.decision import DecisionModel -from app.model.matter import MatterModel -from app.model.recommendation import RecommendationModel -from app.model.representative import RepresentativeModel -from app.model.stance import StanceModel +import app.model as models from sqlalchemy import desc from sqlalchemy.sql.expression import func @@ -19,26 +15,24 @@ class Core(Controller): def home(self): random.seed(int(datetime.today().timestamp()/100)) try: - g.motd = random.choice(MatterModel.query.filter_by( + g.motd = random.choice(models.MatterModel.query.filter_by( active=True ).all()) except: g.motd = None - g.rotd = random.choice(RepresentativeModel.query.filter_by( - active=True - ).all()) - g.last_decisions = DecisionModel.query.join(DecisionModel.recommendation).order_by(RecommendationModel.date.desc()).limit(10).all() - g.last_stances = StanceModel.query.order_by(StanceModel.date.desc()).limit(10).all() - return render_template("core/home.html") - - def representative(self, representative_id=None): - if representative_id is None: - representative_id = random.choice(RepresentativeModel.query.filter_by( + try: + g.rotd = random.choice(models.RepresentativeModel.query.filter_by( active=True - ).all()).id - g.representative = RepresentativeModel.query.get(representative_id) - g.title = g.representative.name - return render_template("core/representative.html") + ).all()) + except: + g.rotd = None + g.last_decisions = models.DecisionModel.query.join( + models.DecisionModel.recommendation + ).order_by(models.RecommendationModel.date.desc()).limit(10).all() + g.last_stances = models.StanceModel.query.order_by( + models.StanceModel.date.desc() + ).limit(10).all() + return render_template("core/home.html") def about(self): return render_template("core/about.html") diff --git a/app/controller/representative.py b/app/controller/representative.py new file mode 100644 index 0000000..ceb8d04 --- /dev/null +++ b/app/controller/representative.py @@ -0,0 +1,25 @@ +# encoding: utf-8 + +from datetime import datetime +import random + +from flask import g, redirect, render_template, url_for + +from app.controller.controller import Controller +import app.model as models +from sqlalchemy import desc +from sqlalchemy.sql.expression import func + + +class Representative(Controller): + def view(self, representative_id=None): + if representative_id is None: + representative_id = random.choice( + models.RepresentativeModel.query.filter_by(active=True).all() + ).id + g.representative = models.RepresentativeModel.query.get(representative_id) + if g.representative is None: + return redirect(url_for("core.home")) + g.title = g.representative.name + return render_template("representative/view.html") + diff --git a/app/routes.py b/app/routes.py index 0228c91..594b283 100644 --- a/app/routes.py +++ b/app/routes.py @@ -5,6 +5,7 @@ from app.controller.admin import admin_routes from app.controller.api.country import CountriesApi, CountryApi from app.controller.api.representative import RepresentativesApi, RepresentativeApi from app.controller.core import Core +from app.controller.representative import Representative # Adding admin endpoints @@ -14,7 +15,7 @@ for route in admin_routes: # Listing normal endpoints routes = [ ("/", Core.as_view("home")), - ("/representative/", Core.as_view("representative")), + ("/representative/", Representative.as_view("view")), ("/about", Core.as_view("about")), ("/who", Core.as_view("who")), ] diff --git a/app/view/core/representative.html b/app/view/representative/view.html similarity index 100% rename from app/view/core/representative.html rename to app/view/representative/view.html