コード例 #1
0
ファイル: conversations.py プロジェクト: ejplatform/ej-server
def conversation_comment_form(
    conversation, request=None, content=None, user=None, form=None, target=None
):
    """
    Render comment form for conversation.
    """
    # Check user credentials
    user = user or getattr(request, "user", None)
    if not user.is_authenticated:
        conversation_url = conversation.get_absolute_url()
        login = reverse("auth:login")
        return {
            "user": None,
            "login_anchor": a(_("login"), href=f"{login}?next={conversation_url}"),
        }

    # Check if user still have comments left
    n_comments = rules.compute("ej.remaining_comments", conversation, user)
    if conversation.author != user and n_comments <= 0:
        return {"comments_exceeded": True, "user": user}

    # Everything is ok, proceed ;)
    return {
        "user": user,
        "csrf_input": csrf_input(request),
        "n_comments": n_comments,
        "content": content,
        "target": target or "main",
        "form": form or forms.CommentForm(request=request, conversation=conversation),
    }
コード例 #2
0
ファイル: rules.py プロジェクト: zero101010/ej-server
def profile(user):
    """
    Return a profile instance for user.
    """
    profile_class = rules.compute('auth.profile_class')
    try:
        return user.raw_profile
    except profile_class.DoesNotExist:
        return profile_class.objects.create(user=user)
コード例 #3
0
def conversation_detail_context(request, conversation):
    """
    Common implementation used by both /conversations/<slug> and inside boards
    in /<board>/conversations/<slug>/
    """
    user = request.user
    is_favorite = user.is_authenticated and conversation.followers.filter(
        user=user).exists()
    n_comments = rules.compute('ej.remaining_comments', conversation, user)
    comment = None

    # User is voting in the current comment. We still need to choose a random
    # comment to display next.
    if request.POST.get('action') == 'vote':
        vote = request.POST['vote']
        comment_id = request.POST['comment_id']
        Comment.objects.get(id=comment_id).vote(user, vote)
        log.info(f'user {user.id} voted {vote} on comment {comment_id}')

    # User is posting a new comment. We need to validate the form and try to
    # keep the same comment that was displayed before.
    elif request.POST.get('action') == 'comment':
        # FIXME: do not hardcode this and use a proper form!
        comment = request.POST['comment'].strip()
        comment = comment[:210]
        comment = conversation.create_comment(user, comment)
        log.info(
            f'user {user.id} posted comment {comment.id} on {conversation.id}')

    # User toggled the favorite status of conversation.
    elif request.POST.get('action') == 'favorite':
        conversation.toggle_favorite(user)
        log.info(
            f'user {user.id} toggled favorite status of conversation {conversation.id}'
        )

    # User is probably trying to something nasty ;)
    elif request.method == 'POST':
        log.warning(
            f'user {user.id} sent invalid POST request: {request.POST}')
        return HttpResponseServerError('invalid action')

    return {
        # Objects
        'conversation': conversation,
        'comment': comment or conversation.next_comment(user, None),
        'comments_left': n_comments,
        'login_link': login_link(_('login'), conversation),

        # Permissions and predicates
        'is_favorite': is_favorite,
        'can_view_comment': user.is_authenticated,
        'can_comment': user.has_perm('ej.can_comment', conversation),
        'can_edit': user.has_perm('ej.can_edit_conversation', conversation),
        'cannot_comment_reason': '',
    }
コード例 #4
0
    def next_comment(self, user, default=NOT_GIVEN):
        """
        Returns a random comment that user didn't vote yet.

        If default value is not given, raises a Comment.DoesNotExit exception
        if no comments are available for user.
        """
        comment = rules.compute("ej.next_comment", self, user)
        if comment:
            return comment
        return None
コード例 #5
0
ファイル: conversation.py プロジェクト: pencil-labs/ej-server
    def next_comment(self, user, default=NOT_GIVEN):
        """
        Returns a random comment that user didn't vote yet.

        If default value is not given, raises a Comment.DoesNotExit exception
        if no comments are available for user.
        """
        comment = rules.compute('ej_conversations.next_comment', self, user)
        if comment is not None:
            return comment
        elif default is NOT_GIVEN:
            msg = _('No comments available for this user')
            raise Comment.DoesNotExist(msg)
        else:
            return default
コード例 #6
0
ファイル: comments.py プロジェクト: zero101010/ej-server
def comment_list(request, conversation):
    if not conversation.is_promoted:
        raise Http404

    user = request.user
    comments = conversation.comments.filter(author=user)
    n_comments = rules.compute('ej_conversations.remaining_comments', conversation, user)
    return {
        'content_title': _('List conversations'),
        'conversation': conversation,
        'approved': comments.approved(),
        'rejected': comments.rejected(),
        'pending': comments.pending(),
        'remaining_comments': n_comments,
        'can_comment': user.has_perm('ej.can_comment', conversation),
        'can_edit': user.has_perm('ej.can_edit_conversation', conversation),
    }
コード例 #7
0
def comment_form(conversation, request=None, comment_content=None, **kwargs):
    """
    Render comment form for one conversation.
    """
    user = getattr(request, 'user', None)
    n_comments = rules.compute('ej_conversations.remaining_comments', conversation, user)
    conversation_url = conversation.get_absolute_url()
    login = reverse('auth:login')
    login_anchor = a(_('login'), href=f'{login}?next={conversation_url}')
    return {
        'can_comment': user.is_authenticated,
        'comments_left': n_comments,
        'user_is_owner': conversation.author == user,
        'csrf_input': csrf_input(request),
        'comment_content': comment_content,
        'login_anchor': login_anchor,
    }
コード例 #8
0
def detail(request, conversation, owner=None):
    user = request.user
    comment = conversation.next_comment(user, None)
    n_comments = rules.compute('ej_conversations.remaining_comments',
                               conversation, user)
    favorites = FavoriteConversation.objects.filter(conversation=conversation)
    ctx = {
        'conversation':
        conversation,
        'comment':
        comment,
        'owner':
        owner,
        'edit_perm':
        user.has_perm('ej_conversations.can_edit_conversation', conversation),
        'can_comment':
        user.has_perm('ej_conversations.can_comment', conversation),
        'remaining_comments':
        n_comments,
        'login_link':
        a(_('login'), href=reverse('auth:login')),
        'favorites':
        favorites,
    }

    if comment and request.POST.get('action') == 'vote':
        vote = request.POST['vote']
        if vote not in {'agree', 'skip', 'disagree'}:
            return HttpResponseServerError('invalid parameter')
        comment.vote(user, vote)

    elif request.POST.get('action') == 'comment':
        comment = request.POST['comment'].strip()

        # FIXME: do not hardcode this and use a proper form!
        comment = comment[:210]
        try:
            ctx['comment'] = conversation.create_comment(user, comment)
        except (PermissionError, ValidationError) as ex:
            ctx['comment_error'] = str(ex)
    elif request.POST.get('action') == 'favorite':
        conversation.toggle_favorite(user)
    return ctx
コード例 #9
0
ファイル: rules.py プロジェクト: cidadedemocratica/ej-server
def can_add_conversation(user, board):
    return bool(
        user.has_perm("ej.can_edit_board", board)
        and (board.conversations.count() < rules.compute("ej.board_conversation_limit", user))
    )
コード例 #10
0
ファイル: conversations.py プロジェクト: zero101010/ej-server
def get_conversation_detail_context(request, conversation):
    """
    Common implementation used by both /conversations/<slug> and inside boards
    in /<board>/conversations/<slug>/
    """
    user = request.user
    is_favorite = user.is_authenticated and conversation.followers.filter(
        user=user).exists()
    comment = None
    comment_form = CommentForm(None, conversation=conversation)
    # User is voting in the current comment. We still need to choose a random
    # comment to display next.
    if request.POST.get('action') == 'vote':
        vote = request.POST['vote']
        comment_id = request.POST['comment_id']
        Comment.objects.get(id=comment_id).vote(user, vote)
        log.info(f'user {user.id} voted {vote} on comment {comment_id}')

    # User is posting a new comment. We need to validate the form and try to
    # keep the same comment that was displayed before.
    elif request.POST.get('action') == 'comment':
        comment_form = CommentForm(request.POST, conversation=conversation)
        if comment_form.is_valid():
            new_comment = comment_form.cleaned_data['content']
            new_comment = conversation.create_comment(user, new_comment)
            comment_form = CommentForm(conversation=conversation)
            log.info(
                f'user {user.id} posted comment {new_comment.id} on {conversation.id}'
            )

    # User toggled the favorite status of conversation.
    elif request.POST.get('action') == 'favorite':
        conversation.toggle_favorite(user)
        log.info(
            f'user {user.id} toggled favorite status of conversation {conversation.id}'
        )

    # User is probably trying to something nasty ;)
    elif request.method == 'POST':
        log.warning(
            f'user {user.id} se nt invalid POST request: {request.POST}')
        return HttpResponseServerError('invalid action')
    n_comments_under_moderation = rules.compute(
        'ej_conversations.comments_under_moderation', conversation, user)
    if user.is_authenticated:
        comments_made = user.comments.filter(conversation=conversation).count()
    else:
        comments_made = 0
    return {
        # Objects
        'conversation': conversation,
        'comment': comment or conversation.next_comment(user, None),
        'login_link': login_link(_('login'), conversation),
        'comment_form': comment_form,

        # Permissions and predicates
        'is_favorite': is_favorite,
        'can_comment': user.is_authenticated,
        'can_edit': user.has_perm('ej.can_edit_conversation', conversation),
        'cannot_comment_reason': '',
        'comments_under_moderation': n_comments_under_moderation,
        'comments_made': comments_made,
        'max_comments': max_comments_per_conversation(),
    }