From bcc419c0a79459f3dd693ff2a3a4d408cf101f4c Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 2 Mar 2019 17:48:50 +0100 Subject: [PATCH] Allow deleting location and add some messages --- map/templates/map/delete_location.html | 13 ++++++ map/urls.py | 5 ++- map/views.py | 62 ++++++++++++++++---------- 3 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 map/templates/map/delete_location.html diff --git a/map/templates/map/delete_location.html b/map/templates/map/delete_location.html new file mode 100644 index 0000000..ef9b15d --- /dev/null +++ b/map/templates/map/delete_location.html @@ -0,0 +1,13 @@ +{% extends 'map/base.html' %} +{% load crispy_forms_filters %} + +{% block h1 %}Delete your location{% endblock %} + +{% block content %} +

Are you sure you want to delete your location ? There is no turning back.

+
+ {% csrf_token %} + + Cancel +
+{% endblock %} diff --git a/map/urls.py b/map/urls.py index 33afa7f..1ac221d 100644 --- a/map/urls.py +++ b/map/urls.py @@ -4,6 +4,7 @@ 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'), + path('change-location', views.EditLocationView.as_view(), name='change-location'), + path('add-location', views.AddLocationView.as_view(), name='add-location'), + path('delete-location', views.DeleteLocationView.as_view(), name='delete-location'), ] diff --git a/map/views.py b/map/views.py index 6718a43..1d5c4b0 100644 --- a/map/views.py +++ b/map/views.py @@ -1,6 +1,5 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin -from django.shortcuts import redirect from django.urls import reverse_lazy from django.views import generic @@ -15,22 +14,22 @@ class MapView(LoginRequiredMixin, QuickActionsMixin, generic.ListView): template_name = 'map/map.html' def get_quick_actions(self): - 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' - }, - ] + if hasattr(self.request.user, 'location'): + return [{ + 'url': reverse_lazy('change-location'), + 'category': 'primary', + 'display': f'Change your location' + }, { + 'url': reverse_lazy('delete-location'), + 'category': 'warning', + 'display': f'Delete your location' + }] + else: + return [{ + 'url': reverse_lazy('add-location'), + 'category': 'primary', + 'display': f'Add your location' + }] class EditLocationView(LoginRequiredMixin, generic.UpdateView): @@ -47,13 +46,12 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView): success_url = reverse_lazy('map') - def dispatch(self, request, *args, **kwargs): - obj = self.get_object() # type: models.FriendLocation - if obj is None or obj.friend != request.user: - messages.warning(request, "This is not your location.") - return redirect('map') + def get_object(self, queryset=None): + return self.request.user.location - 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): @@ -70,3 +68,21 @@ class AddLocationView(LoginRequiredMixin, generic.CreateView): def get_form(self, form_class=None): 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()