Skip to content

Commit

Permalink
Change club serialization to align with approval status
Browse files Browse the repository at this point in the history
  • Loading branch information
aviupadhyayula committed Oct 4, 2024
1 parent 245e557 commit 7c65598
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
62 changes: 40 additions & 22 deletions backend/clubs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,30 +994,48 @@ def get_fields(self):

def to_representation(self, instance):
"""
Return the previous approved version of a club for users
that should not see unapproved content.
Return appropriate version of club based on user permissions and club status:
- Privileged users see current version (or when bypass=True)
- For non-privileged users:
- During renewal period: show last approved version from past calendar year
- Outside renewal period: show last approved version from past calendar year
"""
if instance.ghost and not instance.approved:
user = self.context["request"].user
user = self.context["request"].user
can_see_pending = user.has_perm("clubs.see_pending_clubs") or user.has_perm(
"clubs.manage_club"
)
is_member = (
user.is_authenticated
and instance.membership_set.filter(person=user).exists()
)
bypass = (
self.context["request"].query_params.get("bypass", "").lower() == "true"
)

can_see_pending = user.has_perm("clubs.see_pending_clubs") or user.has_perm(
"clubs.manage_club"
)
is_member = (
user.is_authenticated
and instance.membership_set.filter(person=user).exists()
)
if not can_see_pending and not is_member:
historical_club = (
instance.history.filter(approved=True)
.order_by("-approved_on")
.first()
)
if historical_club is not None:
approved_instance = historical_club.instance
approved_instance._is_historical = True
return super().to_representation(approved_instance)
return super().to_representation(instance)
if can_see_pending or is_member or instance.approved or bypass:
return super().to_representation(instance)

now = timezone.now()
renewal_start, renewal_end = settings.RENEWAL_PERIOD
in_renewal_period = renewal_start <= now <= renewal_end

start_date = (
renewal_start
if not in_renewal_period
else now.replace(year=now.year - 1, month=1, day=1)
)
approved_version = (
instance.history.filter(approved=True, approved_on__gte=start_date)
.order_by("-approved_on")
.first()
)

if approved_version is not None:
approved_instance = approved_version.instance
approved_instance._is_historical = True
return super().to_representation(approved_instance)

raise serializers.ValidationError("Club not found", code="not_found")

class Meta:
model = Club
Expand Down
12 changes: 7 additions & 5 deletions backend/pennclubs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import datetime
import os

import dj_database_url
Expand Down Expand Up @@ -204,11 +205,12 @@

OSA_EMAILS = ["[email protected]"]


# Controls whether existing clubs can submit for reapproval
REAPPROVAL_QUEUE_OPEN = True
# Controls whether new clubs can submit for initial approval
NEW_APPROVAL_QUEUE_OPEN = True
REAPPROVAL_QUEUE_OPEN = True # controls whether existing clubs can request reapproval
NEW_APPROVAL_QUEUE_OPEN = True # controls whether new clubs can request approval
RENEWAL_PERIOD = (
datetime.date(datetime.date.today().year, 8, 1),
datetime.date(datetime.date.today().year, 9, 30),
) # defines renewal period for club visibility

# File upload settings

Expand Down

0 comments on commit 7c65598

Please sign in to comment.