Skip to content

Commit

Permalink
Add the the consultation level when doing an first nation import (#2231)
Browse files Browse the repository at this point in the history
* Add the the consultation level when doing an first nation import

* fix linting issues
  • Loading branch information
jadmsaadaot authored May 15, 2024
1 parent 22286d2 commit 4e7e0cd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
29 changes: 29 additions & 0 deletions epictrack-api/src/api/models/indigenous_work_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright © 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Model to handle complex queries related to Indigenous Work."""

from sqlalchemy.orm import joinedload

from api.models import db, IndigenousWork, Work


def find_all_by_project_id(project_id: int):
"""Return all Indigenous Work for a project."""
query = (db.session.query(IndigenousWork)
.join(Work, IndigenousWork.work_id == Work.id)
.filter(Work.project_id == project_id)
.options(joinedload(IndigenousWork.work))
.distinct(Work.project_id, IndigenousWork.indigenous_nation_id)
)
return query.all()
22 changes: 15 additions & 7 deletions epictrack-api/src/api/services/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from collections import defaultdict
from datetime import datetime, timedelta
from io import BytesIO
from itertools import product
from typing import Dict, List, Optional

import pandas as pd
Expand Down Expand Up @@ -52,6 +51,7 @@
from api.models.event_template import EventTemplateVisibilityEnum
from api.models.indigenous_nation import IndigenousNation
from api.models.indigenous_work import IndigenousWork
from api.models.indigenous_work_queries import find_all_by_project_id
from api.models.pagination_options import PaginationOptions
from api.models.phase_code import PhaseVisibilityEnum
from api.models.special_field import EntityEnum
Expand Down Expand Up @@ -834,13 +834,21 @@ def import_first_nations(cls, work_id: int, indigenous_nation_ids: [int]):
).update({"is_active": True})
current_app.logger.info(f"Enabled {enabled_count} IndigenousWorks")

keys = ("work_id", "indigenous_nation_id")
indigenous_works = [
task_assignee
for i, j in product([work_id], indigenous_nation_ids)
if (task_assignee := dict(zip(keys, (i, j)))) not in existing_first_nations
work = Work.find_by_id(work_id)
nations_in_same_project = find_all_by_project_id(work.project_id)
selected_nations = [
nation for nation in nations_in_same_project
if nation.indigenous_nation_id in indigenous_nation_ids
]
nations_to_insert = [
{
'work_id': work.id,
'indigenous_nation_id': nation.indigenous_nation_id,
'indigenous_consultation_level_id': nation.indigenous_consultation_level_id,
}
for nation in selected_nations
]
db.session.bulk_insert_mappings(IndigenousWork, mappings=indigenous_works)
db.session.bulk_insert_mappings(IndigenousWork, mappings=nations_to_insert)
db.session.commit()
return "Imported successfully"

Expand Down

0 comments on commit 4e7e0cd

Please sign in to comment.