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 __get_all_address_records(addresses: Pagination) -> Dict: """ get all address records without types :params addresses """ schools_address_ids = [ address.id for address in addresses.items if address.type == "school" ] users_address_ids = [ address.id for address in addresses.items if address.type == "user" ] students_address_ids = [ address.id for address in addresses.items if address.type == "student" ] return { "school": SchoolService.get_schools_by_address_ids( tuple(schools_address_ids)), "user": UserService.get_user_by_address_ids(tuple(users_address_ids)), "student": StudentService.get_students_by_address_ids( tuple(students_address_ids)) }
def __return_address_types( page: int, per_page: int, address_type: str) -> Optional[Tuple[Pagination, Dict]]: """ return address by requested types :params page :params :per_page """ if address_type not in ["school", "user", "student"]: return None, None else: addresses = AddressModel.get_all_addresses_by_type( page, per_page, address_type) address_ids = tuple([address.id for address in addresses.items]) if address_type == "school": return addresses, { address_type: SchoolService.get_schools_by_address_ids(address_ids) } if address_type == "user": return addresses, { address_type: UserService.get_user_by_address_ids(address_ids) } if address_type == "student": return addresses, { address_type: StudentService.get_students_by_address_ids(address_ids) }
def get_all_addresses(self) -> List[Dict[str, Any]]: """ get all addresses :return: """ self.logger.info("Get all addresses list") try: return SchoolService.get_all_school_address() + UserService.get_all_user_address() + \ StudentService.get_all_student_address() except SQLAlchemyError: self.logger.error("Get all addresses fail. error %s", traceback.format_exc()) raise SQLCustomError(description="GET address SQL ERROR")
def get_all_addresses(self, page: int = 1) -> (List[Dict[str, Any]], int): """ get all addresses :params page :return: """ self.logger.info("Get all addresses list") try: schools, schools_count = SchoolService.get_all_school_address(page) users, users_count = UserService.get_all_user_address(page) student, students_count = StudentService.get_all_student_address(page) return schools + users + student, schools_count+users_count+students_count except SQLAlchemyError: self.logger.error("Get all addresses fail. error %s", traceback.format_exc()) raise SQLCustomError(description="GET address SQL ERROR")
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
"""API route for Student API""" import traceback from datetime import datetime from flask import request, current_app, jsonify from flask_cors import cross_origin from flask_jwt_extended import jwt_required from common.aws_client import get_s3_url from common.config import S3_BUCKET from common.error import SQLCustomError, RequestDataEmpty, ValidateFail, ThingahaCustomError from controller.api import address_service from controller.api import api, post_request_empty, custom_error, sub_admin, full_admin, get_default_address from service.student.student_service import StudentService student_service = StudentService() @api.route("/students", methods=["GET"]) @jwt_required @cross_origin() def get_students(): try: page = request.args.get("page", 1, type=int) per_page = request.args.get("per_page", 20, type=int) current_app.logger.info("Get all student records") return jsonify( {"data": student_service.get_all_students(page, per_page)}), 200 except SQLCustomError as error: current_app.logger.error("Error in get all student records") return jsonify({"errors": [error.__dict__]}), 400