Allow editing location
This commit is contained in:
parent
e50bf025f9
commit
2cdf6f1e2b
6 changed files with 75 additions and 9 deletions
8
map/mixins.py
Normal file
8
map/mixins.py
Normal 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
|
|
@ -2,3 +2,7 @@
|
||||||
height: 70vh;
|
height: 70vh;
|
||||||
margin-top: 1rem;
|
margin-top: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.messages-container {
|
||||||
|
margin-top: 1em;
|
||||||
|
}
|
|
@ -25,14 +25,18 @@
|
||||||
{% include 'map/navbar.html' %}
|
{% include 'map/navbar.html' %}
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
{% for message in messages %}
|
{% if messages %}
|
||||||
<div class="alert alert-dismissible alert-{{ message.tags }} fade show" role="alert">
|
<div class="messages-container">
|
||||||
{{ message|safe }}
|
{% for message in messages %}
|
||||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
<div class="alert alert-dismissible alert-{{ message.tags }} fade show" role="alert">
|
||||||
<span aria-hidden="true">×</span>
|
{{ message|safe }}
|
||||||
</button>
|
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
|
||||||
|
<span aria-hidden="true">×</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
|
|
12
map/templates/map/change_location.html
Normal file
12
map/templates/map/change_location.html
Normal 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 %}
|
|
@ -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'),
|
||||||
]
|
]
|
||||||
|
|
40
map/views.py
40
map/views.py
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue