|
| 1 | + |
1 | 2 | from django.contrib.auth import get_user_model |
2 | 3 | from django.core.exceptions import ValidationError as ModelValidationError |
3 | 4 | from profanity_check import predict |
@@ -98,10 +99,32 @@ def get_condition(self, obj): |
98 | 99 |
|
99 | 100 |
|
100 | 101 | class SubletDataSerializer(ModelSerializer): |
| 102 | + latitude = SerializerMethodField() |
| 103 | + longitude = SerializerMethodField() |
| 104 | + |
101 | 105 | class Meta: |
102 | 106 | model = Sublet |
103 | | - fields = ["street_address", "beds", "baths", "start_date", "end_date"] |
| 107 | + fields = [ |
| 108 | + "street_address", |
| 109 | + "beds", |
| 110 | + "baths", |
| 111 | + "start_date", |
| 112 | + "end_date", |
| 113 | + "latitude", |
| 114 | + "longitude", |
| 115 | + ] |
| 116 | + |
| 117 | + def get_latitude(self, obj): |
| 118 | + approx_lat, _ = obj.approximate_location |
| 119 | + if approx_lat is not None: |
| 120 | + return float(approx_lat) |
| 121 | + return None |
104 | 122 |
|
| 123 | + def get_longitude(self, obj): |
| 124 | + _, approx_lon = obj.approximate_location |
| 125 | + if approx_lon is not None: |
| 126 | + return float(approx_lon) |
| 127 | + return None |
105 | 128 |
|
106 | 129 | # Unified serializer for all listing types (Items and Sublets); used for CRUD operations |
107 | 130 | class ListingSerializer(ListingTypeMixin, ModelSerializer): |
@@ -265,12 +288,23 @@ def _create_item(self, validated_data, additional_data): |
265 | 288 | def _create_sublet(self, validated_data, additional_data): |
266 | 289 | tags = validated_data.pop("tags", None) |
267 | 290 |
|
| 291 | + latitude = additional_data.get("latitude") |
| 292 | + longitude = additional_data.get("longitude") |
| 293 | + |
| 294 | + |
| 295 | + if latitude is not None: |
| 296 | + latitude = float(latitude) |
| 297 | + if longitude is not None: |
| 298 | + longitude = float(longitude) |
| 299 | + |
268 | 300 | sublet = Sublet.objects.create( |
269 | 301 | street_address=additional_data.get("street_address"), |
270 | 302 | beds=additional_data.get("beds"), |
271 | 303 | baths=additional_data.get("baths"), |
272 | 304 | start_date=additional_data.get("start_date"), |
273 | 305 | end_date=additional_data.get("end_date"), |
| 306 | + latitude=latitude, |
| 307 | + longitude=longitude, |
274 | 308 | **validated_data, |
275 | 309 | ) |
276 | 310 |
|
@@ -323,10 +357,16 @@ def _update_item(self, instance, additional_data): |
323 | 357 |
|
324 | 358 | def _update_sublet(self, instance, additional_data): |
325 | 359 | sublet = instance.sublet |
326 | | - sublet_fields = ["street_address", "beds", "baths", "start_date", "end_date"] |
327 | | - for field in sublet_fields: |
| 360 | + str_fields = ["street_address", "beds", "baths", "start_date", "end_date"] |
| 361 | + float_fields = ["latitude", "longitude"] |
| 362 | + for field in str_fields: |
328 | 363 | if field in additional_data: |
329 | 364 | setattr(sublet, field, additional_data[field]) |
| 365 | + |
| 366 | + for field in float_fields: |
| 367 | + if field in additional_data: |
| 368 | + value = additional_data[field] |
| 369 | + setattr(sublet, field, float(value) if value is not None else None) |
330 | 370 | sublet.full_clean() |
331 | 371 | sublet.save() |
332 | 372 |
|
|
0 commit comments