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 = [
|
||||
path('', views.MapView.as_view(), name='map'),
|
||||
path('change-location/<int:pk>', views.EditLocationView.as_view(), name='change-location'),
|
||||
path('add-location/<int:pk>', 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'),
|
||||
]
|
||||
|
|
58
map/views.py
58
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',
|
||||
if hasattr(self.request.user, 'location'):
|
||||
return [{
|
||||
'url': reverse_lazy('change-location'),
|
||||
'category': 'primary',
|
||||
'display': f'Change your location'
|
||||
},
|
||||
]
|
||||
except models.FriendLocation.DoesNotExist:
|
||||
return [
|
||||
{
|
||||
'url': reverse_lazy('add-location', kwargs={'pk': self.request.user.pk}),
|
||||
'category': 'secondary',
|
||||
}, {
|
||||
'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()
|
||||
|
|
Loading…
Reference in a new issue