diff --git a/colleges/models/potential_fellowship.py b/colleges/models/potential_fellowship.py index 2c39230ce9c382212b6ae48387163c03bf09fd15..40877b3c20f5a9f4e046982c336de1e24f928ea0 100644 --- a/colleges/models/potential_fellowship.py +++ b/colleges/models/potential_fellowship.py @@ -57,6 +57,18 @@ class PotentialFellowship(models.Model): def __str__(self): return '%s, %s' % (self.profile.__str__(), self.get_status_display()) + def can_vote(self, user): + """ + Determines whether user can vote on election for this PotentialFellow. + Qualifying conditions (either of the following): + * is Admin + * is in AdvisoryBoard for this College's Academic Field + * is a Senior Fellow in the College proposed + """ + return (user.contributor.is_sp_admin() or + user.contributor.is_in_advisory_board and user.contributor.profile.acad_field == self.college.acad_field or + user.contributor.fellowships.senior().filter(college=self.college).exists()) + def latest_event_details(self): event = self.potentialfellowshipevent_set.order_by('-noted_on').first() if not event: diff --git a/colleges/permissions.py b/colleges/permissions.py index fdd9769c89d9e5e686f5c1a9eccd14c763671082..f27cf1a4fc52643f759feb5e43aff2dd6c30224f 100644 --- a/colleges/permissions.py +++ b/colleges/permissions.py @@ -32,17 +32,3 @@ def fellowship_or_admin_required(): return True raise PermissionDenied return user_passes_test(test) - - -def can_vote_on_potential_fellowship_for_college(user, college): - """ - Qualifying conditions (either of the following): - * is Admin - * is in AdvisoryBoard for this College's Academic Field - * is a Senior Fellow in this College - """ - if is_in_group(user, 'SciPost Administrators'): - return True - if is_in_group(user, 'Advisory Board') and user.contributor.profile.acad_field == college.acad_field: - return True - return user.contributor.fellowships.senior().filter(college=college).exists() diff --git a/colleges/views.py b/colleges/views.py index 9edd490c69701408e92a3419b6c07fc2492cd005..e3e8bbe618645056459e9a6edc48d2d845b0fcfe 100644 --- a/colleges/views.py +++ b/colleges/views.py @@ -463,8 +463,7 @@ class PotentialFellowshipDetailView(PermissionsMixin, DetailView): @permission_required('scipost.can_vote_on_potentialfellowship', raise_exception=True) def vote_on_potential_fellowship(request, potfel_id, vote): potfel = get_object_or_404(PotentialFellowship, pk=potfel_id) - from colleges.permissions import can_vote_on_potential_fellowship_for_college - if not can_vote_on_potential_fellowship_for_college(request.user, potfel.college): + if not potfel.can_vote(request.user): raise Http404 potfel.in_agreement.remove(request.user.contributor) potfel.in_abstain.remove(request.user.contributor) diff --git a/scipost/models.py b/scipost/models.py index 4fd2fd3ee7dfb0f1e8478c9e88eee8c49b3ad486..9c3f3f72301643f3e291f5b43e2e30e9cd518273 100644 --- a/scipost/models.py +++ b/scipost/models.py @@ -130,6 +130,11 @@ class Contributor(models.Model): return (self.user.groups.filter(name='Editorial Administrators').exists() or self.user.is_superuser) + def is_in_advisory_board(self): + """Check if Contributor is in the Advisory Board.""" + return (self.user.groups.filter(name='Advisory Board').exists() + or self.user.is_superuser) + def is_active_fellow(self): """Check if Contributor is a member of the Editorial College.""" return self.fellowships.active().exists() or self.user.is_superuser