コード例 #1
0
def get_banks():

    try:
        ifsc = request.args.get('ifsc')

        if not ifsc:
            return jsonify({
                "msg": "ifsc param is missing"
            }), 400

        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        # get the entire results
        banks = bank_details_service(ifsc)

        # paginate and display
        results = get_paginated_results(banks, request.base_url, int(offset), int(limit))

        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #2
0
 def run(self):
     try:
         self.result = self.func(*self.args)  # unwrap the arguments
         logger.info(str(self.func), "returned => ", self.result)
     except Exception as e:
         logger.exception(e)
         self.result = -1
コード例 #3
0
def get_branches_details():
    try:
        bank_name = request.args.get('bank_name')
        city = request.args.get('city')

        if not city:
            return jsonify({
                "msg": "city param is missing"
            }), 400

        if not bank_name:
            return jsonify({
                "msg": "bank_name param is missing"
            }), 400

        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        # get the entire results
        branches = bank_branches_in_city_service(bank_name, city)

        # paginate and display
        results = get_paginated_results(branches, request.base_url, int(offset), int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #4
0
ファイル: error.py プロジェクト: rzte/blog
def sec_exception(e):
    """
    SecExcpetion
    """
    try:
        addr = request.remote_addr
        logger.error("{}: {}:".format(session.get('username'), addr),
                     exc_info=1)
    except Exception as ec:
        logger.exception(ec)
    resp = make_response(Resp(Resp.ERROR, str(e)).to_json(), 200)
    resp.headers['Content-Type'] = "application/json"
    return resp
コード例 #5
0
def verify_token(token):
    try:
        payload = jwt.decode(str(token).encode("utf-8"), config['SECRET_KEY'])

        result = get_user_credentials(payload["sub"])
        if result:
            return 1
        return 0
    except jwt.exceptions.DecodeError as e:
        logger.exception(e)
        return -1
    except jwt.exceptions.ExpiredSignatureError as e:
        logger.exception(e)
        return -2
コード例 #6
0
def get_all_students():
    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        students = get_all_student_names()
        results = get_paginated_results(students, request.base_url,
                                        int(offset), int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #7
0
def students_who_attended(class_id):
    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        students_enrolled = get_students_enrolled(class_id)

        results = get_paginated_results(students_enrolled, request.base_url,
                                        int(offset), int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #8
0
def studentwise_performance(class_id):
    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        classwise_performance = get_student_performance_in_class(class_id)

        results = get_paginated_results(classwise_performance,
                                        request.base_url, int(offset),
                                        int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #9
0
def final_grade_sheet(class_id):
    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        results = final_grade_sheet_service(class_id)

        if "status" in results.keys():
            return jsonify({"msg": results["msg"]}), results["status"]

        results = get_paginated_results(results, request.base_url, int(offset),
                                        int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #10
0
def classwise_performance_of_a_student(student_id):
    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        performance_classwise = get_classes_performance(student_id)

        if "status" in performance_classwise.keys():
            return jsonify({"msg": performance_classwise["msg"]
                            }), performance_classwise["status"]

        results = get_paginated_results(performance_classwise,
                                        request.base_url, int(offset),
                                        int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #11
0
def classes_taken_by(student_id):

    try:
        # reading the request arguments
        offset = request.args.get('offset', 0)
        limit = request.args.get('limit', 10)

        classes_taken = get_classes_taken(student_id)

        # if the given student id does not exist in DB
        if "status" in classes_taken.keys():
            return jsonify({"msg":
                            classes_taken["msg"]}), classes_taken["status"]

        # if the given student_id exists in DB
        results = get_paginated_results(classes_taken, request.base_url,
                                        int(offset), int(limit))
        return jsonify(results), 200

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #12
0
def login():
    try:
        if request.authorization:

            if authenticate_user(request.authorization["username"],
                                 request.authorization["password"]):
                return jsonify({
                    "token":
                    generate_token(request.authorization["username"])
                }), 200
            else:
                return jsonify({
                    "msg":
                    "The login credentials are either wrong or unavailable in the users database."
                }), 403
        else:
            return jsonify(
                {"msg":
                 "Username/Password not found in authorization header"}), 400
    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #13
0
def register_new_user():
    try:

        body = request.json

        if not body:
            return jsonify({"msg": "request json not found"}), 400

        if "username" not in body:
            return jsonify({"msg":
                            "username not present in request body"}), 400

        if "password" not in body:
            return jsonify({"msg":
                            "password not present in request body"}), 400

        result = register_user(body["username"], body["password"])

        return jsonify(result), 201

    except Exception as e:
        logger.exception(e)
        abort(500)
コード例 #14
0
def available_apis():
    try:
        # if pagination is disabled then do not show the common params
        if config['PAGINATE_RESULTS'] in ['off', '0', 'false', 'False']:
            common_params = "switched off"
        else:
            common_params = [{
                "offset":
                "<int> to start the results from the provided offset(DEFAULT: 0)"
            }, {
                "limit": "<int> results in a batch(DEFAULT: 10)"
            }]

        # if authentication is disabled then do not show the auth params
        if config['AUTHENTICATION'] in ['off', '0', 'false', 'False']:
            auth_endpoints = "switched off"
        else:
            auth_endpoints = {
                1: "JWT tokens are used to authenticate the api.",
                2: {
                    "Login":
                    "******",
                    "path":
                    "/auth/login",
                    "description":
                    "Existing users can login into the application by"
                    " passing <username> and <password> in the request Authorization."
                    "GET the request token from this API and use it as a part of the Authorization header in "
                    "all other APIs"
                },
                3: {
                    "Register":
                    "To register a new user",
                    "path":
                    "/auth/register",
                    "description":
                    "pass a json body {username: <username>, password:<password>} to the POST request"
                }
            }

        return jsonify({
            "api_endpoints": [{
                "Students": "/students",
                "description": "GET list of all the students"
            }, {
                "Classes Taken By a Student":
                "/student/<student_id>/classes",
                "description":
                "GET list of all the classes taken by a given student_id"
            }, {
                "Studentwise Total marks":
                "/student/{student_id}/performance",
                "description":
                "GET list of classes its performance taken by a student by student_id"
            }, {
                "Classes":
                "/classes",
                "description":
                "GET list of all classes which are taken by any student"
            }, {
                "Students Enrolled":
                "/class/<class_id>/student",
                "description":
                "GET list of students who enrolled for the given class_id"
            }, {
                "Classwise Total marks":
                "/class/<class_id>/performance",
                "description":
                "GET the total marks of each student enrolled in the given class_id"
            }, {
                "Final-Grade-Sheet":
                "/class/<class_id>/final-grade-sheet",
                "description":
                "GET the final-grade-sheet of all the students who took a particular class"
            }, {
                "Student + Class Type 1":
                "/class/<class_id>/student/<student_id>",
                "description":
                "GET the student_id, student_name, class_id and marks "
                "obtained by the student in the given class by its class_id"
            }, {
                "Student + Class Type 2":
                "/student/<student_id>/class/<class_id>",
                "description":
                "GET the student_id, student_name, class_id and marks "
                "obtained by the student in the given class by its class_id"
            }],
            "common_params":
            common_params,
            "auth":
            auth_endpoints
        }), 200
    except Exception as e:
        logger.exception(e)
        abort(500)