Skip to content

Commit

Permalink
Added util to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyPancake committed Sep 9, 2024
1 parent 0dfc064 commit ab11026
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions amelie/activities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from amelie.members.forms import PersonSearchForm
from amelie.members.models import Person, Photographer
from amelie.members.query_forms import MailingForm
from amelie.members.utils import is_committee
from amelie.tools import amelie_messages, types
from amelie.tools.decorators import require_actief, require_lid, require_committee, require_board
from amelie.tools.forms import PeriodForm, ExportForm, PeriodKeywordForm
Expand Down Expand Up @@ -252,7 +253,7 @@ def activity(request, pk, deanonymise=False):

# Enable opengraph on this page
metadata_enable_opengraph = True
is_rd = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
is_rd = is_committee(request, "RD")

return render(request, "activity.html", locals())

Expand Down Expand Up @@ -569,7 +570,7 @@ def activity_enrollment_person_search(request, pk):
Search for a person to enroll for this activity.
"""
activity = get_object_or_404(Activity, pk=pk)
is_rd = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
is_rd = is_committee(request, "RD")
if not (activity.can_edit(request.person) or is_rd):
raise PermissionDenied

Expand Down Expand Up @@ -678,7 +679,7 @@ def activity_enrollment_form(request, activity, person=None):
# Django messages have been set in check_enrollment_allowed
return redirect(activity)

is_rd = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
is_rd = is_committee(request, "RD")
if indirect and not (activity.can_edit(request.person) or is_rd):
raise PermissionDenied

Expand Down Expand Up @@ -1309,7 +1310,7 @@ def test_requirement(self, request):

is_board = hasattr(request, 'is_board') and request.is_board
is_organization = obj.organizer in request.person.current_committees()
is_rd = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
is_rd = is_committee(request, "RD")

return is_board or is_organization or is_rd

Expand Down
3 changes: 3 additions & 0 deletions amelie/members/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Defines if a member is in the room duty committee.
def is_committee(request, abbreviation):
return request.person.function_set.filter(committee__abbreviation=abbreviation, end__isnull=True).exists()
3 changes: 2 additions & 1 deletion amelie/members/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from amelie.members.models import Payment, PaymentType, Committee, Function, Membership, MembershipType, Employee, \
Person, Student, Study, StudyPeriod, Preference, PreferenceCategory, UnverifiedEnrollment, Dogroup, \
DogroupGeneration
from amelie.members.utils import is_committee
from amelie.personal_tab.forms import RFIDCardForm
from amelie.personal_tab.models import Authorization, AuthorizationType, Transaction, SEPA_CHAR_VALIDATOR
from amelie.tools.auth import get_oauth_link_code, send_oauth_link_code_email, get_user_info
Expand Down Expand Up @@ -314,7 +315,7 @@ def person_view(request, id, slug):
accounts = []

can_be_anonymized, unable_to_anonymize_reasons = _person_can_be_anonymized(obj)
is_rd = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
is_rd = is_committee(request, "RD")

return render(request, "person.html", locals())

Expand Down
3 changes: 2 additions & 1 deletion amelie/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from amelie.news.models import NewsItem
from amelie.members.forms import PersonalDetailsEditForm, PersonalStudyEditForm
from amelie.members.models import Person, Committee, StudyPeriod
from amelie.members.utils import is_committee
from amelie.education.models import Complaint, EducationEvent
from amelie.statistics.decorators import track_hits
from amelie.tools.auth import get_user_info, unlink_totp, unlink_acount
Expand Down Expand Up @@ -237,7 +238,7 @@ def frontpage(request):
end__isnull=True).exists()

# Room Duty check
context['is_rd'] = request.person.function_set.filter(committee__abbreviation="RD", end__isnull=True).exists()
context['is_rd'] = is_committee(request, "RD")

# Birthdays
context['birthdays'] = Person.objects.members().filter(date_of_birth__day=date.today().day,
Expand Down

0 comments on commit ab11026

Please sign in to comment.