Skip to content

Commit a9f2859

Browse files
committed
Refactor PSR vote views
1 parent ccc0276 commit a9f2859

File tree

3 files changed

+134
-90
lines changed

3 files changed

+134
-90
lines changed

junction/proposals/forms.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ class ProposalReviewForm(forms.Form):
152152

153153

154154
class ProposalReviewerVoteForm(forms.Form):
155-
156155
"""
157156
Used by ProposalSectionReviewers to vote on proposals.
158157
"""

junction/proposals/utils.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
from django.core.exceptions import PermissionDenied
2+
3+
from junction.base.constants import PSRVotePhase, ProposalCommentType
4+
from junction.proposals import permissions
5+
from junction.proposals.models import ProposalComment, ProposalSectionReviewer, \
6+
ProposalSectionReviewerVote, ProposalSectionReviewerVoteValue
7+
8+
9+
def get_reviewer_vote_info(user, conference, proposal, vote_phase):
10+
11+
if vote_phase == PSRVotePhase.PRIMARY:
12+
comment_type = ProposalCommentType.GENERAL
13+
elif vote_phase == PSRVotePhase.SECONDARY:
14+
comment_type = ProposalCommentType.SECONDARY_VOTING
15+
16+
if not (permissions.is_proposal_section_reviewer(user,
17+
conference, proposal) and
18+
permissions.is_proposal_voting_allowed(proposal)):
19+
raise PermissionDenied
20+
21+
voter = ProposalSectionReviewer.objects.get(
22+
conference_reviewer__reviewer=user,
23+
conference_reviewer__conference=conference,
24+
proposal_section=proposal.proposal_section)
25+
26+
try:
27+
psr_vote = ProposalSectionReviewerVote.objects.get(proposal=proposal, voter=voter, phase=vote_phase)
28+
except ProposalSectionReviewerVote.DoesNotExist:
29+
psr_vote = None
30+
31+
try:
32+
vote_comment = ProposalComment.objects.get(
33+
proposal=proposal,
34+
commenter=user,
35+
vote=True,
36+
deleted=False,
37+
comment_type=comment_type,
38+
)
39+
except:
40+
vote_comment = None
41+
42+
return psr_vote, vote_comment
43+
44+
45+
def update_reviewer_vote_info(user, psr_vote, vote_value, comment, phase, proposal, conference):
46+
47+
if phase == PSRVotePhase.PRIMARY:
48+
comment_type = ProposalCommentType.GENERAL
49+
elif phase == PSRVotePhase.SECONDARY:
50+
comment_type = ProposalCommentType.SECONDARY_VOTING
51+
52+
voter = ProposalSectionReviewer.objects.filter(
53+
conference_reviewer__reviewer=user,
54+
conference_reviewer__conference=conference,
55+
proposal_section=proposal.proposal_section)[0]
56+
57+
vote_value = ProposalSectionReviewerVoteValue.objects.filter(vote_value=vote_value)[0]
58+
59+
psr_vote, _ = ProposalSectionReviewerVote.objects.update_or_create(
60+
proposal=proposal, voter=voter, phase=phase,
61+
defaults={'vote_value': vote_value}
62+
)
63+
64+
p_comment, _ = ProposalComment.objects.update_or_create(
65+
proposal=proposal, commenter=user, vote=True, comment_type=comment_type,
66+
defaults={'comment': comment}
67+
)
68+
69+
return psr_vote, p_comment

junction/proposals/votes_views.py

Lines changed: 65 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
# -*- coding: utf-8 -*-
2-
3-
# Third Party Stuff
42
from django.contrib.auth.decorators import login_required
5-
from django.core.exceptions import PermissionDenied
63
from django.core.urlresolvers import reverse
74
from django.http import HttpResponseForbidden
85
from django.http.response import HttpResponse, HttpResponseRedirect
96
from django.shortcuts import get_object_or_404, render
107
from django.views.decorators.http import require_http_methods
118

12-
# Junction Stuff
13-
from junction.base.constants import ConferenceSettingConstants, ProposalUserVoteRole
9+
from junction.base.constants import ConferenceSettingConstants, \
10+
ProposalUserVoteRole
1411
from junction.conferences.models import Conference
1512

1613
from . import permissions
14+
from . import utils
1715
from .forms import ProposalReviewerVoteForm
18-
from .models import (
19-
Proposal,
20-
ProposalComment,
21-
ProposalCommentType,
22-
ProposalCommentVote,
23-
ProposalSectionReviewer,
24-
ProposalSectionReviewerVote,
25-
ProposalSectionReviewerVoteValue,
26-
ProposalVote,
27-
PSRVotePhase,
28-
)
16+
from .models import PSRVotePhase, Proposal, ProposalComment, \
17+
ProposalCommentVote, ProposalSectionReviewer, ProposalSectionReviewerVote, \
18+
ProposalSectionReviewerVoteValue, ProposalVote
2919

3020

3121
@login_required
@@ -113,62 +103,30 @@ def proposal_comment_down_vote(request, conference_slug, proposal_slug,
113103

114104
@login_required
115105
@require_http_methods(['GET', 'POST'])
116-
def proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=None):
117-
if not vote_phase:
118-
vote_phase = PSRVotePhase.PRIMARY
119-
comment_type = ProposalCommentType.GENERAL
120-
elif vote_phase == PSRVotePhase.SECONDARY:
121-
print('aaaaaaaaaaaaaaaaaaaaa')
122-
comment_type = ProposalCommentType.SECONDARY_VOTING
123-
106+
def proposal_reviewer_vote(request, conference_slug, proposal_slug):
107+
user = request.user
108+
vote_phase = PSRVotePhase.PRIMARY
124109
conference = get_object_or_404(Conference, slug=conference_slug)
125110
proposal = get_object_or_404(Proposal, slug=proposal_slug,
126111
conference=conference)
127112

128-
if not (permissions.is_proposal_section_reviewer(request.user,
129-
conference, proposal) and
130-
permissions.is_proposal_voting_allowed(proposal)):
131-
raise PermissionDenied
132-
133-
vote_value = None
134-
135-
try:
136-
vote = ProposalSectionReviewerVote.objects.get(
137-
proposal=proposal,
138-
voter=ProposalSectionReviewer.objects.get(
139-
conference_reviewer__reviewer=request.user,
140-
conference_reviewer__conference=conference,
141-
proposal_section=proposal.proposal_section),
142-
phase=vote_phase,
143-
)
144-
vote_value = vote.vote_value.vote_value
145-
except ProposalSectionReviewerVote.DoesNotExist:
146-
vote = None
147-
148-
try:
149-
vote_comment = ProposalComment.objects.get(
150-
proposal=proposal,
151-
commenter=request.user,
152-
vote=True,
153-
deleted=False,
154-
comment_type=comment_type,
155-
)
156-
except:
157-
vote_comment = None
113+
psr_vote, p_comment = utils.get_reviewer_vote_info(user, conference, proposal, vote_phase)
114+
158115
if request.method == 'GET':
159-
if vote_comment:
116+
if psr_vote and p_comment:
160117
proposal_vote_form = ProposalReviewerVoteForm(
161-
initial={'vote_value': vote_value,
162-
'comment': vote_comment.comment},
163-
conference=conference)
118+
conference=conference,
119+
initial={
120+
'vote_value': psr_vote.vote_value.vote_value,
121+
'comment': p_comment.comment
122+
},
123+
)
164124
else:
165-
proposal_vote_form = ProposalReviewerVoteForm(
166-
initial={'vote_value': vote_value},
167-
conference=conference)
125+
proposal_vote_form = ProposalReviewerVoteForm(conference=conference)
168126
ctx = {
169127
'proposal': proposal,
170128
'form': proposal_vote_form,
171-
'vote': vote,
129+
'vote': psr_vote,
172130
}
173131

174132
return render(request, 'proposals/vote.html', ctx)
@@ -184,36 +142,54 @@ def proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=N
184142
# Valid Form
185143
vote_value = form.cleaned_data['vote_value']
186144
comment = form.cleaned_data['comment']
187-
if not vote:
188-
vote = ProposalSectionReviewerVote.objects.create(
189-
proposal=proposal,
190-
voter=ProposalSectionReviewer.objects.filter(
191-
conference_reviewer__reviewer=request.user,
192-
conference_reviewer__conference=conference,
193-
proposal_section=proposal.proposal_section)[0],
194-
vote_value=ProposalSectionReviewerVoteValue.objects.filter(
195-
vote_value=vote_value)[0],
196-
phase=vote_phase,
197-
)
198-
else:
199-
vote.vote_value = ProposalSectionReviewerVoteValue.objects.filter(
200-
vote_value=vote_value)[0]
201-
vote.save()
202-
if not vote_comment:
203-
vote_comment = ProposalComment.objects.create(
204-
proposal=proposal,
205-
commenter=request.user,
206-
comment=comment,
207-
vote=True,
208-
comment_type=comment_type,
209-
)
210-
else:
211-
vote_comment.comment = comment
212-
vote_comment.save()
145+
print(comment)
146+
utils.update_reviewer_vote_info(user, psr_vote, vote_value, comment, vote_phase, proposal, conference)
213147
return HttpResponseRedirect(reverse('proposals-to-review',
214148
args=[conference.slug]))
215149

216150

151+
@login_required
152+
@require_http_methods(['GET', 'POST'])
217153
def proposal_reviewer_secondary_vote(request, conference_slug, proposal_slug):
218154
vote_phase = PSRVotePhase.SECONDARY
219-
return proposal_reviewer_vote(request, conference_slug, proposal_slug, vote_phase=vote_phase)
155+
user = request.user
156+
conference = get_object_or_404(Conference, slug=conference_slug)
157+
proposal = get_object_or_404(Proposal, slug=proposal_slug,
158+
conference=conference)
159+
160+
psr_vote, p_comment = utils.get_reviewer_vote_info(user, conference, proposal, vote_phase)
161+
162+
if request.method == 'GET':
163+
if psr_vote and p_comment:
164+
proposal_vote_form = ProposalReviewerVoteForm(
165+
conference=conference,
166+
initial={
167+
'vote_value': psr_vote.vote_value.vote_value,
168+
'comment': p_comment.comment
169+
},
170+
)
171+
else:
172+
proposal_vote_form = ProposalReviewerVoteForm(conference=conference)
173+
ctx = {
174+
'proposal': proposal,
175+
'form': proposal_vote_form,
176+
'vote': psr_vote,
177+
}
178+
179+
return render(request, 'proposals/vote.html', ctx)
180+
181+
# POST Workflow
182+
form = ProposalReviewerVoteForm(data=request.POST, conference=conference)
183+
if not form.is_valid():
184+
ctx = {'form': form,
185+
'proposal': proposal,
186+
'form_errors': form.errors}
187+
return render(request, 'proposals/vote.html', ctx)
188+
189+
# Valid Form
190+
vote_value = form.cleaned_data['vote_value']
191+
comment = form.cleaned_data['comment']
192+
print(comment)
193+
utils.update_reviewer_vote_info(user, psr_vote, vote_value, comment, vote_phase, proposal, conference)
194+
return HttpResponseRedirect(reverse('proposals-to-review',
195+
args=[conference.slug]))

0 commit comments

Comments
 (0)