def get_context_data(self, *args, **kwargs): context = super(MeetingDetailView, self).get_context_data(**kwargs) cm = context['object'] colors = {} speakers = cm.parts.order_by('speaker__mk').values_list( 'header', 'speaker__mk').distinct() n = speakers.count() for (i, (p, mk)) in enumerate(speakers): (r, g, b) = colorsys.hsv_to_rgb(float(i) / n, 0.5 if mk else 0.3, 255) colors[p] = 'rgb(%i, %i, %i)' % (r, g, b) context['title'] = _('%(committee)s meeting on %(date)s') % { 'committee': cm.committee.name, 'date': cm.date_string } context['description'] = self._resolve_committee_meeting_description( cm) page = self.request.GET.get('page', None) if page: context['description'] += _(' page %(page)s') % {'page': page} context['colors'] = colors parts_lengths = {} for part in cm.parts.all(): parts_lengths[part.id] = len(part.body) context['parts_lengths'] = json.dumps(parts_lengths) context['paginate_by'] = models.COMMITTEE_PROTOCOL_PAGINATE_BY if cm.committee.type != 'plenum' and \ waffle.flag_is_active(self.request, 'show_member_presence'): # get meeting members with presence calculation meeting_members_ids = set(member.id for member in cm.mks_attended.all()) members = members_by_presence(cm.committee, ids=meeting_members_ids) context['show_member_presence'] = True else: members = cm.mks_attended.order_by('name') context['show_member_presence'] = False links = list(Link.objects.for_model(Member)) links_by_member = {} for k, g in itertools.groupby(links, lambda x: x.object_pk): links_by_member[str(k)] = list(g) for member in members: member.cached_links = links_by_member.get(str(member.pk), []) context['members'] = members meeting_text = [cm.topics] + [part.body for part in cm.parts.all()] context[ 'tag_suggestions'] = ok_tag.tag_suggestions.extract_suggested_tags( cm.tags, meeting_text) context['mentioned_lobbyists'] = cm.main_lobbyists_mentioned context[ 'mentioned_lobbyist_corporations'] = cm.main_lobbyist_corporations_mentioned return context
def get_context_data(self, *args, **kwargs): context = super(MeetingDetailView, self).get_context_data(**kwargs) cm = context['object'] colors = {} speakers = cm.parts.order_by('speaker__mk').values_list('header', 'speaker__mk').distinct() n = speakers.count() for (i, (p, mk)) in enumerate(speakers): (r, g, b) = colorsys.hsv_to_rgb(float(i) / n, 0.5 if mk else 0.3, 255) colors[p] = 'rgb(%i, %i, %i)' % (r, g, b) context['title'] = _('%(committee)s meeting on %(date)s') % { 'committee': cm.committee.name, 'date': cm.date_string} context['description'] = self._resolve_committee_meeting_description( cm) page = self.request.GET.get('page', None) if page: context['description'] += _(' page %(page)s') % {'page': page} context['colors'] = colors parts_lengths = {} for part in cm.parts.all(): parts_lengths[part.id] = len(part.body) context['parts_lengths'] = json.dumps(parts_lengths) context['paginate_by'] = models.COMMITTEE_PROTOCOL_PAGINATE_BY if cm.committee.type != 'plenum' and \ waffle.flag_is_active(self.request, 'show_member_presence'): # get meeting members with presence calculation meeting_members_ids = set( member.id for member in cm.mks_attended.all()) members = members_by_presence(cm.committee, ids=meeting_members_ids) context['show_member_presence'] = True else: members = cm.mks_attended.order_by('name') context['show_member_presence'] = False links = list(Link.objects.for_model(Member)) links_by_member = {} for k, g in itertools.groupby(links, lambda x: x.object_pk): links_by_member[str(k)] = list(g) for member in members: member.cached_links = links_by_member.get(str(member.pk), []) context['members'] = members meeting_text = [cm.topics] + [part.body for part in cm.parts.all()] context[ 'tag_suggestions'] = ok_tag.tag_suggestions.extract_suggested_tags( cm.tags, meeting_text) context['mentioned_lobbyists'] = cm.main_lobbyists_mentioned context[ 'mentioned_lobbyist_corporations'] = cm.main_lobbyist_corporations_mentioned return context
def _build_context_data(self, cached_context, cm): cached_context['chairpersons'] = cm.chairpersons.all() cached_context['replacements'] = cm.replacements.all() if waffle.flag_is_active(self.request, 'show_member_presence'): cached_context['show_member_presence'] = True members = members_by_presence(cm, current_only=True) else: cached_context['show_member_presence'] = False members = cm.members_by_name(current_only=True) links = list(Link.objects.for_model(Member)) links_by_member = {} for k, g in itertools.groupby(links, lambda x: x.object_pk): links_by_member[str(k)] = list(g) for member in members: member.cached_links = links_by_member.get(str(member.pk), []) cached_context['members'] = members recent_meetings, more_meetings_available = cm.recent_meetings( limit=self.SEE_ALL_THRESHOLD) cached_context['meetings_list'] = recent_meetings cached_context['more_meetings_available'] = more_meetings_available future_meetings, more_future_meetings_available = cm.future_meetings( limit=self.SEE_ALL_THRESHOLD) cached_context['future_meetings_list'] = future_meetings cached_context[ 'more_future_meetings_available'] = more_future_meetings_available cur_date = datetime.datetime.now() not_yet_published_meetings, more_unpublished_available = cm.protocol_not_yet_published_meetings( end_date=cur_date, limit=self.SEE_ALL_THRESHOLD) cached_context[ 'protocol_not_yet_published_list'] = not_yet_published_meetings cached_context[ 'more_unpublished_available'] = more_unpublished_available cached_context['annotations'] = cm.annotations.order_by('-timestamp') if waffle.flag_is_active(self.request, 'show_committee_topics'): cached_context['topics'] = cm.topic_set.summary()[:5]
def get_context_data(self, **kwargs): context = super(MemberDetailView, self).get_context_data(**kwargs) member = context['object'] current_knesset_start_date = Knesset.objects.current_knesset().start_date if self.request.user.is_authenticated(): profile = self.request.user.profiles.get() watched = profile.is_watching_member(member) cached_context = None else: watched = False cached_context = cache.get('mk_%d' % member.id) if cached_context is None: presence = {} self.calc_percentile(member, presence, 'average_weekly_presence_hours', 'average_weekly_presence_hours', 'average_weekly_presence_hours_percentile') self.calc_percentile(member, presence, 'average_monthly_committee_presence', 'average_monthly_committee_presence', 'average_monthly_committee_presence_percentile') bills_statistics = {} # TODO: can move to an offline job self.calc_bill_stats(member, bills_statistics, 'proposed') self.calc_bill_stats(member, bills_statistics, 'pre') self.calc_bill_stats(member, bills_statistics, 'first') self.calc_bill_stats(member, bills_statistics, 'approved') agendas = self.get_agenda_data(member) # TODO: Move to model or service factional_discipline = VoteAction.objects.select_related( 'vote').filter(member=member, against_party=True, vote__time__gt=current_knesset_start_date) votes_against_own_bills = VoteAction.objects.select_related( 'vote').filter(member=member, against_own_bill=True, vote__time__gt=current_knesset_start_date) general_discipline_params = {'member': member, 'vote__time__gt': current_knesset_start_date} is_coalition = member.current_party.is_coalition if is_coalition: general_discipline_params['against_coalition'] = True else: general_discipline_params['against_opposition'] = True general_discipline = VoteAction.objects.filter( **general_discipline_params).select_related('vote') about_videos = get_videos_queryset(member, group='about')[:1] if len(about_videos): about_video = about_videos[0] about_video_embed_link = about_video.embed_link about_video_image_link = about_video.image_link else: about_video_embed_link = '' about_video_image_link = '' related_videos = get_videos_queryset(member, group='related') related_videos = related_videos.filter( Q(published__gt=date.today() - timedelta(days=30)) | Q(sticky=True) ).order_by('sticky').order_by('-published')[:5] actions = actor_stream(member) for a in actions: a.actor = member legislation_actions = actor_stream(member).filter( verb__in=('proposed', 'joined')) # this ugly code groups all the committee actions according to plenum and committee # it stop iterating when both committee and plenum actions reach the maximum (MEMBER_INITIAL_DATA) # it also stops iterating when reaching 20 iterations committee_actions_more = {'committee': False, 'plenum': False} committee_actions = {'committee': [], 'plenum': []} i = 0 for action in actor_stream(member).filter(verb='attended'): i = i + 1 if i == 20: # JESUS what language are we writing here? and is this a way to do a "limit"? break committee_type = (action and action.target and action.target.committee and action.target.committee.type) if committee_type in ['plenum', 'committee']: if len(committee_actions[committee_type]) == self.MEMBER_INITIAL_DATA: committee_actions_more[committee_type] = True if committee_actions_more['plenum'] == True and committee_actions_more[ 'committee'] == True: break else: committee_actions[committee_type].append(action) committees_presence = [] has_protocols_not_published = False committees = member.get_active_committees() for committee in committees: committee_member = members_by_presence(committee, ids=[member.id])[0] committees_presence.append({"committee": committee, "presence": committee_member.meetings_percentage}) if committee.protocol_not_published: has_protocols_not_published = True committees_presence.sort(cmp=lambda x, y: y["presence"] - x["presence"]) mmm_documents = member.mmm_documents.order_by('-publication_date') content_type = ContentType.objects.get_for_model(Member) num_followers = Follow.objects.filter( object_id=member.pk, content_type=content_type).count() protocol_part_annotation_actions = Action.objects.filter( actor_content_type=ContentType.objects.get_for_model(Person), actor_object_id__in=member.person.values_list('pk', flat=True), verb='got annotation for protocol part' ) # since parties are prefetch_releated, will list and slice them previous_parties = list(member.parties.all())[1:] cached_context = { 'watched_member': watched, 'num_followers': num_followers, 'actions_more': actions.count() > self.MEMBER_INITIAL_DATA, 'actions': actions[:self.MEMBER_INITIAL_DATA], 'legislation_actions_more': legislation_actions.count() > self.MEMBER_INITIAL_DATA, 'legislation_actions': legislation_actions[:self.MEMBER_INITIAL_DATA], 'committee_actions_more': committee_actions_more['committee'], 'committee_actions': committee_actions['committee'], 'plenum_actions_more': committee_actions_more['plenum'], 'plenum_actions': committee_actions['plenum'], 'mmm_documents_more': mmm_documents.count() > self.MEMBER_INITIAL_DATA, 'mmm_documents': mmm_documents[:self.MEMBER_INITIAL_DATA], 'bills_statistics': bills_statistics, 'agendas': agendas, 'presence': presence, 'factional_discipline': factional_discipline, 'votes_against_own_bills': votes_against_own_bills, 'general_discipline': general_discipline, 'about_video_embed_link': about_video_embed_link, 'about_video_image_link': about_video_image_link, 'related_videos': related_videos, 'num_related_videos': related_videos.count(), 'INITIAL_DATA': self.MEMBER_INITIAL_DATA, 'previous_parties': previous_parties, 'committees_presence': committees_presence, 'protocol_part_annotation_actions': protocol_part_annotation_actions, 'has_protocols_not_published': has_protocols_not_published, } if not self.request.user.is_authenticated(): cache.set('mk_%d' % member.id, cached_context, settings.LONG_CACHE_TIME) context.update(cached_context) return context
def get_context_data(self, **kwargs): context = super(MemberDetailView, self).get_context_data(**kwargs) member = context['object'] current_knesset_start_date = Knesset.objects.current_knesset( ).start_date if self.request.user.is_authenticated(): profile = self.request.user.profiles.get() watched = profile.is_watching_member(member) cached_context = None else: watched = False cached_context = cache.get('mk_%d' % member.id) if cached_context is None: presence = {} self.calc_percentile(member, presence, 'average_weekly_presence_hours', 'average_weekly_presence_hours', 'average_weekly_presence_hours_percentile') self.calc_percentile( member, presence, 'average_monthly_committee_presence', 'average_monthly_committee_presence', 'average_monthly_committee_presence_percentile') bills_statistics = {} # TODO: can move to an offline job self.calc_bill_stats(member, bills_statistics, 'proposed') self.calc_bill_stats(member, bills_statistics, 'pre') self.calc_bill_stats(member, bills_statistics, 'first') self.calc_bill_stats(member, bills_statistics, 'approved') agendas = self.get_agenda_data(member) # TODO: Move to model or service factional_discipline = VoteAction.objects.select_related( 'vote').filter(member=member, against_party=True, vote__time__gt=current_knesset_start_date) votes_against_own_bills = VoteAction.objects.select_related( 'vote').filter(member=member, against_own_bill=True, vote__time__gt=current_knesset_start_date) general_discipline_params = { 'member': member, 'vote__time__gt': current_knesset_start_date } is_coalition = member.current_party.is_coalition if is_coalition: general_discipline_params['against_coalition'] = True else: general_discipline_params['against_opposition'] = True general_discipline = VoteAction.objects.filter( **general_discipline_params).select_related('vote') about_videos = get_videos_queryset(member, group='about')[:1] if len(about_videos): about_video = about_videos[0] about_video_embed_link = about_video.embed_link about_video_image_link = about_video.image_link else: about_video_embed_link = '' about_video_image_link = '' related_videos = get_videos_queryset(member, group='related') related_videos = related_videos.filter( Q(published__gt=date.today() - timedelta(days=30)) | Q(sticky=True)).order_by('sticky').order_by('-published')[:5] actions = actor_stream(member) for a in actions: a.actor = member legislation_actions = actor_stream(member).filter( verb__in=('proposed', 'joined')) # this ugly code groups all the committee actions according to plenum and committee # it stop iterating when both committee and plenum actions reach the maximum (MEMBER_INITIAL_DATA) # it also stops iterating when reaching 20 iterations committee_actions_more = {'committee': False, 'plenum': False} committee_actions = {'committee': [], 'plenum': []} i = 0 for action in actor_stream(member).filter(verb='attended'): i = i + 1 if i == 20: # JESUS what language are we writing here? and is this a way to do a "limit"? break committee_type = (action and action.target and action.target.committee and action.target.committee.type) if committee_type in ['plenum', 'committee']: if len(committee_actions[committee_type] ) == self.MEMBER_INITIAL_DATA: committee_actions_more[committee_type] = True if committee_actions_more[ 'plenum'] == True and committee_actions_more[ 'committee'] == True: break else: committee_actions[committee_type].append(action) committees_presence = [] has_protocols_not_published = False committees = member.get_active_committees() for committee in committees: committee_member = members_by_presence(committee, ids=[member.id])[0] committees_presence.append({ "committee": committee, "presence": committee_member.meetings_percentage }) if committee.protocol_not_published: has_protocols_not_published = True committees_presence.sort( cmp=lambda x, y: y["presence"] - x["presence"]) mmm_documents = member.mmm_documents.order_by('-publication_date') content_type = ContentType.objects.get_for_model(Member) num_followers = Follow.objects.filter( object_id=member.pk, content_type=content_type).count() protocol_part_annotation_actions = Action.objects.filter( actor_content_type=ContentType.objects.get_for_model(Person), actor_object_id__in=member.person.values_list('pk', flat=True), verb='got annotation for protocol part') # since parties are prefetch_releated, will list and slice them previous_parties = list(member.parties.all())[1:] cached_context = { 'watched_member': watched, 'num_followers': num_followers, 'actions_more': actions.count() > self.MEMBER_INITIAL_DATA, 'actions': actions[:self.MEMBER_INITIAL_DATA], 'legislation_actions_more': legislation_actions.count() > self.MEMBER_INITIAL_DATA, 'legislation_actions': legislation_actions[:self.MEMBER_INITIAL_DATA], 'committee_actions_more': committee_actions_more['committee'], 'committee_actions': committee_actions['committee'], 'plenum_actions_more': committee_actions_more['plenum'], 'plenum_actions': committee_actions['plenum'], 'mmm_documents_more': mmm_documents.count() > self.MEMBER_INITIAL_DATA, 'mmm_documents': mmm_documents[:self.MEMBER_INITIAL_DATA], 'bills_statistics': bills_statistics, 'agendas': agendas, 'presence': presence, 'factional_discipline': factional_discipline, 'votes_against_own_bills': votes_against_own_bills, 'general_discipline': general_discipline, 'about_video_embed_link': about_video_embed_link, 'about_video_image_link': about_video_image_link, 'related_videos': related_videos, 'num_related_videos': related_videos.count(), 'INITIAL_DATA': self.MEMBER_INITIAL_DATA, 'previous_parties': previous_parties, 'committees_presence': committees_presence, 'protocol_part_annotation_actions': protocol_part_annotation_actions, 'has_protocols_not_published': has_protocols_not_published, } if not self.request.user.is_authenticated(): cache.set('mk_%d' % member.id, cached_context, settings.LONG_CACHE_TIME) context.update(cached_context) return context