Create a custom view for location sharing management

This commit is contained in:
Gabriel Augendre 2019-03-03 20:09:15 +01:00
parent 3d83dbc91b
commit 50219adbbb
7 changed files with 97 additions and 29 deletions

View file

@ -23,14 +23,10 @@ class LocationForm(forms.ModelForm):
return self.request.user
class ProfileForm(forms.ModelForm):
class LocationSharingForm(forms.ModelForm):
class Meta:
model = Friend
fields = [
'username',
'first_name',
'last_name',
'email',
'shares_location_to',
]
@ -38,3 +34,4 @@ class ProfileForm(forms.ModelForm):
super().__init__(*args, **kwargs)
self.request = request
self.fields['shares_location_to'].queryset = Friend.objects.exclude(pk=request.user.pk)
self.fields['shares_location_to'].label = 'Share location to'

View file

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

View file

@ -1,10 +1,10 @@
{% extends 'map/base.html' %}
{% extends 'map/base_change.html' %}
{% load crispy_forms_filters %}
{% block title %}Change your location{% endblock %}
{% block h1 %}Change your location{% endblock %}
{% block content %}
{% block notice %}
<div class="alert alert-info">
<h4 class="alert-heading">Notice</h4>
<div>
@ -20,9 +20,4 @@
you.
</div>
</div>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% endblock %}

View file

@ -1,10 +1,10 @@
{% extends 'map/base.html' %}
{% extends 'map/base_change.html' %}
{% load crispy_forms_filters %}
{% block title %}Change your profile{% endblock %}
{% block h1 %}Change your profile{% endblock %}
{% block content %}
{% block notice %}
<div class="alert alert-info">
<h4 class="alert-heading">Notice</h4>
<div>
@ -23,11 +23,9 @@
your data if you change your mind.
</div>
</div>
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button type="submit" class="btn btn-primary">Save</button>
{% endblock %}
{% block form-buttons %}
<a href="{% url 'password_change' %}" class="btn btn-secondary">Change your password</a>
<a href="{% url 'delete-profile' %}" class="btn btn-warning">Permanently delete your profile</a>
</form>
{% endblock %}

View file

@ -0,0 +1,15 @@
{% extends 'map/base_change.html' %}
{% load crispy_forms_filters %}
{% block title %}Share your location{% endblock %}
{% block h1 %}Share your location{% endblock %}
{% block notice %}
<div class="alert alert-info">
<h4 class="alert-heading">Notice</h4>
<div>
Select here who you want to share your location to.
Only selected users will be able to see your marker on the map.
</div>
</div>
{% endblock %}

View file

@ -7,6 +7,7 @@ urlpatterns = [
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'),
path('share-location', views.LocationSharingView.as_view(), name='share-location'),
path('accounts/profile', views.UpdateProfileView.as_view(), name='change-profile'),
path('accounts/profile/delete', views.DeleteProfileView.as_view(), name='delete-profile'),
]

View file

@ -4,7 +4,7 @@ from django.urls import reverse_lazy
from django.views import generic
from map import models
from map.forms import LocationForm, ProfileForm
from map.forms import LocationForm, LocationSharingForm
from map.mixins import QuickActionsMixin
@ -34,14 +34,20 @@ class MapView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView):
'display': f'Add your location'
}]
actions.append({
'url': reverse_lazy('share-location'),
'category': 'secondary',
'display': 'Share your location'
})
return actions
class EditLocationView(LoginRequiredMixin, generic.UpdateView):
class EditLocationView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = models.FriendLocation
context_object_name = 'location'
template_name = 'map/change_location.html'
success_url = reverse_lazy('map')
fields = [
'latitude',
'longitude',
@ -49,7 +55,12 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView):
'end_date',
]
success_url = reverse_lazy('map')
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
def get_object(self, queryset=None):
return self.request.user.location
@ -59,12 +70,19 @@ class EditLocationView(LoginRequiredMixin, generic.UpdateView):
return super().get_success_url()
class AddLocationView(LoginRequiredMixin, generic.CreateView):
class AddLocationView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
model = models.FriendLocation
context_object_name = 'location'
template_name = 'map/change_location.html'
success_url = reverse_lazy('map')
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
def get_initial(self):
initial = super().get_initial()
initial = initial.copy()
@ -93,14 +111,24 @@ class DeleteLocationView(LoginRequiredMixin, generic.DeleteView):
return super().get_success_url()
class UpdateProfileView(LoginRequiredMixin, generic.UpdateView):
class UpdateProfileView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = models.Friend
context_object_name = 'friend'
template_name = 'map/change_profile.html'
success_url = reverse_lazy('map')
fields = [
'username',
'first_name',
'last_name',
'email',
]
def get_form(self, form_class=None):
return ProfileForm(self.request, **self.get_form_kwargs())
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
def get_object(self, queryset=None):
return self.request.user
@ -110,6 +138,26 @@ class UpdateProfileView(LoginRequiredMixin, generic.UpdateView):
return super().get_success_url()
class LocationSharingView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
model = models.Friend
context_object_name = 'friend'
template_name = 'map/share_location.html'
success_url = reverse_lazy('map')
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
def get_object(self, queryset=None):
return self.request.user
def get_form(self, form_class=None):
return LocationSharingForm(self.request, **self.get_form_kwargs())
class DeleteProfileView(LoginRequiredMixin, generic.DeleteView):
model = models.Friend
context_object_name = 'friend'