|
| 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,23 @@ 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 = ["street_address", "beds", "baths", "start_date", "end_date", |
| 108 | + "latitude", "longitude"] |
104 | 109 |
|
| 110 | + def get_latitude(self, obj): |
| 111 | + if obj.approximate_location is not None: |
| 112 | + return float(obj.approximate_location[0]) |
| 113 | + return None |
| 114 | + |
| 115 | + def get_longitude(self, obj): |
| 116 | + if obj.approximate_location is not None: |
| 117 | + return float(obj.approximate_location[1]) |
| 118 | + return None |
105 | 119 |
|
106 | 120 | # Unified serializer for all listing types (Items and Sublets); used for CRUD operations |
107 | 121 | class ListingSerializer(ListingTypeMixin, ModelSerializer): |
@@ -265,12 +279,23 @@ def _create_item(self, validated_data, additional_data): |
265 | 279 | def _create_sublet(self, validated_data, additional_data): |
266 | 280 | tags = validated_data.pop("tags", None) |
267 | 281 |
|
| 282 | + latitude = additional_data.get("latitude") |
| 283 | + longitude = additional_data.get("longitude") |
| 284 | + |
| 285 | + |
| 286 | + if latitude is not None: |
| 287 | + latitude = float(latitude) |
| 288 | + if longitude is not None: |
| 289 | + longitude = float(longitude) |
| 290 | + |
268 | 291 | sublet = Sublet.objects.create( |
269 | 292 | street_address=additional_data.get("street_address"), |
270 | 293 | beds=additional_data.get("beds"), |
271 | 294 | baths=additional_data.get("baths"), |
272 | 295 | start_date=additional_data.get("start_date"), |
273 | 296 | end_date=additional_data.get("end_date"), |
| 297 | + latitude=latitude, |
| 298 | + longitude=longitude, |
274 | 299 | **validated_data, |
275 | 300 | ) |
276 | 301 |
|
@@ -323,10 +348,16 @@ def _update_item(self, instance, additional_data): |
323 | 348 |
|
324 | 349 | def _update_sublet(self, instance, additional_data): |
325 | 350 | sublet = instance.sublet |
326 | | - sublet_fields = ["street_address", "beds", "baths", "start_date", "end_date"] |
327 | | - for field in sublet_fields: |
| 351 | + str_fields = ["street_address", "beds", "baths", "start_date", "end_date"] |
| 352 | + float_fields = ["latitude", "longitude"] |
| 353 | + for field in str_fields: |
328 | 354 | if field in additional_data: |
329 | 355 | setattr(sublet, field, additional_data[field]) |
| 356 | + |
| 357 | + for field in float_fields: |
| 358 | + if field in additional_data: |
| 359 | + value = additional_data[field] |
| 360 | + setattr(sublet, field, float(value) if value is not None else None) |
330 | 361 | sublet.full_clean() |
331 | 362 | sublet.save() |
332 | 363 |
|
|
0 commit comments