Exemple #1
0
def api_add_user():
    request_data = request.get_json()
    if "uid" in request_data:
        user = Users.get_user_by_uid(request_data["uid"])
        print(user)
        if user:
            result = {
                'status': 'success',
                'message': 'User Already exists with the uid.',
                'user': user.serialize()
            }
            response = Response(dumps(result),
                                200,
                                mimetype='application/json')
            return response

    new_user = Users()
    if "uid" in request_data:
        new_user.uid = request_data["uid"]
    if "email" in request_data:
        new_user.email = request_data["email"]
    if "firstname" in request_data:
        new_user.first_name = request_data["firstname"]
    if "lastname" in request_data:
        new_user.last_name = request_data["lastname"]
    if "mobile" in request_data:
        new_user.mobile = request_data["mobile"]
    if "details" in request_data:
        new_user.details = request_data["details"]
    # new_user.created_user = get_jwt_identity().id

    success, error = Users.add_user(new_user)
    if success:
        # add the student role
        user_role = UserRoles(user=new_user.id,
                              role=Roles.get_role(
                                  request.headers["role_keyword"]))
        UserRoles.add_user_role(user_role)
        result = {
            'status': 'success',
            'message': 'Successfully registered.',
            'user': success
        }
        response = Response(dumps(result), 201, mimetype='application/json')
        return response
    if error:
        result = {
            'status': 'failure',
            'message': 'Internal Error.',
            'user': {
                'msg': str(error)
            }
        }
        response = Response(dumps(result), 501, mimetype='application/json')
        return response

    return Response({h: 'hhf'}, 501, mimetype='application/json')
Exemple #2
0
def api_users_all():
    result = {
        "status": "success",
        "message": "Users found.",
        "user": Users.get_all_users()
    }
    return Response(dumps(result), 200, mimetype='application/json')
Exemple #3
0
    def wrapper(*args, **kwargs):
        body = request.get_json()
        if "uid" not in body:
            error = {
                "status": "failure",
                "message": "Bad Input, Please enter valid uid."
            }
            return Response(dumps(error), 400, mimetype="application/json")

        if "role_keyword" in request.headers and not request.headers[
                "role_keyword"]:
            error = {
                "status": "failure",
                "message": "Bad Input, Please enter valid user role."
            }
            return Response(dumps(error), 400, mimetype="application/json")

        request_data = request.get_json()
        if "uid" in request_data:
            user = Users.get_user_by_uid(request_data["uid"])
            if not user:
                result = {
                    'status': 'failure',
                    'message': 'User not found with the uid.',
                }
                response = Response(dumps(result),
                                    400,
                                    mimetype='application/json')
                return response

        return func(*args, **kwargs)
Exemple #4
0
def register():
    request_data = request.get_json()
    if "uid" in request_data:
        new_user = Users.get_user_by_uid(request_data["uid"])
        if not new_user:
            result = {
                'status': 'failure',
                'message': 'User Enrollment not done, please contact your Administrator.',
            }
            response = Response(dumps(result), 400, mimetype='application/json')
            return response

        if new_user.password:
            result = {
                'status': 'success',
                'message': 'User already registered.',
            }
            response = Response(dumps(result), 200, mimetype='application/json')
            return response

    if "uid" in request_data:
        new_user.uid = request_data["uid"]
    if "password" in request_data:
        new_user.password = request_data["password"]
        new_user.password = Users.generate_hash(new_user.password)


    success, error = new_user.update_user()
    response = None
    if success:
        result = {
            'status': 'success',
            'message': 'Successfully registered.',
            'user': success
        }
        response = Response(dumps(result), 201, mimetype='application/json')
        return response
    if error:
        result = {
            'status': 'failure',
            'message': 'Internal Error.',
            'user': {'msg': str(error)}
        }
        response = Response(dumps(result), 501, mimetype='application/json')
        return response

    return Response({h:'hhf'}, 501, mimetype='application/json')
Exemple #5
0
def api_users_filter():
    filter_dict = request.args.to_dict()
    result = {
        "status": "success",
        "message": "Retrieved all profiles successfully.",
        "object": Users.get_users_by_filter(filter_dict)
    }
    return Response(dumps(result), 200, mimetype='application/json')
Exemple #6
0
def api_users_by_text(text):
    user_list = Users.get_users_from_text(text)
    if not user_list:
        result = {
            "status": "failure",
            "message": "User not found.",
            "user": text
        }
    else:
        result = {
            "status": "success",
            "message": "User found.",
            "options": user_list
        }
    return Response(dumps(result), 200, mimetype='application/json')
Exemple #7
0
def api_user_by_email_exist():
    if 'email' in request.args:
        email = request.args['email']
    else:
        result = {
            "status": "no",
            "message":
            "Error: No email field provided. Please specify an email."
        }
        response = Response(dumps(result), 400, mimetype='application/json')
        return response

    user = Users.get_user_by_email(email)
    if not user:
        result = {"status": "no", "message": "User not found."}
    else:
        result = {"status": "yes", "message": "User found."}
    return Response(dumps(result), 200, mimetype='application/json')
Exemple #8
0
def api_update_user():
    request_data = request.get_json()
    user = Users.get_user_by_uid(request_data["uid"])

    if "uid" in request_data:
        user.uid = request_data["uid"]
    if "email" in request_data:
        user.email = request_data["email"]
    if "firstname" in request_data:
        user.first_name = request_data["firstname"]
    if "lastname" in request_data:
        user.last_name = request_data["lastname"]
    if "mobile" in request_data:
        user.mobile = request_data["mobile"]
    if "details" in request_data:
        user.details = request_data["details"]
    # new_user.created_user = get_jwt_identity().id

    success, error = user.update_user()
    if success:
        result = {
            'status': 'success',
            'message': 'Successfully updated.',
            'user': success
        }
        response = Response(dumps(result), 201, mimetype='application/json')
        return response
    if error:
        result = {
            'status': 'failure',
            'message': 'Internal Error.',
            'user': {
                'msg': str(error)
            }
        }
        response = Response(dumps(result), 501, mimetype='application/json')
        return response

    return Response({h: 'hhf'}, 501, mimetype='application/json')
Exemple #9
0
def api_user_by_mobile():
    if 'mobile' in request.args:
        mobile = request.args['mobile']
    else:
        result = {
            "status":
            "failure",
            "message":
            "Error: No mobile field provided. Please specify an username."
        }
        response = Response(dumps(result), 400, mimetype='application/json')
        return response

    user = Users.get_user_by_mobile(mobile)
    if not user:
        result = {
            "status": "failure",
            "message": "User not found.",
            "user": user
        }
    else:
        result = {"status": "success", "message": "User found.", "user": user}
    return Response(dumps(result), 200, mimetype='application/json')