Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Sublet Amenity Return Formatting #251

Merged
merged 16 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/sublet/migrations/0003_alter_sublet_baths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.9 on 2024-03-05 21:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("sublet", "0002_auto_20240209_1649"),
]

operations = [
migrations.AlterField(
model_name="sublet",
name="baths",
field=models.DecimalField(blank=True, decimal_places=1, max_digits=3, null=True),
),
]
2 changes: 1 addition & 1 deletion backend/sublet/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Sublet(models.Model):
title = models.CharField(max_length=255)
address = models.CharField(max_length=255, null=True, blank=True)
beds = models.IntegerField(null=True, blank=True)
baths = models.IntegerField(null=True, blank=True)
baths = models.DecimalField(max_digits=3, decimal_places=1, null=True, blank=True)
description = models.TextField(null=True, blank=True)
external_link = models.URLField(max_length=255)
price = models.IntegerField()
Expand Down
8 changes: 6 additions & 2 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def destroy(self, instance):


class SubletSerializerRead(serializers.ModelSerializer):
amenities = AmenitySerializer(many=True, required=False)
amenities = serializers.PrimaryKeyRelatedField(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Can we confirm this change by adding an additional check in one of our test cases? Should just be one additional assert or something to assert that the type now returned is a list of strings

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed! 19a351c checks this by a direct comparison to the expected string list output.

many=True, queryset=Amenity.objects.all(), required=False
)
images = SubletImageURLSerializer(many=True, required=False)

class Meta:
Expand All @@ -149,7 +151,9 @@ class Meta:

# simple sublet serializer for use when pulling all serializers/etc
class SubletSerializerSimple(serializers.ModelSerializer):
amenities = AmenitySerializer(many=True, required=False)
amenities = serializers.PrimaryKeyRelatedField(
many=True, queryset=Amenity.objects.all(), required=False
)
images = SubletImageURLSerializer(many=True, required=False)

class Meta:
Expand Down
12 changes: 12 additions & 0 deletions backend/sublet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,30 @@
router.register(r"properties", Properties, basename="properties")

additional_urls = [
# List of all amenities
path("amenities/", Amenities.as_view(), name="amenities"),
# All favorites for user
path("favorites/", UserFavorites.as_view(), name="user-favorites"),
# All offers made by user
path("offers/", UserOffers.as_view(), name="user-offers"),
# Favorites
# post: add a sublet to the user's favorites
# delete: remove a sublet from the user's favorites
path(
"properties/<sublet_id>/favorites/",
Favorites.as_view({"post": "create", "delete": "destroy"}),
),
# Offers
# get: list all offers for a sublet
# post: create an offer for a sublet
# delete: delete an offer for a sublet
path(
"properties/<sublet_id>/offers/",
Offers.as_view({"get": "list", "post": "create", "delete": "destroy"}),
),
# Image Creation
path("properties/<sublet_id>/images/", CreateImages.as_view()),
# Image Deletion
path("properties/images/<image_id>/", DeleteImage.as_view()),
]

Expand Down
8 changes: 5 additions & 3 deletions backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_create_sublet(self):
"title": "Test Sublet1",
"address": "1234 Test Street",
"beds": 2,
"baths": 1,
"baths": "1.5",
"description": "This is a test sublet.",
"external_link": "https://example.com",
"price": 1000,
Expand All @@ -64,6 +64,7 @@ def test_create_sublet(self):
"expires_at",
"start_date",
"end_date",
"amenities",
]
[self.assertEqual(payload[key], res_json[key]) for key in match_keys]
self.assertIn("id", res_json)
Expand Down Expand Up @@ -184,7 +185,7 @@ def test_browse_sublet(self):
"title": "Test Sublet2",
"address": "1234 Test Street",
"beds": 2,
"baths": 1,
"baths": "1.5",
"description": "This is a test sublet.",
"external_link": "https://example.com",
"price": 1000,
Expand All @@ -201,7 +202,8 @@ def test_browse_sublet(self):
self.assertEqual(res_json["title"], "Test Sublet2")
self.assertEqual(res_json["address"], "1234 Test Street")
self.assertEqual(res_json["beds"], 2)
self.assertEqual(res_json["baths"], 1)
self.assertEqual(res_json["baths"], "1.5")
self.assertEqual(res_json["amenities"], ["Amenity1", "Amenity2"])

def test_delete_sublet(self):
sublets_count = Sublet.objects.all().count()
Expand Down
Loading