def create_comment_view(request, pk, model): obj = get_object_or_404( get_activity_queryset(request, model), pk=pk, ) has_perm_or_403(request.user, "activities.create_comment", obj) form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.content_object = obj comment.community = request.community comment.owner = request.user comment.save() comment.notify_on_create() messages.success(request, _("Your comment has been posted")) return redirect(obj) return (TurboStream("comment-form").replace.template( "activities/includes/comment_form.html", { "form": form, "object": obj }).response(request))
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 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))
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 get_activity_or_404(request, model_or_queryset, pk, *, permission=None): queryset = (model_or_queryset if isinstance(model_or_queryset, QuerySet) else get_activity_queryset(request, model_or_queryset)) obj = get_object_or_404(queryset, pk=pk) if permission: has_perm_or_403(request.user, permission, obj) return obj
def event_cancel_view(request, pk): event = get_object_or_404(get_activity_queryset(request, Event), pk=pk) has_perm_or_403(request.user, "events.cancel", event) event.canceled = timezone.now() event.save() event.notify_on_cancel(request.user) messages.success(request, _("This event has been canceled")) return redirect(event)
def wrapper(request, *args, **kwargs): if not request.community.active: if request.is_ajax(): raise Http404(_("No community is available for this domain")) return redirect("community_not_found") if ( not request.user.has_perm("communities.view_community", request.community) and not allow_non_members and not request.community.public ): if request.is_ajax(): raise PermissionDenied(_("You must be a member of this community")) return redirect("community_welcome") if permission: has_perm_or_403(request.user, permission, request.community) return view(request, *args, **kwargs)
def message_recipient_create_view(request, username): has_perm_or_403(request.user, "private_messages.create_message", request.community) recipient = get_object_or_404( get_user_model().objects.exclude(pk=request.user.id).for_community( request.community).exclude_blocking(request.user), username__iexact=username, ) stream = TurboStream("modal") 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.save() message.notify_on_send() messages.success( request, _("Your message has been sent to %(recipient)s") % {"recipient": recipient.get_display_name()}, ) return stream.update.response() else: form = MessageForm() form["message"].label = _("Send message to %(recipient)s" % {"recipient": recipient.get_display_name()}) return stream.update.template( "private_messages/includes/modal_message_form.html", { "form": form, "recipient": recipient }, ).response(request)
def event_create_view(request, model, form_class, **kwargs): has_perm_or_403(request.user, "activities.create_activity", request.community) try: [day, month, year ] = [int(request.GET[param]) for param in ("day", "month", "year")] starts = datetime.datetime(day=day, month=month, year=year, hour=9) except (KeyError, ValueError): starts = None initial = {"timezone": request.user.default_timezone, "starts": starts} return handle_activity_create(request, model, form_class, initial=initial, **kwargs)
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))
def activity_create_view( request, model, form_class, template_name, is_private=False, extra_context=None, ): has_perm_or_403(request.user, "activities.create_activity", request.community) return handle_activity_create( request, model, form_class, template_name, is_private=is_private, extra_context=extra_context, )
def join_request_delete_view(request, pk): join_req = get_object_or_404(JoinRequest, pk=pk) has_perm_or_403(request.user, "join_requests.delete", join_req) join_req.delete() if join_req.sender == request.user: success_message = _( "Your join request for %(community)s has been deleted") % { "community": join_req.community.name } success_url = ("join_requests:sent_list" if JoinRequest.objects.for_sender( request.user).exists() else settings.HOME_PAGE_URL) else: success_message = _("Join request for %(sender)s has been deleted") % { "sender": join_req.sender.get_display_name() } success_url = "join_requests:list" messages.info(request, success_message) return redirect(success_url)
def get_membership_or_404(request, pk, permission=None): obj = get_object_or_404(get_membership_queryset(request), pk=pk) if permission: has_perm_or_403(request.user, permission, obj) return obj