From 5f9a15dbb445e6f1a50458bf8cc9a7d1b49af70c Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 2 Mar 2019 17:39:53 +0100 Subject: [PATCH] Allow adding location when none exist --- map/forms.py | 23 +++++++++++++++++++++++ map/urls.py | 1 + map/views.py | 40 +++++++++++++++++++++++++++++++++------- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 map/forms.py diff --git a/map/forms.py b/map/forms.py new file mode 100644 index 0000000..36eb9e6 --- /dev/null +++ b/map/forms.py @@ -0,0 +1,23 @@ +from django import forms + +from map.models import FriendLocation + + +class LocationForm(forms.ModelForm): + class Meta: + model = FriendLocation + fields = [ + 'latitude', + 'longitude', + 'start_date', + 'end_date', + 'friend', + ] + widgets = {'friend': forms.HiddenInput()} + + def __init__(self, request, *args, **kwargs): + super().__init__(*args, **kwargs) + self.request = request + + def clean_friend(self): + return self.request.user diff --git a/map/urls.py b/map/urls.py index 1b91adf..33afa7f 100644 --- a/map/urls.py +++ b/map/urls.py @@ -5,4 +5,5 @@ from map import views urlpatterns = [ path('', views.MapView.as_view(), name='map'), path('change-location/', views.EditLocationView.as_view(), name='change-location'), + path('add-location/', views.AddLocationView.as_view(), name='add-location'), ] diff --git a/map/views.py b/map/views.py index 466654a..6718a43 100644 --- a/map/views.py +++ b/map/views.py @@ -5,6 +5,7 @@ from django.urls import reverse_lazy from django.views import generic from map import models +from map.forms import LocationForm from map.mixins import QuickActionsMixin @@ -14,13 +15,22 @@ class MapView(LoginRequiredMixin, QuickActionsMixin, generic.ListView): template_name = 'map/map.html' def get_quick_actions(self): - return [ - { - 'url': reverse_lazy('change-location', kwargs={'pk': self.request.user.location.pk}), - 'category': 'secondary', - 'display': f'Change your location' - }, - ] + try: + return [ + { + 'url': reverse_lazy('change-location', kwargs={'pk': self.request.user.location.pk}), + 'category': 'secondary', + 'display': f'Change your location' + }, + ] + except models.FriendLocation.DoesNotExist: + return [ + { + 'url': reverse_lazy('add-location', kwargs={'pk': self.request.user.pk}), + 'category': 'secondary', + 'display': f'Add your location' + }, + ] class EditLocationView(LoginRequiredMixin, generic.UpdateView): @@ -44,3 +54,19 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView): return redirect('map') return super().dispatch(request, *args, **kwargs) + + +class AddLocationView(LoginRequiredMixin, generic.CreateView): + model = models.FriendLocation + context_object_name = 'location' + template_name = 'map/change_location.html' + success_url = reverse_lazy('map') + + def get_initial(self): + initial = super().get_initial() + initial = initial.copy() + initial['friend'] = self.request.user + return initial + + def get_form(self, form_class=None): + return LocationForm(self.request, **self.get_form_kwargs())