コード例 #1
0
ファイル: views.py プロジェクト: danjac/localhub
def invite_create_view(request):

    if request.method == "POST":
        form = InviteForm(data=request.POST, community=request.community)
        if form.is_valid():

            invite = form.save(commit=False)
            invite.sender = request.user
            invite.community = request.community
            invite.sent = timezone.now()
            invite.save()

            # send email to recipient
            send_invitation_email(invite)

            messages.success(
                request,
                _("Your invitation has been sent to %(email)s") %
                {"email": invite.email},
            )
            return redirect_303("invites:list")

    else:
        form = InviteForm(community=request.community)
    return render_form_response(request, form, "invites/invite_form.html")
コード例 #2
0
def handle_flag_create(request, model):
    if request.method == "POST":
        form = FlagForm(request.POST)
        if form.is_valid():
            flag = form.save(commit=False)
            flag.content_object = model
            flag.community = request.community
            flag.user = request.user
            flag.save()

            flag.notify()

            success_message = _(
                "This %(model)s has been flagged to the moderators") % {
                    "model": model._meta.verbose_name
                }
            messages.success(request, success_message)
            return redirect_303(model)
    else:
        form = FlagForm()

    return render_form_response(request,
                                form,
                                "flags/flag_form.html",
                                context={"parent": model})
コード例 #3
0
def message_create_view(request):

    if request.method == "POST":
        form = MessageRecipientForm(request.POST,
                                    community=request.community,
                                    sender=request.user)

        if form.is_valid():
            message = form.save(commit=False)
            message.community = request.community
            message.sender = request.user
            message.save()
            message.notify_on_send()

            messages.success(
                request,
                _("Your message has been sent to %(recipient)s") %
                {"recipient": message.recipient.get_display_name()},
            )

            return redirect_303(message)

    else:
        form = MessageRecipientForm(community=request.community,
                                    sender=request.user)

    return render_form_response(
        request,
        form,
        "private_messages/message_form.html",
    )
コード例 #4
0
def render_activity_create_form(request,
                                model,
                                form,
                                template_name,
                                *,
                                is_private=False,
                                extra_context=None):

    view_name = "create_private" if is_private else "create"

    submit_actions = [(
        resolve_url(activity_model, view_name),
        model_translation_string(_("Submit %(model)s"),
                                 activity_model,
                                 capitalize=True),
    ) for activity_model in get_activity_models() if activity_model != model]

    return render_form_response(
        request,
        form,
        template_name,
        {
            "model": model,
            "is_private": is_private,
            "is_new": True,
            "submit_actions": submit_actions,
            **(extra_context or {}),
        },
    )
コード例 #5
0
def message_reply_view(request, pk, is_follow_up=False):

    has_perm_or_403(request.user, "private_messages.create_message",
                    request.community)

    qs = (Message.objects.for_community(request.community).exclude_blocked(
        request.user).common_select_related())

    if is_follow_up:
        qs = qs.for_sender(request.user)
    else:
        qs = qs.for_recipient(request.user)

    parent = get_object_or_404(qs, pk=pk)
    recipient = parent.get_other_user(request.user)

    if request.method == "POST":
        form = MessageForm(request.POST)

        if form.is_valid():

            message = form.save(commit=False)
            message.community = request.community
            message.sender = request.user
            message.recipient = recipient
            message.parent = parent
            message.save()

            message.notify_on_reply()

            messages.success(
                request,
                _("Your message has been sent to %(recipient)s") %
                {"recipient": recipient.get_display_name()},
            )

            return redirect_303(message)

    else:
        form = MessageForm()

    form["message"].label = (_("Send follow-up to %(recipient)s")
                             if is_follow_up else
                             _("Send reply to %(recipient)s")) % {
                                 "recipient": recipient.get_display_name()
                             }

    return render_form_response(
        request,
        form,
        "private_messages/message_form.html",
        {
            "recipient": recipient,
            "parent": parent
        },
    )
コード例 #6
0
ファイル: views.py プロジェクト: n4cr/django-saas-starter
def signup(request):
    with handle_form(request, UserCreationForm) as (form, is_success):
        if is_success:
            form.save()
            user = authenticate(
                username=form.cleaned_data["username"],
                password=form.cleaned_data["password1"],
            )
            auth_login(request, user)
            return redirect_303(settings.LOGIN_REDIRECT_URL)
        return render_form_response(request, form, "account/signup.html")
コード例 #7
0
ファイル: views.py プロジェクト: danjac/localhub
def membership_update_view(request, pk):
    member = get_membership_or_404(request,
                                   pk,
                                   permission="communities.change_membership")
    if request.method == "POST":
        form = MembershipForm(request.POST, instance=member)
        if form.is_valid():
            form.save()
            messages.success(request, _("Membership has been updated"))
            return redirect_303(member)
    else:
        form = MembershipForm(instance=member)

    return render_form_response(request, form,
                                "communities/membership_form.html",
                                {"membership": member})
コード例 #8
0
def join_request_create_view(request):
    if request.method == "POST":
        form = JoinRequestForm(data=request.POST,
                               user=request.user,
                               community=request.community)
        if form.is_valid():
            join_req = form.save()
            send_join_request_email(join_req)
            messages.success(
                request,
                _("Your request has been sent to the community admins"))
            return redirect_303(settings.HOME_PAGE_URL if request.community.
                                public else "community_welcome")
    else:
        form = JoinRequestForm(user=request.user, community=request.community)
    return render_form_response(request, form,
                                "join_requests/joinrequest_form.html")
コード例 #9
0
ファイル: views.py プロジェクト: danjac/localhub
def community_update_view(request):
    if request.method == "POST":
        form = CommunityForm(request.POST,
                             request.FILES,
                             instance=request.community)
        if form.is_valid():
            form.save()
            messages.success(request,
                             _("Community settings have been updated"))
            return redirect_303(request.path)
    else:
        form = CommunityForm(instance=request.community)

    return render_form_response(
        request,
        form,
        "communities/community_form.html",
    )
コード例 #10
0
def render_activity_update_form(request,
                                obj,
                                form,
                                template_name,
                                *,
                                extra_context=None):

    return render_form_response(
        request,
        form,
        template_name,
        {
            "object": obj,
            "model": obj,
            "is_new": False,
            **(extra_context or {}),
        },
    )
コード例 #11
0
def user_update_view(request):
    if request.method == "POST":
        form = UserForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            request.user.notify_on_update()
            messages.success(request, _("Your details have been updated"))
            return redirect(request.path)
    else:
        form = UserForm(instance=request.user)
    return render_form_response(
        request,
        form,
        "users/user_form.html",
        {
            "is_community":
            request.community.active
            and is_member(request.user, request.community)
        },
    )
コード例 #12
0
ファイル: views.py プロジェクト: n4cr/django-saas-starter
def login(request):
    redirect_url = get_redirect_url(request) or settings.LOGIN_REDIRECT_URL
    if request.user.is_authenticated:
        return redirect(redirect_url)

    with handle_form(request, AuthenticationForm, _request=request) as (
        form,
        is_success,
    ):
        if is_success:
            auth_login(request, form.get_user())
            return redirect_303(redirect_url)

        return render_form_response(
            request, form, "account/login.html",
            {
                "redirect_field_name": REDIRECT_FIELD_NAME,
                "redirect_field_value": redirect_url,
            },

        )
コード例 #13
0
from radiofeed.shortcuts import handle_form

from .forms import UserPreferencesForm


@login_required
def user_preferences(request: HttpRequest) -> HttpResponse:
    if result := handle_form(request,
                             UserPreferencesForm,
                             instance=request.user):

        result.form.save()
        messages.success(request, "Your preferences have been saved")
        return redirect_303(request.path)

    return render_form_response(request, result.form,
                                "account/preferences.html")


@login_required
def user_stats(request: HttpRequest) -> HttpResponse:

    logs = AudioLog.objects.filter(user=request.user)
    subscriptions = Subscription.objects.filter(user=request.user)
    favorites = Favorite.objects.filter(user=request.user)

    most_listened = (Podcast.objects.filter(
        episode__audiolog__user=request.user).annotate(num_listened=Count(
            "episode__audiolog")).order_by("-num_listened").distinct()[:5])

    return TemplateResponse(
        request,