def test_template_response(self, rf):
     req = rf.get("/")
     resp = TurboFrame("my-form").template("simple.html", {}).response(req)
     assert resp.status_code == 200
     assert "is_turbo_frame" in resp.context_data
     content = resp.render().content
     assert b"my content" in content
     assert b'<turbo-frame id="my-form"' in content
Ejemplo n.º 2
0
def message_bookmark_view(request, pk, remove=False):
    message = get_object_or_404(
        Message.objects.for_community(
            request.community).for_sender_or_recipient(request.user),
        pk=pk,
    )
    if remove:
        Bookmark.objects.filter(user=request.user, message=message).delete()
        messages.info(request, _("Your bookmark has been removed"))
    else:

        try:
            Bookmark.objects.create(
                user=request.user,
                community=request.community,
                content_object=message,
            )
            messages.success(request, _("You have bookmarked this message"))
        except IntegrityError:
            pass

    return (TurboFrame(f"message-{message.id}-bookmark").template(
        "private_messages/includes/bookmark.html",
        {
            "object": message,
            "has_bookmarked": not (remove)
        },
    ).response(request))
Ejemplo n.º 3
0
def activity_bookmark_view(request, pk, model, remove=False):
    obj = get_activity_or_404(request,
                              model,
                              pk,
                              permission="activities.bookmark_activity")

    if remove:
        obj.get_bookmarks().filter(user=request.user).delete()
        messages.info(request, _("You have removed this bookmark"))
    else:
        try:
            Bookmark.objects.create(
                user=request.user,
                community=request.community,
                content_object=obj,
            )
            messages.success(
                request,
                model_translation_string(
                    _("You have bookmarked this %(model)s"), obj),
            )
        except IntegrityError:
            pass

    if request.turbo:
        return (TurboFrame(f"{obj.get_dom_id()}-bookmark").template(
            "activities/includes/bookmark.html",
            {
                "object": obj,
                "has_bookmarked": not (remove)
            },
        ).response(request))
    return redirect(obj)
Ejemplo n.º 4
0
def answer_vote_view(request, pk):

    answer = get_object_or_404(
        Answer.objects.filter(
            poll__community=request.community).select_related(
                "poll", "poll__community"),
        pk=pk,
    )

    has_perm_or_403(request.user, "polls.vote", answer.poll)

    has_voted = False

    for voted in Answer.objects.filter(voters=request.user, poll=answer.poll):
        voted.voters.remove(request.user)
        has_voted = True

    answer.voters.add(request.user)

    # send notification only the first time someone votes

    if not has_voted:
        answer.poll.notify_on_vote(request.user)

    # reload to get with updated answers
    poll = get_object_or_404(Poll.objects.with_answers(), pk=answer.poll_id)

    return (TurboFrame(f"poll-answers-{poll.id}").template(
        "polls/includes/answers.html",
        {
            "object": poll,
            "object_type": "poll",
        },
    ).response(request))
Ejemplo n.º 5
0
def podcast_cover_image(request: HttpRequest, podcast_id: int) -> HttpResponse:
    """Lazy-loaded podcast image"""
    return (TurboFrame(request.turbo.frame).template(
        "podcasts/_cover_image.html",
        {
            "podcast": get_podcast_or_404(podcast_id)
        },
    ).response(request))
Ejemplo n.º 6
0
def podcast_actions(request: HttpRequest, podcast_id: int) -> HttpResponse:
    podcast = get_podcast_or_404(podcast_id)

    if request.turbo.frame:
        return (TurboFrame(request.turbo.frame).template(
            "podcasts/_actions.html",
            {
                "podcast": podcast,
                "is_subscribed": podcast.is_subscribed(request.user),
            },
        ).response(request))
    return redirect(podcast.get_absolute_url())
Ejemplo n.º 7
0
def render_paginated_response(request: HttpRequest,
                              queryset: QuerySet,
                              template_name: str,
                              extra_context: Optional[Dict] = None,
                              **pagination_kwargs) -> HttpResponse:
    return (TurboFrame(request.turbo.frame).template(
        template_name,
        {
            "page_obj": paginate(request, queryset, **pagination_kwargs),
            **(extra_context or {}),
        },
    ).response(request))
Ejemplo n.º 8
0
def tag_block_view(request, pk, remove=False):

    tag = get_object_or_404(Tag, pk=pk)

    if remove:
        request.user.blocked_tags.remove(tag)
        messages.info(request, _("You are no longer blocking #%(tag)s" % {"tag": tag}))
    else:
        request.user.blocked_tags.add(tag)
        messages.success(request, _("You are now blocking #%(tag)s" % {"tag": tag}))

    return (
        TurboFrame(f"hashtag-{tag.id}-block")
        .template(
            "hashtags/includes/block.html",
            {"object": tag, "is_blocked": not (remove)},
        )
        .response(request)
    )
Ejemplo n.º 9
0
def event_attend_view(request, pk, remove=False):

    event = get_object_or_404(get_activity_queryset(request, Event), pk=pk)
    has_perm_or_403(request.user, "events.attend", event)

    if remove:
        event.attendees.remove(request.user)
        messages.info(request, _("You are no longer attending this event"))
    else:
        event.attendees.add(request.user)
        event.notify_on_attend(request.user)
        messages.success(request, _("You are now attending this event"))

    return (TurboFrame(event.get_dom_id() + "-attend").template(
        "events/includes/attend.html",
        {
            "object": event,
            "is_attending": not (remove)
        },
    ).response(request))
Ejemplo n.º 10
0
def activity_like_view(request, pk, model, remove=False):
    obj = get_activity_or_404(request,
                              model,
                              pk,
                              permission="activities.like_activity")

    if remove:
        obj.get_likes().filter(user=request.user).delete()
        messages.info(
            request,
            model_translation_string(
                _("You have stopped liking this %(model)s"), obj),
        )
    else:

        try:
            Like.objects.create(
                user=request.user,
                community=request.community,
                recipient=obj.owner,
                content_object=obj,
            ).notify()

            messages.success(
                request,
                model_translation_string(_("You have liked this %(model)s"),
                                         obj),
            )

        except IntegrityError:
            pass

    if request.turbo:
        return (TurboFrame(f"{obj.get_dom_id()}-like").template(
            "activities/includes/like.html",
            {
                "object": obj,
                "has_liked": not (remove)
            },
        ).response(request))
    return redirect(obj)
Ejemplo n.º 11
0
def user_follow_view(request, username, remove=False):

    user = get_user_or_404(request, username, permission="users.follow_user")
    if remove:
        request.user.following.remove(user)
        messages.info(request, _("You are no longer following this user"))
    else:
        request.user.following.add(user)
        request.user.notify_on_follow(user, request.community)
        messages.success(request, _("You are following this user"))

    is_detail = request.POST.get("is_detail", False)

    return (TurboFrame(f"user-{user.id}-follow").template(
        "users/includes/follow.html",
        {
            "object": user,
            "is_following": not (remove),
            "is_detail": is_detail,
        },
    ).response(request))
 def test_template(self):
     s = TurboFrame("my-form").template("simple.html", {}).render()
     assert "my content" in s
     assert '<turbo-frame id="my-form">' in s
 def test_render(self):
     s = TurboFrame("my-form").render("OK")
     assert s == '<turbo-frame id="my-form">OK</turbo-frame>'
 def test_response(self):
     resp = TurboFrame("my-form").response("OK")
     assert resp.status_code == 200
     assert b"OK" in resp.content
     assert b'<turbo-frame id="my-form"' in resp.content