Skip to content

Commit

Permalink
Default special history insertion (bcgov#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
saravanpa-aot authored Jan 18, 2024
1 parent 9a50bdd commit e83a3fa
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""default special field
Revision ID: 2edbfb8b9c0e
Revises: 17dea08aa1b3
Create Date: 2024-01-17 16:33:49.371022
"""
from alembic import op
from flask import current_app, g
from sqlalchemy.dialects.postgresql.ranges import Range

from api.models import Work, SpecialField, Proponent, Project

# revision identifiers, used by Alembic.
revision = '2edbfb8b9c0e'
down_revision = '17dea08aa1b3'
branch_labels = None
depends_on = None


def upgrade():
if current_app.config.get('TESTING'):
return

entities = [
# the third key start_date_attr is used if they model has a field which can be used as a start date of the special history field
{'model': Work, 'entity_name': 'WORK', 'field_names': ['responsible_epd_id', 'work_lead_id'],
'start_date_attr': 'start_date'},
{'model': Proponent, 'entity_name': 'PROPONENT', 'field_names': ['name']},
{'model': Project, 'entity_name': 'PROJECT', 'field_names': ['name', 'proponent_id']}
]

# the dict wil look like (WORK,54) = ['responsible_epd_id']
special_histories_map = _get_special_history_map()
upper_limit = None
g.jwt_oidc_token_info = {"email": 'system'}

for entity_info in entities:
entity_model = entity_info['model']
field_names = entity_info['field_names']
start_date_attr = entity_info.get('start_date_attr', '')

for entity in entity_model.find_all():
for field_name in field_names:
special_field_entity = entity_info.get('entity_name')
key = (special_field_entity, entity.id)
history_exists = key in special_histories_map and field_name in special_histories_map[key]

if not history_exists:
special_field_value = getattr(entity, field_name, None)
start_date = getattr(entity, start_date_attr, current_app.config.get('MIN_WORK_START_DATE'))
time_range = Range(
start_date, upper_limit, bounds="[)"
)
if special_field_value:
special_history = SpecialField(
entity=special_field_entity,
entity_id=entity.id,
field_name=field_name,
field_value=special_field_value,
time_range=time_range
)
special_history.save()


def downgrade():
# Delete records from SpecialField where created_by is 'system'
op.execute("DELETE FROM special_fields WHERE created_by = 'system'")
pass


def _get_special_history_map():
special_histories = SpecialField.find_all()
special_histories_map = {}

for special_history in special_histories:
key = (special_history.entity.name, special_history.entity_id)
if key not in special_histories_map:
special_histories_map[key] = [special_history.field_name]
else:
special_histories_map[key].append(special_history.field_name)

return special_histories_map
4 changes: 3 additions & 1 deletion epictrack-api/src/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class _Config(): # pylint: disable=too-few-public-methods
CACHE_TYPE = constants.CACHE_TYPE
CACHE_DEFAULT_TIMEOUT = constants.CACHE_DEFAULT_TIMEOUT

MIN_WORK_START_DATE = _get_config('MIN_WORK_START_DATE', default='1995-06-30')


class DevConfig(_Config): # pylint: disable=too-few-public-methods
"""Dev config."""
Expand Down Expand Up @@ -209,7 +211,7 @@ class ProdConfig(_Config): # pylint: disable=too-few-public-methods
DEBUG = False


class MigrationConfig(): # pylint: disable=too-few-public-methods
class MigrationConfig(_Config): # pylint: disable=too-few-public-methods
"""Config for db migration."""

TESTING = False
Expand Down

0 comments on commit e83a3fa

Please sign in to comment.