diff --git a/backend/clubs/admin.py b/backend/clubs/admin.py index cbfa005b8..bf8dead38 100644 --- a/backend/clubs/admin.py +++ b/backend/clubs/admin.py @@ -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): diff --git a/backend/clubs/migrations/0119_rename_withdrew_ownershiprequest_withdrawn_and_more.py b/backend/clubs/migrations/0119_rename_withdrew_ownershiprequest_withdrawn_and_more.py new file mode 100644 index 000000000..42cc01f7b --- /dev/null +++ b/backend/clubs/migrations/0119_rename_withdrew_ownershiprequest_withdrawn_and_more.py @@ -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 + ), + ), + ] diff --git a/backend/clubs/models.py b/backend/clubs/models.py index 9c08e3437..d564a3189 100644 --- a/backend/clubs/models.py +++ b/backend/clubs/models.py @@ -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 "".format( - self.person.username, self.club.code, self.person.email - ) + return f"" 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( @@ -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): diff --git a/backend/clubs/serializers.py b/backend/clubs/serializers.py index d1fa6375e..988d75b61 100644 --- a/backend/clubs/serializers.py +++ b/backend/clubs/serializers.py @@ -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 @@ -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"] ) ] @@ -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) @@ -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): diff --git a/backend/clubs/views.py b/backend/clubs/views.py index 5f8e5de2b..c415ec2f3 100644 --- a/backend/clubs/views.py +++ b/backend/clubs/views.py @@ -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) @@ -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. @@ -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"]) @@ -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}, ) @@ -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) )