friendsmap/map/views.py

173 lines
5.1 KiB
Python
Raw Normal View History

2019-03-02 16:17:48 +01:00
from django.contrib import messages
2019-03-02 15:53:27 +01:00
from django.contrib.auth.mixins import LoginRequiredMixin
2019-03-02 16:17:48 +01:00
from django.urls import reverse_lazy
from django.views import generic
2019-03-02 15:53:27 +01:00
2019-03-02 12:47:44 +01:00
from map import models
from map.forms import LocationForm, LocationSharingForm
2019-03-02 16:17:48 +01:00
from map.mixins import QuickActionsMixin
2019-03-02 12:04:27 +01:00
2019-03-02 12:47:44 +01:00
2019-03-03 18:54:16 +01:00
class MapView(LoginRequiredMixin, QuickActionsMixin, generic.DetailView):
model = models.Friend
context_object_name = 'friend'
2019-03-02 12:47:44 +01:00
template_name = 'map/map.html'
2019-03-02 16:17:48 +01:00
2019-03-03 18:54:16 +01:00
def get_object(self, queryset=None):
return self.request.user
2019-03-02 16:17:48 +01:00
def get_quick_actions(self):
if hasattr(self.request.user, 'location'):
actions = [{
'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:
actions = [{
'url': reverse_lazy('add-location'),
'category': 'primary',
'display': f'Add your location'
}]
2019-03-02 16:17:48 +01:00
actions.append({
'url': reverse_lazy('share-location'),
'category': 'secondary',
'display': 'Share your location'
})
return actions
2019-03-02 16:17:48 +01:00
class EditLocationView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
2019-03-02 16:17:48 +01:00
model = models.FriendLocation
context_object_name = 'location'
template_name = 'map/change_location.html'
success_url = reverse_lazy('map')
2019-03-02 16:17:48 +01:00
fields = [
'latitude',
'longitude',
'start_date',
'end_date',
]
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
2019-03-02 16:17:48 +01:00
def get_object(self, queryset=None):
return self.request.user.location
2019-03-02 16:17:48 +01:00
def get_success_url(self):
messages.success(self.request, 'Your location has been successfully edited')
return super().get_success_url()
2019-03-02 17:39:53 +01:00
class AddLocationView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
2019-03-02 17:39:53 +01:00
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'
}]
2019-03-02 17:39:53 +01:00
def get_initial(self):
initial = super().get_initial()
initial = initial.copy()
initial['friend'] = self.request.user
return initial
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()
2019-03-03 13:42:06 +01:00
class UpdateProfileView(LoginRequiredMixin, QuickActionsMixin, generic.UpdateView):
2019-03-03 13:42:06 +01:00
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',
]
2019-03-03 13:42:06 +01:00
def get_quick_actions(self):
return [{
'url': reverse_lazy('map'),
'category': 'secondary',
'display': 'Back to map'
}]
2019-03-03 13:42:06 +01:00
def get_object(self, queryset=None):
return self.request.user
def get_success_url(self):
messages.success(self.request, 'Your profile has been successfully updated')
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())
2019-03-03 13:42:06 +01:00
class DeleteProfileView(LoginRequiredMixin, generic.DeleteView):
model = models.Friend
context_object_name = 'friend'
template_name = 'map/delete_profile.html'
success_url = reverse_lazy('map')
def get_object(self, queryset=None):
return self.request.user
def get_success_url(self):
messages.success(self.request, 'Your profile has been successfully and permanently deleted')
return super().get_success_url()