Skip to content

Commit

Permalink
address code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwang-gsa committed Aug 22, 2023
1 parent 2023b0b commit a66ab42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion training-front-end/src/components/LoginlessFlow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
<h2 class="usa-prose">
Check your email
</h2>
<p>We sent you an email at <b>{{ user_input.email }}</b> with a link to access {{ linkDestinationText }}. This link is only active for 24 hours.</p>
<p>We sent you an email at <b>{{ user_input.email }}</b> with a link to access {{ linkDestinationText }}. This link is only active for 24 hours. If you have not received the email within 15 minutes, please check your spam folder.</p>

<p>Not the right email? <a href=".">Send another email</a></p>
</div>
Expand Down
53 changes: 26 additions & 27 deletions training/repositories/agency.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

from sqlalchemy.orm import Session
from training import models, schemas
from training.schemas.agency import AgencyWithBureaus, Bureau
from .base import BaseRepository
from sqlalchemy.sql.expression import collate
from sqlalchemy.sql.expression import collate, case
from itertools import groupby


class AgencyRepository(BaseRepository[models.Agency]):
Expand All @@ -24,35 +26,32 @@ def find_by_name(self, agency: schemas.AgencyCreate) -> models.Agency | None:
return self._session.query(models.Agency).filter(models.Agency.name == agency.name, models.Agency.bureau == agency.bureau).first()

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
'''
get agencies_with_bureaus return a list of parent agencies with bureaus list, if parent agency doesn't have bureaus, its bureaus list =[].
parent agencies are those db records with bureau value is null
UI want to sort agency alphabetically but needs 'Other' option to be displayed at the end. Both parent agency and bureau has 'Other' option.
serveral sort order are combined and is put in a spefic order to achieve this.
groupby is used to group same agency together
'''

# set 'Other' order rule for both agency and bureau
agency_other_order = case((models.Agency.name == "Other", 1), else_=0)
bureau_other_order = case((models.Agency.bureau == "Other", 2), (models.Agency.bureau==None, 0), else_=1) # noqa E711

db_results = self._session.query(models.Agency).order_by(
agency_other_order, # agency named Other should be at the bottom of parent agencies list
collate(models.Agency.name, 'C'), # alphebetic order on agency's name
bureau_other_order, # bureau named 'Other' should be at the bollom of the bureau list
collate(models.Agency.bureau, 'C') # alphbetic order on bureau
).all()

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]
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)
# 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)
for _, group in groupby(db_results, lambda row: row.name):
parent = next(group) # Parent is first in the group

transform_angecies.append({
'id': record.id,
'name': record.name,
'bureaus': bureaus
'id': parent.id,
'name': parent.name,
'bureaus': [{"id": b.id, "name": b.bureau} for b in group]
})
return transform_angecies

0 comments on commit a66ab42

Please sign in to comment.