def loginme(request): if request.method != 'POST': if request.session.has_key('username') and request.session['username'] is not None: return HttpResponseRedirect('home') else: return render(request, 'user/login.html') else: if request.session.has_key('username') and request.session['username'] is not None: return HttpResponseRedirect('home') else: context = {} username = request.POST['username'] password = request.POST['password'] user = models.Login_user.getUser(username) if user: if check_password(password, user.password): request.session['username'] = user.username return HttpResponseRedirect('home') else: context = { 'status': 'Username or Password Incorrect'} else: context = {'status': 'Login unsuccessfull'} return render(request, 'user/login.html', context=context)
def authenticate(self, request=None, username=None, password=None): credentials = self.query_credentials(username) if credentials and check_password(password, credentials.password): try: user = User.objects.get(username=username) except User.DoesNotExist: user = User.objects.create_user(username, credentials.email, 'password_never_used') if credentials.level == 0: user.is_active = False user.save() return None # no right self._update_infos(user, credentials) self._update_groups(user, credentials) return user return None
def authenticate(self, username=None, password=None): credentials = self.query_credentials(username) if credentials and check_password(password, credentials.password): try: user = User.objects.get(username=username) except User.DoesNotExist: user = User.objects.create_user(username, credentials.email, 'password_never_used') if credentials.level == 0: user.is_active = False user.save() return None # no right self._update_infos(user, credentials) self._update_groups(user, credentials) return user return None
def post(self, request): data = request.data serializer = self.serializer_class(data=data) serializer.is_valid(raise_exception=True) try: user = User.objects.get(email=data.get('email')) except ObjectDoesNotExist: return Response({'detail': "Wrong password or email"}, status=status.HTTP_400_BAD_REQUEST) if check_password(data.get('password'), user.password): token = generate_token(user) payload = { 'message': "Successfully logged in", 'token': token, 'data': UserSerializer(user).data } payload['data']['isStaff'] = user.is_staff return Response(payload, status=status.HTTP_200_OK) return Response({'detail': 'Wrong password or email'}, status=status.HTTP_400_BAD_REQUEST)