Advertisement
Guest User

Untitled

a guest
Nov 6th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1.  
  2. from django.contrib.auth import authenticate, login, logout
  3. from django.utils.decorators import method_decorator
  4. from django.views.decorators.csrf import csrf_exempt
  5. from django.shortcuts import render_to_response
  6. from django.views.generic import View
  7. from django.http.response import HttpResponse, HttpResponseRedirect
  8. from django.template import RequestContext
  9. from django.contrib.auth.models import User
  10.  
  11. from posts.views import JsonResponseMixin, DataError
  12. from .forms import RegistrationForm
  13.  
  14.  
  15. # Create your views here.
  16.  
  17.  
  18. class LoginView(JsonResponseMixin, View):
  19.     def get_context_data(self, request, *args, **kwargs):
  20.         try:
  21.             data = json.loads(request.body.decode('utf-8'))
  22.             username = data['login']
  23.             password = data['password']
  24.         except KeyError:
  25.             raise DataError
  26.  
  27.         user = authenticate(username=username, password=password)
  28.         if user is not None and user.is_active:
  29.             login(request, user)
  30.             return [{'login': username}]
  31.         else:
  32.             raise DataError
  33.  
  34.     @method_decorator(csrf_exempt)
  35.     def dispatch(self, request, *args, **kwargs):
  36.         return super(LoginView, self).dispatch(request, *args, **kwargs)
  37.  
  38.  
  39. class CheckLoginView(JsonResponseMixin, View):
  40.     def get_context_data(self, request, *args, **kwargs):
  41.         if request.user.is_authenticated():
  42.             return {'isLogined': True, 'username': request.user.username}
  43.         else:
  44.             return {'isLogined': False}
  45.  
  46.     @method_decorator(csrf_exempt)
  47.     def dispatch(self, request, *args, **kwargs):
  48.         return super(CheckLoginView, self).dispatch(request, *args, **kwargs)
  49.  
  50.  
  51. class LogoutView(View):
  52.     def get(self, request, *args, **kwargs):
  53.         if request.user.is_authenticated():
  54.             logout(request)
  55.         return HttpResponse()
  56.  
  57.  
  58. class RegisterView(View):
  59.     def get(self, request, *args, **kwargs):
  60.         form = RegistrationForm()
  61.         context = RequestContext(request, {'form': form})
  62.         return render_to_response("registration.html", context,)
  63.  
  64.     def post(self, request, *args, **kwargs):
  65.         print(request.body)
  66.         form = RegistrationForm(request.POST)
  67.         print(form)
  68.         if form.is_valid():
  69.             user = User.objects.create_user(
  70.                 username=form.cleaned_data['username'],
  71.                 password=form.cleaned_data['password1'],
  72.                 email=form.cleaned_data['email']
  73.             )
  74.             user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password1'])
  75.             login(request, user)
  76.         return HttpResponseRedirect('/')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement