From 34a80f932210d7fece7097ba4580e81b3fb075ad Mon Sep 17 00:00:00 2001 From: weiwang-gsa Date: Thu, 17 Aug 2023 13:39:13 -0400 Subject: [PATCH 1/5] address code review comments --- training/api/api_v1/users.py | 32 +++++++++++--------------- training/repositories/user.py | 10 ++++---- training/tests/test_user_repository.py | 8 +------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/training/api/api_v1/users.py b/training/api/api_v1/users.py index db55b167..f955d374 100644 --- a/training/api/api_v1/users.py +++ b/training/api/api_v1/users.py @@ -2,12 +2,12 @@ from io import StringIO import logging from training.api.auth import RequireRole -from fastapi import APIRouter, status, HTTPException, Response, Depends +from fastapi import APIRouter, status, HTTPException, Response, Depends, Query from training.schemas import User, UserCreate, UserSearchResult from training.repositories import UserRepository from training.api.deps import user_repository from training.api.auth import user_from_form - +from typing import Annotated router = APIRouter() @@ -70,24 +70,18 @@ def download_report_csv(user=Depends(user_from_form), repo: UserRepository = Dep return Response(output.getvalue(), headers=headers, media_type='application/csv') -''' -Get/users is used to search users for admin portal, currently only support search by user name, it may have additional search criteira in future. -page_number param is used to support UI pagination functionality. -It returns UserSearchResult object with a list of users and total_count returned. -''' - - @router.get("/users", response_model=UserSearchResult) def get_users( - name: str | None = None, + name: Annotated[str, Query(min_length=1)], page_number: int = 1, - repo: UserRepository = Depends(user_repository), - user=Depends(RequireRole(["Admin"])) + repo: UserRepository = Depends(user_repository)#, + #user=Depends(RequireRole(["Admin"])) ): - try: - return repo.get_users(name, page_number) - except ValueError: - raise HTTPException( - status_code=status.HTTP_400_BAD_REQUEST, - detail="invalid search criteria" - ) + ''' + Get/users is used to search users for admin portal + currently search only support search by user name, name is required field. + It may have additional search criteira in future, which will require logic update. + page_number param is used to support UI pagination functionality. + It returns UserSearchResult object with a list of users and total_count used for UI pagination + ''' + return repo.get_users(name, page_number) diff --git a/training/repositories/user.py b/training/repositories/user.py index 1ae5ab06..aa9e325c 100644 --- a/training/repositories/user.py +++ b/training/repositories/user.py @@ -18,9 +18,9 @@ def find_by_email(self, email: str) -> models.User | None: def find_by_agency(self, agency_id: int) -> list[models.User]: return self._session.query(models.User).filter(models.User.agency_id == agency_id).all() - - # edit_user_for_reporting is function allow admin to modify specific user and assign report role and associate report agencies to the user + def edit_user_for_reporting(self, user_id: int, report_agencies_list: list[int]) -> models.User: + # edit_user_for_reporting allow admin to assign report role and associate report agencies to specific user db_user = self._session.query(models.User).filter(models.User.id == user_id).first() if db_user is None: raise ValueError("invalid user id") @@ -37,7 +37,7 @@ def edit_user_for_reporting(self, user_id: int, report_agencies_list: list[int]) self._session.commit() db_user.roles.append(role) else: - # if report_agencies_list =[], it means remove all user associated agencies and thus remove user report role. + # if report_agencies_list =[], it will remove all user associated agencies and thus remove user report role. if len(report_role_exist) > 0: db_user.roles = [obj for obj in db_user.roles if obj.name != "Report"] db_user.report_agencies.clear() @@ -67,7 +67,7 @@ def get_user_quiz_completion_report(self, report_user_id: int) -> list[UserQuizC raise ValueError("Invalid Report User") def get_users(self, name: str, page_number: int) -> UserSearchResult: - # currently UI only support search by name, future may provide more search criteria + # current UI only support search by user name, and it is required field. if (name and name.strip() != '' and page_number > 0): count = self._session.query(models.User).filter(models.User.name.ilike(f"%{name}%")).count() page_size = 25 @@ -75,5 +75,3 @@ def get_users(self, name: str, page_number: int) -> UserSearchResult: search_results = self._session.query(models.User).filter(models.User.name.ilike(f"%{name}%")).limit(page_size).offset(offset).all() user_search_result = UserSearchResult(users=search_results, total_count=count) return user_search_result - else: - raise ValueError("Invalid search criteria") diff --git a/training/tests/test_user_repository.py b/training/tests/test_user_repository.py index 6e1a114e..ca762513 100644 --- a/training/tests/test_user_repository.py +++ b/training/tests/test_user_repository.py @@ -106,10 +106,4 @@ def test_get_users(user_repo_with_data: UserRepository, valid_user_ids: List[int result = user_repo_with_data.get_users(search_criteria, 1) assert result is not None for item in result.users: - assert search_criteria in item.name - - -def test_invalid_get_users(user_repo_with_data: UserRepository): - search_criteria = None - with pytest.raises(Exception): - user_repo_with_data.get_users(search_criteria, 0) + assert search_criteria in item.name \ No newline at end of file From 880656f49ac44e852d78bc04bc262197b3163e03 Mon Sep 17 00:00:00 2001 From: weiwang-gsa Date: Thu, 17 Aug 2023 13:44:12 -0400 Subject: [PATCH 2/5] fix lint error --- training/api/api_v1/users.py | 4 ++-- training/tests/test_user_repository.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/training/api/api_v1/users.py b/training/api/api_v1/users.py index f955d374..e50f40cb 100644 --- a/training/api/api_v1/users.py +++ b/training/api/api_v1/users.py @@ -74,8 +74,8 @@ def download_report_csv(user=Depends(user_from_form), repo: UserRepository = Dep def get_users( name: Annotated[str, Query(min_length=1)], page_number: int = 1, - repo: UserRepository = Depends(user_repository)#, - #user=Depends(RequireRole(["Admin"])) + repo: UserRepository = Depends(user_repository), + user=Depends(RequireRole(["Admin"])) ): ''' Get/users is used to search users for admin portal diff --git a/training/tests/test_user_repository.py b/training/tests/test_user_repository.py index ca762513..b960bce4 100644 --- a/training/tests/test_user_repository.py +++ b/training/tests/test_user_repository.py @@ -106,4 +106,4 @@ def test_get_users(user_repo_with_data: UserRepository, valid_user_ids: List[int result = user_repo_with_data.get_users(search_criteria, 1) assert result is not None for item in result.users: - assert search_criteria in item.name \ No newline at end of file + assert search_criteria in item.name From f497045745a0e043163f6048e80111653521b7ba Mon Sep 17 00:00:00 2001 From: weiwang-gsa Date: Thu, 17 Aug 2023 13:46:25 -0400 Subject: [PATCH 3/5] fix lint error --- training/repositories/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/training/repositories/user.py b/training/repositories/user.py index aa9e325c..05d90960 100644 --- a/training/repositories/user.py +++ b/training/repositories/user.py @@ -18,7 +18,7 @@ def find_by_email(self, email: str) -> models.User | None: def find_by_agency(self, agency_id: int) -> list[models.User]: return self._session.query(models.User).filter(models.User.agency_id == agency_id).all() - + def edit_user_for_reporting(self, user_id: int, report_agencies_list: list[int]) -> models.User: # edit_user_for_reporting allow admin to assign report role and associate report agencies to specific user db_user = self._session.query(models.User).filter(models.User.id == user_id).first() From 8be7de5762dc76d7a9f36574da918bf27c35d471 Mon Sep 17 00:00:00 2001 From: weiwang-gsa Date: Tue, 22 Aug 2023 09:24:35 -0400 Subject: [PATCH 4/5] address final agency list change --- data/seedsdata.yaml | 391 +++++++++++++++++--------------- training/database/seed.py | 2 +- training/repositories/agency.py | 18 +- 3 files changed, 228 insertions(+), 183 deletions(-) diff --git a/data/seedsdata.yaml b/data/seedsdata.yaml index 6587acea..7274d90b 100644 --- a/data/seedsdata.yaml +++ b/data/seedsdata.yaml @@ -1,76 +1,20 @@ agencies: - - name: Advisory Commission on Inter-governmental Relations - bureau: null - - name: Agency for International Development - bureau: null - - name: American Battle Monuments Commission - bureau: null - name: Architect of the Capitol bureau: null - name: Armed Forces Retirement Home bureau: null - - name: Central Intelligence Agency - bureau: null - - name: Commemorative Commissions - bureau: null - - name: Commodity Future Trading Commission - bureau: null - - name: Congress - bureau: null - name: Consumer Financial Protection Bureau bureau: null - name: Consumer Product Safety Commission bureau: null - - name: Corporation for National and Community Service - bureau: null - - name: Corps of Engineers, Civil - bureau: null - - name: Court Services and Offenders Supervision Agency (CSOSA) - bureau: null - - name: Defense Civil Preparedness Agency (Exp. Code) - bureau: null - - name: Defense Nuclear Facilities Safety Board (DNFSB) - bureau: null - - name: U.S. Department of Agriculture - bureau: null - - name: Department of Commerce - bureau: null - - name: Department of Defense (Office of the Secretary of Defense) - bureau: null - - name: Department of Education - bureau: null - - name: Department of Energy - bureau: null - - name: Department of Health and Human Services - bureau: null - - name: Department of Homeland Security - bureau: null - - name: Department of Housing and Urban Development + - name: Corporation for National and Community Service - AmeriCorps bureau: null - - name: Department of Justice + - name: Defense Nuclear Facilities Safety Board bureau: null - - name: Department of Labor - bureau: null - - name: Department of State - bureau: null - - name: Department of the Air Force - bureau: null - - name: Department of the Army - bureau: null - - name: Department of the Interior - bureau: null - - name: Department of the Navy - bureau: null - - name: Department of the Treasury - bureau: null - - name: Department of Transportation - bureau: null - - name: Department of Veterans Affairs + - name: Denali Commission bureau: null - name: District of Columbia bureau: null - - name: Environmental Protection Agency - bureau: null - name: Equal Employment Opportunity Commission bureau: null - name: Executive Office of the President @@ -79,51 +23,43 @@ agencies: bureau: null - name: Farm Credit Administration bureau: null + - name: Farm Credit System Insurance Corporation + bureau: null - name: Federal Communications Commission bureau: null - name: Federal Deposit Insurance Corporation bureau: null - name: Federal Energy Regulatory Commission bureau: null - - name: Federal Home Loan Bank Board + - name: Federal Housing Finance Agency + bureau: null + - name: Federal Housing Finance Agency - OIG bureau: null - name: Federal Labor Relations Authority bureau: null - name: Federal Maritime Commission bureau: null - - name: Federal Mediation and Conciliation Service + - name: Federal Mine Safety and Health Review Commission bureau: null - name: Federal Retirement Thrift Investment Board bureau: null - name: Federal Trade Commission bureau: null - - name: Foreign Claims Settlement Comm. Of the U.S. (Exp. Code) - bureau: null - - name: General Services Administration - bureau: null - - name: Government Publishing Office + - name: Gulf Coast Ecosystem Restoration Council bureau: null - - name: Independent U.S. Government Offices + - name: Inter-American Foundation bureau: null - name: International Trade Commission bureau: null - - name: Judiciary - bureau: null - - name: Legislative, All Other - bureau: null - name: Library of Congress bureau: null - name: Merit Systems Protection Board bureau: null - name: Millennium Challenge Corporation bureau: null - - name: Miscellaneous Commissions - bureau: null - name: National Aeronautics and Space Administration bureau: null - - name: National Archives and Records Administration - bureau: null - - name: National Credit Union Administration + - name: National Capital Planning Commission bureau: null - name: National Endowment for the Arts bureau: null @@ -135,183 +71,209 @@ agencies: bureau: null - name: National Mediation Board bureau: null - - name: National Science Foundation + - name: National Railroad Passenger Corporation - Amtrak bureau: null - - name: National Transportation Safety Board (NTSB) + - name: National Science Foundation bureau: null - - name: Native Nations + - name: National Transportation Safety Board bureau: null - - name: NeighborWorks America + - name: Native American Tribal Governments bureau: null - name: Nuclear Regulatory Commission bureau: null - - name: Nuclear Waste Technical Review Board + - name: Occupational Safety & Health Review Commission + bureau: null + - name: Office of Government Ethics bureau: null - name: Office of Personnel Management bureau: null - - name: Office of Technology and Assessment (Exp. Code) + - name: Peace Corps bureau: null - - name: Other Government Agency + - name: Pension Benefit Guaranty Corporation bureau: null - - name: Peace Corps + - name: Privacy & Civil Liberties Oversight Board bureau: null - - name: Pennsylvania Avenue Development Corporation + - name: Securities and Exchange Commission bureau: null - - name: Pension Benefit Guaranty Corporation + - name: Selective Service System bureau: null - - name: Permitting and Enforcement Federal Inspector for the Alaska Gas Pipeline + - name: Small Business Administration bureau: null - - name: Pretrial Services Agency (PSA) + - name: Smithsonian Institution bureau: null - - name: Railroad Retirement Board + - name: Tennessee Valley Authority bureau: null - - name: Regional Commissions + - name: U.S. Access Board bureau: null - - name: Resolution Trust Corporation (Inactive Eff. 08/30/1996) + - name: U.S. African Development Foundation bureau: null - - name: Securities and Exchange Commission + - name: U.S. Agency for Global Media bureau: null - - name: Selective Service System + - name: U.S. Chemical Safety & Hazard Investigation Board bureau: null - - name: Small Business Administration + - name: U.S. Court of Appeals for Veterans Claims bureau: null - - name: Smithsonian Institution + - name: U.S. Department of Agriculture bureau: null - - name: Social Security Administration + - name: U.S. Department of Commerce bureau: null - - name: State Governors (FTS Only) + - name: U.S. Department of Defense - Defense Logistics Agency bureau: null - - name: Synthetic Fuels Corporation + - name: U.S. Department of Defense - Department of the Air Force bureau: null - - name: Tennessee Valley Authority + - name: U.S. Department of Defense - Department of the Army bureau: null - - name: Tribal Government-Bishop Paiute Tribe + - name: U.S. Department of Defense - Department of the Navy bureau: null - - name: U.S. Government Accountability Office + - name: U.S. Department of Defense - Independent Agencies bureau: null - - name: U.S. International Development Finance Corporation (DFC) + - name: U.S. Department of Education bureau: null - - name: U.S. Office of Special Counsel + - name: U.S. Department of Energy bureau: null - - name: U.S. Trade and Development Agency + - name: U.S. Department of Health and Human Services bureau: null - - name: United States Agency for Global Media + - name: U.S. Department of Homeland Security bureau: null - - name: United States Arms Control and Disarmament Agency + - name: U.S. Department of Homeland Security - OIG bureau: null - - name: United States Information Agency + - name: U.S. Department of Housing and Urban Development bureau: null - - name: United States Postal Service + - name: U.S. Department of Housing and Urban Development - OIG bureau: null - - name: United States Railway Association + - name: U.S. Department of Justice bureau: null - - name: United States Tax Court + - name: U.S. Department of Labor bureau: null - - name: United States Trade Representative + - name: U.S. Department of State bureau: null - - name: US African Development Foundation + - name: U.S. Department of the Interior bureau: null - - name: US Election Assistance Commission (EAC) + - name: U.S. Department of the Treasury bureau: null - - name: Vendor + - name: U.S. Department of Transportation bureau: null - - name: Visitor + - name: U.S. Department of Veterans Affairs bureau: null - - name: Water Resources Council + - name: U.S. Election Assistance Commission bureau: null - - name: Department of Justice + - name: U.S. Environmental Protection Agency + bureau: null + - name: U.S. General Services Administration + bureau: null + - name: U.S. Government Accountability Office + bureau: null + - name: U.S. Government Publishing Office + bureau: null + - name: U.S. House of Representatives + bureau: null + - name: U.S. International Development Finance Corporation + bureau: null + - name: U.S. National Archives and Records Administration + bureau: null + - name: U.S. Senate + bureau: null + - name: U.S. Social Security Administration + bureau: null + - name: United States Postal Service + bureau: null + - name: United States Tax Court + bureau: null + - name: Other + bureau: null + - name: U.S. Department of Justice bureau: Bureau of Alcohol, Tobacco, Firearms and Explosives (ATF) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Drug Enforcement Administration (DEA) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Federal Bureau of Prisons (BOP) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Federal Bureau of Investigation (FBI) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Federal Prison Industries (FPI) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Justice Management Division-Offices, Boards and Divisions (OBD) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Office of Justice Programs (OJP) - - name: Department of Justice + - name: U.S. Department of Justice bureau: U.S. Marshals Service (USMS) - - name: Department of Justice + - name: U.S. Department of Justice bureau: Other - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Alcohol and Tobacco Tax and Trade Bureau - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Bureau of the Fiscal Service - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Bureau of Engraving and Printing - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Community Development Financial Institutions Fund - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Comptroller of the Currency - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Financial Crimes Enforcement Network - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Internal Revenue Service - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Special Inspector General for the Troubled Asset Relief Program - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Special Inspector General for Pandemic Recovery - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Treasury Inspector General for Tax Administration - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: United States Mint - - name: Department of the Treasury + - name: U.S. Department of the Treasury bureau: Other - - name: Department of Education + - name: U.S. Department of Education bureau: Departmental Management - - name: Department of Education + - name: U.S. Department of Education bureau: Disaster Education Recovery - - name: Department of Education + - name: U.S. Department of Education bureau: Institute of Education Sciences - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Career, Technical, and Adult Education - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Elementary and Secondary Education - - name: Department of Education + - name: U.S. Department of Education bureau: Office of English Language Acquisition - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Federal Student Aid - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Innovation and Improvement - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Postsecondary Education - - name: Department of Education + - name: U.S. Department of Education bureau: Office of Special Education and Rehabilitative Services - - name: Department of Education + - name: U.S. Department of Education bureau: Other - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Federal Aviation Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Federal Highway Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Federal Motor Carrier Safety Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Federal Railroad Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Federal Transit Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Great Lakes St. Lawrence Seaway Development Corporation - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Maritime Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: National Highway Traffic Safety Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Office of Inspector General - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Office of the Secretary - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Pipeline and Hazardous Materials Safety Administration - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Transportation Safety Institute - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Volpe National Transportation Systems Center - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Working Capital Fund - - name: Department of Transportation + - name: U.S. Department of Transportation bureau: Other - name: National Science Foundation bureau: Directorate for Biological Sciences (BIO/OAD) @@ -341,35 +303,35 @@ agencies: bureau: Office of the Director (OD) - name: National Science Foundation bureau: Other - - name: General Services Administration + - name: U.S. General Services Administration bureau: Federal Acquisition Service - - name: General Services Administration + - name: U.S. General Services Administration bureau: Public Buildings Service - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Administrative Services - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Civil Rights - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Congressional & Intergovernmental Affairs - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Customer Experience - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of General Counsel - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Government-wide Policy - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Human Resource Management - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Mission Assurance - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of Small and Disadvantaged Business Utilization - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office Strategic Communication - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of the Chief Information Officer (GSA IT) - - name: General Services Administration + - name: U.S. General Services Administration bureau: Office of the Chief Financial Officer - - name: General Services Administration + - name: U.S. General Services Administration bureau: Other - name: U.S. Department of Agriculture bureau: Agriculture Marketing Service @@ -435,8 +397,77 @@ agencies: bureau: The White House Liaison Office - name: U.S. Department of Agriculture bureau: Other - - + - name: U.S. Department of Health and Human Services + bureau: Administration for Children and Families (ACF) + - name: U.S. Department of Health and Human Services + bureau: Administration for Community Living (ACL) + - name: U.S. Department of Health and Human Services + bureau: Agency for Healthcare Research and Quality (AHRQ) + - name: U.S. Department of Health and Human Services + bureau: Agency for Toxic Substances and Disease Registry (ATSDR) + - name: U.S. Department of Health and Human Services + bureau: Assistant Secretary for Preparedness and Response (ASPR) + - name: U.S. Department of Health and Human Services + bureau: Centers for Disease Control and Prevention (CDC) + - name: U.S. Department of Health and Human Services + bureau: Centers for Medicare & Medicaid Services (CMS) + - name: U.S. Department of Health and Human Services + bureau: Health Resources and Services Administration (HRSA) + - name: U.S. Department of Health and Human Services + bureau: Food and Drug Administration (FDA) + - name: U.S. Department of Health and Human Services + bureau: Indian Health Service (IHS) + - name: U.S. Department of Health and Human Services + bureau: National Institutes of Health (NIH) + - name: U.S. Department of Health and Human Services + bureau: Office of the Inspector General (OIG) + - name: U.S. Department of Health and Human Services + bureau: Office of the Secretary (OS) + - name: U.S. Department of Health and Human Services + bureau: Substance Abuse and Mental Health Services Administration (SAMHSA) + - name: U.S. Department of Health and Human Services + bureau: Other + - name: U.S. Department of Veterans Affairs + bureau: Departmental Administration Offices + - name: U.S. Department of Veterans Affairs + bureau: National Cemetery Administration + - name: U.S. Department of Veterans Affairs + bureau: Office of Information Technology + - name: U.S. Department of Veterans Affairs + bureau: Office of Inspector General + - name: U.S. Department of Veterans Affairs + bureau: Veterans Health Administration + - name: U.S. Department of Veterans Affairs + bureau: Veterans Benefits Administration + - name: U.S. Department of Veterans Affairs + bureau: Other + - name: U.S. Department of Commerce + bureau: Bureau of Economic Analysis + - name: U.S. Department of Commerce + bureau: Bureau of Industry and Security + - name: U.S. Department of Commerce + bureau: Economic Development Administration + - name: U.S. Department of Commerce + bureau: FirstNet + - name: U.S. Department of Commerce + bureau: International Trade Administration + - name: U.S. Department of Commerce + bureau: Minority Business Development Agency + - name: U.S. Department of Commerce + bureau: National Institute of Standards and Technology + - name: U.S. Department of Commerce + bureau: National Oceanic and Atmospheric Administration + - name: U.S. Department of Commerce + bureau: National Technical Information Service + - name: U.S. Department of Commerce + bureau: National Telecommunications and Information Administration + - name: U.S. Department of Commerce + bureau: US Census Bureau + - name: U.S. Department of Commerce + bureau: US Patent and Trademark Office + - name: U.S. Department of Commerce + bureau: Other + admins: - email: andrew.lee@gsa.gov name: Andrew Lee diff --git a/training/database/seed.py b/training/database/seed.py index 60e5f909..fc81bcae 100644 --- a/training/database/seed.py +++ b/training/database/seed.py @@ -35,7 +35,7 @@ user_repo = UserRepository(SessionLocal()) for item in data["admins"]: - fas = AgencyCreate(name="General Services Administration", bureau="Federal Acquisition Service") + fas = AgencyCreate(name="U.S. General Services Administration", bureau="Federal Acquisition Service") smartpay_agency = agency_repo.find_by_name(fas) user = UserCreate(name=item['name'], email=item['email'], agency_id=smartpay_agency.id) print("User:", user.name, "Email:", user.email, "Agency_id:", smartpay_agency.id, end=" - ") diff --git a/training/repositories/agency.py b/training/repositories/agency.py index e171b2e8..44b8f425 100644 --- a/training/repositories/agency.py +++ b/training/repositories/agency.py @@ -26,15 +26,29 @@ def find_by_name(self, agency: schemas.AgencyCreate) -> models.Agency | None: def get_agencies_with_bureaus(self) -> list[AgencyWithBureaus]: db_results = self._session.query(models.Agency).order_by(collate(models.Agency.name, 'C')).all() parent_agencies = [record for record in db_results if record.bureau is None] + + # For UI display, sort all parent agency by alphabetical order except for 'Other', 'Other" needs to be placed at the bottom of the list. + other_agency = [record for record in parent_agencies if record.name.lower() == 'other'] + rest_agencies = [record for record in parent_agencies if record.name.lower() != 'other'] + sorted_parent_agencies = rest_agencies + other_agency + transform_angecies = [] - for record in parent_agencies: + for record in sorted_parent_agencies: agency_with_bureaus = [obj for obj in db_results if obj.bureau and obj.name == record.name] bureaus = [] if len(agency_with_bureaus) > 0: sorted_bureaus = sorted(agency_with_bureaus, key=lambda x: x.bureau) + other_bureau = {} for obj in sorted_bureaus: bureau = Bureau(id=obj.id, name=obj.bureau) - bureaus.append(bureau) + # for bureau name that is not 'Other', add them in alphebetic order + if (bureau.name.lower() != 'other'): + bureaus.append(bureau) + else: + other_bureau = bureau + # if agency has "Other" bureau, put it all the way in the end + if (other_bureau.id): + bureaus.append(other_bureau) transform_angecies.append({ 'id': record.id, From 2023b0b3a558ee6078e758c2d7dbbc11f05a2985 Mon Sep 17 00:00:00 2001 From: weiwang-gsa Date: Tue, 22 Aug 2023 09:38:47 -0400 Subject: [PATCH 5/5] address lint error --- training/repositories/agency.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/training/repositories/agency.py b/training/repositories/agency.py index 44b8f425..03440484 100644 --- a/training/repositories/agency.py +++ b/training/repositories/agency.py @@ -26,12 +26,12 @@ def find_by_name(self, agency: schemas.AgencyCreate) -> models.Agency | None: def get_agencies_with_bureaus(self) -> list[AgencyWithBureaus]: db_results = self._session.query(models.Agency).order_by(collate(models.Agency.name, 'C')).all() parent_agencies = [record for record in db_results if record.bureau is None] - + # For UI display, sort all parent agency by alphabetical order except for 'Other', 'Other" needs to be placed at the bottom of the list. other_agency = [record for record in parent_agencies if record.name.lower() == 'other'] rest_agencies = [record for record in parent_agencies if record.name.lower() != 'other'] sorted_parent_agencies = rest_agencies + other_agency - + transform_angecies = [] for record in sorted_parent_agencies: agency_with_bureaus = [obj for obj in db_results if obj.bureau and obj.name == record.name]