You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 1.8 KiB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from django.conf import settings
  2. from django.contrib import messages
  3. from django.contrib.auth import authenticate, login, logout
  4. from django.contrib.auth.decorators import login_required
  5. from django.contrib.auth.models import Permission
  6. from django.core.urlresolvers import reverse
  7. from django.http import HttpResponseRedirect
  8. from django.template.response import TemplateResponse
  9. from django.utils.functional import lazy
  10. from django.views.generic import CreateView
  11. from .forms import UserCreationForm
  12. from users.models import User
  13. def reverse_lazy(name=None, *args):
  14. return lazy(reverse, str)(name, args=args)
  15. class CreateUser(CreateView):
  16. template_name = 'users/register.html'
  17. model = User
  18. form_class = UserCreationForm
  19. success_url = reverse_lazy('core:recent-pins')
  20. def get(self, request, *args, **kwargs):
  21. if not settings.ALLOW_NEW_REGISTRATIONS:
  22. messages.error(request, "The admin of this service is not allowing new registrations.")
  23. return HttpResponseRedirect(reverse('core:recent-pins'))
  24. return super(CreateUser, self).get(request, *args, **kwargs)
  25. def form_valid(self, form):
  26. redirect = super(CreateUser, self).form_valid(form)
  27. permissions = Permission.objects.filter(codename__in=['add_pin', 'add_image'])
  28. user = authenticate(username=form.cleaned_data['username'],
  29. password=form.cleaned_data['password'])
  30. user.user_permissions = permissions
  31. login(self.request, user)
  32. return redirect
  33. @login_required
  34. def logout_user(request):
  35. logout(request)
  36. messages.success(request, 'You have successfully logged out.')
  37. return HttpResponseRedirect(reverse('core:recent-pins'))
  38. def private(request):
  39. return TemplateResponse(request, 'users/private.html', None)