No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

views.py 1.8 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. reverse_lazy = lambda name=None, *args: lazy(reverse, str)(name, args=args)
  14. class CreateUser(CreateView):
  15. template_name = 'users/register.html'
  16. model = User
  17. form_class = UserCreationForm
  18. success_url = reverse_lazy('core:recent-pins')
  19. def get(self, request, *args, **kwargs):
  20. if not settings.ALLOW_NEW_REGISTRATIONS:
  21. messages.error(request, "The admin of this service is not allowing new registrations.")
  22. return HttpResponseRedirect(reverse('core:recent-pins'))
  23. return super(CreateUser, self).get(request, *args, **kwargs)
  24. def form_valid(self, form):
  25. redirect = super(CreateUser, self).form_valid(form)
  26. permissions = Permission.objects.filter(codename__in=['add_pin', 'add_image'])
  27. user = authenticate(username=form.cleaned_data['username'],
  28. password=form.cleaned_data['password'])
  29. user.user_permissions = permissions
  30. login(self.request, user)
  31. return redirect
  32. @login_required
  33. def logout_user(request):
  34. logout(request)
  35. messages.success(request, 'You have successfully logged out.')
  36. return HttpResponseRedirect(reverse('core:recent-pins'))
  37. def private(request):
  38. return TemplateResponse(request, 'users/private.html', None)