def get(self, request, *args, **kwargs): account = request.user if account.is_authenticated: logout(request) if account.is_active: token, created = Token.objects.get_or_create(user=account) if not settings.FRONTEND_LOGIN_REDIRECT_URL: serialized_account = serializers.AccountSerializer(account) data = serialized_account.data data['token'] = token.pk return Response(data) else: return redirect('{}?token={}'.format( settings.FRONTEND_LOGIN_REDIRECT_URL, base64.urlsafe_b64encode(token.pk.encode()).decode())) else: return Response( { 'status': 'Unauthorized', 'message': _('Your account has been disabled.') }, status=status.HTTP_401_UNAUTHORIZED) else: return Response( { 'status': 'Unauthorized', 'message': _('Username/password combination invalid.') }, status=status.HTTP_401_UNAUTHORIZED)
def post(self, request, *args, **kwargs): """ Authenticate the user --- parameters: - name: username description: Account username required: true type: string - name: password description: Account password required: true type: string response_serializer: serializers.AccountSerializer responseMessages: - code: 401 message: Unauthorized """ data = request.data username = str(data.get('username')).lower() password = data.get('password') account = authenticate(username=username, password=password) if account: if account.is_active: if account.is_verified: login(request, account) serialized_account = serializers.AccountSerializer(account) data = serialized_account.data return Response(data) else: return Response( { 'status': 'Unauthorized', 'message': 'You need to verify you account first.' }, status=status.HTTP_401_UNAUTHORIZED) else: return Response( { 'status': 'Unauthorized', 'message': 'Your account has been disabled.' }, status=status.HTTP_401_UNAUTHORIZED) else: return Response( { 'status': 'Unauthorized', 'message': 'Username/password combination invalid.' }, status=status.HTTP_401_UNAUTHORIZED)
def students(self, request): serializer = account_serializers.AccountSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() account_models.Student.objects.create(user=user) return Response(status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: serializer = serializers.AccountSerializer(data=request.data) validated_data = serializer.validate(request.data) serializer.is_valid(raise_exception=True) User.objects.create_user(username=validated_data['username'], email=validated_data['email'], password=validated_data['password']) return Response(data=dict(message="User created"), status=status.HTTP_201_CREATED) except ValidationError as e: raise e except Exception as e: logging.exception(str(e)) return Response(data=dict(message="Something went wrong"), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request, *args, **kwargs): """ Authentication for account """ serializer = serializers.LoginSerializer(data=request.data) if not serializer.is_valid(): return Response({'errors': serializer.errors}, status.HTTP_400_BAD_REQUEST) data = serializer.data email = data.get('email') password = data.get('password') try: account = models.Account.objects.get(email=email) except models.Account.DoesNotExist: account = None if account and account.check_password(password): if account.is_active: serialized_account = serializers.AccountSerializer(account) data = serialized_account.data token, created = Token.objects.get_or_create(user=account) data['token'] = token.pk return Response(data) else: return Response( { 'status': 'Unauthorized', 'message': _('Your account has been disabled.') }, status=status.HTTP_401_UNAUTHORIZED) else: return Response( { 'status': 'Unauthorized', 'message': _('Username/password combination invalid.') }, status=status.HTTP_401_UNAUTHORIZED)
def user_info(self, request): if request.user.teacher_set.all(): serializer = account_serializers.AccountSerializer(request.user) return Response(serializer.data, status=status.HTTP_200_OK) else: return Response(status=status.HTTP_400_BAD_REQUEST)