mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-19 12:59:36 +02:00
Add geocoding functionality and enhance Adventure model with location fields
This commit is contained in:
parent
14e71626f6
commit
d52e302e9b
8 changed files with 141 additions and 65 deletions
|
@ -9,6 +9,8 @@ from django.contrib.auth import get_user_model
|
|||
from django.contrib.postgres.fields import ArrayField
|
||||
from django.forms import ValidationError
|
||||
from django_resized import ResizedImageField
|
||||
from worldtravel.models import City, Country, Region
|
||||
from adventures.geocoding import reverse_geocode
|
||||
|
||||
def validate_file_extension(value):
|
||||
import os
|
||||
|
@ -525,9 +527,16 @@ class Adventure(models.Model):
|
|||
rating = models.FloatField(blank=True, null=True)
|
||||
link = models.URLField(blank=True, null=True, max_length=2083)
|
||||
is_public = models.BooleanField(default=False)
|
||||
|
||||
longitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
latitude = models.DecimalField(max_digits=9, decimal_places=6, null=True, blank=True)
|
||||
|
||||
city = models.ForeignKey(City, on_delete=models.SET_NULL, blank=True, null=True)
|
||||
region = models.ForeignKey(Region, on_delete=models.SET_NULL, blank=True, null=True)
|
||||
country = models.ForeignKey(Country, on_delete=models.SET_NULL, blank=True, null=True)
|
||||
|
||||
collection = models.ForeignKey('Collection', on_delete=models.CASCADE, blank=True, null=True)
|
||||
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
@ -567,6 +576,22 @@ class Adventure(models.Model):
|
|||
}
|
||||
)
|
||||
self.category = category
|
||||
|
||||
if self.latitude and self.longitude:
|
||||
reverse_geocode_result = reverse_geocode(self.latitude, self.longitude, self.user_id)
|
||||
print(reverse_geocode_result)
|
||||
if 'region_id' in reverse_geocode_result:
|
||||
region = Region.objects.filter(id=reverse_geocode_result['region_id']).first()
|
||||
if region:
|
||||
self.region = region
|
||||
if 'city_id' in reverse_geocode_result:
|
||||
city = City.objects.filter(id=reverse_geocode_result['city_id']).first()
|
||||
if city:
|
||||
self.city = city
|
||||
if 'country_id' in reverse_geocode_result:
|
||||
country = Country.objects.filter(country_code=reverse_geocode_result['country_id']).first()
|
||||
if country:
|
||||
self.country = country
|
||||
|
||||
return super().save(force_insert, force_update, using, update_fields)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue