def save(self): comment = Comment.objects.create(owner=self.owner, story=self.story, body=self.cleaned_data['comment'], status=Comment.PUBLISHED) self.story.update_comment_count() self.story.set_updated_at_to_now() self.story.save(update_fields=['updated_at', 'comment_count']) StoryFollow.objects.get_or_create( follower=comment.owner, target=comment.story, defaults={ 'reason': StoryFollow.REASON_COMMENTED}) """ Send notification. TODO: Run notify actions to celery task. """ for follow in StoryFollow.objects.filter(target=comment.story)\ .exclude(follower=comment.owner): ntype_slug = { StoryFollow.REASON_CREATED: 'new_comment_on_my_story', StoryFollow.REASON_COMMENTED: 'new_comment_on_commented_story' }[follow.reason] notify(ntype_slug, comment, comment.story, follow.follower, comment.story.get_absolute_url()) return comment
def _user_created_story_callback_task(story): _update_related_question_meta_of_story(story) _update_related_profile_of_story(story) # If story is answer of a question notify questioner if story.question: notify(ntype_slug='user_answered_my_question', sub=story.question.questionee, obj=story.question, recipient=story.question.questioner, ignored_recipients=[story.owner], url=story.get_absolute_url()) # If questionmeta has followers. Notify them new answers. meta = story.question_meta for follow in QuestionFollow.objects.filter( status=QuestionFollow.FOLLOWING, target=meta): notify(ntype_slug='new_answer_to_following_question', sub=story, obj=meta, recipient=follow.follower, ignored_recipients=[story.owner], url=meta.get_absolute_url()) # Set avatar for user if necessary. if settings.AVATAR_QUESTIONMETA_ID == story.question_meta_id: _set_avatar_to_answer(story)
def like(request): if not request.user.is_authenticated(): return render_to_json( {'errMsg': _('You have to login to complete this action.')}, HttpResponse, 401) # TODO: Make it decorator if not request.POST: return render_to_json( {'errMsg': _('You have send bad data.')}, HttpResponse, 400) sid, val = request.POST.get('sid'), request.POST.get('val') if not sid: return HttpResponse(status=400) story = get_object_or_404(Story, id=int(sid)) is_liked = story.set_like(request.user, liked=bool(int(val))) notify(ntype_slug='user_liked_my_answer', sub=request.user, obj=story, recipient=story.owner, ignored_recipients=[request.user], url=story.get_absolute_url()) like_count = story.get_like_count_from_redis() return HttpResponse(json.dumps( {'like_count': like_count, 'is_liked': is_liked}))
def like(request): if not request.user.is_authenticated(): return render_to_json( {'errMsg': _('You have to login to complete this action.')}, HttpResponse, 401) # TODO: Make it decorator if not request.POST: return render_to_json({'errMsg': _('You have send bad data.')}, HttpResponse, 400) sid, val = request.POST.get('sid'), request.POST.get('val') if not sid: return HttpResponse(status=400) story = get_object_or_404(Story, id=int(sid)) is_liked = story.set_like(request.user, liked=bool(int(val))) notify(ntype_slug='user_liked_my_answer', sub=request.user, obj=story, recipient=story.owner, url=story.get_absolute_url()) like_count = story.get_like_count_from_redis() return HttpResponse( json.dumps({ 'like_count': like_count, 'is_liked': is_liked }))
def test_user_liked_my_answer(self): # When user_user_liked_my_answer created. notify('user_liked_my_answer', self.u2, self.s1, self.u1, '/abc/') # There must be a NotificationMeta object. self.assertEqual(NotificationMeta.objects.count(), 1) # And that NotificationMeta must have 1 sub. nm = NotificationMeta.objects.first() self.assertEqual(len(nm.subs), 1) # When that notificationMeta is published. nm.publish() # SiteNotification object must be created. sn = SiteNotification.objects.first() self.assertEqual( sn.template_name(), 'notification/user_liked_my_answer/' 'sub_sing_obj_sing/site_notification.html') # Cleanup... NotificationMeta.objects.all().delete() SiteNotification.objects.all().delete()
def create_new_comment(self): comment = Comment.objects.create(owner=self.owner, story=self.story, body=self.cleaned_data['comment'], status=Comment.PUBLISHED) self.story.comment_count = Comment.objects.filter( status=Comment.PUBLISHED, story=self.story).count() self.story.save(update_fields=['comment_count']) StoryFollow.objects.get_or_create( follower=comment.owner, target=comment.story, defaults={'reason': StoryFollow.REASON_COMMENTED}) """ Send notificationsself. TODO: Run notify actions to celery task. """ for follow in StoryFollow.objects.filter(target=comment.story)\ .exclude(follower=comment.owner): ntype_slug = { StoryFollow.REASON_CREATED: 'new_comment_on_my_story', StoryFollow.REASON_COMMENTED: 'new_comment_on_commented_story' }[follow.reason] notify(ntype_slug, comment, comment.story, follow.follower, comment.story.get_absolute_url()) return comment
def test_user_liked_own_answer(self): # When user likes his/her own story. notify('user_liked_my_answer', self.u2, self.s1, self.u1, '/abc/', ignored_recipients=[self.u1]) # There must be no NotificationMeta objects created. self.assertEqual(NotificationMeta.objects.count(), 0)
def test_user_liked_my_answer(self): notify('user_liked_my_answer', self.u2, self.s1, self.u1, '/abc/') self.assertEqual(NotificationMeta.objects.count(), 1) nm = NotificationMeta.objects.first() self.assertEqual(len(nm.subs), 1) nm.publish() sn = SiteNotification.objects.first() self.assertEqual( sn.template_name(), 'notification/user_liked_my_answer/' 'sub_sing_obj_sing/site_notification.html') NotificationMeta.objects.all().delete() SiteNotification.objects.all().delete()
def _publish_story(request, story): if story.status == Story.DRAFT: story.status = Story.PUBLISHED story.save() if story.question: story.question.status = Question.ANSWERED story.question.answer = story story.question.save() if story.question.questioner: notify(ntype_slug='user_answered_my_question', sub=story.question.questionee, obj=story.question, recipient=story.question.questioner, url=story.get_absolute_url()) messages.success(request, _('Your story published.')) user_created_story.send(sender=story) return HttpResponseRedirect(story.get_absolute_url())
def pending_follow_request_action(request): if request.method == 'POST': frpk = request.POST.get('pk') try: frpk = int(frpk) except ValueError: return render_to_json({'errMsg': 'Invalida data'}, HttpResponseBadRequest) action = request.POST['action'] follow_request = get_object_or_404(UserFollow, pk=frpk, target=request.user) if action == 'accept': follow_request.status = UserFollow.FOLLOWING follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) return render_to_json({'success': True}) elif action == 'accept-restricted': follow_request.status = UserFollow.FOLLOWING_RESTRICTED follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) if action == 'decline': follow_request.delete() return render_to_json({'success': True}) return render_to_json({'success': False, 'message': 'Invalida data'})
def pending_follow_request_action(request): if request.method == 'POST': frpk = request.POST.get('pk') try: frpk = int(frpk) except ValueError: return render_to_json( {'errMsg': 'Invalida data'}, HttpResponseBadRequest) action = request.POST['action'] follow_request = get_object_or_404( UserFollow, pk=frpk, target=request.user) if action == 'accept': follow_request.status = UserFollow.FOLLOWING follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) return render_to_json({'success': True}) elif action == 'accept-restricted': follow_request.status = UserFollow.FOLLOWING_RESTRICTED follow_request.save() follower_count_changed.send(sender=request.user) notify(ntype_slug='user_accepted_my_follow_request', sub=follow_request.target, obj=follow_request, recipient=follow_request.follower, url=follow_request.target.get_absolute_url()) if action == 'decline': follow_request.delete() return render_to_json({'success': True}) return render_to_json({'success': False, 'message': 'Invalida data'})
def test_user_liked_your_story_answer_sub(self): notify('user_liked_my_answer', self.u2, self.s1, self.u1, '/abc/') notify('user_liked_my_answer', self.u3, self.s1, self.u1, '/abc/') notify('user_liked_my_answer', self.u4, self.s1, self.u1, '/abc/') self.assertEqual(NotificationMeta.objects.count(), 1) nm = NotificationMeta.objects.first() self.assertEqual(len(nm.subs), 3) nm.publish() sn = SiteNotification.objects.first() self.assertEqual( sn.template_name(), 'notification/user_liked_my_answer/' 'sub_plur_obj_sing/site_notification.html')
def test_user_liked_your_answer_with_multiple_objects(self): notify('user_liked_my_answer', self.u2, self.s1, self.u1, '/abc/') notify('user_liked_my_answer', self.u3, self.s1, self.u1, '/abc/') notify('user_liked_my_answer', self.u4, self.s1, self.u1, '/abc/') self.assertEqual(NotificationMeta.objects.count(), 1) nm = NotificationMeta.objects.first() self.assertEqual(len(nm.subs), 3) nm.publish() sn = SiteNotification.objects.first() self.assertEqual( sn.template_name(), 'notification/user_liked_my_answer/' 'sub_plur_obj_sing/site_notification.html')
def test_undefined_notification(self): # No NotificationMeta objects must be created. notify('non_existing_notification', None, None, None, None) self.assertEqual(NotificationMeta.objects.count(), 0)
def test_throwing_non_existing_notification(self): notify('non_existing_notification', None, None, self.u1, None) self.assertEqual(NotificationMeta.objects.count(), 0)
def test_throwing_existing_notification(self): notify('existing_notification', self.u2, self.s, self.u1, '/abc/') self.assertEqual(NotificationMeta.objects.count(), 1)