Skip to content

Commit

Permalink
rename, update ownershiprequest model fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeweng committed Oct 18, 2024
1 parent 5fba7b8 commit 6b511f0
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
9 changes: 4 additions & 5 deletions backend/clubs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,19 @@ class OwnershipRequestAdmin(admin.ModelAdmin):
"person__username",
"person__email",
"club__name",
"club__pk",
"created_at",
)
list_display = ("person", "club", "email", "withdrew", "created_at")
list_filter = ("withdrew",)
list_display = ("person", "club", "email", "withdrawn", "created_at")
list_filter = ("withdrawn",)

def person(self, obj):
return obj.person.username
return obj.requester.username

def club(self, obj):
return obj.club.name

def email(self, obj):
return obj.person.email
return obj.requester.email


class MembershipAdmin(admin.ModelAdmin):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 5.0.4 on 2024-10-18 11:42

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("clubs", "0118_ownershiprequest"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.RenameField(
model_name="ownershiprequest",
old_name="withdrew",
new_name="withdrawn",
),
migrations.RenameField(
model_name="ownershiprequest",
old_name="person",
new_name="requester",
),
migrations.AlterField(
model_name="ownershiprequest",
name="club",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="ownership_requests",
to="clubs.club"
),
),
migrations.AlterField(
model_name="ownershiprequest",
name="requester",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="ownership_requests",
to=settings.AUTH_USER_MODEL
),
),
]
34 changes: 19 additions & 15 deletions backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,28 +1127,34 @@ class OwnershipRequest(models.Model):
Represents a user's request to take ownership of a club
"""

person = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
club = models.ForeignKey(Club, on_delete=models.CASCADE)
requester = models.ForeignKey(
get_user_model(), on_delete=models.CASCADE, related_name="ownership_requests"
)
club = models.ForeignKey(
Club, on_delete=models.CASCADE, related_name="ownership_requests"
)

withdrew = models.BooleanField(default=False)
withdrawn = models.BooleanField(default=False)

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return "<OwnershipRequest: {} for {}, with email {}>".format(
self.person.username, self.club.code, self.person.email
)
return f"<OwnershipRequest: {self.requester.username} for {self.club.code}>"

def send_request(self, request=None):
domain = get_domain(request)

edit_url = settings.EDIT_URL.format(domain=domain, club=self.club.code)

club_name = self.club.name

full_name = self.requester.get_full_name()

context = {
"club_name": self.club.name,
"edit_url": "{}/member".format(
settings.EDIT_URL.format(domain=domain, club=self.club.code)
),
"full_name": self.person.get_full_name(),
"club_name": club_name,
"edit_url": f"{edit_url}/member",
"full_name": full_name,
}

owner_emails = list(
Expand All @@ -1159,15 +1165,13 @@ def send_request(self, request=None):

send_mail_helper(
name="ownershiprequest",
subject="Ownership Request from {} for {}".format(
self.person.get_full_name(), self.club.name
),
subject=f"Ownership Request from {full_name} for {club_name}",
emails=owner_emails,
context=context,
)

class Meta:
unique_together = (("person", "club"),)
unique_together = (("requester", "club"),)


class Advisor(models.Model):
Expand Down
24 changes: 13 additions & 11 deletions backend/clubs/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2005,20 +2005,22 @@ class OwnershipRequestSerializer(serializers.ModelSerializer):
Used by club owners to see who has requested to be owner of the club.
"""

person = serializers.HiddenField(default=serializers.CurrentUserDefault())
requester = serializers.HiddenField(default=serializers.CurrentUserDefault())
club = serializers.SlugRelatedField(queryset=Club.objects.all(), slug_field="code")
name = serializers.SerializerMethodField("get_full_name")
username = serializers.CharField(source="person.username", read_only=True)
email = serializers.EmailField(source="person.email", read_only=True)
username = serializers.CharField(source="requester.username", read_only=True)
email = serializers.EmailField(source="requester.email", read_only=True)

school = SchoolSerializer(many=True, source="person.profile.school", read_only=True)
major = MajorSerializer(many=True, source="person.profile.major", read_only=True)
school = SchoolSerializer(
many=True, source="requester.profile.school", read_only=True
)
major = MajorSerializer(many=True, source="requester.profile.major", read_only=True)
graduation_year = serializers.IntegerField(
source="person.profile.graduation_year", read_only=True
source="requester.profile.graduation_year", read_only=True
)

def get_full_name(self, obj):
return obj.person.get_full_name()
return obj.requester.get_full_name()

class Meta:
model = OwnershipRequest
Expand All @@ -2029,13 +2031,13 @@ class Meta:
"graduation_year",
"major",
"name",
"person",
"requester",
"school",
"username",
)
validators = [
validators.UniqueTogetherValidator(
queryset=OwnershipRequest.objects.all(), fields=["club", "person"]
queryset=OwnershipRequest.objects.all(), fields=["club", "requester"]
)
]

Expand All @@ -2045,7 +2047,7 @@ class UserOwnershipRequestSerializer(serializers.ModelSerializer):
Used by the users to return the clubs that the user has sent OwnershipRequest to.
"""

person = serializers.HiddenField(default=serializers.CurrentUserDefault())
requester = serializers.HiddenField(default=serializers.CurrentUserDefault())
club = serializers.SlugRelatedField(queryset=Club.objects.all(), slug_field="code")
club_name = serializers.CharField(source="club.name", read_only=True)

Expand All @@ -2061,7 +2063,7 @@ def create(self, validated_data):

class Meta:
model = OwnershipRequest
fields = ("club", "club_name", "person")
fields = ("club", "club_name", "requester")


class MinimalUserProfileSerializer(serializers.ModelSerializer):
Expand Down
24 changes: 12 additions & 12 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3832,12 +3832,12 @@ def create(self, request, *args, **kwargs):
"""
club = request.data.get("club", None)
obj = OwnershipRequest.objects.filter(
club__code=club, person=request.user
club__code=club, requester=request.user
).first()
if obj is not None:
obj.withdrew = False
obj.withdrawn = False
obj.created_at = timezone.now()
obj.save(update_fields=["withdrew", "created_at"])
obj.save(update_fields=["withdrawn", "created_at"])
return Response(UserOwnershipRequestSerializer(obj).data)

return super().create(request, *args, **kwargs)
Expand All @@ -3850,20 +3850,20 @@ def destroy(self, request, *args, **kwargs):
owners with requests.
"""
obj = self.get_object()
obj.withdrew = True
obj.save(update_fields=["withdrew"])
obj.withdrawn = True
obj.save(update_fields=["withdrawn"])

return Response({"success": True})

def get_queryset(self):
return OwnershipRequest.objects.filter(
person=self.request.user,
withdrew=False,
requester=self.request.user,
withdrawn=False,
club__archived=False,
)


class OwnershipRequestOwnerViewSet(XLSXFormatterMixin, viewsets.ModelViewSet):
class OwnershipRequestOwnerViewSet(viewsets.ModelViewSet):
"""
list:
Return a list of users who have sent ownership request to the club.
Expand All @@ -3875,11 +3875,11 @@ class OwnershipRequestOwnerViewSet(XLSXFormatterMixin, viewsets.ModelViewSet):
serializer_class = OwnershipRequestSerializer
permission_classes = [OwnershipRequestPermission | IsSuperuser]
http_method_names = ["get", "post", "delete"]
lookup_field = "person__username"
lookup_field = "requester__username"

def get_queryset(self):
return OwnershipRequest.objects.filter(
club__code=self.kwargs["club_code"], withdrew=False
club__code=self.kwargs["club_code"], withdrawn=False
)

@action(detail=True, methods=["post"])
Expand All @@ -3903,7 +3903,7 @@ def accept(self, request, *ages, **kwargs):
"""
request_object = self.get_object()
membership, created = Membership.objects.get_or_create(
person=request_object.person,
requester=request_object.requester,
club=request_object.club,
defaults={"role": Membership.ROLE_OWNER},
)
Expand All @@ -3926,7 +3926,7 @@ class OwnershipRequestSuperuserAPIView(generics.ListAPIView):

def get_queryset(self):
return OwnershipRequest.objects.filter(
withdrew=False, created_at__lte=timezone.now() - datetime.timedelta(days=7)
withdrawn=False, created_at__lte=timezone.now() - datetime.timedelta(days=7)
)


Expand Down

0 comments on commit 6b511f0

Please sign in to comment.