-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #705 from uktrade/feature/migration-commands
Migration commands to fix investment projects.
- Loading branch information
Showing
9 changed files
with
1,129 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
datahub/dbmaintenance/management/commands/update_investment_project_company.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import uuid | ||
import reversion | ||
|
||
from datahub.investment.models import InvestmentProject | ||
from ..base import CSVBaseCommand | ||
|
||
|
||
class Command(CSVBaseCommand): | ||
"""Command to update investment_project. | ||
investor_company, intermediate_company, uk_company, uk_company_decided. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
"""Define extra arguments.""" | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--simulate', | ||
action='store_true', | ||
dest='simulate', | ||
default=False, | ||
help='If True it only simulates the command without saving the changes.', | ||
) | ||
|
||
def _parse_company_id(self, company_id): | ||
""" | ||
:param company_id: string representing uuid of the company | ||
:return: instance of UUID or None | ||
""" | ||
if not company_id or company_id.lower().strip() == 'null': | ||
return None | ||
return uuid.UUID(company_id) | ||
|
||
def _should_update(self, | ||
investment_project, | ||
investor_company_id, | ||
intermediate_company_id, | ||
uk_company_id, | ||
uk_company_decided): | ||
""" | ||
Checks if Investment project should be updated. | ||
:param investment_project: instance of InvestmentProject | ||
:param investor_company: instance of Company or None | ||
:param intermediate_company: instance of Company or None | ||
:param uk_company: instance of Company or None | ||
:param uk_company_decided: Boolean | ||
:return: True if investment project needs to be updated | ||
""" | ||
return (investment_project.investor_company_id != investor_company_id or | ||
investment_project.intermediate_company_id != intermediate_company_id or | ||
investment_project.uk_company_id != uk_company_id or | ||
investment_project.uk_company_decided != uk_company_decided) | ||
|
||
def get_uk_company_decided(self, uk_company_decided): | ||
""" | ||
:param uk_company_decided: string containing either '1' or '0' | ||
:return: Boolean | ||
""" | ||
translate = { | ||
'0': False, | ||
'1': True, | ||
} | ||
return translate[uk_company_decided.strip()] | ||
|
||
def _process_row(self, row, simulate=False, **options): | ||
"""Process one single row.""" | ||
investment_project = InvestmentProject.objects.get(pk=row['id']) | ||
investor_company_id = self._parse_company_id(row['investor_company_id']) | ||
intermediate_company_id = self._parse_company_id(row['intermediate_company_id']) | ||
uk_company_id = self._parse_company_id(row['uk_company_id']) | ||
uk_company_decided = self.get_uk_company_decided(row['uk_company_decided']) | ||
|
||
if self._should_update( | ||
investment_project, | ||
investor_company_id, | ||
intermediate_company_id, | ||
uk_company_id, | ||
uk_company_decided, | ||
): | ||
investment_project.investor_company_id = investor_company_id | ||
investment_project.intermediate_company_id = intermediate_company_id | ||
investment_project.uk_company_id = uk_company_id | ||
investment_project.uk_company_decided = uk_company_decided | ||
if not simulate: | ||
with reversion.create_revision(): | ||
investment_project.save( | ||
update_fields=( | ||
'investor_company', | ||
'intermediate_company', | ||
'uk_company', | ||
'uk_company_decided', | ||
) | ||
) | ||
reversion.set_comment('Companies data migration.') |
105 changes: 105 additions & 0 deletions
105
...nance/management/commands/update_investment_project_referral_source_activity_marketing.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from functools import lru_cache | ||
|
||
import reversion | ||
|
||
from datahub.investment.models import InvestmentProject | ||
from datahub.metadata.models import ReferralSourceActivity, ReferralSourceMarketing | ||
from ..base import CSVBaseCommand | ||
|
||
|
||
class Command(CSVBaseCommand): | ||
"""Command to update investment_project.referral_source_activity/_marketing.""" | ||
|
||
def add_arguments(self, parser): | ||
"""Define extra arguments.""" | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--simulate', | ||
action='store_true', | ||
dest='simulate', | ||
default=False, | ||
help='If True it only simulates the command without saving the changes.', | ||
) | ||
|
||
@lru_cache(maxsize=None) | ||
def get_referral_source_activity(self, referral_source_activity_id): | ||
""" | ||
:param referral_source_activity_id: uuid of the referral source activity | ||
:return: instance of ReferralSourceActivity | ||
with id == referral_source_activity_id if it exists, | ||
None otherwise | ||
""" | ||
if ( | ||
not referral_source_activity_id or | ||
referral_source_activity_id.lower().strip() == 'null' | ||
): | ||
return None | ||
return ReferralSourceActivity.objects.get(id=referral_source_activity_id) | ||
|
||
@lru_cache(maxsize=None) | ||
def get_referral_source_activity_marketing(self, referral_source_activity_marketing_id): | ||
""" | ||
:param referral_source_activity_marketing_id: uuid of the referral source marketing | ||
:return: instance of ReferralSourceMarketing | ||
with id == referral_source_activity_marketing_id if it exists, | ||
None otherwise | ||
""" | ||
if ( | ||
not referral_source_activity_marketing_id or | ||
referral_source_activity_marketing_id.lower().strip() == 'null' | ||
): | ||
return None | ||
return ReferralSourceMarketing.objects.get(id=referral_source_activity_marketing_id) | ||
|
||
def _should_update(self, | ||
investment_project, | ||
referral_source_activity, | ||
referral_source_activity_marketing | ||
): | ||
""" | ||
Checks if Investment project should be updated. | ||
:param investment_project: instance of InvestmentProject | ||
:param referral_source_activity: instance of ReferralSourceActivity or None | ||
:param referral_source_activity_marketing: | ||
instance of ReferralSourceMarketing or None | ||
:return: True if investment project needs to be updated | ||
""" | ||
return ( | ||
investment_project.referral_source_activity_id != | ||
referral_source_activity.id or | ||
investment_project.referral_source_activity_marketing_id != | ||
referral_source_activity_marketing.id | ||
) | ||
|
||
def _process_row(self, row, simulate=False, **options): | ||
"""Process one single row.""" | ||
investment_project = InvestmentProject.objects.get(pk=row['id']) | ||
|
||
referral_source_activity = self.get_referral_source_activity( | ||
row['referral_source_activity_id'] | ||
) | ||
|
||
referral_source_activity_marketing = self.get_referral_source_activity_marketing( | ||
row['referral_source_activity_marketing_id'] | ||
) | ||
|
||
if self._should_update( | ||
investment_project, | ||
referral_source_activity, | ||
referral_source_activity_marketing, | ||
): | ||
investment_project.referral_source_activity = \ | ||
referral_source_activity | ||
investment_project.referral_source_activity_marketing = \ | ||
referral_source_activity_marketing | ||
|
||
if not simulate: | ||
with reversion.create_revision(): | ||
investment_project.save( | ||
update_fields=( | ||
'referral_source_activity', | ||
'referral_source_activity_marketing', | ||
) | ||
) | ||
reversion.set_comment('ReferralSourceActivityMarketing migration.') |
103 changes: 103 additions & 0 deletions
103
...tenance/management/commands/update_investment_project_referral_source_activity_website.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
from functools import lru_cache | ||
|
||
import reversion | ||
|
||
from datahub.investment.models import InvestmentProject | ||
from datahub.metadata.models import ReferralSourceActivity, ReferralSourceWebsite | ||
from ..base import CSVBaseCommand | ||
|
||
|
||
class Command(CSVBaseCommand): | ||
"""Command to update investment_project.referral_source_activity/_website.""" | ||
|
||
def add_arguments(self, parser): | ||
"""Define extra arguments.""" | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--simulate', | ||
action='store_true', | ||
dest='simulate', | ||
default=False, | ||
help='If True it only simulates the command without saving the changes.', | ||
) | ||
|
||
@lru_cache(maxsize=None) | ||
def get_referral_source_activity(self, referral_source_activity_id): | ||
""" | ||
:param referral_source_activity_id: uuid of the referral source activity | ||
:return: instance of ReferralSourceActivity | ||
with id == referral_source_activity_id if it exists, | ||
None otherwise | ||
""" | ||
if ( | ||
not referral_source_activity_id or | ||
referral_source_activity_id.lower().strip() == 'null' | ||
): | ||
return None | ||
return ReferralSourceActivity.objects.get(id=referral_source_activity_id) | ||
|
||
@lru_cache(maxsize=None) | ||
def get_referral_source_activity_website(self, referral_source_activity_website_id): | ||
""" | ||
:param referral_source_activity_website_id: uuid of the referral source website | ||
:return: instance of ReferralSourceWebsite | ||
with id == referral_source_activity_website_id if it exists, | ||
None otherwise | ||
""" | ||
if ( | ||
not referral_source_activity_website_id or | ||
referral_source_activity_website_id.lower().strip() == 'null' | ||
): | ||
return None | ||
return ReferralSourceWebsite.objects.get(id=referral_source_activity_website_id) | ||
|
||
def _should_update(self, | ||
investment_project, | ||
referral_source_activity, | ||
referral_source_activity_website | ||
): | ||
""" | ||
Checks if Investment project should be updated. | ||
:param investment_project: instance of InvestmentProject | ||
:param referral_source_activity: instance of ReferralSourceActivity or None | ||
:param referral_source_activity_website: | ||
instance of ReferralSourceWebsite or None | ||
:return: True if investment project needs to be updated | ||
""" | ||
return ( | ||
investment_project.referral_source_activity_id != | ||
referral_source_activity.id or | ||
investment_project.referral_source_activity_website_id != | ||
referral_source_activity_website.id | ||
) | ||
|
||
def _process_row(self, row, simulate=False, **options): | ||
"""Process one single row.""" | ||
investment_project = InvestmentProject.objects.get(pk=row['id']) | ||
|
||
referral_source_activity = self.get_referral_source_activity( | ||
row['referral_source_activity_id'] | ||
) | ||
|
||
referral_source_activity_website = self.get_referral_source_activity_website( | ||
row['referral_source_activity_website_id'] | ||
) | ||
|
||
if self._should_update( | ||
investment_project, | ||
referral_source_activity, | ||
referral_source_activity_website, | ||
): | ||
investment_project.referral_source_activity = referral_source_activity | ||
investment_project.referral_source_activity_website = referral_source_activity_website | ||
|
||
if not simulate: | ||
with reversion.create_revision(): | ||
investment_project.save( | ||
update_fields=( | ||
'referral_source_activity', | ||
'referral_source_activity_website', | ||
) | ||
) | ||
reversion.set_comment('ReferralSourceActivityWebsite migration.') |
67 changes: 67 additions & 0 deletions
67
datahub/dbmaintenance/management/commands/update_investment_project_sector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from functools import lru_cache | ||
|
||
import reversion | ||
|
||
from datahub.investment.models import InvestmentProject | ||
from datahub.metadata.models import Sector | ||
from ..base import CSVBaseCommand | ||
|
||
|
||
class Command(CSVBaseCommand): | ||
"""Command to update investment_project.sector.""" | ||
|
||
def add_arguments(self, parser): | ||
"""Define extra arguments.""" | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'--simulate', | ||
action='store_true', | ||
dest='simulate', | ||
default=False, | ||
help='If True it only simulates the command without saving the changes.', | ||
) | ||
|
||
@lru_cache(maxsize=None) | ||
def get_sector(self, sector_id): | ||
""" | ||
:param company_id: uuid of the company | ||
:return: instance of Company with id == company_id if it exists, | ||
None otherwise | ||
""" | ||
if not sector_id or sector_id.lower().strip() == 'null': | ||
return None | ||
return Sector.objects.get(id=sector_id) | ||
|
||
def _should_update(self, | ||
investment_project, | ||
old_sector, | ||
): | ||
""" | ||
Checks if Investment project should be updated. | ||
:param investment_project: instance of InvestmentProject | ||
:param old_sector: instance of Company or None | ||
:return: True if investment project needs to be updated | ||
""" | ||
return (investment_project.sector == old_sector) | ||
|
||
def _process_row(self, row, simulate=False, **options): | ||
"""Process one single row.""" | ||
investment_project = InvestmentProject.objects.get(pk=row['id']) | ||
|
||
old_sector = self.get_sector(row['old_sector']) | ||
new_sector = self.get_sector(row['new_sector']) | ||
|
||
if self._should_update( | ||
investment_project, | ||
old_sector, | ||
): | ||
investment_project.sector = new_sector | ||
if not simulate: | ||
with reversion.create_revision(): | ||
investment_project.save( | ||
update_fields=( | ||
'sector', | ||
) | ||
) | ||
reversion.set_comment('Sector migration.') |
Oops, something went wrong.