Ejemplo n.º 1
0
class Committee(models.Model):
    name = models.CharField(max_length=256)
    # comma separated list of names used as name aliases for harvesting
    aliases = models.TextField(null=True, blank=True)
    members = models.ManyToManyField('mks.Member',
                                     related_name='committees',
                                     blank=True)
    chairpersons = models.ManyToManyField('mks.Member',
                                          related_name='chaired_committees',
                                          blank=True)
    replacements = models.ManyToManyField(
        'mks.Member', related_name='replacing_in_committees', blank=True)
    events = generic.GenericRelation(Event,
                                     content_type_field="which_type",
                                     object_id_field="which_pk")
    description = models.TextField(null=True, blank=True)
    portal_knesset_broadcasts_url = models.URLField(max_length=1000,
                                                    blank=True)
    type = models.CharField(max_length=10,
                            default=CommitteeTypes.committee,
                            choices=CommitteeTypes.as_choices(),
                            db_index=True)
    hide = models.BooleanField(default=False)
    # Deprecated? In use? does not look in use
    protocol_not_published = models.BooleanField(default=False)
    knesset_id = models.IntegerField(null=True, blank=True)
    knesset_type_id = models.IntegerField(null=True, blank=True)
    knesset_parent_id = models.IntegerField(null=True, blank=True)
    # Deprecated? In use? does not look
    last_scrape_time = models.DateTimeField(null=True, blank=True)
    name_eng = models.CharField(max_length=256, null=True, blank=True)
    name_arb = models.CharField(max_length=256, null=True, blank=True)
    start_date = models.DateTimeField(null=True, blank=True)
    end_date = models.DateTimeField(null=True, blank=True)
    knesset_description = models.TextField(null=True, blank=True)
    knesset_description_eng = models.TextField(null=True, blank=True)
    knesset_description_arb = models.TextField(null=True, blank=True)
    knesset_note = models.TextField(null=True, blank=True)
    knesset_note_eng = models.TextField(null=True, blank=True)
    knesset_portal_link = models.TextField(null=True, blank=True)

    @property
    def gender_presence(self):
        # returns a touple of (female_presence, male_presence
        r = {'F': 0, 'M': 0}
        for cm in self.meetings.all():
            try:
                results = groupby(cm.mks_attended.all(), lambda mk: mk.gender)
            except ValueError:
                continue
            for i in results:
                key, count = i[0], len(list(i[1]))
                r[key] += count
        return r['F'], r['M']

    def __unicode__(self):
        if self.type == 'plenum':
            return "%s" % ugettext('Plenum')
        else:
            return "%s" % self.name

    @models.permalink
    def get_absolute_url(self):
        if self.type == 'plenum':
            return 'plenum', []
        else:
            return 'committee-detail', [str(self.id)]

    @property
    def annotations(self):
        protocol_part_tn = ProtocolPart._meta.db_table
        meeting_tn = CommitteeMeeting._meta.db_table
        committee_tn = Committee._meta.db_table
        annotation_tn = Annotation._meta.db_table
        protocol_part_ct = ContentType.objects.get_for_model(ProtocolPart)
        ret = Annotation.objects.filter(content_type=protocol_part_ct)
        return ret.extra(
            tables=[protocol_part_tn, meeting_tn, committee_tn],
            where=[
                "%s.object_id=%s.id" % (annotation_tn, protocol_part_tn),
                "%s.meeting_id=%s.id" % (protocol_part_tn, meeting_tn),
                "%s.committee_id=%%s" % meeting_tn
            ],
            params=[self.id]).distinct()

    def members_by_presence(self, ids=None):
        """Return the members with computed presence percentage.
        If ids is not provided, this will return committee members. if ids is
        provided, this will return presence data for the given members.
        """
        def count_percentage(res_set, total_count):
            return (100 * res_set.count() / total_count) if total_count else 0

        def filter_this_year(res_set):
            year_start = date.today().replace(month=1, day=1)
            return res_set.filter(date__gte=year_start)

        if ids is not None:
            members = list(Member.objects.filter(id__in=ids))
        else:
            members = list((self.members.filter(is_current=True)
                            | self.chairpersons.all()
                            | self.replacements.all()).distinct())

        d = Knesset.objects.current_knesset().start_date
        meetings_with_mks = self.meetings.filter(
            mks_attended__isnull=False).distinct()
        all_meet_count = meetings_with_mks.filter(date__gte=d).count()
        year_meet_count = filter_this_year(meetings_with_mks).count()
        for m in members:
            all_member_meetings = m.committee_meetings.filter(committee=self,
                                                              date__gte=d)
            year_member_meetings = filter_this_year(all_member_meetings)
            m.meetings_percentage = count_percentage(all_member_meetings,
                                                     all_meet_count)
            m.meetings_percentage_year = count_percentage(
                year_member_meetings, year_meet_count)

        members.sort(key=lambda x: x.meetings_percentage, reverse=True)
        return members

    def recent_meetings(self, limit=10, do_limit=True):
        relevant_meetings = self.meetings.all().order_by('-date')
        if do_limit:
            more_available = relevant_meetings.count() > limit
            return relevant_meetings[:limit], more_available
        else:
            return relevant_meetings

    def future_meetings(self, limit=10, do_limit=True):
        current_date = datetime.now()
        relevant_events = self.events.filter(
            when__gt=current_date).order_by('when')
        if do_limit:
            more_available = relevant_events.count() > limit
            return relevant_events[:limit], more_available
        else:
            return relevant_events

    def protocol_not_yet_published_meetings(self,
                                            end_date,
                                            limit=10,
                                            do_limit=True):
        start_date = self.meetings.all().order_by('-date').first().date + timedelta(days=1) \
            if self.meetings.count() > 0 \
            else datetime.datetime.now()
        relevant_events = self.events.filter(
            when__gt=start_date, when__lte=end_date).order_by('-when')

        if do_limit:
            more_available = relevant_events.count() > limit
            return relevant_events[:limit], more_available
        else:
            return relevant_events
Ejemplo n.º 2
0
class Committee(models.Model):
    name = models.CharField(max_length=256)
    # comma separated list of names used as name aliases for harvesting
    aliases = models.TextField(null=True, blank=True)
    members = models.ManyToManyField('mks.Member',
                                     related_name='committees',
                                     blank=True)
    chairpersons = models.ManyToManyField('mks.Member',
                                          related_name='chaired_committees',
                                          blank=True)
    replacements = models.ManyToManyField(
        'mks.Member', related_name='replacing_in_committees', blank=True)
    events = generic.GenericRelation(Event,
                                     content_type_field="which_type",
                                     object_id_field="which_pk")
    description = models.TextField(null=True, blank=True)
    portal_knesset_broadcasts_url = models.URLField(max_length=1000,
                                                    blank=True)
    type = models.CharField(max_length=10,
                            default=CommitteeTypes.committee,
                            choices=CommitteeTypes.as_choices(),
                            db_index=True)
    hide = models.BooleanField(default=False)
    # Deprecated? In use? does not look in use
    protocol_not_published = models.BooleanField(default=False)
    knesset_id = models.IntegerField(null=True, blank=True)
    knesset_type_id = models.IntegerField(null=True, blank=True)
    knesset_parent_id = models.IntegerField(null=True, blank=True)
    # Deprecated? In use? does not look
    last_scrape_time = models.DateTimeField(null=True, blank=True)
    name_eng = models.CharField(max_length=256, null=True, blank=True)
    name_arb = models.CharField(max_length=256, null=True, blank=True)
    start_date = models.DateTimeField(null=True, blank=True)
    end_date = models.DateTimeField(null=True, blank=True)
    knesset_description = models.TextField(null=True, blank=True)
    knesset_description_eng = models.TextField(null=True, blank=True)
    knesset_description_arb = models.TextField(null=True, blank=True)
    knesset_note = models.TextField(null=True, blank=True)
    knesset_note_eng = models.TextField(null=True, blank=True)
    knesset_portal_link = models.TextField(null=True, blank=True)

    @property
    def gender_presence(self):
        # returns a touple of (female_presence, male_presence
        r = {'F': 0, 'M': 0}
        for cm in self.meetings.all():
            try:
                results = groupby(cm.mks_attended.all(), lambda mk: mk.gender)
            except ValueError:
                continue
            for i in results:
                key, count = i[0], len(list(i[1]))
                r[key] += count
        return r['F'], r['M']

    def __unicode__(self):
        if self.type == 'plenum':
            return "%s" % ugettext('Plenum')
        else:
            return "%s" % self.name

    @models.permalink
    def get_absolute_url(self):
        if self.type == 'plenum':
            return 'plenum', []
        else:
            return 'committee-detail', [str(self.id)]

    @property
    def annotations(self):
        protocol_part_tn = ProtocolPart._meta.db_table
        meeting_tn = CommitteeMeeting._meta.db_table
        committee_tn = Committee._meta.db_table
        annotation_tn = Annotation._meta.db_table
        protocol_part_ct = ContentType.objects.get_for_model(ProtocolPart)
        ret = Annotation.objects.select_related().filter(
            content_type=protocol_part_ct)
        return ret.extra(
            tables=[protocol_part_tn, meeting_tn, committee_tn],
            where=[
                "%s.object_id=%s.id" % (annotation_tn, protocol_part_tn),
                "%s.meeting_id=%s.id" % (protocol_part_tn, meeting_tn),
                "%s.committee_id=%%s" % meeting_tn
            ],
            params=[self.id]).distinct()

    def members_by_name(self, ids=None, current_only=False):
        """Return a queryset of all members, sorted by their name."""
        members = members_extended(self, current_only=current_only, ids=ids)
        return members.order_by('name')

    def recent_meetings(self, limit=10, do_limit=True):
        relevant_meetings = self.meetings.all().order_by('-date')
        if do_limit:
            more_available = relevant_meetings.count() > limit
            return relevant_meetings[:limit], more_available
        else:
            return relevant_meetings

    def future_meetings(self, limit=10, do_limit=True):
        current_date = datetime.now()
        relevant_events = self.events.filter(
            when__gt=current_date).order_by('when')
        if do_limit:
            more_available = relevant_events.count() > limit
            return relevant_events[:limit], more_available
        else:
            return relevant_events

    def protocol_not_yet_published_meetings(self,
                                            end_date,
                                            limit=10,
                                            do_limit=True):
        start_date = self.meetings.all().order_by(
            '-date').first().date + timedelta(days=1) \
            if self.meetings.count() > 0 \
            else datetime.now()
        relevant_events = self.events.filter(
            when__gt=start_date, when__lte=end_date).order_by('-when')

        if do_limit:
            more_available = relevant_events.count() > limit
            return relevant_events[:limit], more_available
        else:
            return relevant_events