def show_event()

in moderator/moderate/views.py [0:0]


def show_event(request, e_slug, q_id=None):
    """Render event questions."""
    event = get_object_or_404(Event, slug=e_slug)
    question = None
    user = request.user

    # Do not display NDA events to non NDA members or non employees.
    if event.is_nda and not user.userprofile.is_nda_member:
        raise Http404

    if q_id:
        question = Question.objects.get(id=q_id)

    questions_q = Question.objects.filter(event=event, is_accepted=True).annotate(
        vote_count=Count("votes")
    )
    if user.userprofile.is_admin or event.archived:
        questions = questions_q.order_by("-vote_count")
    else:
        questions = questions_q.order_by("?")

    question_form = QuestionForm(
        request.POST or None, instance=question, **{"is_locked": True}
    )

    is_new_question = False
    if question_form.is_valid() and not event.archived:
        question_obj = question_form.save(commit=False)
        # Do not change the user if posting a reply
        moderator_ids = event.moderators.values_list("id", flat=True)
        accept_question = not event.is_moderated or user.id in moderator_ids
        if not question_obj.id:
            is_new_question = True
            # mark as accepted for non moderated events
            if accept_question:
                question_obj.is_accepted = True
            if not question_obj.is_anonymous:
                question_obj.asked_by = user
                question_obj.submitter_contact_info = user.email
        elif not can_moderate_event(event, user):
            raise Http404
        question_obj.event = event
        question_obj.save()
        msg = "Your question has been successfully submitted. "
        if not accept_question:
            msg += "Review is pending by an event moderator."
        if is_new_question:
            messages.success(request, msg)

        return redirect(reverse("event", kwargs={"e_slug": event.slug}))

    return render(
        request,
        "questions.jinja",
        {
            "user": user,
            "open": not event.archived,
            "event": event,
            "questions": questions,
            "q_form": question_form,
        },
    )