Skip to content

Commit 566de03

Browse files
authored
Update share code booking up and add owner name display (#375)
1 parent 567ed35 commit 566de03

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

backend/gsr_booking/serializers.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,11 @@ class Meta:
8585
class GSRShareCodeSerializer(serializers.ModelSerializer):
8686
status = serializers.SerializerMethodField()
8787
expires_at = serializers.SerializerMethodField()
88-
booking_id = serializers.PrimaryKeyRelatedField(
89-
source="booking", queryset=GSRBooking.objects.all(), write_only=True
88+
booking_id = serializers.SlugRelatedField(
89+
slug_field="booking_id",
90+
source="booking",
91+
queryset=GSRBooking.objects.all(),
92+
write_only=True,
9093
)
9194

9295
class Meta:
@@ -126,11 +129,23 @@ class SharedGSRBookingSerializer(serializers.ModelSerializer):
126129

127130
building = serializers.CharField(source="gsr.name")
128131
is_valid = serializers.SerializerMethodField()
132+
owner_name = serializers.SerializerMethodField()
129133

130134
class Meta:
131135
model = GSRBooking
132-
fields = ["room_name", "building", "start", "end", "is_valid"]
136+
fields = ["room_name", "building", "start", "end", "is_valid", "owner_name"]
133137
read_only_fields = fields
134138

139+
def get_owner_name(self, obj):
140+
user = obj.reservation.creator if obj.reservation else obj.user
141+
if not user:
142+
return "Unknown"
143+
144+
full_name = f"{user.first_name} {user.last_name}".strip()
145+
if full_name:
146+
return full_name
147+
148+
return user.username
149+
135150
def get_is_valid(self, obj):
136151
return obj.end and obj.end > timezone.now()

backend/tests/gsr_booking/test_share_codes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def setUp(self):
5656
def test_create_share_code_success(self):
5757
# Creates a gsr share code successfully
5858
self.client.force_authenticate(user=self.owner)
59-
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
59+
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
6060
self.assertEqual(response.status_code, 201)
6161
payload = json.loads(response.content)
6262

@@ -72,13 +72,13 @@ def test_create_share_code_duplicate(self):
7272
self.client.force_authenticate(user=self.owner)
7373

7474
# First creation
75-
response1 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
75+
response1 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
7676
self.assertEqual(response1.status_code, 201)
7777
payload1 = json.loads(response1.content)
7878
first_code = payload1["code"]
7979

8080
# Second creation (should return existing code)
81-
response2 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
81+
response2 = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
8282
self.assertEqual(response2.status_code, 201) # Changed from 200 to 201
8383
payload2 = json.loads(response2.content)
8484

@@ -89,7 +89,7 @@ def test_create_share_code_duplicate(self):
8989
self.assertEqual(GSRShareCode.objects.filter(booking=self.booking).count(), 1)
9090

9191
def test_create_share_code_without_auth(self):
92-
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
92+
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
9393
self.assertEqual(response.status_code, 403)
9494
self.assertEqual(GSRShareCode.objects.count(), 0)
9595

@@ -217,7 +217,7 @@ def test_create_share_code_for_expired_booking_code_invalid(self):
217217
self.booking.save(update_fields=["end"])
218218

219219
self.client.force_authenticate(user=self.owner)
220-
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
220+
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
221221
self.assertEqual(response.status_code, 201)
222222
payload = json.loads(response.content)
223223
self.assertEqual(payload["status"], "expired")
@@ -247,7 +247,7 @@ def test_create_share_code_replaces_expired(self):
247247

248248
# Create again
249249
self.client.force_authenticate(user=self.owner)
250-
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.id})
250+
response = self.client.post("/api/gsr/share/", {"booking_id": self.booking.booking_id})
251251
self.assertEqual(response.status_code, 201) # Changed from 200 to 201
252252
payload = json.loads(response.content)
253253

0 commit comments

Comments
 (0)