예제 #1
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).annotate(
        vote_count=Count("votes"))
    if user.userprofile.is_admin:
        questions = questions_q.order_by("-vote_count")
    else:
        questions = questions_q.order_by("?")

    question_form = QuestionForm(request.POST or None, instance=question)

    is_replied = 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
        if not question_obj.id:
            if not question_obj.is_anonymous:
                question_obj.asked_by = user
        elif not user.userprofile.is_admin:
            raise Http404
        else:
            is_replied = True
        question_obj.event = event
        question_obj.save()

        if (not Vote.objects.filter(user=user, question=question_obj).exists()
                and not is_replied):
            Vote.objects.create(user=user, question=question_obj)

        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,
        },
    )
예제 #2
0
def event(request, e_slug, q_id=None):
    """Render event questions."""
    event = Event.objects.get(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).annotate(
        vote_count=Count('votes'))
    questions = questions_q.order_by('-vote_count')

    question_form = QuestionForm(request.POST or None, instance=question)

    is_replied = 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
        if not question_obj.id:
            question_obj.asked_by = user
        elif not user.userprofile.is_admin:
            raise Http404
        else:
            is_replied = True
        question_obj.event = event
        question_obj.save()

        if not Vote.objects.filter(
                user=user, question=question_obj).exists() and not is_replied:
            Vote.objects.create(user=user, question=question_obj)

        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
        })