Skip to content

Commit 9808016

Browse files
author
Anthony Li
committed
fix approx geocoords for 0.0 values
1 parent 80a346c commit 9808016

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

backend/market/models.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,15 @@ class Sublet(Listing):
132132
baths = models.PositiveIntegerField()
133133
start_date = models.DateField()
134134
end_date = models.DateField()
135-
136135
latitude = models.FloatField(null=True, blank=True)
137136
longitude = models.FloatField(null=True, blank=True)
138137

139-
140138
def clean(self):
141139
super().clean()
142140
if self.start_date and self.end_date and self.start_date >= self.end_date:
143141
raise ValidationError({"end_date": "End date must be after start date"})
144142

145143
def _calculate_approximate_location(self, latitude, longitude):
146-
147-
148144
if latitude is None or longitude is None:
149145
return None, None
150146

@@ -165,20 +161,12 @@ def _calculate_approximate_location(self, latitude, longitude):
165161
return approx_lat, approx_lon
166162

167163
@property
168-
def approximate_latitude(self):
169-
if self.latitude and self.longitude:
170-
approximate_latitude, _ = self._calculate_approximate_location(
171-
self.latitude, self.longitude)
172-
return approximate_latitude
173-
return None
174-
175-
@property
176-
def approximate_longitude(self):
177-
if self.latitude and self.longitude:
178-
_, approximate_lon = self._calculate_approximate_location(
164+
def approximate_location(self):
165+
if self.latitude is not None and self.longitude is not None:
166+
approximate_location = self._calculate_approximate_location(
179167
self.latitude, self.longitude)
180-
return approximate_lon
181-
return None
168+
return approximate_location
169+
return None, None
182170

183171
def save(self, *args, **kwargs):
184172
self.full_clean()

backend/market/serializers.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ class Meta:
108108
"latitude", "longitude"]
109109

110110
def get_latitude(self, obj):
111-
if obj.approximate_latitude:
112-
return float(obj.approximate_latitude)
111+
if obj.approximate_location is not None:
112+
return float(obj.approximate_location[0])
113113
return None
114114

115115
def get_longitude(self, obj):
116-
if obj.approximate_longitude:
117-
return float(obj.approximate_longitude)
116+
if obj.approximate_location is not None:
117+
return float(obj.approximate_location[1])
118118
return None
119119

120120
# Unified serializer for all listing types (Items and Sublets); used for CRUD operations
@@ -340,14 +340,16 @@ def _update_item(self, instance, additional_data):
340340

341341
def _update_sublet(self, instance, additional_data):
342342
sublet = instance.sublet
343-
sublet_fields = ["street_address", "beds", "baths", "start_date", "end_date"]
344-
for field in sublet_fields:
343+
str_fields = ["street_address", "beds", "baths", "start_date", "end_date"]
344+
float_fields = ["latitude", "longitude"]
345+
for field in str_fields:
345346
if field in additional_data:
346347
setattr(sublet, field, additional_data[field])
347-
if "latitude" in additional_data:
348-
sublet.latitude = float(additional_data["latitude"])
349-
if "longitude" in additional_data:
350-
sublet.longitude = float(additional_data["longitude"])
348+
349+
for field in float_fields:
350+
if field in additional_data:
351+
value = additional_data[field]
352+
setattr(sublet, field, float(value) if value is not None else None)
351353
sublet.full_clean()
352354
sublet.save()
353355

0 commit comments

Comments
 (0)