Skip to content

Commit

Permalink
Tenants in common order groups by sequence number (#1959)
Browse files Browse the repository at this point in the history
* Tenants in common order groups by sequence number

Signed-off-by: Doug Lovett <doug@diamante.ca>

* PPR API restore account when removed and belongs to account.

Signed-off-by: Doug Lovett <doug@diamante.ca>

---------

Signed-off-by: Doug Lovett <doug@diamante.ca>
  • Loading branch information
doug-lovett authored Jun 29, 2024
1 parent f981599 commit bdd4640
Show file tree
Hide file tree
Showing 28 changed files with 324 additions and 133 deletions.
4 changes: 4 additions & 0 deletions mhr_api/src/mhr_api/models/batch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ def set_batch_json_owners(reg_json: dict, current_json: dict, doc_type: str, reg
del reg_json['addOwnerGroups']
elif not reg_json.get('ownerGroups') and current_json:
reg_json['ownerGroups'] = current_json.get('ownerGroups')
if reg_json.get('ownerGroups'):
for group in reg_json.get('ownerGroups'):
if group.get('groupSequenceNumber'):
del group['groupSequenceNumber']
return reg_json


Expand Down
2 changes: 2 additions & 0 deletions mhr_api/src/mhr_api/models/db2/descript.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ def registration_json(self):
description['engineerDate'] = model_utils.format_local_date(self.engineer_date)
if self.circa == '?':
description['baseInformation']['circa'] = True
else:
description['baseInformation']['circa'] = False
return description

@staticmethod
Expand Down
5 changes: 4 additions & 1 deletion mhr_api/src/mhr_api/models/db2/manuhome.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def json(self): # pylint: disable=too-many-branches, too-many-statements
elif group.status not in (Db2Owngroup.StatusTypes.PREVIOUS, Db2Owngroup.StatusTypes.DRAFT):
existing_count += 1
man_home['addOwnerGroups'] = legacy_reg_utils.update_group_type(add_groups, existing_count)
man_home['addOwnerGroups'] = legacy_reg_utils.sort_owner_groups(man_home['addOwnerGroups'])
man_home['deleteOwnerGroups'] = delete_groups
else:
if self.reg_owner_groups:
Expand All @@ -491,6 +492,7 @@ def json(self): # pylint: disable=too-many-branches, too-many-statements
owner['ownerId'] = owner_id
groups.append(group_json)
man_home['ownerGroups'] = legacy_reg_utils.update_group_type(groups, 0)
man_home['ownerGroups'] = legacy_reg_utils.sort_owner_groups(man_home['ownerGroups'])
if self.reg_location:
man_home['location'] = self.reg_location.registration_json
if self.reg_descript:
Expand All @@ -501,7 +503,6 @@ def json(self): # pylint: disable=too-many-branches, too-many-statements
notes.append(note.registration_json)
# Now sort in descending timestamp order.
man_home['notes'] = legacy_reg_utils.sort_notes(notes)

return man_home

@property
Expand Down Expand Up @@ -545,6 +546,7 @@ def registration_json(self): # pylint: disable=too-many-branches
owner['ownerId'] = owner_id
groups.append(group_json)
man_home['ownerGroups'] = legacy_reg_utils.update_group_type(groups, 0)
man_home['ownerGroups'] = legacy_reg_utils.sort_owner_groups(man_home['ownerGroups'])
if self.reg_location:
man_home['location'] = self.reg_location.registration_json
if self.reg_descript:
Expand Down Expand Up @@ -605,6 +607,7 @@ def new_registration_json(self): # pylint: disable=too-many-branches
owner['ownerId'] = owner_id
groups.append(group_json)
man_home['ownerGroups'] = legacy_reg_utils.update_group_type(groups, 0)
man_home['ownerGroups'] = legacy_reg_utils.sort_owner_groups(man_home['ownerGroups'])
if self.reg_location:
man_home['location'] = self.reg_location.new_registration_json
if self.reg_descript:
Expand Down
1 change: 1 addition & 0 deletions mhr_api/src/mhr_api/models/db2/owngroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ def registration_json(self):
self.strip()
group = {
'groupId': self.group_id,
'groupSequenceNumber': self.sequence_number,
'type': LEGACY_TENANCY_NEW.get(self.tenancy_type),
'status': LEGACY_STATUS_NEW.get(self.status),
'interest': self.interest,
Expand Down
59 changes: 51 additions & 8 deletions mhr_api/src/mhr_api/models/db2/registration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ def update_description(registration,
return manuhome


def get_group_sequence_num(manuhome, add_count: int, group_id: int) -> int:
"""Derive the group sequence number from the group id of an existing group with a default of 1."""
sequence_num: int = add_count
if not group_id:
return sequence_num
for group in manuhome.reg_owner_groups:
if group.group_id == group_id:
return group.sequence_number
return sequence_num


def update_owner_groups(registration, manuhome, reg_json: dict):
"""Update owner groups for transfer and correction/amendment registration owner changes."""
# Update owner groups: group ID increments with each change.
Expand All @@ -516,10 +527,14 @@ def update_owner_groups(registration, manuhome, reg_json: dict):
group = Db2Owngroup.create_from_registration(registration, new_group, group_id, group_id)
group.modified = True
group_id += 1
if not group.interest or not group.interest_numerator or group.interest_numerator == 0:
group.sequence_number = 1
else:
group.sequence_number = new_group.get('groupId')
manuhome.reg_owner_groups.append(group)
adjust_group_interest(manuhome.reg_owner_groups, False)
registration.id = reg_id
set_owner_sequence_num(manuhome.reg_owner_groups)
# set_owner_sequence_num(manuhome.reg_owner_groups)


def set_transfer_group_json(registration, reg_json) -> dict:
Expand Down Expand Up @@ -559,26 +574,54 @@ def set_active_groups_json(registration, reg_json: dict) -> dict:
return reg_json
groups = []
existing_count: int = 0
group_id: int = 1
# group_id: int = 1
new_reg_doc_id: str = reg_json.get('documentId')
for group in registration.manuhome.reg_owner_groups:
if group.status in (Db2Owngroup.StatusTypes.ACTIVE, Db2Owngroup.StatusTypes.EXEMPT):
if group.reg_document_id != new_reg_doc_id:
existing_count += 1
group_json = group.registration_json
group_json['groupId'] = group_id
group_id += 1
# group_json['groupId'] = group_id
# group_id += 1
group_json['existing'] = True
groups.append(group_json)
elif not reg_json.get('addOwnerGroups'):
group_json = group.registration_json
group_json['groupId'] = group_id
group_id += 1
# group_json['groupId'] = group_id
# group_id += 1
groups.append(group_json)
if reg_json.get('addOwnerGroups'):
for group in reg_json.get('addOwnerGroups'):
group['groupId'] = group_id
group_id += 1
# group['groupId'] = group_id
# group_id += 1
groups.append(group)
reg_json['ownerGroups'] = update_group_type(groups, existing_count)
reg_json['ownerGroups'] = sort_owner_groups(reg_json['ownerGroups'])
return reg_json


def sort_key_groups_sequence(item):
"""Sort the tenants in common owner groups by group sequence number."""
return item.get('groupSequenceNumber', 1)


def sort_common_owner_groups(owner_groups):
"""Sort tenants in common owner groups by group sequence number."""
owner_groups.sort(key=sort_key_groups_sequence)
return owner_groups


def sort_owner_groups(owner_groups: dict, cleanup: bool = False) -> dict:
"""Sort tenants in common owner groups by group sequence number, remove groupSequenceNumber."""
if not owner_groups:
return owner_groups
if len(owner_groups) == 1:
if cleanup and owner_groups[0].get('groupSequenceNumber'):
del owner_groups[0]['groupSequenceNumber']
return owner_groups
sorted_groups = sort_common_owner_groups(owner_groups)
if cleanup:
for group in sorted_groups:
if group.get('groupSequenceNumber'):
del group['groupSequenceNumber']
return sorted_groups
2 changes: 2 additions & 0 deletions mhr_api/src/mhr_api/models/mhr_owner_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MhrOwnerGroup(db.Model): # pylint: disable=too-many-instance-attributes
interest_numerator = db.mapped_column('interest_numerator', db.Integer, nullable=False)
interest_denominator = db.mapped_column('interest_denominator', db.Integer, nullable=False)
tenancy_specified = db.mapped_column('tenancy_specified', db.String(1), nullable=False)
group_sequence_number = db.mapped_column('group_sequence_number', db.Integer, nullable=True)

# parent keys
registration_id = db.mapped_column('registration_id', db.Integer, db.ForeignKey('mhr_registrations.id'),
Expand All @@ -57,6 +58,7 @@ def json(self) -> dict:
"""Return the owner group as a json object."""
group = {
'groupId': self.group_id,
'groupSequenceNumber': self.group_sequence_number if self.group_sequence_number else self.group_id,
'type': self.tenancy_type,
'status': self.status_type,
'tenancySpecified': True
Expand Down
16 changes: 10 additions & 6 deletions mhr_api/src/mhr_api/models/mhr_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def json(self) -> dict:
reg_json = reg_json_utils.set_description_json(self, reg_json, False, doc_json.get('documentType'))
# Set owner groups for all registration types.
if self.registration_type in (MhrRegistrationTypes.MHREG, MhrRegistrationTypes.MHREG_CONVERSION):
reg_json = reg_json_utils.set_group_json(self, reg_json, False)
reg_json = reg_json_utils.set_group_json(self, reg_json, False, True)
else:
reg_json = reg_json_utils.set_transfer_group_json(self, reg_json, doc_json.get('documentType'))
if self.registration_type == MhrRegistrationTypes.TRANS and \
Expand Down Expand Up @@ -204,7 +204,7 @@ def registration_json(self) -> dict:
reg_json = reg_json_utils.set_submitting_json(self, reg_json)
reg_json = reg_json_utils.set_location_json(self, reg_json, self.current_view)
reg_json = reg_json_utils.set_description_json(self, reg_json, self.current_view)
reg_json = reg_json_utils.set_group_json(self, reg_json, self.current_view)
reg_json = reg_json_utils.set_group_json(self, reg_json, self.current_view, True)
notes = reg_json_utils.get_notes_json(self, True, self.staff)
if notes:
reg_json['notes'] = notes
Expand Down Expand Up @@ -884,6 +884,7 @@ def create_new_groups(self, json_data):
sequence += 1
group: MhrOwnerGroup = MhrOwnerGroup.create_from_json(group_json, self.id)
group.group_id = sequence
group.group_sequence_number = sequence
# Add owners
for owner_json in group_json.get('owners'):
party_type = owner_json.get('partyType', None)
Expand All @@ -906,13 +907,16 @@ def add_new_groups(self, json_data, existing_count: int):
# Update owner groups: group ID increments with each change.
group_id: int = existing_count + 1
if json_data.get('addOwnerGroups'):
for group_json in json_data.get('addOwnerGroups'):
for new_json in json_data.get('addOwnerGroups'):
current_app.logger.info(f'Creating owner group id={group_id}')
new_group: MhrOwnerGroup = MhrOwnerGroup.create_from_change_json(group_json, self.id, self.id,
group_id)
new_group: MhrOwnerGroup = MhrOwnerGroup.create_from_change_json(new_json, self.id, self.id, group_id)
group_id += 1
if not new_group.interest or not new_group.interest_numerator or new_group.interest_numerator == 0:
new_group.group_sequence_number = 1
else:
new_group.group_sequence_number = new_json.get('groupId')
# Add owners
for owner_json in group_json.get('owners'):
for owner_json in new_json.get('owners'):
party_type = owner_json.get('partyType', None)
if not party_type and owner_json.get('individualName'):
party_type = MhrPartyTypes.OWNER_IND
Expand Down
6 changes: 3 additions & 3 deletions mhr_api/src/mhr_api/models/registration_history_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""This module holds methods to support a manufactured home registration history model mapping to dict/json."""
from flask import current_app
from mhr_api.models import utils as model_utils
from mhr_api.models.registration_json_utils import set_group_json
from mhr_api.models.registration_json_utils import set_group_json, sort_owner_groups
from mhr_api.models.registration_utils import find_cancelled_note, get_document_description
from mhr_api.models.type_tables import (
MhrDocumentTypes,
Expand Down Expand Up @@ -179,7 +179,7 @@ def set_group_extra_json(base_reg: MhrRegistration, reg_id: int, group_id: int,
for group in reg.owner_groups:
group_count += 1
if group_id == group.id:
active_group_id = group_count
active_group_id = group.group_sequence_number
group_type = group.tenancy_type
elif reg.id < reg_id and reg.owner_groups:
for group in reg.owner_groups:
Expand Down Expand Up @@ -431,5 +431,5 @@ def set_change_group_json(registration: MhrRegistration, base_reg: MhrRegistrati
if existing.registration_id != registration.id and existing.change_registration_id == registration.id:
delete_groups.append(existing.json)
reg_json['deleteOwnerGroups'] = delete_groups
reg_json['addOwnerGroups'] = add_groups
reg_json['addOwnerGroups'] = sort_owner_groups(add_groups)
return reg_json
55 changes: 50 additions & 5 deletions mhr_api/src/mhr_api/models/registration_json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ def set_permit_json(registration, reg_json: dict) -> dict: # pylint: disable=to
if reg.documents[0].document_type == MhrDocumentTypes.REG_103:
permit_number = reg.documents[0].document_registration_number
permit_ts = reg.registration_ts
# current_app.logger.debug(f'set_permit # {permit_number}')
# Registrations are in chronological order: get the latest permit, use latest amendment status, expiry.
if reg.documents[0].document_type in (MhrDocumentTypes.REG_103, MhrDocumentTypes.AMEND_PERMIT):
if reg.notes:
permit_status = reg.notes[0].status_type
expiry_ts = reg.notes[0].expiry_date
# current_app.logger.debug(f'set permit reg id {reg.id} set_permit status {permit_status}')
permit_reg_id = reg.id
if permit_number:
reg_json['permitRegistrationNumber'] = permit_number
Expand Down Expand Up @@ -282,7 +284,36 @@ def set_description_json(registration, reg_json, current: bool, doc_type: str =
return reg_json


def set_group_json(registration, reg_json, current: bool) -> dict:
def sort_owner_groups(owner_groups: dict, cleanup: bool = False) -> dict:
"""Sort tenants in common owner groups by group sequence number, remove groupSequenceNumber."""
if not owner_groups:
return owner_groups
if len(owner_groups) == 1:
if cleanup and owner_groups[0].get('groupSequenceNumber'):
del owner_groups[0]['groupSequenceNumber']
return owner_groups
sorted_groups = sort_common_owner_groups(owner_groups)
if cleanup:
for group in sorted_groups:
if group.get('groupSequenceNumber'):
del group['groupSequenceNumber']
return sorted_groups


def cleanup_owner_groups(reg_json: dict) -> dict:
"""Cleanup registration owner groups, removing groupSequenceNumber."""
if reg_json.get('ownerGroups'):
for group in reg_json.get('ownerGroups'):
if group.get('groupSequenceNumber'):
del group['groupSequenceNumber']
if reg_json.get('addOwnerGroups'):
for group in reg_json.get('addOwnerGroups'):
if group.get('groupSequenceNumber'):
del group['groupSequenceNumber']
return reg_json


def set_group_json(registration, reg_json, current: bool, cleanup: bool = False) -> dict:
"""Build the owner group JSON conditional on current."""
owner_groups = []
if registration.owner_groups:
Expand All @@ -298,7 +329,8 @@ def set_group_json(registration, reg_json, current: bool) -> dict:
for group in reg.owner_groups:
if group.status_type in (MhrOwnerStatusTypes.ACTIVE, MhrOwnerStatusTypes.EXEMPT):
owner_groups.append(group.json)
reg_json['ownerGroups'] = owner_groups
# Sort by group sequence number, remove groupSequenceNumber after sorting.
reg_json['ownerGroups'] = sort_owner_groups(owner_groups, cleanup)
return reg_json


Expand Down Expand Up @@ -327,7 +359,9 @@ def set_transfer_group_json(registration, reg_json, doc_type: str) -> dict:
reg_json['deleteOwnerGroups'] = delete_groups
if not delete_groups and not add_groups and model_utils.is_legacy(): # Legacy MH home
current_app.logger.debug(f'Transfer legacy MHR {registration.mhr_number} using legacy owner groups.')
return legacy_reg_utils.set_transfer_group_json(registration, reg_json)
reg_json = legacy_reg_utils.set_transfer_group_json(registration, reg_json)
if reg_json.get('addOwnerGroups'):
reg_json['addOwnerGroups'] = sort_owner_groups(reg_json.get('addOwnerGroups'), False)
return reg_json


Expand Down Expand Up @@ -390,6 +424,17 @@ def sort_notes(notes):
return notes


def sort_key_groups_sequence(item):
"""Sort the tenants in common owner groups by group sequence number."""
return item.get('groupSequenceNumber', 1)


def sort_common_owner_groups(owner_groups):
"""Sort tenants in common owner groups by group sequence number."""
owner_groups.sort(key=sort_key_groups_sequence)
return owner_groups


def get_notes_json(registration, search: bool, staff: bool = False) -> dict: # pylint: disable=too-many-branches; 13
"""Fetch all the unit notes for the manufactured home. Search has special conditions on what is included."""
notes = []
Expand Down Expand Up @@ -579,9 +624,9 @@ def set_reg_groups_json(current_reg, # pylint: disable=too-many-branches
if group.status_type == MhrOwnerStatusTypes.PREVIOUS and group.change_registration_id == reg_id:
delete_groups.append(group.json)
if groups and transfer:
reg_json['addOwnerGroups'] = groups
reg_json['addOwnerGroups'] = sort_owner_groups(groups)
elif groups:
reg_json['ownerGroups'] = groups
reg_json['ownerGroups'] = sort_owner_groups(groups)
if delete_groups and transfer:
reg_json['deleteOwnerGroups'] = delete_groups
return reg_json
Expand Down
Loading

0 comments on commit bdd4640

Please sign in to comment.