Allow deleting location and add some messages
This commit is contained in:
parent
5f9a15dbb4
commit
bcc419c0a7
3 changed files with 55 additions and 25 deletions
13
map/templates/map/delete_location.html
Normal file
13
map/templates/map/delete_location.html
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
{% extends 'map/base.html' %}
|
||||||
|
{% load crispy_forms_filters %}
|
||||||
|
|
||||||
|
{% block h1 %}Delete your location{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<p>Are you sure you want to delete your location ? There is no turning back.</p>
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
<button type="submit" class="btn btn-danger">Yes I'm sure</button>
|
||||||
|
<a href="{% url 'map' %}" class="btn btn-secondary">Cancel</a>
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
|
@ -4,6 +4,7 @@ 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', views.EditLocationView.as_view(), name='change-location'),
|
||||||
path('add-location/<int:pk>', views.AddLocationView.as_view(), name='add-location'),
|
path('add-location', views.AddLocationView.as_view(), name='add-location'),
|
||||||
|
path('delete-location', views.DeleteLocationView.as_view(), name='delete-location'),
|
||||||
]
|
]
|
||||||
|
|
58
map/views.py
58
map/views.py
|
@ -1,6 +1,5 @@
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
from django.shortcuts import redirect
|
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
|
|
||||||
|
@ -15,22 +14,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):
|
||||||
try:
|
if hasattr(self.request.user, 'location'):
|
||||||
return [
|
return [{
|
||||||
{
|
'url': reverse_lazy('change-location'),
|
||||||
'url': reverse_lazy('change-location', kwargs={'pk': self.request.user.location.pk}),
|
'category': 'primary',
|
||||||
'category': 'secondary',
|
|
||||||
'display': f'Change your location'
|
'display': f'Change your location'
|
||||||
},
|
}, {
|
||||||
]
|
'url': reverse_lazy('delete-location'),
|
||||||
except models.FriendLocation.DoesNotExist:
|
'category': 'warning',
|
||||||
return [
|
'display': f'Delete your location'
|
||||||
{
|
}]
|
||||||
'url': reverse_lazy('add-location', kwargs={'pk': self.request.user.pk}),
|
else:
|
||||||
'category': 'secondary',
|
return [{
|
||||||
|
'url': reverse_lazy('add-location'),
|
||||||
|
'category': 'primary',
|
||||||
'display': f'Add your location'
|
'display': f'Add your location'
|
||||||
},
|
}]
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class EditLocationView(LoginRequiredMixin, generic.UpdateView):
|
class EditLocationView(LoginRequiredMixin, generic.UpdateView):
|
||||||
|
@ -47,13 +46,12 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView):
|
||||||
|
|
||||||
success_url = reverse_lazy('map')
|
success_url = reverse_lazy('map')
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
def get_object(self, queryset=None):
|
||||||
obj = self.get_object() # type: models.FriendLocation
|
return self.request.user.location
|
||||||
if obj is None or obj.friend != request.user:
|
|
||||||
messages.warning(request, "This is not your location.")
|
|
||||||
return redirect('map')
|
|
||||||
|
|
||||||
return super().dispatch(request, *args, **kwargs)
|
def get_success_url(self):
|
||||||
|
messages.success(self.request, 'Your location has been successfully edited')
|
||||||
|
return super().get_success_url()
|
||||||
|
|
||||||
|
|
||||||
class AddLocationView(LoginRequiredMixin, generic.CreateView):
|
class AddLocationView(LoginRequiredMixin, generic.CreateView):
|
||||||
|
@ -70,3 +68,21 @@ class AddLocationView(LoginRequiredMixin, generic.CreateView):
|
||||||
|
|
||||||
def get_form(self, form_class=None):
|
def get_form(self, form_class=None):
|
||||||
return LocationForm(self.request, **self.get_form_kwargs())
|
return LocationForm(self.request, **self.get_form_kwargs())
|
||||||
|
|
||||||
|
def get_success_url(self):
|
||||||
|
messages.success(self.request, 'Your location has been successfully added')
|
||||||
|
return super().get_success_url()
|
||||||
|
|
||||||
|
|
||||||
|
class DeleteLocationView(LoginRequiredMixin, generic.DeleteView):
|
||||||
|
model = models.FriendLocation
|
||||||
|
context_object_name = 'location'
|
||||||
|
template_name = 'map/delete_location.html'
|
||||||
|
success_url = reverse_lazy('map')
|
||||||
|
|
||||||
|
def get_object(self, queryset=None):
|
||||||
|
return self.request.user.location
|
||||||
|
|
||||||
|
def get_success_url(self):
|
||||||
|
messages.success(self.request, 'Your location has been successfully deleted')
|
||||||
|
return super().get_success_url()
|
||||||
|
|
Loading…
Reference in a new issue