2018-10-01 19:19:21 +00:00
|
|
|
try:
|
|
|
|
from django.urls import reverse
|
|
|
|
except ImportError:
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
2019-03-07 09:15:18 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
2017-10-31 14:57:26 +00:00
|
|
|
from django.views.generic import DetailView, ListView, RedirectView, UpdateView
|
2017-10-08 20:21:51 +00:00
|
|
|
|
2017-10-31 14:57:26 +00:00
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
2017-10-08 20:21:51 +00:00
|
|
|
|
2017-10-31 14:57:26 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2019-03-04 12:39:27 +00:00
|
|
|
from apps.map.models import CaseStudy
|
|
|
|
|
2017-10-31 14:57:26 +00:00
|
|
|
|
|
|
|
class UserDetailView(LoginRequiredMixin, DetailView):
|
|
|
|
model = get_user_model()
|
|
|
|
# These next two lines tell the view to index lookups by username
|
|
|
|
slug_field = 'username'
|
|
|
|
slug_url_kwarg = 'username'
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return get_user_model().objects.get(username=self.request.user.username)
|
|
|
|
|
2019-03-04 12:39:27 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
# Get case studies for this user
|
|
|
|
case_studies = CaseStudy.objects.filter(author=self.request.user.id)
|
|
|
|
context['case_studies'] = case_studies
|
|
|
|
|
2019-03-07 09:15:18 +00:00
|
|
|
context['enable_edit'] = settings.FF_CAN_EDIT
|
|
|
|
|
2019-03-04 12:39:27 +00:00
|
|
|
return context
|
|
|
|
|
2017-10-31 14:57:26 +00:00
|
|
|
|
|
|
|
class UserRedirectView(LoginRequiredMixin, RedirectView):
|
|
|
|
permanent = False
|
|
|
|
|
|
|
|
def get_redirect_url(self):
|
|
|
|
return reverse('profile:detail')
|
|
|
|
|
|
|
|
|
|
|
|
class UserUpdateView(LoginRequiredMixin, UpdateView):
|
|
|
|
|
|
|
|
fields = ['first_name', 'last_name']
|
|
|
|
|
|
|
|
# we already imported User in the view code above, remember?
|
|
|
|
model = get_user_model()
|
|
|
|
|
|
|
|
# send the user back to their own page after a successful update
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse('profile:detail')
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
# Only get the User record for the user making the request
|
|
|
|
return get_user_model().objects.get(username=self.request.user.username)
|
|
|
|
|
|
|
|
|
|
|
|
class UserListView(LoginRequiredMixin, ListView):
|
|
|
|
model = get_user_model()
|
|
|
|
# These next two lines tell the view to index lookups by username
|
|
|
|
slug_field = 'username'
|
|
|
|
slug_url_kwarg = 'username'
|