Skip to content

Commit

Permalink
Refactor PSR vote views
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillarAnand committed Aug 6, 2016
1 parent ccc0276 commit a9f2859
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 90 deletions.
1 change: 0 additions & 1 deletion junction/proposals/forms.py
Expand Up @@ -152,7 +152,6 @@ class ProposalReviewForm(forms.Form):


class ProposalReviewerVoteForm(forms.Form):

"""
Used by ProposalSectionReviewers to vote on proposals.
"""
Expand Down
69 changes: 69 additions & 0 deletions junction/proposals/utils.py
@@ -0,0 +1,69 @@
from django.core.exceptions import PermissionDenied

from junction.base.constants import PSRVotePhase, ProposalCommentType
from junction.proposals import permissions
from junction.proposals.models import ProposalComment, ProposalSectionReviewer, \
ProposalSectionReviewerVote, ProposalSectionReviewerVoteValue


def get_reviewer_vote_info(user, conference, proposal, vote_phase):

if vote_phase == PSRVotePhase.PRIMARY:
comment_type = ProposalCommentType.GENERAL
elif vote_phase == PSRVotePhase.SECONDARY:
comment_type = ProposalCommentType.SECONDARY_VOTING

if not (permissions.is_proposal_section_reviewer(user,
conference, proposal) and
permissions.is_proposal_voting_allowed(proposal)):
raise PermissionDenied

voter = ProposalSectionReviewer.objects.get(
conference_reviewer__reviewer=user,
conference_reviewer__conference=conference,
proposal_section=proposal.proposal_section)

try:
psr_vote = ProposalSectionReviewerVote.objects.get(proposal=proposal, voter=voter, phase=vote_phase)
except ProposalSectionReviewerVote.DoesNotExist:
psr_vote = None

try:
vote_comment = ProposalComment.objects.get(
proposal=proposal,
commenter=user,
vote=True,
deleted=False,
comment_type=comment_type,
)
except:
vote_comment = None

return psr_vote, vote_comment


def update_reviewer_vote_info(user, psr_vote, vote_value, comment, phase, proposal, conference):

if phase == PSRVotePhase.PRIMARY:
comment_type = ProposalCommentType.GENERAL
elif phase == PSRVotePhase.SECONDARY:
comment_type = ProposalCommentType.SECONDARY_VOTING

voter = ProposalSectionReviewer.objects.filter(
conference_reviewer__reviewer=user,
conference_reviewer__conference=conference,
proposal_section=proposal.proposal_section)[0]

vote_value = ProposalSectionReviewerVoteValue.objects.filter(vote_value=vote_value)[0]

psr_vote, _ = ProposalSectionReviewerVote.objects.update_or_create(
proposal=proposal, voter=voter, phase=phase,
defaults={'vote_value': vote_value}
)

p_comment, _ = ProposalComment.objects.update_or_create(
proposal=proposal, commenter=user, vote=True, comment_type=comment_type,
defaults={'comment': comment}
)

return psr_vote, p_comment
154 changes: 65 additions & 89 deletions junction/proposals/votes_views.py
@@ -1,31 +1,21 @@
# -*- coding: utf-8 -*-

# Third Party Stuff
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseForbidden
from django.http.response import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.views.decorators.http import require_http_methods

# Junction Stuff
from junction.base.constants import ConferenceSettingConstants, ProposalUserVoteRole
from junction.base.constants import ConferenceSettingConstants, \
ProposalUserVoteRole
from junction.conferences.models import Conference

from . import permissions
from . import utils
from .forms import ProposalReviewerVoteForm
from .models import (
Proposal,
ProposalComment,
ProposalCommentType,
ProposalCommentVote,
ProposalSectionReviewer,
ProposalSectionReviewerVote,
ProposalSectionReviewerVoteValue,
ProposalVote,
PSRVotePhase,
)
from .models import PSRVotePhase, Proposal, ProposalComment, \
ProposalCommentVote, ProposalSectionReviewer, ProposalSectionReviewerVote, \
ProposalSectionReviewerVoteValue, ProposalVote


@login_required
Expand Down Expand Up @@ -113,62 +103,30 @@ def proposal_comment_down_vote(request, conference_slug, proposal_slug,

@login_required
@require_http_methods(['GET', 'POST'])
def proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=None):
if not vote_phase:
vote_phase = PSRVotePhase.PRIMARY
comment_type = ProposalCommentType.GENERAL
elif vote_phase == PSRVotePhase.SECONDARY:
print('aaaaaaaaaaaaaaaaaaaaa')
comment_type = ProposalCommentType.SECONDARY_VOTING

def proposal_reviewer_vote(request, conference_slug, proposal_slug):
user = request.user
vote_phase = PSRVotePhase.PRIMARY
conference = get_object_or_404(Conference, slug=conference_slug)
proposal = get_object_or_404(Proposal, slug=proposal_slug,
conference=conference)

if not (permissions.is_proposal_section_reviewer(request.user,
conference, proposal) and
permissions.is_proposal_voting_allowed(proposal)):
raise PermissionDenied

vote_value = None

try:
vote = ProposalSectionReviewerVote.objects.get(
proposal=proposal,
voter=ProposalSectionReviewer.objects.get(
conference_reviewer__reviewer=request.user,
conference_reviewer__conference=conference,
proposal_section=proposal.proposal_section),
phase=vote_phase,
)
vote_value = vote.vote_value.vote_value
except ProposalSectionReviewerVote.DoesNotExist:
vote = None

try:
vote_comment = ProposalComment.objects.get(
proposal=proposal,
commenter=request.user,
vote=True,
deleted=False,
comment_type=comment_type,
)
except:
vote_comment = None
psr_vote, p_comment = utils.get_reviewer_vote_info(user, conference, proposal, vote_phase)

if request.method == 'GET':
if vote_comment:
if psr_vote and p_comment:
proposal_vote_form = ProposalReviewerVoteForm(
initial={'vote_value': vote_value,
'comment': vote_comment.comment},
conference=conference)
conference=conference,
initial={
'vote_value': psr_vote.vote_value.vote_value,
'comment': p_comment.comment
},
)
else:
proposal_vote_form = ProposalReviewerVoteForm(
initial={'vote_value': vote_value},
conference=conference)
proposal_vote_form = ProposalReviewerVoteForm(conference=conference)
ctx = {
'proposal': proposal,
'form': proposal_vote_form,
'vote': vote,
'vote': psr_vote,
}

return render(request, 'proposals/vote.html', ctx)
Expand All @@ -184,36 +142,54 @@ def proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=N
# Valid Form
vote_value = form.cleaned_data['vote_value']
comment = form.cleaned_data['comment']
if not vote:
vote = ProposalSectionReviewerVote.objects.create(
proposal=proposal,
voter=ProposalSectionReviewer.objects.filter(
conference_reviewer__reviewer=request.user,
conference_reviewer__conference=conference,
proposal_section=proposal.proposal_section)[0],
vote_value=ProposalSectionReviewerVoteValue.objects.filter(
vote_value=vote_value)[0],
phase=vote_phase,
)
else:
vote.vote_value = ProposalSectionReviewerVoteValue.objects.filter(
vote_value=vote_value)[0]
vote.save()
if not vote_comment:
vote_comment = ProposalComment.objects.create(
proposal=proposal,
commenter=request.user,
comment=comment,
vote=True,
comment_type=comment_type,
)
else:
vote_comment.comment = comment
vote_comment.save()
print(comment)
utils.update_reviewer_vote_info(user, psr_vote, vote_value, comment, vote_phase, proposal, conference)
return HttpResponseRedirect(reverse('proposals-to-review',
args=[conference.slug]))


@login_required
@require_http_methods(['GET', 'POST'])
def proposal_reviewer_secondary_vote(request, conference_slug, proposal_slug):
vote_phase = PSRVotePhase.SECONDARY
return proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=vote_phase)
user = request.user
conference = get_object_or_404(Conference, slug=conference_slug)
proposal = get_object_or_404(Proposal, slug=proposal_slug,
conference=conference)

psr_vote, p_comment = utils.get_reviewer_vote_info(user, conference, proposal, vote_phase)

if request.method == 'GET':
if psr_vote and p_comment:
proposal_vote_form = ProposalReviewerVoteForm(
conference=conference,
initial={
'vote_value': psr_vote.vote_value.vote_value,
'comment': p_comment.comment
},
)
else:
proposal_vote_form = ProposalReviewerVoteForm(conference=conference)
ctx = {
'proposal': proposal,
'form': proposal_vote_form,
'vote': psr_vote,
}

return render(request, 'proposals/vote.html', ctx)

# POST Workflow
form = ProposalReviewerVoteForm(data=request.POST, conference=conference)
if not form.is_valid():
ctx = {'form': form,
'proposal': proposal,
'form_errors': form.errors}
return render(request, 'proposals/vote.html', ctx)

# Valid Form
vote_value = form.cleaned_data['vote_value']
comment = form.cleaned_data['comment']
print(comment)
utils.update_reviewer_vote_info(user, psr_vote, vote_value, comment, vote_phase, proposal, conference)
return HttpResponseRedirect(reverse('proposals-to-review',
args=[conference.slug]))

0 comments on commit a9f2859

Please sign in to comment.