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
|
2019-03-02 17:39:53 +01:00
|
|
|
from map.forms import LocationForm
|
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):
|
2019-03-02 17:48:50 +01:00
|
|
|
if hasattr(self.request.user, 'location'):
|
2019-03-03 18:45:47 +01:00
|
|
|
actions = [{
|
2019-03-02 17:48:50 +01:00
|
|
|
'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:
|
2019-03-03 18:45:47 +01:00
|
|
|
actions = [{
|
2019-03-02 17:48:50 +01:00
|
|
|
'url': reverse_lazy('add-location'),
|
|
|
|
'category': 'primary',
|
|
|
|
'display': f'Add your location'
|
|
|
|
}]
|
2019-03-02 16:17:48 +01:00
|
|
|
|
2019-03-03 18:45:47 +01:00
|
|
|
actions.append({
|
|
|
|
'url': reverse_lazy('manage-groups'),
|
|
|
|
'category': 'secondary',
|
|
|
|
'display': f'Manage your groups'
|
|
|
|
})
|
|
|
|
|
|
|
|
return actions
|
|
|
|
|
2019-03-02 16:17:48 +01:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2019-03-02 17:48:50 +01:00
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return self.request.user.location
|
2019-03-02 16:17:48 +01:00
|
|
|
|
2019-03-02 17:48:50 +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, generic.CreateView):
|
|
|
|
model = models.FriendLocation
|
|
|
|
context_object_name = 'location'
|
|
|
|
template_name = 'map/change_location.html'
|
|
|
|
success_url = reverse_lazy('map')
|
|
|
|
|
|
|
|
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())
|
2019-03-02 17:48:50 +01:00
|
|
|
|
|
|
|
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, 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_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 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()
|
2019-03-03 18:45:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ManageGroupsView(LoginRequiredMixin, QuickActionsMixin, generic.ListView):
|
|
|
|
model = models.LocationSharingGroup
|
|
|
|
context_object_name = 'groups'
|
|
|
|
template_name = 'map/list_groups.html'
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return self.request.user.location_sharing_groups.all()
|
|
|
|
|
|
|
|
def get_quick_actions(self):
|
|
|
|
return [{
|
|
|
|
'url': reverse_lazy('add-group'),
|
|
|
|
'category': 'primary',
|
|
|
|
'display': 'Create a group'
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
|
|
class CreateGroupView(LoginRequiredMixin, QuickActionsMixin, generic.CreateView):
|
|
|
|
model = models.LocationSharingGroup
|
|
|
|
context_object_name = 'group'
|
|
|
|
template_name = 'map/change_group.html'
|
|
|
|
success_url = reverse_lazy('manage-groups')
|
|
|
|
|
|
|
|
fields = [
|
|
|
|
'name'
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.friends.add(self.request.user)
|
|
|
|
messages.success(self.request, 'The group has been successfully created')
|
|
|
|
return super().get_success_url()
|
|
|
|
|
|
|
|
|
|
|
|
class LeaveGroupView(LoginRequiredMixin, generic.UpdateView):
|
|
|
|
model = models.LocationSharingGroup
|
|
|
|
context_object_name = 'group'
|
|
|
|
template_name = 'map/leave_group.html'
|
|
|
|
fields = []
|
|
|
|
success_url = reverse_lazy('manage-groups')
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.friends.remove(self.request.user)
|
|
|
|
if self.object.friends.count() == 0:
|
|
|
|
self.object.delete()
|
|
|
|
messages.success(self.request, 'You successfully left the group')
|
|
|
|
return super().get_success_url()
|