Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

88 rader
2.8 KiB

  1. import json
  2. from django.conf import settings
  3. from django.contrib import messages
  4. from django.contrib.auth import authenticate, login, logout
  5. from django.contrib.auth.decorators import login_required
  6. from django.contrib.auth.models import Permission
  7. from django.core.urlresolvers import reverse
  8. from django.http import HttpResponseRedirect, HttpResponseBadRequest, HttpResponse
  9. from django.template.response import TemplateResponse
  10. from django.utils.functional import lazy
  11. from django.views.generic import CreateView
  12. from rest_framework.renderers import JSONRenderer
  13. from core.serializers import UserSerializer
  14. from .forms import UserCreationForm
  15. from users.models import User
  16. def reverse_lazy(name=None, *args):
  17. return lazy(reverse, str)(name, args=args)
  18. class CreateUser(CreateView):
  19. template_name = 'users/register.html'
  20. model = User
  21. form_class = UserCreationForm
  22. success_url = reverse_lazy('core:recent-pins')
  23. def get(self, request, *args, **kwargs):
  24. if not settings.ALLOW_NEW_REGISTRATIONS:
  25. messages.error(request, "The admin of this service is not allowing new registrations.")
  26. return HttpResponseRedirect(reverse('core:recent-pins'))
  27. return super(CreateUser, self).get(request, *args, **kwargs)
  28. def form_valid(self, form):
  29. redirect = super(CreateUser, self).form_valid(form)
  30. permissions = Permission.objects.filter(codename__in=['add_pin', 'add_image'])
  31. user = authenticate(username=form.cleaned_data['username'],
  32. password=form.cleaned_data['password'])
  33. user.user_permissions = permissions
  34. login(self.request, user)
  35. return redirect
  36. def login_user(request):
  37. try:
  38. data = json.loads(request.body)
  39. except json.JSONDecodeError:
  40. return HttpResponseBadRequest()
  41. if 'username' not in data:
  42. return HttpResponseBadRequest(
  43. json.dumps({"username": "this field is required"})
  44. )
  45. if 'password' not in data:
  46. return HttpResponseBadRequest(
  47. json.dumps({"password": "this field is required"})
  48. )
  49. user = authenticate(
  50. request,
  51. username=data['username'],
  52. password=data['password']
  53. )
  54. if not user:
  55. return HttpResponseBadRequest(
  56. json.dumps({"password": "username and password doesn't match"})
  57. )
  58. login(request, user)
  59. data = UserSerializer(
  60. user,
  61. context={'request': request},
  62. ).data
  63. return HttpResponse(
  64. JSONRenderer().render(data),
  65. content_type="application/json"
  66. )
  67. @login_required
  68. def logout_user(request):
  69. logout(request)
  70. messages.success(request, 'You have successfully logged out.')
  71. return HttpResponseRedirect(reverse('core:recent-pins'))
  72. def private(request):
  73. return TemplateResponse(request, 'users/private.html', None)