Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Add liststosources management command
Browse files Browse the repository at this point in the history
This command will be used as part of the backward compatible migration process.
It creates or updates `Source` objects from `FacilityList` objects and also
updates the `source` foreign key on related `FacilityListItems`.

It is intended that this command be removed after deployed application code is
updated to create and reference `Source` objects.
  • Loading branch information
jwalgran committed Oct 2, 2019
1 parent 3742057 commit f4c5a37
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/django/api/management/commands/liststosources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.core.management.base import (BaseCommand)
from django.db import transaction

from api.models import FacilityList, FacilityListItem, Source


@transaction.atomic
def list_to_source(facility_list):
source_qs = Source.objects.filter(
source_type=Source.LIST, facility_list=facility_list)
if source_qs.exists():
source = source_qs.first()
else:
source = Source(source_type=Source.LIST,
facility_list=facility_list)

source.contributor = facility_list.contributor
source.is_public = facility_list.is_public
source.is_active = facility_list.is_active
source.save()

# Override auto_now values by using a queryset update
Source.objects.filter(pk=source.pk).update(
created_at=facility_list.created_at,
updated_at=facility_list.updated_at)

FacilityListItem.objects \
.filter(facility_list=facility_list) \
.update(source=source)


def lists_to_sources():
for facility_list in FacilityList.objects.all():
list_to_source(facility_list)


class Command(BaseCommand):
help = ('Create/update Source records from FacilityList records and '
'connect the Source records to FacilityListItem records.')

def handle(self, *args, **options):
lists_to_sources()

0 comments on commit f4c5a37

Please sign in to comment.