Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Schema Config to include new geo fields and COG picklists #5257

Merged
merged 28 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c77a61c
Add migration for geo fields
sharadsw Sep 3, 2024
942b8a4
Update Schema Config README
sharadsw Sep 3, 2024
7ff1982
Add COG picklist migration
CarolineDenis Sep 4, 2024
c20eeb2
Add missing file
CarolineDenis Sep 4, 2024
87db686
Use correct picklist in cogtype business rule
sharadsw Sep 30, 2024
6a4171d
Merge remote-tracking branch 'origin/production' into issue-5215
sharadsw Oct 4, 2024
bee072c
Update discipline relations and change dependency
sharadsw Oct 4, 2024
e7f82b4
Update cogtype -> type picklist
sharadsw Oct 4, 2024
57e2489
Merge remote-tracking branch 'origin/production' into issue-5215
sharadsw Oct 8, 2024
0a5547d
Rename migration
sharadsw Oct 8, 2024
aabdc8f
merge issue-5317
sharadsw Oct 14, 2024
b37b222
Fix migration order
sharadsw Oct 17, 2024
7a3076e
remove old migration
sharadsw Oct 17, 2024
30c8e47
Merge remote-tracking branch 'origin/issue-5317' into issue-5215
sharadsw Oct 17, 2024
15a1b0d
fix frontend types
sharadsw Oct 17, 2024
84431d7
fix incorrect merge
sharadsw Oct 17, 2024
50e5166
fix migration dependency
sharadsw Oct 17, 2024
3c55587
fix env
maxpatiiuk Nov 30, 2023
858c59a
Merge branch 'issue-5317' into issue-5215
sharadsw Oct 17, 2024
0416d21
Improve reverting locale strings
sharadsw Oct 17, 2024
762dd82
Merge remote-tracking branch 'origin/issue-5215' into issue-5215
sharadsw Oct 17, 2024
dac4da4
Fix duplicate schema config entries
sharadsw Oct 18, 2024
1fcefb5
temp change revert
sharadsw Oct 18, 2024
b132374
Revert age migration
sharadsw Oct 21, 2024
723e536
Fix duplicate fields being added in migration
sharadsw Oct 21, 2024
58ef9de
Add changes for COG -> children
sharadsw Oct 22, 2024
9aeb7d3
Change formatter name to table name
sharadsw Oct 22, 2024
d8800df
Merge branch 'production' into issue-5215
sharadsw Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion specifyweb/businessrules/rules/cogtype_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def cogtype_pre_save(cog_type):
# NOTE: Maybe add constraint on the cog_type name in the future.
default_cog_types_picklist = Picklist.objects.get(
name="Default Collection Object Group Types",
tablename="collectionobjectgrouptype",
collection=cog_type.collection
)
if Picklistitem.objects.filter(picklist=default_cog_types_picklist, value=cog_type.type).count() == 0:
Expand Down
18 changes: 18 additions & 0 deletions specifyweb/frontend/js_src/lib/components/PickLists/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export const userTypes = [
'Guest',
] as const;

export const collectionObjectGroupTypeTypes = [
'Discrete',
'Consolidated',
'Drill Core',
] as const;

export const PickListTypes = {
// Items are defined in the PickListItems table
ITEMS: 0,
Expand Down Expand Up @@ -210,6 +216,18 @@ export const getFrontEndPickLists = f.store<{
.set('tableName', 'collectionobjecttype')
.set('fieldName', 'name'),
},
CollectionObjectGroupType: {
name: definePicklist('_CollectionObjectGroupType', [])
.set('type', PickListTypes.FIELDS)
.set('tableName', 'collectionobjectgrouptype')
.set('fieldName', 'name'),
type: definePicklist(
'_CollectionObjectGroupTypeType',
collectionObjectGroupTypeTypes.map((title) =>
createPickListItem(title, title)
)
),
},
CollectionRelType: {
name: definePicklist('_CollectionRelType', [])
.set('type', PickListTypes.FIELDS)
Expand Down
13 changes: 8 additions & 5 deletions specifyweb/frontend/js_src/lib/components/SchemaConfig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ fields and formatters/aggregators.

Tables used:

- SpLocaleContainer
- SpLocaleContainerItem
- SpLocaleItemStr
- SpLocaleContainer: Refers to each table in Schema Config. Each Specify table has one `SpLocaleContainer` record for each discipline.
- SpLocaleContainerItem: Contains the entries (fields/relations) for tables in Schema Config.
- SpLocaleItemStr: Contains the name and description strings for each `SpLocaleContainerItem`.

Also, the PickList tables:

- PickList
- PickListItem
- PickList: Defines a picklist in the database. Picklists can be one of 3 types:
- `type=0`: Refers to user-defined picklists. i.e: picklists containing explicitly specified values.
- `type=1`: Picklists that are related to a table. The values of the picklist are based on a specified formatter. e.g: CollectionObject > CollectionObjectType.
- `type=2`: Picklists that generates its values from a field in a given table. Essentially works as a user-defined picklist, but with autogenerated values.
- PickListItem: Contains the values for user-defined picklists. Only used for `type 1` picklists.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const HIDDEN_GEO_TABLES = new Set([
'CollectionObjectType',
'CollectionObjectGroup',
'CollectionObjectGroupJoin',
'CollectionObjectGroupType',
]);
export function TablesListEdit({
isNoRestrictionMode,
Expand Down
4 changes: 1 addition & 3 deletions specifyweb/specify/migrations/0002_geo.py
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ def create_default_collection_object_types():
for collection in Collection.objects.all():
cog_type_picklist = Picklist.objects.create(
name='Default Collection Object Group Types',
tablename='Collectionobjectgrouptype',
issystem=False,
type=1,
type=0,
readonly=False,
collection=collection
)
Expand All @@ -140,7 +139,6 @@ def revert_default_collection_object_types():
for collection in Collection.objects.all():
cog_type_picklist_qs = Picklist.objects.filter(
name='Default Collection Object Group Types',
tablename='Collectionobjectgrouptype',
collection=collection
)
if cog_type_picklist_qs.exists():
Expand Down
177 changes: 177 additions & 0 deletions specifyweb/specify/migrations/0004_schema_config_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""
This migration updates the Schema Config entries for pre-geo tables.
Geo migration added new fields to already existing tables. This migration updates the Schema Config to reflect those changes.

Fields added:
Collection -> collectionObjectType
GeographyTreeDef -> discipline
GeologicTimePeriodTreeDef -> discipline
LithostratTreeDef -> discipline
StorageTreeDef -> institution
TaxonTreeDef -> discipline
"""
from django.db import migrations

FIELD_DATA = [
{
"table": "Collection",
"field": "collectionObjectType",
"isrequired": False,
},
{
"table": "GeographyTreeDef",
"field": "discipline",
"isrequired": True,
},
{
"table": "GeologicTimePeriodTreeDef",
"field": "discipline",
"isrequired": True,
},
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
{
"table": "LithostratTreeDef",
"field": "discipline",
"isrequired": True,
},
{
"table": "StorageTreeDef",
"field": "institution",
"isrequired": True,
},
{
"table": "TaxonTreeDef",
"field": "discipline",
"isrequired": True,
},
]

PICKLIST_NAME = 'CollectionObjectGroupType'
COGTYPE_FIELD_NAME = 'cogType'
PICKLIST_TEXT = 'Collection Object Group Type'

def add_fields(apps):
Splocalecontainer = apps.get_model('specify', 'Splocalecontainer')
Splocalecontaineritem = apps.get_model('specify', 'Splocalecontaineritem')
Splocaleitemstr = apps.get_model('specify', 'Splocaleitemstr')

for data in FIELD_DATA:
for container in Splocalecontainer.objects.filter(name=data['table'], schematype=0):
container_item, _ = Splocalecontaineritem.objects.get_or_create(
name=data["field"],
type='ManyToOne',
container=container,
isrequired=data["isrequired"]
)
Splocaleitemstr.objects.get_or_create(
language='en',
text=data["field"],
itemname=container_item
)
Splocaleitemstr.objects.get_or_create(
language='en',
text=data["field"],
itemdesc=container_item
)

def remove_fields(apps):
Splocalecontaineritem = apps.get_model('specify', 'Splocalecontaineritem')
Splocaleitemstr = apps.get_model('specify', 'Splocaleitemstr')

for data in FIELD_DATA:
Splocaleitemstr.objects.filter(
text=data["field"].title(),
sharadsw marked this conversation as resolved.
Show resolved Hide resolved
itemname__name=data["field"],
itemname__container__name=data["table"],
itemname__container__schematype=0
).delete()
Splocaleitemstr.objects.filter(
text=data["field"],
itemdesc__name=data["field"],
itemdesc__container__name=data["table"],
itemdesc__container__schematype=0
).delete()
Splocalecontaineritem.objects.filter(
name=data["field"],
container__name=data["table"],
container__schematype=0,
).delete()

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

# Create a cogtype picklist for each collection
for collection in Collection.objects.all():
Picklist.objects.get_or_create(
name=PICKLIST_NAME,
issystem=True,
readonly=True,
sizelimit=-1,
sorttype=1,
type=1,
tablename='collectionobjectgrouptype',
collection=collection,
formatter=PICKLIST_NAME
)

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

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

# Updates COG -> cogtype to use the type 1 picklist created above
def update_cogtype_splocalecontaineritem(apps):
Splocalecontaineritem = apps.get_model('specify', 'Splocalecontaineritem')

Splocalecontaineritem.objects.filter(container__name='collectionobjectgroup', container__schematype=0, name=COGTYPE_FIELD_NAME).update(
picklistname=PICKLIST_NAME,
type='ManyToOne',
isrequired=True
)

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

Splocalecontaineritem.objects.filter(container__name='collectionobjectgroup', container__schematype=0, name=COGTYPE_FIELD_NAME).update(
picklistname=None,
type=None,
isrequired=None
)

# Updates cogtype -> type to use the Default COGType picklist (Drill Core, Discrete, Consolidated)
def update_cogtype_type_splocalecontaineritem(apps):
Splocalecontaineritem = apps.get_model('specify', 'Splocalecontaineritem')

Splocalecontaineritem.objects.filter(container__name='collectionobjectgrouptype', container__schematype=0, name='type').update(
picklistname='Default Collection Object Group Types',
isrequired=True
)

def revert_cogtype_type_splocalecontaineritem(apps):
Splocalecontaineritem = apps.get_model('specify', 'Splocalecontaineritem')

Splocalecontaineritem.objects.filter(container__name='collectionobjectgrouptype', container__schematype=0, name='type').update(
picklistname=None,
isrequired=None
)

class Migration(migrations.Migration):
dependencies = [
('specify', '0003_cotype_picklist'),
]

def apply_migration(apps, schema_editor):
add_fields(apps)
create_cogtype_picklist(apps)
update_cogtype_splocalecontaineritem(apps)
update_cogtype_type_splocalecontaineritem(apps)

def revert_migration(apps, schema_editor):
remove_fields(apps)
revert_cogtype_picklist(apps)
revert_cogtype_splocalecontaineritem(apps)
revert_cogtype_type_splocalecontaineritem(apps)

operations = [
migrations.RunPython(apply_migration, revert_migration, atomic=True)
]
Loading