예제 #1
0
    def get_flags(self, obj):
        flags = {
            'like': {'active': False, 'users': None},
            'dislike': {'active': False, 'users': None},
            'removal': {'active': False, 'count': None},
        }
        users_likedit, users_dislikedit = None, None

        if has_app_model_option(obj)['allow_flagging']:
            users_flagging = obj.users_flagging(CommentFlag.SUGGEST_REMOVAL)
            if self.request.user in users_flagging:
                flags['removal']['active'] = True
            if self.request.user.has_perm("django_comments.can_moderate"):
                flags['removal']['count'] = len(users_flagging)

        if (
                has_app_model_option(obj)['allow_feedback'] or
                has_app_model_option(obj)['show_feedback']
        ):
            users_likedit = obj.users_flagging(LIKEDIT_FLAG)
            users_dislikedit = obj.users_flagging(DISLIKEDIT_FLAG)

        if has_app_model_option(obj)['allow_feedback']:
            if self.request.user in users_likedit:
                flags['like']['active'] = True
            elif self.request.user in users_dislikedit:
                flags['dislike']['active'] = True
        if has_app_model_option(obj)['show_feedback']:
            flags['like']['users'] = [
                "%d:%s" % (user.id, settings.COMMENTS_XTD_API_USER_REPR(user))
                for user in users_likedit]
            flags['dislike']['users'] = [
                "%d:%s" % (user.id, settings.COMMENTS_XTD_API_USER_REPR(user))
                for user in users_dislikedit]
        return flags
예제 #2
0
def dislike(request, comment_id, next=None):
    """
    Dislike a comment. Confirmation on GET, action on POST.

    Templates: :template:`django_comments_xtd/dislike.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """
    comment = get_object_or_404(get_comment_model(), pk=comment_id,
                                site__pk=settings.SITE_ID)
    if not has_app_model_option(comment)['allow_feedback']:
        ctype = ContentType.objects.get_for_model(comment.content_object)
        raise Http404("Comments posted to instances of '%s.%s' are not "
                      "explicitly allowed to receive 'disliked it' flags. "
                      "Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                      "setting." % (ctype.app_label, ctype.model))
    # Flag on POST
    if request.method == 'POST':
        perform_dislike(request, comment)
        return next_redirect(request,
                             fallback=(next or 'comments-xtd-dislike-done'),
                             c=comment.pk)
    # Render a form on GET
    else:
        disliked_it = request.user in comment.users_flagging(DISLIKEDIT_FLAG)
        return render(request, 'django_comments_xtd/dislike.html',
                      {'comment': comment,
                       'already_disliked_it': disliked_it,
                       'next': next})
예제 #3
0
def like(request, comment_id, next=None):
    """
    Like a comment. Confirmation on GET, action on POST.

    Templates: :template:`django_comments_xtd/like.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """

    comment = get_object_or_404(get_comment_model(), pk=comment_id,
                                    site__pk=settings.SITE_ID)
    next = comment.get_absolute_url()
    if request.user.is_authenticated():
        if not has_app_model_option(comment)['allow_feedback']:
            ctype = ContentType.objects.get_for_model(comment.content_object)
            raise Http404("Comments posted to instances of '%s.%s' are not "
                          "explicitly allowed to receive 'liked it' flags. "
                          "Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                          "setting." % (ctype.app_label, ctype.model))

        perform_like(request, comment)
        return next_redirect(request,
                             fallback=next or 'comments-xtd-like-done',
                             c=comment.pk)
    messages.error(request, '只有登录用户才能点赞, 请在侧边栏内登录再尝试点赞!', extra_tags='bg-warning text-warning')
    return next_redirect(request,
                         fallback=next or 'comments-xtd-like-done',
                         c=comment.pk)
예제 #4
0
def flag(request, comment_id, next=None):
    """
    Flags a comment. Confirmation on GET, action on POST.

    Templates: :template:`comments/flag.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """
    comment = get_object_or_404(get_comment_model(),
                                pk=comment_id, site__pk=settings.SITE_ID)
    if not has_app_model_option(comment)['allow_flagging']:
        ctype = ContentType.objects.get_for_model(comment.content_object)
        raise Http404("Comments posted to instances of '%s.%s' are not "
                      "explicitly allowed to receive 'removal suggestion' "
                      "flags. Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                      "setting." % (ctype.app_label, ctype.model))
    # Flag on POST
    if request.method == 'POST':
        perform_flag(request, comment)
        return next_redirect(request, fallback=next or 'comments-flag-done',
                             c=comment.pk)

    # Render a form on GET
    else:
        return render(request, 'comments/flag.html',
                      {'comment': comment,
                       'next': next})
예제 #5
0
def like(request, comment_id, next=None):
    """
    Like a comment. Confirmation on GET, action on POST.

    Templates: :template:`django_comments_xtd/like.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """
    comment = get_object_or_404(get_comment_model(), pk=comment_id,
                                site__pk=get_current_site_id(request))
    if not has_app_model_option(comment)['allow_feedback']:
        ctype = ContentType.objects.get_for_model(comment.content_object)
        raise Http404("Comments posted to instances of '%s.%s' are not "
                      "explicitly allowed to receive 'liked it' flags. "
                      "Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                      "setting." % (ctype.app_label, ctype.model))
    # Flag on POST
    if request.method == 'POST':
        perform_like(request, comment)
        return next_redirect(request,
                             fallback=next or 'comments-xtd-like-done',
                             c=comment.pk)
    # Render a form on GET
    else:
        flag_qs = comment.flags.prefetch_related('user')\
                                .filter(flag=LIKEDIT_FLAG)
        users_likedit = [item.user for item in flag_qs]
        return render(request, 'django_comments_xtd/like.html',
                      {'comment': comment,
                       'already_liked_it': request.user in users_likedit,
                       'next': next})
예제 #6
0
def dislike(request, comment_id, next=None):
    """
    Dislike a comment. Confirmation on GET, action on POST.

    Templates: :template:`django_comments_xtd/dislike.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """
    comment = get_object_or_404(get_comment_model(), pk=comment_id,
                                site__pk=settings.SITE_ID)
    if not has_app_model_option(comment)['allow_feedback']:
        ctype = ContentType.objects.get_for_model(comment.content_object)
        raise Http404("Comments posted to instances of '%s.%s' are not "
                      "explicitly allowed to receive 'disliked it' flags. "
                      "Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                      "setting." % (ctype.app_label, ctype.model))
    # Flag on POST
    if request.method == 'POST':
        perform_dislike(request, comment)
        return next_redirect(request,
                             fallback=(next or 'comments-xtd-dislike-done'),
                             c=comment.pk)
    # Render a form on GET
    else:
        disliked_it = request.user in comment.users_flagging(DISLIKEDIT_FLAG)
        return render(request, 'django_comments_xtd/dislike.html',
                      {'comment': comment,
                       'already_disliked_it': disliked_it,
                       'next': next})
예제 #7
0
def flag(request, comment_id, next=None):
    """
    Flags a comment. Confirmation on GET, action on POST.

    Templates: :template:`comments/flag.html`,
    Context:
        comment
            the flagged `comments.comment` object
    """
    comment = get_object_or_404(get_comment_model(),
                                pk=comment_id, site__pk=settings.SITE_ID)
    if not has_app_model_option(comment)['allow_flagging']:
        ctype = ContentType.objects.get_for_model(comment.content_object)
        raise Http404("Comments posted to instances of '%s.%s' are not "
                      "explicitly allowed to receive 'removal suggestion' "
                      "flags. Check the COMMENTS_XTD_APP_MODEL_OPTIONS "
                      "setting." % (ctype.app_label, ctype.model))
    # Flag on POST
    if request.method == 'POST':
        perform_flag(request, comment)
        return next_redirect(request, fallback=next or 'comments-flag-done',
                             c=comment.pk)

    # Render a form on GET
    else:
        return render(request, 'comments/flag.html',
                      {'comment': comment,
                       'next': next})
예제 #8
0
    def get_flags(self, obj):
        flags = {
            'like': {
                'active': False,
                'users': None
            },
            'dislike': {
                'active': False,
                'users': None
            },
            'removal': {
                'active': False,
                'count': None
            },
        }
        users_likedit, users_dislikedit = None, None

        if has_app_model_option(obj)['allow_flagging']:
            users_flagging = obj.users_flagging(CommentFlag.SUGGEST_REMOVAL)
            if self.request.user in users_flagging:
                flags['removal']['active'] = True
            if self.request.user.has_perm("django_comments.can_moderate"):
                flags['removal']['count'] = len(users_flagging)

        if (has_app_model_option(obj)['allow_feedback']
                or has_app_model_option(obj)['show_feedback']):
            users_likedit = obj.users_flagging(LIKEDIT_FLAG)
            users_dislikedit = obj.users_flagging(DISLIKEDIT_FLAG)

        if has_app_model_option(obj)['allow_feedback']:
            if self.request.user in users_likedit:
                flags['like']['active'] = True
            elif self.request.user in users_dislikedit:
                flags['dislike']['active'] = True
        if has_app_model_option(obj)['show_feedback']:
            flags['like']['users'] = [
                "%d:%s" % (user.id, settings.COMMENTS_XTD_API_USER_REPR(user))
                for user in users_likedit
            ]
            flags['dislike']['users'] = [
                "%d:%s" % (user.id, settings.COMMENTS_XTD_API_USER_REPR(user))
                for user in users_dislikedit
            ]
        return flags
 def validate(self, data):
     # Validate flag.
     if data['flag'] not in self.flag_choices:
         raise serializers.ValidationError("Invalid flag.")
     # Check commenting options on object being commented.
     option = ''
     if data['flag'] in ['like', 'dislike']:
         option = 'allow_feedback'
     elif data['flag'] == 'report':
         option = 'allow_flagging'
     comment = data['comment']
     if not has_app_model_option(comment)[option]:
         ctype = ContentType.objects.get_for_model(comment.content_object)
         raise serializers.ValidationError(
             "Comments posted to instances of '%s.%s' are not explicitly "
             "allowed to receive '%s' flags. Check the "
             "COMMENTS_XTD_APP_MODEL_OPTIONS setting." %
             (ctype.app_label, ctype.model, data['flag']))
     data['flag'] = self.flag_choices[data['flag']]
     return data
예제 #10
0
 def validate(self, data):
     # Validate flag.
     if data['flag'] not in self.flag_choices:
         raise serializers.ValidationError("Invalid flag.")
     # Check commenting options on object being commented.
     option = ''
     if data['flag'] in ['like', 'dislike']:
         option = 'allow_feedback'
     elif data['flag'] == 'report':
         option = 'allow_flagging'
     comment = data['comment']
     if not has_app_model_option(comment)[option]:
         ctype = ContentType.objects.get_for_model(comment.content_object)
         raise serializers.ValidationError(
             "Comments posted to instances of '%s.%s' are not explicitly "
             "allowed to receive '%s' flags. Check the "
             "COMMENTS_XTD_APP_MODEL_OPTIONS setting." % (
                 ctype.app_label, ctype.model, data['flag']
             )
         )
     data['flag'] = self.flag_choices[data['flag']]
     return data