def create_attendance(attendance_service: AttendanceService): """ create attendance by post body :param attendance_service: :return: """ data = request.get_json() if data is None: return post_request_empty() try: attendance_id = attendance_service.create_attendance({ "student_id": int(data.get("student_id")), "school_id": int(data.get("school_id")), "grade": data.get("grade"), "year": int(data.get("year")), "enrolled_date": attendance_service.thingaha_helper.standardize_str_to_date( data.get("enrolled_date")) }) current_app.logger.info("Create school success. school_name %s", data.get("school_name")) return get_attendance_by_id(attendance_id), 200 except (RequestDataEmpty, SQLCustomError, ValidateFail, ValueError) as error: current_app.logger.error(f"Create attendance request fail. {error}") return jsonify({"errors": [error.__dict__]}), 400
def update_attendance(attendance_id: int): """ update attendance by ID :param attendance_id: :return: """ data = request.get_json() if data is None: return post_request_empty() try: status = attendance_service.update_attendance_by_id( attendance_id, data) if status: current_app.logger.info( "Success update attendance for attendance_id: %s", attendance_id) return get_attendance_by_id(attendance_id) else: current_app.logger.error( "Fail update attendance for attendance_id: %s", attendance_id) return custom_error("Fail to update attendance id : %", attendance_id) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update attendance fail: attendance_id: %s", attendance_id) return jsonify({"errors": [error.__dict__]}), 400
def create_user(): """ create user 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") }) user_id = user_service.create_user({ "name": data.get("name"), "email": data.get("email"), "address_id": address_id, "password": data.get("password"), "role": data.get("role"), "country": data.get("country") }) current_app.logger.info("create user success. user_name %s", data.get("name")) 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("name"), error) return jsonify({ "errors": { "error": error.__dict__ } }), 400
def update_user(user_id: int): """ update user info by id """ data = request.get_json() if data is None: return post_request_empty() try: user = user_service.get_user_by_id(user_id) if not user: return custom_error("Invalid user id supplied.") address_data = data.get("address") address_updated = True if address_data: address_updated = address_service.update_address_by_id( user["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": "user" }) if address_updated: user_update_status = user_service.update_user_by_id( user_id, { "username": data.get("username"), "display_name": data.get("display_name"), "email": data.get("email"), "role": data.get("role"), "address_id": user["address"]["id"], "country": data.get("country"), "donation_active": True if data.get("donation_active") else False }) if user_update_status: current_app.logger.info("Success user update for user_id: %s", user_id) else: current_app.logger.error("Fail user update for user_id: %s", user_id) return custom_error("Update fail for user_id: %s", user_id) return get_user_by_id(user_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("Fail to update user: %s, error: %s", user_id, error.description) return jsonify({"errors": [error.__dict__]}), 400
def delete_student_photo(student_id: int): """ delete student photo """ if not student_id: current_app.logger.error("Empty url or empty student id") return post_request_empty() student = student_service.get_student_by_id(student_id) try: if not student: raise ThingahaCustomError("Invalid student id.") if student["photo"] is None: raise ThingahaCustomError( "Cannot delete photo that doesn't exist anymore.") result = student_service.delete_file( student["photo"]) and student_service.update_photo_path_by_id( student_id, "") if result: current_app.logger.info("Delete file for URL %s success", student["photo"]) return "", 200 else: current_app.logger.error("Delete file for URL %s fail", student["photo"]) return "", 400 except TypeError: current_app.logger.error("Student id must be integer") return custom_error("Student id must be integer") except SQLCustomError as error: current_app.logger.error("Error for student photo delete {}".format( error.__dict__)) return custom_error("Error updating student photo.")
def create_attendance(): """ create attendance by post body :return: """ data = request.get_json() if data is None: return post_request_empty() try: attendance_id = attendance_service.create_attendance({ "student_id": data.get("student_id"), "school_id": data.get("school_id"), "grade": data.get("grade"), "year": data.get("year"), "enrolled_date": data.get("enrolled_date") }) current_app.logger.info("Create school success. school_name %s", data.get("school_name")) return get_attendance_by_id(attendance_id), 200 except (RequestDataEmpty, SQLCustomError, ValidateFail) as error: current_app.logger.error("Create attendance request fail") return jsonify({"errors": [error.__dict__]}), 400
def create_address(): """ create address data :return: """ data = request.get_json() if data is None: return post_request_empty() try: current_app.logger.info("Create address") 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") }) current_app.logger.info("Create address success. address %s", data.get("street_address")) return get_address_by_id(address_id) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: return jsonify({"errors": {"error": error.__dict__}}), 400
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 update_extra_funds(extra_fund_id: int): """ update extra_funds data :param extra_fund_id: :return: """ data = request.get_json() if data is None: return post_request_empty() try: status = extra_funds_service.update_extra_fund_by_id( extra_fund_id, data) if status: current_app.logger.info( "Success update extra_funds for extra_funds_id: %s", extra_fund_id) return get_extra_funds_by_id(extra_fund_id) else: current_app.logger.error( "Fail update extra_funds for extra_funds_id: %s", extra_fund_id) return custom_error( "Fail to update extra fund id: {}".format(extra_fund_id)) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update extra_funds fail: extra_funds_id: %s", extra_fund_id) return jsonify({"errors": [error.__dict__]}), 400
def update_donation_status(donation_service: DonationService, donation_id: int): """ update donation by ID :param donation_id: :return: """ data = request.get_json() status = data.get("status") if data is None or status is None: return post_request_empty() if status not in ["paid", "pending"]: return custom_error("Status should be paid or pending".format(status)) donation = donation_service.get_donation_by_id(donation_id) if not donation: return custom_error("No donation record for requested id: {}".format(donation_id)) try: success = donation_service.update_donation_status_by_id(donation_id, status) if success: current_app.logger.info("Success update donation for donation_id: {}".format(donation_id)) return get_donation_by_id(donation_id) else: current_app.logger.error("Fail update donation for donation_id: {}".format(donation_id)) return custom_error("Fail to update donation id: {}".format(donation_id)) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.exception("Update donation fail: donation_id: {}".format(donation_id)) return jsonify({"errors": [error.__dict__]}), 400
def delete_s3_file(): """ delete S3 file """ data = request.get_json() url = data.get("url") student_id = data.get("student_id") if not url or not student_id: current_app.logger.error("Empty url or empty student id") return post_request_empty() try: if int(student_id) not in StudentService.get_all_student_ids(): raise ThingahaCustomError("Invalid student ID") result = student_service.delete_file( url) and student_service.update_photo_path_by_id(student_id, "") if result: current_app.logger.info("Delete file for URL %s success", url) return "", 200 else: current_app.logger.error("Delete file for URL %s fail", url) return "", 400 except TypeError: current_app.logger.error("Student id must be integer") return custom_error("Student id must be integer")
def create_transfers(): """ create transfer data :return: """ data = request.get_json() if data is None: return post_request_empty() try: current_app.logger.info("Create transfer record") transfer_id = transfer_service.create_transfer({ "year": data.get("year"), "month": data.get("month"), "total_mmk": data.get("total_mmk"), "total_jpy": data.get("total_jpy") }) current_app.logger.info("Create transfer success. transfer %s", data.get("month")) return get_transfer_by_id(transfer_id) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: return jsonify({"errors": {"error": error.__dict__}}), 400
def change_password(): """ change password by userid """ data = request.get_json() user_id = get_jwt_identity() if data is None or user_id is None: return post_request_empty() try: current_pwd = data.get("current_password") new_pwd = data.get("new_password") new_confirm_pwd = data.get("new_confirm_password") user_data = user_service.get_user_by_id(user_id) user = user_service.get_user_by_email(user_data["email"]) if not user_service.check_password(current_pwd, user): return custom_error("Current password is incorrect.") if new_pwd == new_confirm_pwd: current_app.logger.info("All fields are valid!") password_update_status = user_service.change_password_by_id( user_id, new_pwd) return jsonify({"status": password_update_status}), 200 else: return custom_error("Two fields of new password does not match.") except SQLCustomError as error: current_app.logger.error("Fail to change user password for user id:%s", user_id) return jsonify({"errors": [error.__dict__]}), 400
def update_donation(donation_id: int): """ update donation by ID :param donation_id: :return: """ data = request.get_json() if data is None: return post_request_empty() donation = donation_service.get_donation_by_id(donation_id) if not donation: return custom_error("No donation record for requested id: {}".format(donation_id)) try: status = donation_service.update_donation_by_id(donation_id, data) if status: current_app.logger.info("Success update donation for donation_id: %s", donation_id) return get_donation_by_id(donation_id) else: current_app.logger.error("Fail update donation for donation_id: %s", donation_id) return custom_error("Fail to update donation id: {}".format(donation_id)) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update donation fail: donation_id: %s", donation_id) return jsonify({"errors": [error.__dict__]}), 400
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
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
def update_file(): """ update s3 file, delete file first and upload new files """ old_url = request.form["old_url"] if not old_url: current_app.logger.error("Old url for student required") return post_request_empty() if not student_service.delete_file(old_url): current_app.logger.error("Can't delete file before update") return custom_error("Update file error") return upload_s3_file()
def reset_password(): """ reset password by full admin """ data = request.get_json() if data is None: return post_request_empty() try: return jsonify({"status": user_service.reset_password(data)}), 200 except (SQLCustomError, ValidateFail) as error: current_app.logger.error("Fail to reset user password for user id:%s", data.get("user_id")) return jsonify({"errors": [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 change_password(): """ change password by userid """ data = request.get_json() user_id = get_jwt_identity() if data is None or user_id is None: return post_request_empty() try: return jsonify({"status": user_service.change_password(user_id, data)}), 200 except (SQLCustomError, ValidateFail, ThingahaCustomError) as error: current_app.logger.error("Fail to change user password for user id:%s", user_id) return jsonify({"errors": [error.__dict__]}), 400
def delete_s3_file(): """ delete S3 file """ data = request.get_json() url = data.get("url") if not url: current_app.logger.error("Empty url") return post_request_empty() result = student_service.delete_file(url) if result: current_app.logger.info("Delete file for URL %s success", url) return "", 200 else: current_app.logger.error("Delete file for URL %s fail", url) return "", 400
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
def update_donation(donation_id: int): """ update donation by ID :param donation_id: :return: """ data = request.get_json() if data is None: return post_request_empty() try: current_app.logger.info("Update donation for donation_id: %s", donation_id) return jsonify({ "status": donation_service.update_donation_by_id(donation_id, data) }), 200 except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update donation fail: donation_id: %s", donation_id) return jsonify({"errors": {"error": error.__dict__}}), 400
def update_address(address_id: int): """ update address data :param address_id: :return: """ data = request.get_json() if data is None: return post_request_empty() try: current_app.logger.info("Update address for address_id: %s", address_id) return jsonify({ "status": address_service.update_address_by_id(address_id, data) }), 200 except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update address fail: address_id: %s", address_id) 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
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
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 upload_s3_file(): """ Upload a file to an S3 bucket :return: True if file was uploaded, else False """ img = request.files["img"] student_id = request.form.get("student_id") if student_id is None or img is None or img.filename == "": return post_request_empty() file_extension = allowed_file(img.filename) if not file_extension: return custom_error("File extension should be .png or .jpg or .jpeg") file_name = student_id + "." + file_extension result = student_service.upload_file(img, file_name) if result: return jsonify({"url": result}), 200 else: return "", 400
def update_attendance(attendance_service: AttendanceService, attendance_id: int): """ update attendance by ID :param attendance_id: :param attendance_service: :return: """ data = request.get_json() if data is None: return post_request_empty() try: status = attendance_service.update_attendance_by_id( attendance_id, { "student_id": int(data.get("student_id")), "school_id": int(data.get("school_id")), "grade": data.get("grade"), "year": int(data.get("year")), "enrolled_date": attendance_service.thingaha_helper.standardize_str_to_date( data.get("enrolled_date")) }) if status: current_app.logger.info( "Success update attendance for attendance_id: %s", attendance_id) return get_attendance_by_id(attendance_id) else: current_app.logger.error( "Fail update attendance for attendance_id: %s", attendance_id) return custom_error("Fail to update attendance id : %", attendance_id) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update attendance fail: attendance_id: %s", attendance_id) return jsonify({"errors": [error.__dict__]}), 400