Skip to content

Commit

Permalink
Use apps param
Browse files Browse the repository at this point in the history
  • Loading branch information
CarolineDenis committed Aug 9, 2024
1 parent d8c6475 commit fa958d9
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions specifyweb/specify/migrations/0004_cogtype_picklist.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from django.db import migrations
from specifyweb.specify.models import (
Collection,
Splocalecontainer,
Splocalecontaineritem,
Splocaleitemstr,
Picklist
)

PICKLIST_NAME = 'CollectionObjectGroupType'
FIELD_NAME = 'collectionObjectGroupType'
PICKLIST_TEXT = 'Collection Object Group Type'

def create_cogtype_picklist():
def create_cogtype_picklist(apps):
Collection = apps.get_model('specifyweb', 'Collection')
Picklist = apps.get_model('specifyweb', 'Picklist')

# Create a cogtype picklist for each collection
for collection in Collection.objects.all():
Picklist.objects.get_or_create(
Expand All @@ -24,31 +21,41 @@ def create_cogtype_picklist():
collection=collection
)

def revert_cogtype_picklist():
def revert_cogtype_picklist(apps):
Picklist = apps.get_model('specifyweb', 'Picklist')

Picklist.objects.filter(name=PICKLIST_NAME).delete()


def create_cogtype_splocalecontaineritem():
def create_cogtype_splocalecontaineritem(apps):
Splocalecontainer = apps.get_model('specifyweb', 'Splocalecontainer')
Splocalecontaineritem = apps.get_model('specifyweb', 'Splocalecontaineritem')

# Create a Splocalecontaineritem record for each CollectionObjectGroup Splocalecontainer
# NOTE: Each discipline has its own CollectionObjectGroup Splocalecontainer
for container in Splocalecontainer.objects.filter(name='collectionobjectgroup'):
for container in Splocalecontainer.objects.filter(name='collectionobjectgroup', schematype=0):
Splocalecontaineritem.objects.get_or_create(
name=FIELD_NAME,
picklistname=PICKLIST_NAME,
type='ManyToOne',
container=container
container=container,
isRequired=False
)

def revert_cogtype_splocalecontaineritem():
def revert_cogtype_splocalecontaineritem(apps):
Splocalecontaineritem = apps.get_model('specifyweb', 'Splocalecontaineritem')

Splocalecontaineritem.objects.filter(name=FIELD_NAME).delete()


def create_cogtype_splocaleitemstr():
def create_cogtype_splocaleitemstr(apps):
Splocaleitemstr = apps.get_model('specifyweb', 'Splocaleitemstr')
Splocalecontaineritem = apps.get_model('specifyweb', 'Splocalecontaineritem')
# Create caption & description records for collectionObjectGroupType in Schema Config
for container_item in Splocalecontaineritem.objects.filter(name=FIELD_NAME):
Splocaleitemstr.objects.create(
language='en',
text='Collection Object Group Type',
text=PICKLIST_TEXT,
itemname=container_item
)
Splocaleitemstr.objects.create(
Expand All @@ -57,27 +64,28 @@ def create_cogtype_splocaleitemstr():
itemdesc=container_item
)

def revert_cogtype_splocaleitemstr():
def revert_cogtype_splocaleitemstr(apps):
Splocaleitemstr = apps.get_model('specifyweb', 'Splocaleitemstr')

Splocaleitemstr.objects.filter(text=FIELD_NAME).delete()
Splocaleitemstr.objects.filter(text='Collection Object Group Type').delete()
Splocaleitemstr.objects.filter(text=PICKLIST_TEXT).delete()


class Migration(migrations.Migration):
initial = True

dependencies = [
('specify', '0003_cotype_picklist'),
]

def apply_migration(apps, schema_editor):
create_cogtype_picklist()
create_cogtype_splocalecontaineritem()
create_cogtype_splocaleitemstr()
create_cogtype_picklist(apps)
create_cogtype_splocalecontaineritem(apps)
create_cogtype_splocaleitemstr(apps)

def revert_migration(apps, schema_editor):
revert_cogtype_picklist()
revert_cogtype_splocaleitemstr()
revert_cogtype_splocalecontaineritem()
revert_cogtype_picklist(apps)
revert_cogtype_splocaleitemstr(apps)
revert_cogtype_splocalecontaineritem(apps)

operations = [
migrations.RunPython(apply_migration, revert_migration, atomic=True)
Expand Down

0 comments on commit fa958d9

Please sign in to comment.