예제 #1
0
파일: forms.py 프로젝트: kamilklkn/Resmin
    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
예제 #2
0
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)
예제 #3
0
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}))
예제 #4
0
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
        }))
예제 #5
0
파일: tests.py 프로젝트: adakarci/Resmin
    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()
예제 #6
0
파일: forms.py 프로젝트: rkirmizi/resmin
    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
예제 #7
0
파일: tests.py 프로젝트: adakarci/Resmin
    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)
예제 #8
0
파일: tests.py 프로젝트: cakirx/Resmin
    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)
예제 #9
0
파일: tests.py 프로젝트: rkirmizi/resmin
    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()
예제 #10
0
파일: views.py 프로젝트: rkirmizi/resmin
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())
예제 #11
0
파일: views.py 프로젝트: rkirmizi/resmin
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'})
예제 #12
0
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'})
예제 #13
0
파일: tests.py 프로젝트: cakirx/Resmin
    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()
예제 #14
0
파일: tasks.py 프로젝트: adakarci/Resmin
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)
예제 #15
0
파일: tests.py 프로젝트: rkirmizi/resmin
    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')
예제 #16
0
파일: tests.py 프로젝트: adakarci/Resmin
    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')
예제 #17
0
파일: tests.py 프로젝트: cakirx/Resmin
    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)
예제 #18
0
 def test_throwing_non_existing_notification(self):
     notify('non_existing_notification', None, None, self.u1, None)
     self.assertEqual(NotificationMeta.objects.count(), 0)
예제 #19
0
 def test_throwing_existing_notification(self):
     notify('existing_notification', self.u2, self.s, self.u1, '/abc/')
     self.assertEqual(NotificationMeta.objects.count(), 1)
예제 #20
0
 def test_throwing_non_existing_notification(self):
     notify('non_existing_notification', None, None, self.u1, None)
     self.assertEqual(NotificationMeta.objects.count(), 0)
예제 #21
0
 def test_throwing_existing_notification(self):
     notify('existing_notification', self.u2, self.s, self.u1, '/abc/')
     self.assertEqual(NotificationMeta.objects.count(), 1)
예제 #22
0
파일: tests.py 프로젝트: adakarci/Resmin
    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)