diff --git a/cleansweep/committees/models.py b/cleansweep/committees/models.py index 19656ca..c0465cb 100644 --- a/cleansweep/committees/models.py +++ b/cleansweep/committees/models.py @@ -4,6 +4,8 @@ from ..models import db, Place, place_parents, PlaceType, Member, Place from collections import defaultdict from flask import url_for +from ..app import app + class CommitteeType(db.Model): """Specification of a Committee. @@ -346,6 +348,43 @@ def get_committee(type): return type.committees.filter_by(place_id=self.id).first() or Committee(self, type) return [get_committee(type) for type in committee_types] + # Current there is no way to identify primary committee + # So we are predecting it as follows + # For a place, at it level, if any slug is defined in app config, we try to get a valid committe from it. + # else get the first defined committee for that place. + + def get_primary_committee(self): + """Returns primary commitee for a place. + """ + committee_slug = app.config.get("%s_MASTER_COMMITTEE" % self.type.short_name.upper(), '') + c = self.get_committee(committee_slug) + + # If stale committee or not found, get first committee + if not c or not c.committee_members: + all_committees = self.committees.all() + + if len(all_committees) > 0: + c = all_committees[0] + else: + c = None + + return c + + + def get_incharges(self): + """Returns incharge at a given place. + """ + incharges = [] + + com = self.get_primary_committee() + + if com: + committee_role_slug = app.config.get("%s_MASTER_COMMITTEE_ROLE" % self.type.short_name.upper(), com.type.roles.pop().role) + incharges = [m for m in com.committee_members if m.role.role == committee_role_slug] + + return incharges + + def get_committee(self, slug, _create=True): """Returns a committee with given slug. diff --git a/cleansweep/models.py b/cleansweep/models.py index 020daf3..3d3aafb 100644 --- a/cleansweep/models.py +++ b/cleansweep/models.py @@ -108,8 +108,8 @@ class Place(db.Model, Mixable): # List of parents # Required to list immediate children on the place page - _parents = db.relationship('Place', - secondary=place_parents, + _parents = db.relationship('Place', + secondary=place_parents, primaryjoin=(id==place_parents.c.child_id), secondaryjoin=(id==place_parents.c.parent_id), backref=db.backref('places', lazy='dynamic', order_by='Place.key'), @@ -134,7 +134,7 @@ def get_toplevel_places(): """ return Place.query.filter_by(iparent_id=None).all() - @property + @property def code(self): return self.key.split("/")[-1] @@ -245,7 +245,7 @@ def _get_places_query(self, type=None): def add_place(self, place): """Addes a new place as direct child of this place. - This function takes care of setting parents for the + This function takes care of setting parents for the new place. """ # The place is being added as an immediate child of this node. @@ -272,7 +272,7 @@ def get_siblings(self): return Place.query.filter_by(type=self.type).all() def get_child_places_by_type(self): - """Returns an iterator over type and child-places of that type + """Returns an iterator over type and child-places of that type for all the immediate child places. """ places = self.child_places.all() @@ -297,7 +297,7 @@ def add_member(self, name, email, phone, voterid=None): """ member = Member(self, name, email, phone, voterid) db.session.add(member) - return member + return member def get_pending_members(self, status='pending', limit=100, offset=0): @@ -326,7 +326,7 @@ def add_contacts(self, data): contacts = [ Contact(self, name, email, phone, voterid) for name, email, phone, voterid in data - if name and name.strip() + if name and name.strip() and email not in dup_emails and phone not in dup_phones] db.session.add_all(contacts) @@ -517,7 +517,7 @@ class Contact(db.Model): id = db.Column(db.Integer, primary_key=True) place_id = db.Column(db.Integer, db.ForeignKey("place.id"), nullable=False, index=True) place = db.relationship('Place', foreign_keys=place_id) - + name = db.Column(db.Text, nullable=False) email = db.Column(db.Text, index=True) phone = db.Column(db.Text, index=True) diff --git a/cleansweep/templates/admin/committees.html b/cleansweep/templates/admin/committees.html new file mode 100644 index 0000000..4755418 --- /dev/null +++ b/cleansweep/templates/admin/committees.html @@ -0,0 +1,34 @@ +{% extends "admin/index.html" %} + +{% block subnav %} + {{ subnav(place, tab="committees") }} +{% endblock %} + +{% block page_content %} +