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

Issue #92 Display comittee member information as per requirement #103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions cleansweep/committees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

com.types.roles.pop() looks a bit scary. com.types.roles[0] is safer I think.

Don't worry, I'll take care of that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anandology I think I tried that, but its was throwing some errors, as it was not an array some custom form for list.

incharges = [m for m in com.committee_members if m.role.role == committee_role_slug]

return incharges


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only troubling part.

The committee slug will be different for different levels. The Role for Incharge could be different as well.

I think it is better to take it from a config setting or the first available committee. Lets have a function to get the committee name and role. That'll return first available committee and first role from that, unless a config setting is specified. Lets use WARD_MASTER_COMMITTEE and WARD_MASTER_COMMITTEE_ROLE with WARD substituted with appropriate level name.

Also, In python it is idiomatic to use list comprehensions instead of map/filter. The usual practice is:

incharges = [m for m in c.committee_members if m.role.role == 'Incharges']

def get_committee(self, slug, _create=True):
"""Returns a committee with given slug.

Expand Down
16 changes: 8 additions & 8 deletions cleansweep/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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]

Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand 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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions cleansweep/templates/admin/committees.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends "admin/index.html" %}

{% block subnav %}
{{ subnav(place, tab="committees") }}
{% endblock %}

{% block page_content %}
<h2>Committees</h2>
{% set committees = place.get_committees() %}
{% if committees %}
<ul class="list-group committees">
{% for c in place.get_committees() %}
<li class="list-group-item"><a href="{{ url_for('committees.view_committee', key=place.key, slug=c.type.slug) }}">{{c.type.name}}</a></li>

<div class="committee-members">
{% for role, members in c.get_members() %}
{% if members %}
<div class="committee-role">
<h4>{{role.role}}</h4>
{% for m in members %}
{{widget("VolunteerCard", volunteer=m)}}
{% endfor %}
</div>
{% endif %}
{% endfor %}
</div>


{% endfor %}
</ul>
{% else %}
<em>No committes are available at {{place.type.name}} level.</em>
{% endif %}
{% endblock %}
22 changes: 21 additions & 1 deletion cleansweep/templates/widgets/PlaceList.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@
<h3>{{ subtype.name | pluralize }}</h3>
<ul class="list-group">
{% for p in places %}
<li class="list-group-item"><a href="{{ url_for('place', key=p.key) }}">{{ p.name }}</a></li>
<li class="list-group-item">
<a href="{{ url_for('place', key=p.key) }}">
{{ p.name }}
</a>

<div class="incharges pull-right">
{% set incharges = p.get_incharges() %}
{% if incharges %}
{% for i in incharges%}
{% set member = i.member %}
<a href="{{ url_for('volunteers.profile', id=member.id , hash=member.get_hash())}}" target="_blank">
{{member.name}}
</a>
{% endfor %}
{% else %}
<em>None</em>
{% endif %}
</div>


</li>
{% endfor %}
</ul>
{% endfor %}
Expand Down