Beispiel #1
0
def update_student(student_id: int):
    """
    update student by ID
    :param student_id:
    :return:
    """
    data = request.get_json()
    if data is None:
        return post_request_empty()

    student = student_service.get_student_by_id(student_id)
    if not student:
        return custom_error("Invalid student id supplied.")

    try:
        address_data = data.get("address")
        address_updated = True
        if address_data:
            address_updated = address_service.update_address_by_id(
                student["address"]["id"], {
                    "division": address_data.get("division"),
                    "district": address_data.get("district"),
                    "township": address_data.get("township"),
                    "street_address": address_data.get("street_address"),
                    "type": "student"
                })

        if address_updated:
            student_update_status = student_service.update_student_by_id(
                student_id, {
                    "name": data.get("name"),
                    "deactivated_at": data.get("deactivated_at"),
                    "birth_date": data.get("birth_date"),
                    "father_name": data.get("father_name"),
                    "mother_name": data.get("mother_name"),
                    "parents_occupation": data.get("parents_occupation"),
                    "photo": data.get("photo"),
                    "address_id": student["address"]["id"]
                })
            if student_update_status:
                current_app.logger.info(
                    "Update success for student_id: {}".format(student_id))
                return get_student_by_id(student_id)
            else:
                current_app.logger.error(
                    "Update fail for student_id: {}".format(student_id))
                custom_error(
                    "Update Fail for student id: {}".format(student_id))

    except ValueError as error:
        current_app.logger.error("Value error for address id. error: %s",
                                 error)
        return jsonify({"errors": [error.__dict__]}), 400

    except (SQLCustomError, ValidateFail, RequestDataEmpty) as error:
        current_app.logger.error(
            "Error for student data update id {} Error: {}".format(
                student_id, error))
        return jsonify({"errors": [error.__dict__]}), 400
Beispiel #2
0
def update_school(school_id: int):
    """
    update school by ID
    :param school_id:
    :return:
    """
    data = request.get_json()
    if data is None:
        return post_request_empty()
    try:
        address_data = data.get("address")
        address_updated = True

        school = school_service.get_school_by_id(school_id)
        if not school:
            return custom_error("Invalid school id supplied.")
        if address_data:
            address_updated = address_service.update_address_by_id(
                school["address"]["id"], {
                    "division": address_data.get("division"),
                    "district": address_data.get("district"),
                    "township": address_data.get("township"),
                    "street_address": address_data.get("street_address"),
                    "type": "school"
                })

        if address_updated:
            school_update_status = school_service.update_school_by_id(
                school_id, {
                    "name": data.get("name"),
                    "contact_info": data.get("contact_info"),
                    "photo": data.get("photo"),
                    "address_id": school["address"]["id"]
                })
        else:
            return custom_error("Failed to update address.")
        if school_update_status:
            current_app.logger.info(
                "Update success for school_id: {}:".format(school_id))
            return get_school_by_id(school_id)
        else:
            current_app.logger.error(
                "Update fail for school_id: {}".format(school_id))
            return custom_error(
                "Fail to update school id: {}".format(school_id))

    except ValueError as error:
        current_app.logger.error("Value error for address id. error: %s",
                                 error)
        return jsonify({"errors": [error.__dict__]}), 400

    except (SQLCustomError, ValidateFail, RequestDataEmpty) as error:
        current_app.logger.error(
            "Error for school data update id {} Error: {}".format(
                school_id, error))
        return jsonify({"errors": [error.__dict__]}), 400
Beispiel #3
0
def update_school(school_id: int):
    """
    update school by ID
    :param school_id:
    :return:
    """
    data = request.get_json()
    if data is None:
        return post_request_empty()
    school_update_status = False
    try:
        address_id = int(data.get("address_id"))
        if address_service.update_address_by_id(address_id, {
            "division": data.get("division"),
            "district": data.get("district"),
            "township": data.get("township"),
            "street_address": data.get("street_address"),
            "type": data.get("type")
        }):
            school_update_status = school_service.update_school_by_id(school_id, {
                "school_name": data.get("school_name"),
                "contact_info": data.get("contact_info"),
                "address_id": address_id
            })
        current_app.logger.info("Update success for school_id: {}".format(school_id)) \
            if school_update_status else current_app.logger.error("Update fail for school_id: {}"
                                                                  .format(school_id))
        return jsonify({
            "status": school_update_status
        }), 200
    except ValueError as error:
        current_app.logger.error("Value error for address id. error: %s", error)
        return jsonify({
            "errors": {
                "error": error
            }
        }), 400
    except (SQLCustomError, ValidateFail, RequestDataEmpty) as error:
        current_app.logger.error("Error for school data update id {} Error: {}"
                                 .format(school_id, error))
        return jsonify({
            "errors": {
                "error": error.__dict__
            }
        }), 400
Beispiel #4
0
def update_student(student_id: int):
    """
    update student by ID
    :param student_id:
    :return:
    """
    data, photo = get_student_data_from_request(request)
    if data is None:
        return post_request_empty()

    student = student_service.get_student_by_id(student_id)
    if not student:
        return custom_error("Invalid student id supplied.")

    try:
        address_data = address_service.thingaha_helper.parse_address_data(data)
        address_updated = True

        if address_data:
            address_updated = address_service.update_address_by_id(
                student["address"]["id"], {
                    "division": address_data.get("division"),
                    "district": address_data.get("district"),
                    "township": address_data.get("township"),
                    "street_address": address_data.get("street_address"),
                    "type": "student"
                })

        if address_updated:
            student_update_status = student_service.update_student_by_id(
                student_id, {
                    "name":
                    data.get("name"),
                    "deactivated_at":
                    None if data.get("active") else
                    student_service.thingaha_helper.get_now(),
                    "birth_date":
                    student_service.thingaha_helper.standardize_str_to_date(
                        data.get("birth_date")),
                    "father_name":
                    data.get("father_name"),
                    "mother_name":
                    data.get("mother_name"),
                    "gender":
                    data.get("gender"),
                    "parents_occupation":
                    data.get("parents_occupation"),
                    "photo":
                    photo,
                    "address_id":
                    student["address"]["id"]
                })
            if student_update_status:
                current_app.logger.info(
                    "Update success for student_id: {}".format(student_id))
                return get_student_by_id(student_id)
            else:
                current_app.logger.error(
                    "Update fail for student_id: {}".format(student_id))
                custom_error(
                    "Update Fail for student id: {}".format(student_id))

    except ValueError as error:
        current_app.logger.error(f"Value error for address id. error: {error}")
        return jsonify({"errors": [error.__dict__]}), 400

    except (SQLCustomError, ValidateFail, RequestDataEmpty) as error:
        current_app.logger.error(
            f"Error for student data update id {student_id} Error: {error}")
        return jsonify({"errors": [error.__dict__]}), 400