-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analysis): add path attribute / mixin to structural models
Part of a series of PRs that will lead to the analysis module. Add a denormalisation step to the structural data models (case, workitem, and document) that will allow faster queries across case structures down the road.
- Loading branch information
Showing
12 changed files
with
246 additions
and
8 deletions.
There are no files selected for viewing
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
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
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,19 @@ | ||
from django.db.models.signals import pre_save | ||
from django.dispatch import receiver | ||
|
||
from caluma.caluma_core.events import filter_events | ||
from caluma.caluma_core.models import PathModelMixin | ||
|
||
|
||
@receiver(pre_save) | ||
@filter_events(lambda sender: PathModelMixin in sender.mro()) | ||
def store_path(sender, instance, **kwargs): | ||
"""Store/update the path of the object. | ||
Note: Due to the fact that this structure is relatively rigid, | ||
we don't update our children. For one, they may be difficult to | ||
collect, but also, the parent-child relationship is not expected | ||
to change, and structures are built top-down, so any object | ||
is expected to exist before it's children come into play. | ||
""" | ||
instance.path = instance.calculate_path() |
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
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,34 @@ | ||
# Generated by Django 2.2.22 on 2021-09-29 15:13 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("caluma_form", "0040_add_modified_by_user_group"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="document", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="historicaldocument", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
caluma/caluma_form/migrations/0042_fill_path_attributes.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,18 @@ | ||
# Generated by Django 2.2.22 on 2021-09-29 16:48 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def add_path_attribute(apps, schema_editor): | ||
for doc in apps.get_model("caluma_form.document").objects.all(): | ||
doc.path = doc.calculate_path() | ||
doc.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("caluma_form", "0041_add_path_attribute"), | ||
] | ||
|
||
operations = [migrations.RunPython(add_path_attribute, migrations.RunPython.noop)] |
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
54 changes: 54 additions & 0 deletions
54
caluma/caluma_workflow/migrations/0028_add_path_attribute.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,54 @@ | ||
# Generated by Django 2.2.22 on 2021-09-29 15:13 | ||
|
||
import django.contrib.postgres.fields | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("caluma_workflow", "0027_add_modified_by_user_group"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="case", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="historicalcase", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="historicalworkitem", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
migrations.AddField( | ||
model_name="workitem", | ||
name="path", | ||
field=django.contrib.postgres.fields.ArrayField( | ||
base_field=models.CharField(max_length=150), | ||
default=list, | ||
help_text="Stores a path to the given object", | ||
size=None, | ||
), | ||
), | ||
] |
22 changes: 22 additions & 0 deletions
22
caluma/caluma_workflow/migrations/0029_fill_path_attributes.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,22 @@ | ||
# Generated by Django 2.2.22 on 2021-09-29 15:20 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def add_path_attribute(apps, schema_editor): | ||
for model in [ | ||
"caluma_workflow.workitem", | ||
"caluma_workflow.case", | ||
]: | ||
for obj in apps.get_model(model).objects.all(): | ||
obj.path = obj.calculate_path() | ||
obj.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("caluma_workflow", "0028_add_path_attribute"), | ||
] | ||
|
||
operations = [migrations.RunPython(add_path_attribute, migrations.RunPython.noop)] |
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
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,21 @@ | ||
def test_set_paths(db, case, work_item_factory): | ||
|
||
workitem = work_item_factory(case=case) | ||
# workitem.save() # trigger the signal | ||
assert workitem.path == [str(case.pk), str(workitem.pk)] | ||
|
||
|
||
def test_row_document_path(db, case, form_and_document): | ||
form, document, questions, answers = form_and_document( | ||
use_table=True, use_subform=True | ||
) | ||
|
||
case.document = document | ||
case.save() | ||
document.save() | ||
assert document.path == [str(case.pk), str(document.pk)] | ||
|
||
table_ans = answers["table"] | ||
row_doc = table_ans.documents.first() | ||
row_doc.save() | ||
assert row_doc.path == [str(case.pk), str(document.pk), str(row_doc.pk)] |
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