-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmigration_utils.py
More file actions
75 lines (58 loc) · 3.31 KB
/
Copy pathmigration_utils.py
File metadata and controls
75 lines (58 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from typing import List
from specifyweb.backend.businessrules.uniqueness_rules import create_uniqueness_rule
def catnum_rule_editable(apps, schema_editor=None):
""" Find any CollectionObject catalogNumber must be unique to Collection
rules which are readonly on the frontend (have isDatabaseConstraint=True)
and set their isDatabaseConstraint=False.
Generally should be run only after migration businessrules/0003 has been
applied
"""
UniquenessRule = apps.get_model("businessrules", "UniquenessRule")
model_rules = UniquenessRule.objects.filter(modelName="Collectionobject", isDatabaseConstraint=True)
catalog_number_rules: List[int] = []
for rule in model_rules:
rule_fields = rule.uniquenessrulefield_set.all()
fields = rule_fields.filter(isScope=False)
scopes = rule_fields.filter(isScope=True)
# We're only interested in the rule "CollectionObject catalogNumber
# must be unique to Collection"
# We check for length of fields and scopes because get() raises an
# exception if more than one result is returned
if (len(fields) == 1 and len(scopes) == 1) and (fields.get().fieldPath.lower() == "catalognumber" and scopes.get().fieldPath.lower() == "collection"):
catalog_number_rules.append(rule.id)
rules_to_update = UniquenessRule.objects.filter(id__in=catalog_number_rules)
rules_to_update.update(isDatabaseConstraint=False)
def catnum_rule_uneditable(apps, schema_editor=None):
""" Find any CollectionObject catalogNumber must be unique to Collection
rules which are editable on the frontend (have isDatabaseConstraint=False)
and set their isDatabaseConstraint=True.
Generally should be run when migration businessrules/0003 is being reverted
"""
Discipline = apps.get_model("specify", "Discipline")
UniquenessRule = apps.get_model("businessrules", "UniquenessRule")
for discipline in Discipline.objects.all():
model_rules = UniquenessRule.objects.filter(modelName="Collectionobject", discipline_id=discipline.id, isDatabaseConstraint=False)
has_catalognumber_rule = False
matching_rule_ids: List[int] = []
for rule in model_rules:
rule_fields = rule.uniquenessrulefield_set.all()
fields = rule_fields.filter(isScope=False)
scopes = rule_fields.filter(isScope=True)
# We're only interested in the rule "CollectionObject catalogNumber
# must be unique to Collection"
# We check for length of fields and scopes because get() raises an
# exception if more than one result is returned
if (len(fields) == 1 and len(scopes) == 1) and (fields.get().fieldPath.lower() == "catalognumber" and scopes.get().fieldPath.lower() == "collection"):
has_catalognumber_rule = True
matching_rule_ids.append(rule.id)
if has_catalognumber_rule:
UniquenessRule.objects.filter(id__in=matching_rule_ids).update(isDatabaseConstraint=True)
else:
create_uniqueness_rule(
"Collectionobject",
discipline=discipline,
is_database_constraint=True,
fields=["catalogNumber"],
scopes=["collection"],
registry=apps,
)