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 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 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 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 login(): if not request.is_json: return custom_error("Missing JSON in request") email = request.json.get("email", None) password = request.json.get("password", None) if not email: return custom_error("Missing email parameter") if not password: return custom_error("Missing password parameter") user = user_service.get_user_by_email(email) if not user: return custom_error( "Requested {} is not a registered member".format(email)) if user_service.check_password(password, user): access_token = create_access_token(identity=email, expires_delta=timedelta(days=1)) return jsonify({ "data": { "access_token": access_token, "user": { "id": user.id, "email": user.email, "username": user.name } } }), 200 return custom_error("Bad username or password", 401)
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_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_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 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 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 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 update_donation(donation_service: DonationService, 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, { "user_id": data.get("user_id"), "attendance_id": data.get("attendance_id"), "transfer_id": data.get("transfer_id"), "month": data.get("month"), "year": data.get("year"), "mmk_amount": float(data.get("mmk_amount")), "jpy_amount": float(data.get("jpy_amount")), "paid_at": data.get("paid_at") or donation.get("paid_at") }) 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.exception("Update donation fail: donation_id: %s", donation_id) 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
def upload_s3_file(): """ Upload a file to an S3 bucket :return: True if file was uploaded, else False """ img = request.files.get("img") student_id = request.form.get("student_id") try: if student_id and int( student_id) not in StudentService.get_all_student_ids(): raise ThingahaCustomError("Invalid student ID") if student_id is None or not img or img.filename == "": return post_request_empty() file_extension = student_service.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: url = get_s3_url().format(S3_BUCKET, file_name) if student_service.update_photo_path_by_id(student_id, url): return get_student_by_id(student_id), 200 else: current_app.logger.error( "Can't update student photo url for student id: {}".format( student_id)) return "", 400 except ThingahaCustomError as error: current_app.logger.error("Error for student photo upload {}".format( error.__dict__)) return jsonify({"errors": [error.__dict__]}), 400 except (ValueError, TypeError): current_app.logger.error( "Value error for student photo upload error: {}".format( traceback.format_exc())) return jsonify({ "errors": [ThingahaCustomError("Student ID must be integer").__dict__] }), 400
def update_transfers(transfer_id: int): """ update transfer data :param transfer_id: :return: """ data = request.get_json() if data is None: return post_request_empty() try: status = transfer_service.update_transfer_by_id(transfer_id, data) if status: current_app.logger.info("Success update for transfer_id: %s", transfer_id) return get_transfer_by_id(transfer_id) else: current_app.logger.error("Fail update for transfer_id: %s", transfer_id) return custom_error("Update Fail for transfer_id: %s", transfer_id) except (SQLCustomError, ValidateFail, RequestDataEmpty) as error: current_app.logger.error("Update transfer fail: transfer_id: %s", transfer_id) return jsonify({"errors": [error.__dict__]}), 400
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