Ejemplo n.º 1
0
    def post(self, request, *args, **kwargs):
        json = YpSerialiser()
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user.get_profile()
            comment.save()

            photo_id = kwargs.get("id")
            if photo_id:
                photo = get_object_or_404(PhotosModels.Photos, pk=photo_id)
                photo.comments.add(comment)
                photo.save()
            else:
                return HttpResponse(
                    simplejson.dumps({"id": 0, "status": 2, "txt": "комментарий не добавлен к изображению"}),
                    mimetype="application/json",
                )

            return HttpResponse(
                json.serialize(
                    [comment],
                    excludes=("object_id", "content_type"),
                    relations={"author": {"fields": ("first_name", "last_name", "avatar")}},
                ),
                mimetype="application/json",
            )
        return HttpResponse(simplejson.dumps({"id": 0, "status": form._errors}), mimetype="application/json")
Ejemplo n.º 2
0
    def form_valid(self, form: CommentForm):
        comment = form.save(False)

        comment.user = self.request.user
        comment.article = self.object
        comment.save()

        return super().form_valid(form)
Ejemplo n.º 3
0
def comment_edit(request, object_id, template_name='comments/edit.html'):
    comment = get_object_or_404(Comment, pk=object_id, user=request.user)

    if DELTA > comment.submit_date:
         return comment_error(request)

    if request.method == 'POST':
        form = CommentForm(request.POST, instance=comment)
        if form.is_valid():
            form.save()
            return redirect(request, comment.content_object)
    else:
        form = CommentForm(instance=comment)
    return render(request, template_name, {
        'form': form,
        'comment': comment,
    })
Ejemplo n.º 4
0
def add_comment(request, article):
    """Helper-function for add comment"""
    success = None
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            success = True
            comment = form.save(commit=False)
            # Setting fields forms for save
            comment.article_id = article
            comment.approved = False
            comment.author = form.cleaned_data['author']
            comment.email_author = form.cleaned_data['email_author']
            comment.text = form.cleaned_data['text']
            comment.save()
            form = CommentForm()
    else:
        form = CommentForm()

    return [form, success]
Ejemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        json = YpSerialiser()
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.author = request.user.get_profile()
            comment.save()

            photo_id = kwargs.get('id')
            if photo_id:
                photo = get_object_or_404(PhotosModels.Photos, pk=photo_id)
                photo.comments.add(comment)
                photo.save()
            else:
                return HttpResponse(simplejson.dumps({
                    'id':
                    0,
                    'status':
                    2,
                    'txt':
                    'комментарий не добавлен к изображению'
                }),
                                    mimetype="application/json")

            return HttpResponse(json.serialize(
                [comment],
                excludes=("object_id", "content_type"),
                relations={
                    'author': {
                        'fields': ('first_name', 'last_name', 'avatar')
                    }
                }),
                                mimetype="application/json")
        return HttpResponse(simplejson.dumps({
            'id': 0,
            'status': form._errors
        }),
                            mimetype="application/json")
Ejemplo n.º 6
0
    def test_notification_sending(self):
        """
        Make the system send updates only on the object being followed
        (language vs. video).

        The following is taken directly from the ticket
        -----------------------------------------------

        1. Followers of a video (submitter + anyone who chose to follow the
            video) should:

            * Be listed as followers for each language of this video
            * Get notifications about any changes made to the video or any of
                the related languages.
            * Get notifications about any comments left on the video or any of
                the related videos.

        2. Followers of a language (followers of language +
            transcriber(s)/translator(s) + editor(s) + anyone who chose to
            follow the language) should:

            * Get notifications about any changes made to the subtitles in
                this language, but not in any other language for the same video.
            * Get notifications about comments made on the subtitles in this
                language, but not in any other language for the video, nor on
                the video as a whole entity.
        """

        # Video is submitted by self.user (pk 2, [email protected])
        # The submitter is automatically added to followers via the
        # ``Video.get_or_create_for_url`` method.  Here we do that by hand.
        self.assertEquals(0, Message.objects.count())
        self.assertEquals(0, Comment.objects.count())
        self.video.user = self.user
        self.video.user.notify_by_email = True
        self.video.user.notify_by_message = False
        self.video.user.save()
        self.video.followers.add(self.user)
        self.video.save()

        # Create a user that only follows the language
        user_language_only = User.objects.create(username='******',
                                                 email='*****@*****.**',
                                                 notify_by_email=True,
                                                 notify_by_message=True)

        user_language2_only = User.objects.create(username='******',
                                                  email='*****@*****.**',
                                                  notify_by_email=True,
                                                  notify_by_message=True)

        # Create a user that will make the edits
        user_edit_maker = User.objects.create(username='******',
                                              email='*****@*****.**',
                                              notify_by_email=True,
                                              notify_by_message=True)

        self.language.followers.clear()
        self.language.followers.add(user_language_only)
        latest_version = self.language.get_tip()
        latest_version.title = 'Old title'
        latest_version.description = 'Old description'
        latest_version.save()

        # Create another language
        lan2 = SubtitleLanguage.objects.create(video=self.video,
                                               language_code='ru')
        lan2.followers.add(user_language2_only)
        self.assertEquals(4, SubtitleLanguage.objects.count())

        subtitles = self.language.get_tip().get_subtitles()
        subtitles.append_subtitle(1500, 3000, 'new text')
        version = pipeline.add_subtitles(self.video,
                                         self.language.language_code,
                                         subtitles,
                                         author=user_edit_maker,
                                         title="New title",
                                         description="New description")

        # Clear the box because the above generates some emails
        mail.outbox = []

        # Kick it off
        video_changed_tasks(version.video.id, version.id)

        # --------------------------------------------------------------------

        # How many emails should we have?
        # * The submitter
        # * All video followers who want emails
        # * All followers of the language being changed
        # * Minus the change author
        #
        # In our case that is: languageonly, adam, admin
        people = set(self.video.followers.filter(notify_by_email=True))
        people.update(self.language.followers.filter(notify_by_email=True))

        number = len(list(people)) - 1  # for the editor
        self.assertEqual(len(mail.outbox), number)

        email = mail.outbox[0]
        tos = [item for sublist in mail.outbox for item in sublist.to]

        self.assertTrue('New description' in email.body)
        self.assertTrue('Old description' in email.body)
        self.assertTrue('New title' in email.body)
        self.assertTrue('Old title' in email.body)

        # Make sure that all followers of the video got notified
        # Excluding the author of the new version
        excludes = list(
            User.objects.filter(email__in=[version.author.email]).all())
        self.assertEquals(1, len(excludes))
        followers = self.video.notification_list(excludes)
        self.assertTrue(excludes[0].notify_by_email
                        and excludes[0].notify_by_message)
        self.assertTrue(followers.filter(pk=self.video.user.pk).exists())

        for follower in followers:
            self.assertTrue(follower.email in tos)

        self.assertTrue(self.user.notify_by_email)
        self.assertTrue(self.user.email in tos)

        # Refresh objects
        self.user = User.objects.get(pk=self.user.pk)
        self.video = Video.objects.get(pk=self.video.pk)

        # Messages sent?
        self.assertFalse(self.video.user.notify_by_message)
        self.assertFalse(
            User.objects.get(pk=self.video.user.pk).notify_by_message)
        followers = self.video.followers.filter(
            notify_by_message=True).exclude(pk__in=[e.pk for e in excludes])

        self.assertEquals(followers.count(), 1)
        self.assertNotEquals(followers[0].pk, self.user.pk)

        self.assertEquals(followers.count(), Message.objects.count())
        for follower in followers:
            self.assertTrue(Message.objects.filter(user=follower).exists())

        language_follower_email = None
        for email in mail.outbox:
            if user_language_only.email in email.to:
                language_follower_email = email
                break

        self.assertFalse(language_follower_email is None)

        # --------------------------------------------------------------------
        # Now test comment notifications

        Message.objects.all().delete()
        mail.outbox = []

        # Video comment first
        form = CommentForm(
            self.video, {
                'content': 'Text',
                'object_pk': self.video.pk,
                'content_type': ContentType.objects.get_for_model(
                    self.video).pk
            })
        form.save(self.user, commit=True)

        self.assertEquals(1, Comment.objects.count())
        self.assertEqual(len(mail.outbox), 1)

        emails = []
        for e in mail.outbox:
            for a in e.to:
                emails.append(a)

        followers = self.video.followers.filter(notify_by_email=True)
        self.assertEquals(emails.sort(), [f.email for f in followers].sort())

        followers = self.video.followers.filter(notify_by_email=False)
        for follower in followers:
            self.assertFalse(follower.email in emails)

        followers = self.video.followers.filter(notify_by_message=True)
        self.assertEquals(followers.count(), Message.objects.count())
        for message in Message.objects.all():
            self.assertTrue(isinstance(message.object, Video))
            self.assertTrue(message.user in list(followers))

        # And now test comments on languages
        Message.objects.all().delete()
        mail.outbox = []

        form = CommentForm(
            self.language, {
                'content': 'Text',
                'object_pk': self.language.pk,
                'content_type': ContentType.objects.get_for_model(
                    self.language).pk
            })

        form.save(self.user, commit=True)

        self.assertEquals(
            Message.objects.count(),
            self.language.followers.filter(notify_by_message=True).count())

        followers = self.language.followers.filter(notify_by_message=True)

        # The author of the comment shouldn't get a message
        self.assertFalse(Message.objects.filter(user=self.user).exists())

        lan2 = SubtitleLanguage.objects.get(pk=lan2.pk)
        lan2_followers = lan2.followers.all()

        for message in Message.objects.all():
            self.assertTrue(isinstance(message.object, SubtitleLanguage))
            self.assertTrue(message.user in list(followers))
            self.assertFalse(message.user in list(lan2_followers))