Example #1
0
def delete_user(req_uid, user_id):
    ''' Delete an user '''

    try:
        user_id = int(user_id)
        req_user = User.query.filter_by(id=req_uid).first()

        if req_user.id != user_id and not is_admin(req_uid):
            raise AuthorizationError

        user = User.query.filter_by(id=user_id).first()

        if not user:
            raise ValueError

        DB.session.delete(user)
        DB.session.commit()

        return parse_response(
            200, "{0} successfully deleted.".format(user.username))

    except AuthorizationError as exp:
        return parse_response(exp.code, str(exp))
    except ValueError:
        return parse_response(404, "User does not exist.")
    except OperationalError as exp:
        return parse_response(503, "The service is currently unavailable.")
Example #2
0
def get_all_users(req_uid):
    ''' Get all users '''

    try:
        if not is_admin(req_uid):
            raise AuthorizationError

        users = User.query.order_by(User.created_at.desc()).all()

        all_users = []
        for user in users:
            all_users.append(user.get_dict())

        return parse_response(200, data=all_users)

    except AuthorizationError as exp:
        return parse_response(exp.code, str(exp))
    except OperationalError as exp:
        return parse_response(503, "The service is currently unavailable.")
Example #3
0
def add_user(req_uid):
    ''' Add a user to the database. '''

    try:
        if not is_admin(req_uid):
            raise AuthorizationError

        payload = request.get_json()

        if not payload:
            raise InvalidRequest("Invalid payload.")

        validate_user(payload)

        username = payload.get("username")
        email = payload.get("email")
        admin = payload.get("admin")
        password = payload.get("password")

        ex_user = User.query.filter_by(username=username).first()
        ex_email = User.query.filter_by(email=email).first()

        if ex_user or ex_email:
            raise InvalidRequest("The username or e-mail already exists.")

        new_user = User(username=username,
                        password=password,
                        email=email,
                        admin=admin)

        DB.session.add(new_user)
        DB.session.commit()

        return parse_response(201,
                              "{0} successfully created.".format(username))

    except (AuthorizationError, ValidationError, InvalidRequest) as exp:
        return parse_response(exp.code, str(exp))
    except (exc.IntegrityError, ValueError):
        return parse_response(400, "Invalid payload.")
    except OperationalError as exp:
        return parse_response(503, "The service is currently unavailable.")
Example #4
0
def get_user(req_uid, user_id):
    ''' Get detail of user '''

    try:
        user_id = int(user_id)
        req_user = User.query.filter_by(id=req_uid).first()

        if req_user.id != user_id and not is_admin(req_uid):
            raise AuthorizationError

        user = User.query.filter_by(id=user_id).first()

        if not user:
            raise ValueError

        return parse_response(200, data=user.get_dict())

    except AuthorizationError as exp:
        return parse_response(exp.code, str(exp))
    except ValueError:
        return parse_response(404, "The user does not exist.")
    except OperationalError as exp:
        return parse_response(503, "The service is currently unavailable.")
Example #5
0
def change_user(req_uid):
    ''' Change users details '''

    try:
        if not is_admin(req_uid):
            raise AuthorizationError

        payload = request.get_json()

        if not payload:
            raise InvalidRequest("Invalid payload.")

        validate_user(payload)

        username = payload.get("username")
        email = payload.get("email")
        admin = payload.get("admin")
        password = payload.get("password")

        ex_user = User.query.filter_by(username=username).first()

        ex_user.username = username
        ex_user.email = email
        ex_user.password = User.generate_hash(password)
        ex_user.admin = admin

        DB.session.commit()

        return parse_response(201,
                              "{0} successfully created.".format(username))

    except (AuthorizationError, ValidationError, InvalidRequest) as exp:
        return parse_response(exp.code, str(exp))
    except (exc.IntegrityError, ValueError):
        return parse_response(400, "Invalid payload.")
    except OperationalError as exp:
        return parse_response(503, "The service is currently unavailable.")