def create_school(): """ create school by post body :return: """ data = request.get_json() if data is None: return post_request_empty() try: address_id = address_service.create_address({ "division": data.get("division"), "district": data.get("district"), "township": data.get("township"), "street_address": data.get("street_address"), "type": data.get("type") }) school_id = school_service.create_school({ "school_name": data.get("school_name"), "contact_info": data.get("contact_info"), "address_id": address_id }) current_app.logger.info("Create school success. school_name %s", data.get("school_name")) return get_school_by_id(school_id) except (RequestDataEmpty, SQLCustomError, ValidateFail) as error: current_app.logger.error("Create school request fail") return jsonify({ "errors": { "error": error.__dict__ } }), 400
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
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
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
def create_student(): """ create student by post body :return: """ data = request.get_json() if data is None: return post_request_empty() try: address_id = address_service.create_address({ "division": data.get("division"), "district": data.get("district"), "township": data.get("township"), "street_address": data.get("street_address"), "type": "student" }) student_id = student_service.create_student({ "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": 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) as error: current_app.logger.error("Create student request fail") return jsonify({"errors": [error.__dict__]}), 400