Exemple #1
0
    def post(self):
        try:
            json_data = request.get_json()
        except Exception as e:
            raise MyBadRequestException
        check_user = False
        try:
            check_user = User.get_by_username(json_data["username"])
        except Exception as e:
            raise MyInternalServerErrorException
        if check_user:
            raise MyBadRequestException

        data, errors = user_schema.load(data=json_data)
        user = User(**data)
        if json_data["is_admin"] == 1:
            user.is_admin = True
        else:
            user.is_admin = False
        try:
            user.save()
        except Exception as e:
            raise MyInternalServerErrorException

        return user_schema.dump(user).data, HTTPStatus.CREATED
Exemple #2
0
def record_author():
    header = request.headers.get('Authorization')
    _, token = header.split()
    try:
        data = JWTToken.decode(token)
        author = data["identity"]
    except Exception as e:
        raise MyInternalServerErrorException
    user = User.get_by_username(author)
    return user
Exemple #3
0
 def post(self):
     try:
         data = request.get_json()
         username = data.get('username')
         password = data.get('password')
     except Exception as e:
         raise MyBadRequestException
     try:
         user = User.get_by_username(username=username)
     except Exception as e:
         raise MyInternalServerErrorException
     if user is None or not user or not check_password(
             password, user.password):
         raise MyBadRequestException
     try:
         token, exp = JWTToken.encode(user.username)
         result = {'access_token': token, 'expire_date': exp}
         return {'result': result}, HTTPStatus.CREATED
     except Exception as e:
         raise MyInternalServerErrorException