Пример #1
0
def create_student():
    """
    create student by post body
    :return:
    """
    data, photo = get_student_data_from_request(request)
    if data is None:
        return post_request_empty()
    try:
        address_data = address_service.thingaha_helper.parse_address_data(
            data) if data.get('address[division]') else get_default_address()
        address_id = address_service.create_address(
            {
                "division": address_data.get("division"),
                "district": address_data.get("district"),
                "township": address_data.get("township"),
                "street_address": address_data.get("street_address"),
                "type": "student"
            }, True)
        if not address_id:
            raise ThingahaCustomError("Student address create fail")
        student_id = student_service.create_student({
            "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":
            address_id
        })
        current_app.logger.info("Create student success. student_name %s",
                                data.get("name"))
        return get_student_by_id(student_id), 200
    except (RequestDataEmpty, SQLCustomError, ValidateFail,
            ThingahaCustomError, ValueError) as error:
        current_app.logger.error(
            f"Create student request fail.{traceback.format_exc()}")
        return jsonify({"errors": [error.__dict__]}), 400
Пример #2
0
def create_student():
    """
    create student by post body
    :return:
    """
    data = request.get_json()
    if data is None:
        return post_request_empty()
    try:
        address_data = data.get("address") if data.get(
            "address") else get_default_address()
        address_id = address_service.create_address(
            {
                "division": address_data.get("division"),
                "district": address_data.get("district"),
                "township": address_data.get("township"),
                "street_address": address_data.get("street_address"),
                "type": "student"
            },
            flush=True)
        if not address_id:
            raise ThingahaCustomError("Student address create fail")

        student_id = student_service.create_student({
            "name":
            data.get("name"),
            "deactivated_at":
            None if data.get("active") else
            datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "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":
            address_id
        })
        current_app.logger.info("Create student success. student_name %s",
                                data.get("name"))
        return get_student_by_id(student_id), 200
    except (RequestDataEmpty, SQLCustomError, ValidateFail,
            ThingahaCustomError) as error:
        current_app.logger.error("Create student request fail")
        return jsonify({"errors": [error.__dict__]}), 400
Пример #3
0
def create_user():
    """
    create user by post body
    :return:
    """
    data = request.get_json()
    if data is None:
        return post_request_empty()
    try:
        address_data = data.get("address") if data.get(
            "address") else get_default_address()
        address_id = address_service.create_address(
            {
                "division": address_data.get("division"),
                "district": address_data.get("district"),
                "township": address_data.get("township"),
                "street_address": address_data.get("street_address"),
                "type": "user"
            },
            flush=True)
        if not address_id:
            raise ThingahaCustomError("User address create fail")
        user_id = user_service.create_user({
            "username":
            data.get("username"),
            "display_name":
            data.get("display_name"),
            "email":
            data.get("email"),
            "address_id":
            address_id,
            "password":
            data.get("password"),
            "role":
            data.get("role"),
            "country":
            data.get("country"),
            "donation_active":
            True if data.get("donation_active") else False
        })
        current_app.logger.info("Create user success. user_name %s",
                                data.get("username"))
        return get_user_by_id(user_id)
    except (RequestDataEmpty, SQLCustomError, ValidateFail) as error:
        current_app.logger.error("Create user fail. user_name %s, error: %s",
                                 data.get("username"), error.description)
        return jsonify({"errors": [error.__dict__]}), 400
Пример #4
0
def create_school():
    """
    create school by post body
    :return:
    """
    data = request.get_json()

    if data is None:
        return post_request_empty()

    address_data = data.get("address") if data.get(
        "address") else get_default_address()
    try:
        address_id = address_service.create_address(
            {
                "division": address_data.get("division"),
                "district": address_data.get("district"),
                "township": address_data.get("township"),
                "street_address": address_data.get("street_address"),
                "type": "school"
            },
            flush=True)
        current_app.logger.debug("create address id: %s", address_id)

        if not address_id:
            raise ThingahaCustomError("Address create fail for school")

        school_id = school_service.create_school({
            "name":
            data.get("name"),
            "contact_info":
            data.get("contact_info"),
            "photo":
            data.get("photo"),
            "address_id":
            address_id
        })
        current_app.logger.info("Create school success. name %s",
                                data.get("name"))
        return get_school_by_id(school_id)

    except (RequestDataEmpty, SQLCustomError, ValidateFail,
            ThingahaCustomError) as error:
        current_app.logger.error("Create school request fail")
        return jsonify({"errors": [error.__dict__]}), 400