Beispiel #1
0
def handle_detail_comment(request, conversation):
    """
    User is posting a new comment. We need to validate the form and try to
    keep the same comment that was displayed before.
    """
    form = forms.CommentForm(conversation=conversation, request=request)
    if form.is_valid():
        new_comment = form.cleaned_data["content"]
        user = request.user
        new_comment = conversation.create_comment(user, new_comment)
        toast(request, _("Thank you! Your comment was sent to moderation and will be evaluated soon."))
        log.info(f"user {user.id} posted comment {new_comment.id} on {conversation.id}")
    return {"form": form}
Beispiel #2
0
def handle_detail_favorite(request, conversation):
    """
    User toggled the favorite status of conversation.
    """
    user = request.user
    is_favorite = conversation.toggle_favorite(user)
    if is_favorite:
        toast(request, _("This conversation is now favorite."))
    else:
        toast(request, _("Conversation removed from favorites."))

    log.info(f"user {user.id} toggled favorite status of conversation {conversation.id}")
    return {}
Beispiel #3
0
def handle_detail_vote(request):
    """
    User is voting in the current comment. We still need to choose a random
    comment to display next.
    """
    data = request.POST
    user = request.user
    vote = data["vote"]
    comment_id = data["comment_id"]
    try:
        models.Comment.objects.get(id=comment_id).vote(user, vote)
        toast(request, _("Thanks for voting."))

        log.info(f"user {user.id} voted {vote} on comment {comment_id}")
    except ValidationError:
        # User voted twice and too quickly... We simply ignore the last vote
        log.info(f"duplicated vote for user {user.id} on comment {comment_id}")
    return {}
Beispiel #4
0
def login(request, redirect_to="/"):
    form = forms.LoginForm(request=request)
    error_msg = _("Invalid email or password")
    next_url = request.GET.get("next", redirect_to)
    fast = request.GET.get("fast", "false") == "true" or "fast" in request.GET

    if form.is_valid_post():
        data = form.cleaned_data
        email, password = data["email"], data["password"]

        try:
            user = User.objects.get_by_email(email)
            user = auth.authenticate(request,
                                     email=user.email,
                                     password=password)
            if user is None:
                raise User.DoesNotExist
            auth.login(request, user, backend=user.backend)
            log.info(f"user {user} ({email}) successfully authenticated")
        except User.DoesNotExist:
            form.add_error(None, error_msg)
            log.info(f"invalid login attempt: {email}")
        else:
            toast(request, _("Welcome to EJ!"))
            return redirect(next_url)

    elif fast and request.user.is_authenticated and next_url:
        return redirect(next_url)

    return {
        "user": request.user,
        "form": form,
        "next": next_url,
        "social_js": social_js_template().render(request=request),
        "social_buttons": social_buttons(request),
    }