Allow adding location when none exist
This commit is contained in:
parent
0a68fcceb7
commit
5f9a15dbb4
3 changed files with 57 additions and 7 deletions
23
map/forms.py
Normal file
23
map/forms.py
Normal file
|
@ -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
|
|
@ -5,4 +5,5 @@ from map import views
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.MapView.as_view(), name='map'),
|
path('', views.MapView.as_view(), name='map'),
|
||||||
path('change-location/<int:pk>', views.EditLocationView.as_view(), name='change-location'),
|
path('change-location/<int:pk>', views.EditLocationView.as_view(), name='change-location'),
|
||||||
|
path('add-location/<int:pk>', views.AddLocationView.as_view(), name='add-location'),
|
||||||
]
|
]
|
||||||
|
|
40
map/views.py
40
map/views.py
|
@ -5,6 +5,7 @@ from django.urls import reverse_lazy
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
||||||
from map import models
|
from map import models
|
||||||
|
from map.forms import LocationForm
|
||||||
from map.mixins import QuickActionsMixin
|
from map.mixins import QuickActionsMixin
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,13 +15,22 @@ class MapView(LoginRequiredMixin, QuickActionsMixin, generic.ListView):
|
||||||
template_name = 'map/map.html'
|
template_name = 'map/map.html'
|
||||||
|
|
||||||
def get_quick_actions(self):
|
def get_quick_actions(self):
|
||||||
return [
|
try:
|
||||||
{
|
return [
|
||||||
'url': reverse_lazy('change-location', kwargs={'pk': self.request.user.location.pk}),
|
{
|
||||||
'category': 'secondary',
|
'url': reverse_lazy('change-location', kwargs={'pk': self.request.user.location.pk}),
|
||||||
'display': f'Change your location'
|
'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):
|
class EditLocationView(LoginRequiredMixin, generic.UpdateView):
|
||||||
|
@ -44,3 +54,19 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView):
|
||||||
return redirect('map')
|
return redirect('map')
|
||||||
|
|
||||||
return super().dispatch(request, *args, **kwargs)
|
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())
|
||||||
|
|
Loading…
Reference in a new issue