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")
"""API route for School API""" from flask import request, current_app, jsonify from flask_cors import cross_origin from flask_jwt_extended import jwt_required from common.error import SQLCustomError, RequestDataEmpty, ValidateFail, ThingahaCustomError from controller.api import api, post_request_empty, address_service, custom_error, full_admin, sub_admin, \ get_default_address from service.school.school_service import SchoolService school_service = SchoolService() @api.route("/schools", methods=["GET"]) @jwt_required @cross_origin() def get_school(): """ get all school from school table :return: """ 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 school records.") return jsonify( {"data": school_service.get_all_schools(page, per_page)}), 200 except SQLCustomError as error: current_app.logger.error("Error in get all school records") return jsonify({"errors": [error.__dict__]}), 400