ccdb-api/ccdb/users/views.py
Matthew Dillon 7ba937ef66 INITIAL
2016-01-19 15:10:09 -07:00

51 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.core.urlresolvers import reverse
from django.views.generic import DetailView, ListView, RedirectView, UpdateView
from django.shortcuts import redirect
from braces.views import LoginRequiredMixin
from .models import User
class UserDetailView(LoginRequiredMixin, DetailView):
model = User
# These next two lines tell the view to index lookups by username
slug_field = "username"
slug_url_kwarg = "username"
class UserRedirectView(LoginRequiredMixin, RedirectView):
permanent = False
def get_redirect_url(self):
return reverse("users:detail",
kwargs={"username": self.request.user.username})
class UserUpdateView(LoginRequiredMixin, UpdateView):
fields = ['name', 'timezone']
model = User
def dispatch(self, request, *args, **kwargs):
if request.user.username != kwargs.pop("username", None):
return redirect(reverse("users:detail",
kwargs={"username": request.user.username}))
return super(UserUpdateView, self).dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse("users:detail",
kwargs={"username": self.request.user.username})
def get_object(self):
# Only get the User record for the user making the request
return User.objects.get(username=self.request.user.username)
class UserListView(LoginRequiredMixin, ListView):
model = User
# These next two lines tell the view to index lookups by username
slug_field = "username"
slug_url_kwarg = "username"