Skip to content

Commit

Permalink
Remove Me Groups (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcai122 authored Nov 12, 2024
1 parent f6a1259 commit 44965e9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 99 deletions.
31 changes: 0 additions & 31 deletions backend/gsr_booking/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ def get_is_wharton(self, obj):
return obj["lid"] == 1


class MiniUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ["username", "first_name", "last_name"]


class GroupMembershipSerializer(serializers.ModelSerializer):
user = MiniUserSerializer(read_only=True)
group = serializers.SlugRelatedField(slug_field="name", queryset=Group.objects.all())
color = serializers.SlugRelatedField(slug_field="color", read_only=True, source="group")

class Meta:
model = GroupMembership
fields = [
"user",
"group",
"type",
"pennkey_allow",
Expand Down Expand Up @@ -73,29 +65,6 @@ def to_internal_value(self, data):
return None # TODO: If you want to update based on BookingField, implement this.


class UserSerializer(serializers.ModelSerializer):
booking_groups = serializers.SerializerMethodField()

def get_booking_groups(self, obj):
result = []
for membership in GroupMembership.objects.filter(accepted=True, user=obj):
result.append(
{
"name": membership.group.name,
"id": membership.group.id,
"color": membership.group.color,
"pennkey_allow": membership.pennkey_allow,
"notifications": membership.notifications,
}
)

return result

class Meta:
model = User
fields = ["username", "booking_groups"]


class GSRSerializer(serializers.ModelSerializer):
class Meta:
model = GSR
Expand Down
4 changes: 2 additions & 2 deletions backend/gsr_booking/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
GroupMembershipViewSet,
GroupViewSet,
Locations,
MyMembershipViewSet,
RecentGSRs,
ReservationsView,
UserViewSet,
)
from utils.cache import Cache


router = routers.DefaultRouter()

router.register(r"users", UserViewSet)
router.register(r"mymemberships", MyMembershipViewSet, "mymemberships")
router.register(r"membership", GroupMembershipViewSet)
router.register(r"groups", GroupViewSet)

Expand Down
53 changes: 9 additions & 44 deletions backend/gsr_booking/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,31 @@

from gsr_booking.api_wrapper import APIError, GSRBooker, WhartonGSRBooker
from gsr_booking.models import GSR, Group, GroupMembership, GSRBooking
from gsr_booking.serializers import (
GroupMembershipSerializer,
GroupSerializer,
GSRSerializer,
UserSerializer,
)
from gsr_booking.serializers import GroupMembershipSerializer, GroupSerializer, GSRSerializer
from pennmobile.analytics import Metric, record_analytics


User = get_user_model()


class UserViewSet(viewsets.ReadOnlyModelViewSet):
"""
Can specify `me` instead of the `username` to retrieve details on the current user.
"""

queryset = User.objects.all().prefetch_related(
Prefetch("booking_groups", Group.objects.filter(memberships__accepted=True))
)
class MyMembershipViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [IsAuthenticated]
serializer_class = UserSerializer
lookup_field = "username"
filter_backends = [DjangoFilterBackend]
filterset_fields = ["username", "first_name", "last_name"]

def get_object(self):
lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
param = self.kwargs[lookup_url_kwarg]
if param == "me":
return self.request.user
else:
return super().get_object()
serializer_class = GroupMembershipSerializer

def get_queryset(self):
if not self.request.user.is_authenticated:
return User.objects.none()
return GroupMembership.objects.filter(user=self.request.user, accepted=True)

queryset = User.objects.all()
queryset = queryset.prefetch_related(
Prefetch(
"memberships",
GroupMembership.objects.filter(
group__in=self.request.user.booking_groups.all(), accepted=True
),
)
)
return queryset

@action(detail=True, methods=["get"])
def invites(self, request, username=None):
@action(detail=False, methods=["get"])
def invites(self, request):
"""
Retrieve all invites for a given user.
"""

user = get_object_or_404(User, username=username)
return Response(
GroupMembershipSerializer(
GroupMembership.objects.filter(
user=user, accepted=False, group__in=self.request.user.booking_groups.all()
user=request.user,
accepted=False,
group__in=self.request.user.booking_groups.all(),
),
many=True,
).data
Expand Down
33 changes: 15 additions & 18 deletions backend/tests/gsr_booking/test_gsr_booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
User = get_user_model()


class UserViewTestCase(TestCase):
class MyMembershipViewTestCase(TestCase):
def setUp(self):
self.user1 = User.objects.create_user(
username="user1", password="password", first_name="user", last_name="one"
Expand All @@ -17,28 +17,25 @@ def setUp(self):
username="user2", password="password", first_name="user", last_name="two"
)

self.group = Group.objects.create(owner=self.user1, name="g1", color="blue")
self.group.members.add(self.user1)
memship = self.group.memberships.all()[0]
memship.accepted = True
memship.save()
Group.objects.create(
owner=self.user1, name="g1", color="blue"
) # creating group also adds user
group2 = Group.objects.create(owner=self.user2, name="g2", color="blue")
GroupMembership.objects.create(user=self.user1, group=group2, accepted=True)
group3 = Group.objects.create(owner=self.user2, name="g3", color="blue")
GroupMembership.objects.create(user=self.user1, group=group3)
self.client = APIClient()
self.client.login(username="user1", password="password")

def test_user_list(self):
response = self.client.get("/gsr/users/")
def test_user_memberships(self):
response = self.client.get("/gsr/mymemberships/")
self.assertTrue(200, response.status_code)
self.assertEqual(2, len(response.data))

def test_user_detail_in_group(self):
response = self.client.get("/gsr/users/user1/")
self.assertTrue(200, response.status_code)
self.assertEqual(2, len(response.data["booking_groups"]))

def test_me_user_detail_in_group(self):
response = self.client.get("/gsr/users/me/")
def test_user_invites(self):
response = self.client.get("/gsr/mymemberships/invites/")
self.assertTrue(200, response.status_code)
self.assertEqual(2, len(response.data["booking_groups"]))
self.assertEqual(1, len(response.data))


class MembershipViewTestCase(TestCase):
Expand Down Expand Up @@ -159,7 +156,7 @@ def setUp(self):
def test_get_groups(self):
response = self.client.get("/gsr/groups/")
self.assertEqual(200, response.status_code)
self.assertEqual(2, len(response.data))
self.assertEqual(1, len(response.data))

def test_get_groups_includes_invites(self):
GroupMembership.objects.create(user=self.user1, group=self.group2, accepted=False)
Expand All @@ -173,7 +170,7 @@ def test_get_group_not_involved_fails(self):
def test_make_group(self):
response = self.client.post("/gsr/groups/", {"name": "gx", "color": "blue"})
self.assertEqual(201, response.status_code, response.data)
self.assertEqual(5, Group.objects.count())
self.assertEqual(3, Group.objects.count())
self.assertEqual("user1", Group.objects.get(name="gx").owner.username)

def test_only_accepted_memberships(self):
Expand Down
3 changes: 1 addition & 2 deletions backend/tests/user/test_notifs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from identity.identity import attest, container, get_platform_jwks
from rest_framework.test import APIClient

from gsr_booking.models import GSR, Group, GSRBooking, Reservation
from gsr_booking.models import GSR, GSRBooking, Reservation
from user.models import NotificationSetting, NotificationToken


Expand Down Expand Up @@ -350,7 +350,6 @@ def setUp(self):
start=g.start,
end=g.end,
creator=self.test_user,
group=Group.objects.get(owner=self.test_user),
)

g.reservation = r
Expand Down
2 changes: 0 additions & 2 deletions backend/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.db.models.signals import post_save
from django.dispatch import receiver

from gsr_booking.models import Group
from laundry.models import LaundryRoom
from penndata.models import FitnessRoom

Expand Down Expand Up @@ -71,7 +70,6 @@ def create_or_update_user_profile(sender, instance, created, **kwargs):
object exists for that User, it will create one
"""
Profile.objects.get_or_create(user=instance)
Group.objects.get_or_create(owner=instance, name="Me", color="#14f7d1")

# notifications
token, _ = NotificationToken.objects.get_or_create(user=instance)
Expand Down

0 comments on commit 44965e9

Please sign in to comment.