def main(request): """ Note on annotations: Old: Return annotations by concatenating Annotation last 10 and Comment last 10, adding all related comments (comments on same item that are older). annotations_old = get_annotations( annotations=list(Annotation.objects.all().order_by('-timestamp')[:10]), comments=Comment.objects.all().order_by('-submit_date')[:10]) New: Return annotations by Action filtered to include only: annotation-added (to meeting), ignore annotated (by user) comment-added """ context = cache.get('main_page_context') if not context: context = {} context['title'] = _('Home') actions = list(main_actions()[:10]) annotations = get_annotations( annotations=[ a.target for a in actions if a.verb != 'comment-added' ], comments=[x.target for x in actions if x.verb == 'comment-added']) context['annotations'] = annotations b = get_debated_bills() if b: context['bill'] = get_debated_bills()[0] else: context['bill'] = None context['topics'] = Topic.objects.filter(status__in=PUBLIC_TOPIC_STATUS)\ .order_by('-modified')\ .select_related('creator')[:10] context['has_search'] = True # disable the base template search cache.set('main_page_context', context, 300) # 5 Minutes template_name = '%s.%s%s' % ('main', settings.LANGUAGE_CODE, '.html') return render_to_response(template_name, context, context_instance=RequestContext(request))
def main(request): """ Note on annotations: Old: Return annotations by concatenating Annotation last 10 and Comment last 10, adding all related comments (comments on same item that are older). annotations_old = get_annotations( annotations=list(Annotation.objects.all().order_by('-timestamp')[:10]), comments=Comment.objects.all().order_by('-submit_date')[:10]) New: Return annotations by Action filtered to include only: annotation-added (to meeting), ignore annotated (by user) comment-added """ context = cache.get('main_page_context') if not context: context = {} context['title'] = _('Home') actions = list(main_actions()[:10]) annotations = get_annotations( annotations=[a.target for a in actions if a.verb != 'comment-added'], comments=[x.target for x in actions if x.verb == 'comment-added']) context['annotations'] = annotations b = get_debated_bills() if b: context['bill'] = get_debated_bills()[0] else: context['bill'] = None context['topics'] = Topic.objects.filter(status__in=PUBLIC_TOPIC_STATUS)\ .order_by('-modified')\ .select_related('creator')[:10] context['has_search'] = True # disable the base template search cache.set('main_page_context', context, 300) # 5 Minutes template_name = '%s.%s%s' % ('main', settings.LANGUAGE_CODE, '.html') return render_to_response(template_name, context, context_instance=RequestContext(request))
def get_email_for_user(self, user): logger.debug('get_email_for_user %d' % user.id) updates = dict(zip(self.update_models, ([] for x in self.update_models))) # will contain the updates to be sent updates_html = dict(zip(self.update_models, ([] for x in self.update_models))) follows = Follow.objects.filter(user=user) # everything this user is following # sometime a user follows something several times. we want to filter that out: follows = set([f.actor for f in follows]) for f in follows: if not f: logger.warning('Follow object with None actor. ignoring') continue model_class = f.__class__ model_template = f.__class__.__name__.lower() try: model_name = f.__class__._meta.verbose_name except AttributeError: logger.warning('follows %d has no __class__?' % f.id) model_name = "" content_type = ContentType.objects.get_for_model(f) if model_class in updates: key = model_class else: key = None # put all updates for 'other' classes at the 'None' group try: # get actions that happened since last update last_sent = LastSent.objects.get(user=user, content_type=content_type, object_pk=f.id) last_sent_time = last_sent.time stream = Action.objects.filter(actor_content_type = content_type, actor_object_id = f.id, timestamp__gt=last_sent_time, ).order_by('-timestamp') if stream: # if there are updates to be sent, last_sent.save() # update timestamp of last sent except LastSent.DoesNotExist: # never updated about this actor, send some updates stream = Action.objects.filter(actor_content_type = content_type, actor_object_id = f.id, timestamp__gt=datetime.datetime.now()-datetime.timedelta(self.days_back), ).order_by('-timestamp') last_sent = LastSent.objects.create(user=user,content_type=content_type, object_pk=f.id) if stream: # this actor has some updates try: # genereate the appropriate header for this actor class header = render_to_string(('notify/%(model)s_header.txt' % {'model': model_template}),{'model':model_name,'object':f}) except TemplateDoesNotExist: header = render_to_string(('notify/model_header.txt'),{'model':model_name,'object':f}) try: header_html = render_to_string(('notify/%(model)s_header.html' % {'model': model_template}),{'model':model_name,'object':f,'domain':self.domain}) except TemplateDoesNotExist: header_html = render_to_string(('notify/model_header.html'),{'model':model_name,'object':f,'domain':self.domain}) updates[key].append(header) updates_html[key].append(header_html) for action_instance in stream: # now generate the updates themselves try: action_output = render_to_string(('activity/%(verb)s/action_email.txt' % { 'verb':action_instance.verb.replace(' ','_') }),{ 'action':action_instance },None) except TemplateDoesNotExist: # fallback to the generic template action_output = render_to_string(('activity/action_email.txt'),{ 'action':action_instance },None) try: action_output_html = render_to_string(('activity/%(verb)s/action_email.html' % { 'verb':action_instance.verb.replace(' ','_') }),{ 'action':action_instance,'domain':self.domain },None) except TemplateDoesNotExist: # fallback to the generic template action_output_html = render_to_string(('activity/action_email.html'),{ 'action':action_instance,'domain':self.domain },None) updates[key].append(action_output) updates_html[key].append(action_output_html) if model_class == Agenda: txt,html = self.agenda_update(f) updates[key].append(txt) updates_html[key].append(html) email_body = [] email_body_html = [] # Add the updates for followed models for (model_class,title,title_html) in map(self.get_model_headers, self.update_models): if updates[model_class]: # this model has some updates, add it to the email email_body.append(title.format()) email_body.append('\n'.join(updates[model_class])) email_body_html.append(title_html.format()) email_body_html.append(''.join(updates_html[model_class])) if email_body or email_body_html: # Generate party membership section if needed up = UserProfile.objects.filter(user=user).select_related('party') if up: up = up[0] party = up.party if party: num_members = cache.get('party_num_members_%d' % party.id, None) if not num_members: num_members = party.userprofile_set.count() cache.set('party_num_members_%d' % party.id, num_members, settings.LONG_CACHE_TIME) else: num_members = None debated_bills = get_debated_bills() or [] template_name = 'notify/party_membership' party_membership_txt = render_to_string(template_name + '.txt', {'user':user, 'userprofile':up, 'num_members':num_members, 'bills':debated_bills, 'domain':self.domain}) party_membership_html = render_to_string(template_name + '.html', {'user':user, 'userprofile':up, 'num_members':num_members, 'bills':debated_bills, 'domain':self.domain}) else: logger.warning('Can\'t find user profile') if email_body: email_body.insert(0, party_membership_txt) if email_body_html: email_body_html.insert(0, party_membership_html) return (email_body, email_body_html)
def get_email_for_user(self, user): updates = dict(zip(self.update_models, ([] for x in self.update_models))) # will contain the updates to be sent updates_html = dict(zip(self.update_models, ([] for x in self.update_models))) follows = Follow.objects.filter(user=user) # everything this user is following # sometime a user follows something several times. we want to filter that out: follows = set([f.actor for f in follows]) for f in follows: if not f: logger.warning('Follow object with None actor. ignoring') continue model_class = f.__class__ model_template = f.__class__.__name__.lower() try: model_name = f.__class__._meta.verbose_name except AttributeError: logger.warning('follows %d has no __class__?' % f.id) model_name = "" content_type = ContentType.objects.get_for_model(f) if model_class in updates: key = model_class else: key = None # put all updates for 'other' classes at the 'None' group try: # get actions that happened since last update last_sent = LastSent.objects.get(user=user, content_type=content_type, object_pk=f.id) last_sent_time = last_sent.time stream = Action.objects.filter(actor_content_type = content_type, actor_object_id = f.id, timestamp__gt=last_sent_time, ).order_by('-timestamp') if stream: # if there are updates to be sent, last_sent.save() # update timestamp of last sent except LastSent.DoesNotExist: # never updated about this actor, send some updates stream = Action.objects.filter(actor_content_type = content_type, actor_object_id = f.id, timestamp__gt=datetime.datetime.now()-datetime.timedelta(self.days_back), ).order_by('-timestamp') last_sent = LastSent.objects.create(user=user,content_type=content_type, object_pk=f.id) if stream: # this actor has some updates try: # genereate the appropriate header for this actor class header = render_to_string(('notify/%(model)s_header.txt' % {'model': model_template}),{'model':model_name,'object':f}) except TemplateDoesNotExist: header = render_to_string(('notify/model_header.txt'),{'model':model_name,'object':f}) try: header_html = render_to_string(('notify/%(model)s_header.html' % {'model': model_template}),{'model':model_name,'object':f,'domain':self.domain}) except TemplateDoesNotExist: header_html = render_to_string(('notify/model_header.html'),{'model':model_name,'object':f,'domain':self.domain}) updates[key].append(header) updates_html[key].append(header_html) for action_instance in stream: # now generate the updates themselves try: action_output = render_to_string(('activity/%(verb)s/action_email.txt' % { 'verb':action_instance.verb.replace(' ','_') }),{ 'action':action_instance },None) except TemplateDoesNotExist: # fallback to the generic template action_output = render_to_string(('activity/action_email.txt'),{ 'action':action_instance },None) try: action_output_html = render_to_string(('activity/%(verb)s/action_email.html' % { 'verb':action_instance.verb.replace(' ','_') }),{ 'action':action_instance,'domain':self.domain },None) except TemplateDoesNotExist: # fallback to the generic template action_output_html = render_to_string(('activity/action_email.html'),{ 'action':action_instance,'domain':self.domain },None) updates[key].append(action_output) updates_html[key].append(action_output_html) if stream and model_class == Agenda: txt,html = self.agenda_update(f) updates[key].append(txt) updates_html[key].append(html) email_body = [] email_body_html = [] # Generate party membership section up = UserProfile.objects.filter(user=user).select_related('party') if up: up = up[0] party = up.party if party: num_members = cache.get('party_num_members_%d' % party.id, None) if not num_members: num_members = party.userprofile_set.count() cache.set('party_num_members_%d' % party.id, num_members, settings.LONG_CACHE_TIME) else: num_members = None debated_bills = get_debated_bills() or [] template_name = 'notify/party_membership' party_membership_txt = render_to_string(template_name + '.txt', {'user':user, 'userprofile':up, 'num_members':num_members, 'bills':debated_bills, 'domain':self.domain}) party_membership_html = render_to_string(template_name + '.html', {'user':user, 'userprofile':up, 'num_members':num_members, 'bills':debated_bills, 'domain':self.domain}) else: logger.warning('Can\'t find user profile') # Add the updates for followed models for (model_class,title,title_html) in map(self.get_model_headers, self.update_models): if updates[model_class]: # this model has some updates, add it to the email email_body.append(title.format()) email_body.append('\n'.join(updates[model_class])) email_body_html.append(title_html.format()) email_body_html.append(''.join(updates_html[model_class])) if email_body: email_body.insert(0, party_membership_txt) if email_body_html: email_body_html.insert(0, party_membership_html) return (email_body, email_body_html)