Skip to content

Commit

Permalink
Merge branch 'rookie-subletting' of github.com:pennlabs/penn-mobile i…
Browse files Browse the repository at this point in the history
…nto rookie-subletting
  • Loading branch information
dr-Jess committed Oct 27, 2023
2 parents 09f4c74 + 23d7fde commit 1afb007
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
32 changes: 32 additions & 0 deletions backend/sublet/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,40 @@ class Meta:
exclude = ["favorites"]
read_only_fields = ["id", "created_date", "subletter", "sublettees"]

# def parse_target_populations(self, raw_target_populations):
# if isinstance(raw_target_populations, list):
# ids = raw_target_populations
# else:
# ids = (
# list()
# if len(raw_target_populations) == 0
# else [int(id) for id in raw_target_populations.split(",")]
# )
# return TargetPopulation.objects.filter(id__in=ids)

def create(self, validated_data):
validated_data["subletter"] = self.context["request"].user
# x = validated_data.pop("amenities")
# print(x)

instance = super().create(validated_data)

# # Update target populations
# # If none of a category was selected, then we will auto-select all populations in that categary
data = self.context["request"].data
print("HELLO")
print(data)
print(data["amenities"])
print(Amenity.objects.all())
amenities = Amenity.objects.filter(name__in=data["amenities"])
print(amenities)
# raw_amenities =
# raw_target_populations = self.parse_target_populations(data["target_populations"])
# target_populations = self.update_target_populations(raw_target_populations)

# instance.target_populations.set(target_populations)
# instance.save()

return super().create(validated_data)

def update(self, instance, validated_data):
Expand Down
33 changes: 18 additions & 15 deletions backend/sublet/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,24 @@ def get_queryset(self):
# return Sublet.objects.filter(expires_at__gte=timezone.now())
return Sublet.objects.all()

def create(self, request, *args, **kwargs):
amenities = request.data.pop("amenities", [])

# check if valid amenities
try:
amenities = [Amenity.objects.get(name=amenity) for amenity in amenities]
except Amenity.DoesNotExist:
return Response({"amenities": "Invalid amenity"}, status=status.HTTP_400_BAD_REQUEST)

serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
sublet = serializer.save()
sublet.amenities.set(amenities)
sublet.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
# def create(self, request, *args, **kwargs):
# # amenities = request.data.pop("amenities", [])
# new_data = request.data
# amenities = new_data.pop("amenities", [])


# # check if valid amenities
# try:
# amenities = [Amenity.objects.get(name=amenity) for amenity in amenities]
# except Amenity.DoesNotExist:
# return Response({"amenities": "Invalid amenity"}, status=status.HTTP_400_BAD_REQUEST)

# serializer = self.get_serializer(data=new_data)
# serializer.is_valid(raise_exception=True)
# sublet = serializer.save()
# sublet.amenities.set(amenities)
# sublet.save()
# return Response(serializer.data, status=status.HTTP_201_CREATED)

def list(self, request, *args, **kwargs):
"""Returns a list of Sublets that match query parameters and user ownership."""
Expand Down
33 changes: 21 additions & 12 deletions backend/tests/sublet/test_sublets.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_create_sublet(self):
"amenities": ["Amenity1", "Amenity2"],
}

response = self.client.post("/properties/", payload)
response = self.client.post("/sublet/properties/", payload)
res_json = json.loads(response.content)
self.assertEqual(2, res_json["beds"])
self.assertEqual("Test Sublet1", res_json["title"])
# sublet = Sublet.objects.get(title="Test Sublet1")
# self.assertEqual(sublet.beds, 2)
print(res_json)
self.assertEqual(payload["beds"], res_json["beds"])
self.assertEqual(payload["title"], res_json["title"])
self.assertIn("created_date", res_json)

def test_update_sublet(self):
# Create a sublet to be updated
Expand All @@ -67,20 +67,24 @@ def test_update_sublet(self):
expires_at="2024-02-01T10:48:02-05:00",
start_date="2024-04-09",
end_date="2024-08-07",
subletter=self.user,
amenities=["Amenity1", "Amenity2"],
)

# Update the sublet using the serializer
data = {"title": "New Title", "beds": 3}

response = self.client.patch(f"/properties/{sublet.id}/", data)
response = self.client.patch(f"/sublet/properties/{sublet.id}/", data)
res_json = json.loads(response.content)
self.assertEqual(3, res_json["beds"])
self.assertEqual(self.id, res_json["id"])
self.assertEqual("New Title", Sublet.objects.get(id=self.id).title)
self.assertEqual("New Title", res_json["title"])

def test_browse_sublets(self):
response = self.client.get("/sublet/properties/")
res_json = json.loads(response.content)
print(res_json)
first_length = len(res_json)
payload = {
"title": "Test Sublet1",
"address": "1234 Test Street",
Expand All @@ -95,11 +99,16 @@ def test_browse_sublets(self):
"end_date": "2024-08-07",
"amenities": ["Amenity1", "Amenity2"],
}
self.client.post("/properties/", payload)
response = self.client.get("/properties/")
res_json = json.loads(response.content)
self.assertEqual(1, len(res_json))
self.assertEqual(2, Sublet.objects.all().count())
self.client.post("/sublet/properties/", payload)
response = self.client.get("/sublet/properties/")
self.assertEqual(1+first_length, len(json.loads(response.content)))
sublet = res_json[-1]
self.assertEqual(sublet["title"], "Test Sublet1")
self.assertEqual(sublet["address"], "1234 Test Street")
self.assertEqual(sublet["beds"], 2)
self.assertEqual(sublet["baths"], 1)
self.assertIsNotNone(sublet["created_date"])


def test_browse_sublet(self):
# browse single sublet by id
Expand Down

0 comments on commit 1afb007

Please sign in to comment.