Commit 6edd2dd9 authored by Mindiell's avatar Mindiell
Browse files

feat: Added some filters on representatives page

parent 13ce1bb4
Pipeline #2752 failed with stage
in 0 seconds
......@@ -7,19 +7,59 @@ from sqlalchemy.sql.expression import func, or_
from app.controller.controller import Controller
from app.form.representative import RepresentativeForm
from app.model.constituency import Constituency as ConstituencyModel
from app.model.group import Group as GroupModel
from app.model.mandate import Mandate as MandateModel
from app.model.membership import Membership as MembershipModel
from app.model.representative import Representative as RepresentativeModel
class Representative(Controller):
def index(self):
g.form = RepresentativeForm()
representatives = RepresentativeModel.query
# Select between each actual constituency
g.form.constituency.choices = [(0, "All")] + [
(constituency.id, "%s (%s)" % (constituency.code, constituency.name))
for constituency in ConstituencyModel.query.join(MandateModel)
.filter(MandateModel.enddate >= datetime.today())
.filter(MandateModel.startdate <= datetime.today())
.order_by("name")
.all()
]
# Select between each actual group
g.form.group.choices = [(0, "All")] + [
(group.id, group.name)
for group in GroupModel.query.join(MembershipModel)
.filter(MembershipModel.enddate >= datetime.today())
.filter(MembershipModel.startdate <= datetime.today())
.order_by("name")
.all()
]
representatives = RepresentativeModel.query.join(MandateModel).join(
MembershipModel
)
if g.form.validate_on_submit():
representatives = representatives.filter(
RepresentativeModel.name.like("%%%s%%" % g.form.name.data)
)
g.representatives = representatives.limit(20).all()
# .order_by(func.random())
if g.form.name.data != "":
representatives = representatives.filter(
RepresentativeModel.name.like("%%%s%%" % g.form.name.data)
)
if g.form.constituency.data != 0:
representatives = (
representatives.filter(
MandateModel.constituency_id == g.form.constituency.data
)
.filter(MandateModel.enddate >= datetime.today())
.filter(MandateModel.startdate <= datetime.today())
)
if g.form.group.data != 0:
representatives = (
representatives.filter(
MembershipModel.group_id == g.form.group.data
)
.filter(MembershipModel.enddate >= datetime.today())
.filter(MembershipModel.startdate <= datetime.today())
)
g.representatives = representatives.distinct().limit(10).all()
g.today = datetime.today()
return render_template("representative/index.html")
......@@ -2,9 +2,11 @@
from flask_babel import gettext as _
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms import SelectField, StringField
from wtforms.validators import DataRequired
class RepresentativeForm(FlaskForm):
name = StringField(_("Name"), validators=[DataRequired()])
name = StringField(_("Name"))
constituency = SelectField(_("Constituency"), coerce=int)
group = SelectField(_("Group"), coerce=int)
......@@ -5,6 +5,8 @@
<form method="post" action="{{url_for('representative.index')}}">
{{g.form.hidden_tag()}}
{{render_field(g.form.name)}}
{{render_field(g.form.constituency)}}
{{render_field(g.form.group)}}
<br />
<input type="submit" value="{{_("Filter")}}">
</form>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment