initial geodjango implementation

This commit is contained in:
pacnpal
2024-11-05 04:10:47 +00:00
parent c66fc2b6e3
commit 491be57ab2
22 changed files with 768 additions and 229 deletions

View File

@@ -1,4 +1,4 @@
from decimal import Decimal, ROUND_DOWN
from decimal import Decimal, ROUND_DOWN, InvalidOperation
from django.views.generic import DetailView, ListView, CreateView, UpdateView
from django.shortcuts import get_object_or_404, render
from django.core.serializers.json import DjangoJSONEncoder
@@ -16,6 +16,7 @@ from core.views import SlugRedirectMixin
from moderation.mixins import EditSubmissionMixin, PhotoSubmissionMixin, HistoryMixin
from moderation.models import EditSubmission
from media.models import Photo
from location.models import Location
def location_search(request):
@@ -101,7 +102,7 @@ class ParkListView(ListView):
context_object_name = "parks"
def get_queryset(self):
queryset = Park.objects.select_related("owner").prefetch_related("photos")
queryset = Park.objects.select_related("owner").prefetch_related("photos", "location")
search = self.request.GET.get("search", "").strip()
country = self.request.GET.get("country", "").strip()
@@ -111,25 +112,25 @@ class ParkListView(ListView):
if search:
queryset = queryset.filter(
Q(name__icontains=search)
| Q(city__icontains=search)
| Q(state__icontains=search)
| Q(country__icontains=search)
Q(name__icontains=search) |
Q(location__city__icontains=search) |
Q(location__state__icontains=search) |
Q(location__country__icontains=search)
)
if country:
queryset = queryset.filter(country__icontains=country)
queryset = queryset.filter(location__country__icontains=country)
if region:
queryset = queryset.filter(state__icontains=region)
queryset = queryset.filter(location__state__icontains=region)
if city:
queryset = queryset.filter(city__icontains=city)
queryset = queryset.filter(location__city__icontains=city)
if statuses:
queryset = queryset.filter(status__in=statuses)
return queryset
return queryset.distinct()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
@@ -173,7 +174,8 @@ class ParkDetailView(
'rides',
'rides__manufacturer',
'photos',
'areas'
'areas',
'location'
)
def get_context_data(self, **kwargs):
@@ -242,6 +244,22 @@ class ParkCreateView(LoginRequiredMixin, CreateView):
submission.handled_by = self.request.user
submission.save()
# Create Location record
if form.cleaned_data.get("latitude") and form.cleaned_data.get("longitude"):
Location.objects.create(
content_type=ContentType.objects.get_for_model(Park),
object_id=self.object.id,
name=self.object.name,
location_type='park',
latitude=form.cleaned_data["latitude"],
longitude=form.cleaned_data["longitude"],
street_address=form.cleaned_data.get("street_address", ""),
city=form.cleaned_data.get("city", ""),
state=form.cleaned_data.get("state", ""),
country=form.cleaned_data.get("country", ""),
postal_code=form.cleaned_data.get("postal_code", "")
)
# Handle photo uploads
photos = self.request.FILES.getlist("photos")
for photo_file in photos:
@@ -349,6 +367,31 @@ class ParkUpdateView(LoginRequiredMixin, UpdateView):
submission.handled_by = self.request.user
submission.save()
# Update or create Location record
location_data = {
'name': self.object.name,
'location_type': 'park',
'latitude': form.cleaned_data.get("latitude"),
'longitude': form.cleaned_data.get("longitude"),
'street_address': form.cleaned_data.get("street_address", ""),
'city': form.cleaned_data.get("city", ""),
'state': form.cleaned_data.get("state", ""),
'country': form.cleaned_data.get("country", ""),
'postal_code': form.cleaned_data.get("postal_code", "")
}
if self.object.location.exists():
location = self.object.location.first()
for key, value in location_data.items():
setattr(location, key, value)
location.save()
else:
Location.objects.create(
content_type=ContentType.objects.get_for_model(Park),
object_id=self.object.id,
**location_data
)
# Handle photo uploads
photos = self.request.FILES.getlist("photos")
uploaded_count = 0