Allow editing location

This commit is contained in:
Gabriel Augendre 2019-03-02 16:17:48 +01:00
parent e50bf025f9
commit 2cdf6f1e2b
6 changed files with 75 additions and 9 deletions

8
map/mixins.py Normal file
View file

@ -0,0 +1,8 @@
class QuickActionsMixin:
def get_quick_actions(self):
return []
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['quick_actions'] = self.get_quick_actions()
return context

View file

@ -2,3 +2,7 @@
height: 70vh; height: 70vh;
margin-top: 1rem; margin-top: 1rem;
} }
.messages-container {
margin-top: 1em;
}

View file

@ -25,6 +25,8 @@
{% include 'map/navbar.html' %} {% include 'map/navbar.html' %}
<div class="container"> <div class="container">
{% if messages %}
<div class="messages-container">
{% for message in messages %} {% for message in messages %}
<div class="alert alert-dismissible alert-{{ message.tags }} fade show" role="alert"> <div class="alert alert-dismissible alert-{{ message.tags }} fade show" role="alert">
{{ message|safe }} {{ message|safe }}
@ -33,6 +35,8 @@
</button> </button>
</div> </div>
{% endfor %} {% endfor %}
</div>
{% endif %}
<div class="row"> <div class="row">
<div class="col-12"> <div class="col-12">

View file

@ -0,0 +1,12 @@
{% extends 'map/base.html' %}
{% load crispy_forms_filters %}
{% block h1 %}Change your location{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% endblock %}

View file

@ -1,6 +1,8 @@
from django.urls import path from django.urls import path
from map import views 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'),
] ]

View file

@ -1,10 +1,46 @@
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView from django.shortcuts import redirect
from django.urls import reverse_lazy
from django.views import generic
from map import models from map import models
from map.mixins import QuickActionsMixin
class MapView(LoginRequiredMixin, ListView): class MapView(LoginRequiredMixin, QuickActionsMixin, generic.ListView):
model = models.FriendLocation model = models.FriendLocation
context_object_name = 'locations' context_object_name = 'locations'
template_name = 'map/map.html' 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'
},
]
class EditLocationView(LoginRequiredMixin, generic.UpdateView):
model = models.FriendLocation
context_object_name = 'location'
template_name = 'map/change_location.html'
fields = [
'latitude',
'longitude',
'start_date',
'end_date',
]
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')
return super().dispatch(request, *args, **kwargs)