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")
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})
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", )
def handle_activity_update( request, pk, model, form_class, template_name, *, permission="activities.change_activity", extra_context=None, **form_kwargs, ): obj = get_activity_or_404(request, model, pk, permission=permission) obj, form, success = process_activity_update_form(request, form_class, instance=obj, **form_kwargs) if success: return redirect_303(obj) return render_activity_update_form( request, obj, form, template_name, extra_context=extra_context, )
def handle_activity_create( request, model, form_class, template_name, *, is_private=False, extra_context=None, **form_kwargs, ): obj, form, success = process_activity_create_form(request, model, form_class, is_private=is_private, **form_kwargs) if success: return redirect_303(obj) return render_activity_create_form( request, model, form, template_name, is_private=is_private, extra_context=extra_context, )
def poll_create_view(request, model, form_class, template_name, is_private=False): has_perm_or_403(request.user, "activities.create_activity", request.community) if request.method == "POST": formset = AnswersFormSet(request.POST) else: formset = AnswersFormSet() obj, form, success = process_activity_create_form(request, model, form_class, is_private=is_private) if success and formset.is_valid(): formset.instance = obj formset.save() return redirect_303(obj) return render_activity_create_form( request, model, form, template_name, is_private=is_private, extra_context={"answers_formset": formset}, )
def poll_update_view(request, pk, model, form_class, template_name): obj = get_activity_or_404(request, model, pk, permission="activities.change_activity") if request.method == "POST": formset = AnswersFormSet(request.POST, instance=obj) else: formset = AnswersFormSet(instance=obj) obj, form, success = process_activity_update_form(request, form_class, instance=obj) if success and formset.is_valid(): formset.save() return redirect_303(obj) return render_activity_update_form( request, obj, form, template_name, extra_context={"answers_formset": formset}, )
def delete_account(request: HttpRequest) -> HttpResponse: if request.method == "POST" and "confirm-delete" in request.POST: request.user.delete() logout(request) messages.info(request, "Your account has been deleted") return redirect_303(settings.HOME_URL) return TemplateResponse(request, "account/delete_account.html")
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)
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 }, )
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")
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})
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")
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", )
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, }, )