def save(self, *args, **kwargs): url = reverse('staff_directory:show_thanks', args=()) email_info = EmailInfo( subject="You were thanked in the staff directory!", text_template='staff_directory/email/user_thanked.txt', html_template='staff_directory/email/user_thanked.html', to_address=self.recipient.user.email, ) # Notify recipient title ="%s thanked you for %s" %\ (self.praise_nominator.person.full_name, NOUN[self.cfpb_value]) Notification.set_notification(self.praise_nominator, self.praise_nominator, "thanked", self, self.recipient.user, title, url, email_info) # Notify nominator title = "You thanked %s for %s" %\ (self.recipient.full_name, NOUN[self.cfpb_value]) Notification.set_notification(self.praise_nominator, self.praise_nominator, "thanked", self, self.praise_nominator, title, url) return super(Praise, self).save(*args, **kwargs)
def test_mark_all_as_read(self): self.client.login(username='******', password='******') login_user = get_user_model().objects.get(pk=1) other_user = get_user_model().objects.get(pk=3) notification1 = Notification.set_notification( owner=login_user, actor=login_user, verb="tagged", obj="message", target=login_user, title="You tagged someone", url="http://url.com/") notification2 = Notification.set_notification( owner=other_user, actor=other_user, verb="tagged", obj="message", target=login_user, title="You tagged someone", url="http://url.com/") notification3 = Notification.set_notification( owner=login_user, actor=login_user, verb="tagged", obj="message", target=other_user, title="You tagged someone", url="http://url.com/") self.assertFalse(notification1.viewed) self.assertFalse(notification2.viewed) self.assertFalse(notification3.viewed) response = self.client.post( '/notifications/mark_all_as_read/') self.assertEquals(200, response.status_code) notification1 = Notification.objects.get(pk=notification1.pk) notification2 = Notification.objects.get(pk=notification2.pk) notification3 = Notification.objects.get(pk=notification3.pk) self.assertTrue(notification1.viewed) self.assertTrue(notification2.viewed) self.assertFalse(notification3.viewed)
def send_idea_notifications(sender, comment, request, **kwargs): # If installed in collab try: from core.notifications.models import Notification from core.notifications.email import EmailInfo from core.helpers import normalize idea = comment.content_object title = u"%s %s commented on %s" % (normalize(comment.user.first_name), normalize(comment.user.last_name), idea.title) for user in idea.members: if user != comment.user: email_info = EmailInfo( subject = title, text_template = 'idea/email/new_comment.txt', html_template = 'idea/email/new_comment.html', to_address = user.email, ) Notification.set_notification(comment.user, comment.user, "commented", idea, user, title, idea.url(), email_info) except ImportError: pass
def send_idea_notifications(sender, comment, request, **kwargs): # If installed in collab try: from core.notifications.models import Notification from core.notifications.email import EmailInfo from core.helpers import normalize idea = comment.content_object if comment.is_anonymous: title = u'Someone commented on "%s"' % idea.title text_template = 'new_comment_anonymous.txt' html_template = 'new_comment_anonymous.html' else: title = u'%s %s comented on "%s"' % (normalize( comment.user.first_name), normalize( comment.user.last_name), idea.title) text_template = 'new_comment.txt' html_template = 'new_comment.html' for user in idea.members: if user != comment.user: email_info = EmailInfo( subject=title, text_template='idea/email/%s' % text_template, html_template='idea/email/%s' % html_template, to_address=user.email, ) Notification.set_notification(comment.user, comment.user, "commented", idea, user, title, idea.url(), email_info) except ImportError: pass
def remove_tag(req, person_stub, tag_slug, tag_category): """ web service to remove tag from profile, users are only able to remove their own tags """ person = Person.objects.get(stub=person_stub) tag = Tag.objects.get(slug=tag_slug) taggeditem = TaggedItem.objects.get(tag_category__slug=tag_category, object_id=person.id, tag=tag) taggeditem.delete() person.expire_cache() expire_cache_group('tags') url = reverse('staff_directory:person', args=[person.stub]) if person.user != req.user: # create email info email_info = EmailInfo( subject='A tag was removed from your user profile.', text_template='staff_directory/email/user_untagged.txt', html_template='staff_directory/email/user_untagged.html', to_address=person.user.email, ) # set notification title = '%s %s removed the "%s" tag from your profile' % \ (req.user.first_name, req.user.last_name, tag) Notification.set_notification(req.user, req.user, "untagged", tag, person.user, title, url, email_info) return HttpResponseRedirect(url)
def add_tag(req, person_stub='', tag='', category_slug='', is_ajax=False, redirect_to_tags_page=False): """ adds a tag to a user if they do not have tagging turned off """ if req.method == 'POST': if tag == '': tag = req.POST.get('tag', '').strip() if category_slug == '': category_slug = req.POST.get('tag_category_slug', '').strip() if tag == '': return json_response( {'error': 'Please enter a tag that is not blank.'}) elif person_stub == '': return json_response({'error': 'Person not found.'}) person = Person.objects.get(stub=person_stub) # make sure tag does not already exist try: taggeditem = TaggedItem.objects.get( tag_category__slug=category_slug, object_id=person.id, tag__name__iexact=tag) except Exception: taggeditem = add_tags(person, tag, category_slug, req.user, 'person') person.expire_cache() expire_cache_group('tags') url = reverse('staff_directory:person', args=[person.stub]) if person.user != req.user: email_info = EmailInfo( subject='You were tagged in the staff directory!', text_template='staff_directory/email/user_tagged.txt', html_template='staff_directory/email/user_tagged.html', to_address=person.user.email, ) # set notification title = '%s %s tagged you with "%s"' % \ (req.user.first_name, req.user.last_name, tag) Notification.set_notification(req.user, req.user, "tagged", tag, person.user, title, url, email_info) if is_ajax: if redirect_to_tags_page: return json_response({ 'redirect': reverse('staff_directory:show_by_tag', args=[taggeditem.tag.slug]) }) return json_response({ 'redirect': reverse('staff_directory:person', args=[person.stub]) }) else: return HttpResponseRedirect( reverse('staff_directory:person', args=[person.stub]))
def respond(req, id): user_form = get_object_or_404(Form, slug=id) already_responded = AnonymousResponse.objects.check_dupe(user_form.id, req.user.username) if not already_responded: if req.GET: for field in user_form.field_set.all(): if req.GET.has_key(field.label): field.default_value = req.GET[field.label] field.save() response_form = ResponseForm( req.POST or None, form=user_form, user=req.user) if not user_form.is_closed and response_form.is_valid(): form_response = response_form.save() #set notification title = '%s %s submitted the "%s" form' % \ (req.user.first_name, req.user.last_name, user_form) url = "/forms/results/%s/" % user_form.slug if user_form.owner.exists(): if user_form.collect_users: title = '%s %s submitted the "%s" form' % \ (req.user.first_name, req.user.last_name, user_form) text_template = 'form_respond.txt' html_template = 'form_respond.html' else: title = 'Someone submitted the "%s" form' % user_form text_template = 'form_respond_anonymous.txt' html_template = 'form_respond_anonymous.html' for o in user_form.owner.all(): if o != req.user: email_info = EmailInfo( subject=title, text_template='form_builder/email/%s' % text_template, html_template='form_builder/email/%s' % html_template, to_address=o.email ) Notification.set_notification(req.user, req.user, "submitted", user_form, o, title, url, email_info) return HttpResponseRedirect(reverse('form_builder:form_thanks', args=[form_response.pk])) return render_to_response('form_builder/respond.html', {'user_form': user_form, 'response_form': response_form}, context_instance=RequestContext(req)) else: context = RequestContext(req) context['form_title'] = user_form.title return render_to_response('form_builder/thanks.html', {}, context_instance=context)
def add_tag(req, person_stub='', tag='', category_slug='', is_ajax=False, redirect_to_tags_page=False): """ adds a tag to a user if they do not have tagging turned off """ if req.method == 'POST': if tag == '': tag = req.POST.get('tag', '').strip() if category_slug == '': category_slug = req.POST.get('tag_category_slug', '').strip() if tag == '': return json_response({'error': 'Please enter a tag that is not blank.'}) elif person_stub == '': return json_response({'error': 'Person not found.'}) person = Person.objects.get(stub=person_stub) # make sure tag does not already exist try: taggeditem = TaggedItem.objects.get( tag_category__slug=category_slug, object_id=person.id, tag__name__iexact=tag) except Exception: taggeditem = add_tags(person, tag, category_slug, req.user, 'person') person.expire_cache() expire_cache_group('tags') url = reverse('staff_directory:person', args=[person.stub]) if person.user != req.user: email_info = EmailInfo( subject='You were tagged in the staff directory!', text_template='staff_directory/email/user_tagged.txt', html_template='staff_directory/email/user_tagged.html', to_address=person.user.email, ) # set notification title = '%s %s tagged you with "%s"' % \ (req.user.first_name, req.user.last_name, tag) Notification.set_notification(req.user, req.user, "tagged", tag, person.user, title, url, email_info) if is_ajax: if redirect_to_tags_page: return json_response({'redirect': reverse('staff_directory:show_by_tag', args=[taggeditem.tag.slug])}) return json_response({'redirect': reverse('staff_directory:person', args=[person.stub])}) else: return HttpResponseRedirect(reverse('staff_directory:person', args=[person.stub]))
def test_mark_as_read(self): self.client.login(username='******', password='******') user = get_user_model().objects.get(pk=1) notification = Notification.set_notification( user, user, "tagged", user, user, "You tagged someone", "http://url.com/") self.assertFalse(notification.viewed) response = self.client.post( '/notifications/mark_as_read/' + str(notification.pk)) self.assertEquals(200, response.status_code) notification = Notification.objects.get(pk=notification.pk) self.assertTrue(notification.viewed)
def respond(req, id): user_form = get_object_or_404(Form, slug=id) already_responded = AnonymousResponse.objects.check_dupe( user_form.id, req.user.username) if not already_responded: if req.GET: for field in user_form.field_set.all(): if req.GET.has_key(field.label): field.default_value = req.GET[field.label] field.save() response_form = ResponseForm(req.POST or None, form=user_form, user=req.user) if not user_form.is_closed and response_form.is_valid(): form_response = response_form.save() #set notification title = '%s %s submitted the "%s" form' % \ (req.user.first_name, req.user.last_name, user_form) url = "/forms/results/%s/" % user_form.slug if user_form.owner.exists(): if user_form.collect_users: title = '%s %s submitted the "%s" form' % \ (req.user.first_name, req.user.last_name, user_form) text_template = 'form_respond.txt' html_template = 'form_respond.html' else: title = 'Someone submitted the "%s" form' % user_form text_template = 'form_respond_anonymous.txt' html_template = 'form_respond_anonymous.html' for o in user_form.owner.all(): if o != req.user: email_info = EmailInfo( subject=title, text_template='form_builder/email/%s' % text_template, html_template='form_builder/email/%s' % html_template, to_address=o.email) Notification.set_notification(req.user, req.user, "submitted", user_form, o, title, url, email_info) return HttpResponseRedirect( reverse('form_builder:form_thanks', args=[form_response.pk])) return render_to_response('form_builder/respond.html', { 'user_form': user_form, 'response_form': response_form }, context_instance=RequestContext(req)) else: context = RequestContext(req) context['form_title'] = user_form.title return render_to_response('form_builder/thanks.html', {}, context_instance=context)