2019-03-04 18:44:56 +01:00
|
|
|
from django.conf import settings
|
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-04 18:44:56 +01:00
|
|
|
from map.forms import LocationForm, LocationSharingForm, FriendCreationForm
|
2019-03-02 16:17:48 +01:00
|
|
|
from map.mixins import QuickActionsMixin
|
2019-03-04 18:44:56 +01:00
|
|
|
from map.models import Friend
|
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',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': f'Change your location',
|
2019-03-05 07:49:01 +01:00
|
|
|
'icon': 'fas fa-map-marker-alt',
|
2019-03-02 17:48:50 +01:00
|
|
|
}, {
|
|
|
|
'url': reverse_lazy('delete-location'),
|
|
|
|
'category': 'warning',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': f'Delete your location',
|
|
|
|
'icon': 'fas fa-eraser',
|
2019-03-02 17:48:50 +01:00
|
|
|
}]
|
|
|
|
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',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': f'Add your location',
|
2019-03-05 07:49:01 +01:00
|
|
|
'icon': 'fas fa-map-marker-alt',
|
2019-03-02 17:48:50 +01:00
|
|
|
}]
|
2019-03-02 16:17:48 +01:00
|
|
|
|
2019-03-03 20:09:15 +01:00
|
|
|
actions.append({
|
|
|
|
'url': reverse_lazy('share-location'),
|
|
|
|
'category': 'secondary',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': 'Share your location',
|
|
|
|
'icon': 'fas fa-share-alt'
|
2019-03-03 20:09:15 +01:00
|
|
|
})
|
|
|
|
|
2019-03-03 18:45:47 +01:00
|
|
|
return actions
|
|
|
|
|
2019-03-02 16:17:48 +01:00
|
|
|
|
2019-03-03 20:09:15 +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'
|
2019-03-03 20:09:15 +01:00
|
|
|
success_url = reverse_lazy('map')
|
2019-03-02 16:17:48 +01:00
|
|
|
fields = [
|
|
|
|
'latitude',
|
|
|
|
'longitude',
|
|
|
|
'start_date',
|
|
|
|
'end_date',
|
|
|
|
]
|
|
|
|
|
2019-03-03 20:09:15 +01:00
|
|
|
def get_quick_actions(self):
|
|
|
|
return [{
|
|
|
|
'url': reverse_lazy('map'),
|
|
|
|
'category': 'secondary',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': 'Back to map',
|
|
|
|
'icon': 'fas fa-chevron-circle-left',
|
2019-03-03 20:09:15 +01:00
|
|
|
}]
|
2019-03-02 16:17:48 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-03-03 20:09:15 +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')
|
|
|
|
|
2019-03-03 20:09:15 +01:00
|
|
|
def get_quick_actions(self):
|
|
|
|
return [{
|
|
|
|
'url': reverse_lazy('map'),
|
|
|
|
'category': 'secondary',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': 'Back to map',
|
|
|
|
'icon': 'fas fa-chevron-circle-left',
|
2019-03-03 20:09:15 +01:00
|
|
|
}]
|
|
|
|
|
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())
|
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
|
|
|
|
|
|
|
|
2019-03-04 18:44:56 +01:00
|
|
|
class SignupView(generic.CreateView):
|
|
|
|
model = models.Friend
|
|
|
|
template_name = 'registration/signup.html'
|
|
|
|
success_url = reverse_lazy('login')
|
|
|
|
form_class = FriendCreationForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['registration_disallowed'] = Friend.objects.count() >= settings.USER_THRESHOLD
|
|
|
|
return context
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
if Friend.objects.count() >= settings.USER_THRESHOLD:
|
|
|
|
raise Exception('No more users allowed')
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
self.object.is_active = False
|
|
|
|
self.object.save()
|
|
|
|
messages.success(self.request, 'Your profile has been successfully created, wait for approval.')
|
|
|
|
return super().get_success_url()
|
|
|
|
|
|
|
|
|
2019-03-03 20:09:15 +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')
|
2019-03-03 20:09:15 +01:00
|
|
|
fields = [
|
|
|
|
'username',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
]
|
2019-03-03 13:42:06 +01:00
|
|
|
|
2019-03-03 20:09:15 +01:00
|
|
|
def get_quick_actions(self):
|
|
|
|
return [{
|
|
|
|
'url': reverse_lazy('map'),
|
|
|
|
'category': 'secondary',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': 'Back to map',
|
|
|
|
'icon': 'fas fa-chevron-circle-left',
|
2019-03-03 20:09:15 +01:00
|
|
|
}]
|
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()
|
|
|
|
|
|
|
|
|
2019-03-03 20:09:15 +01:00
|
|
|
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',
|
2019-03-05 07:32:27 +01:00
|
|
|
'display': 'Back to map',
|
|
|
|
'icon': 'fas fa-chevron-circle-left',
|
2019-03-03 20:09:15 +01:00
|
|
|
}]
|
|
|
|
|
|
|
|
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()
|