Skip to content

Commit

Permalink
adds migration #11570
Browse files Browse the repository at this point in the history
  • Loading branch information
chrabyrd committed Nov 14, 2024
1 parent c59b4b5 commit ccc1ff6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 18 deletions.
38 changes: 38 additions & 0 deletions arches/app/models/migrations/11570_harden_editble_future_graphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 5.1.2 on 2024-11-13 18:17

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("models", "10437_node_alias_not_null"),
]

operations = [
migrations.AlterField(
model_name="resource2resourceconstraint",
name="resourceclassfrom",
field=models.ForeignKey(
blank=True,
db_column="resourceclassfrom",
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="resxres_contstraint_classes_from",
to="models.node",
),
),
migrations.AlterField(
model_name="resource2resourceconstraint",
name="resourceclassto",
field=models.ForeignKey(
blank=True,
db_column="resourceclassto",
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="resxres_contstraint_classes_to",
to="models.node",
),
),
]
69 changes: 51 additions & 18 deletions arches/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,34 +858,67 @@ def get_relatable_resources(self):
relatable_resource_ids = [
r2r.resourceclassfrom
for r2r in Resource2ResourceConstraint.objects.filter(
resourceclassto_id=self.nodeid
resourceclassto_id=(
self.source_identifier_id
if self.source_identifier_id
else self.nodeid
)
)
if r2r.resourceclassfrom is not None
]
relatable_resource_ids = relatable_resource_ids + [
r2r.resourceclassto
for r2r in Resource2ResourceConstraint.objects.filter(
resourceclassfrom_id=self.nodeid
resourceclassfrom_id=(
self.source_identifier_id
if self.source_identifier_id
else self.nodeid
)
)
if r2r.resourceclassto is not None
]
return relatable_resource_ids
return list(set(relatable_resource_ids))

def set_relatable_resources(self, new_ids):
old_ids = [res.nodeid for res in self.get_relatable_resources()]
for old_id in old_ids:
if old_id not in new_ids:
Resource2ResourceConstraint.objects.filter(
Q(resourceclassto_id=self.nodeid)
| Q(resourceclassfrom_id=self.nodeid),
Q(resourceclassto_id=old_id) | Q(resourceclassfrom_id=old_id),
).delete()
for new_id in new_ids:
if new_id not in old_ids:
new_r2r = Resource2ResourceConstraint.objects.create(
resourceclassfrom_id=self.nodeid, resourceclassto_id=new_id
new_ids = set(new_ids)
old_ids = set()

for res in self.get_relatable_resources():
if res.source_identifier_id:
old_ids.add(res.source_identifier_id)
if res.nodeid:
old_ids.add(res.nodeid)

self_ids = set()
if self.source_identifier_id:
self_ids.add(self.source_identifier_id)
if self.nodeid:
self_ids.add(self.nodeid)

old_ids.discard(None)
self_ids.discard(None)

old_ids_to_delete = old_ids - new_ids
new_ids_to_create = new_ids - old_ids

if old_ids_to_delete and self_ids:
Resource2ResourceConstraint.objects.filter(
(
Q(resourceclassto_id__in=self_ids)
& Q(resourceclassfrom_id__in=old_ids_to_delete)
)
| (
Q(resourceclassto_id__in=old_ids_to_delete)
& Q(resourceclassfrom_id__in=self_ids)
)
new_r2r.save()
).delete()

for new_id in new_ids_to_create:
new_r2r = Resource2ResourceConstraint(
resourceclassfrom_id=self.source_identifier_id or self.nodeid,
resourceclassto_id=new_id,
)
new_r2r.save()

def serialize(self, fields=None, exclude=None, **kwargs):
ret = JSONSerializer().handle_model(
Expand Down Expand Up @@ -1110,15 +1143,15 @@ class Resource2ResourceConstraint(models.Model):
blank=True,
null=True,
related_name="resxres_contstraint_classes_from",
on_delete=models.SET_NULL,
on_delete=models.CASCADE,
)
resourceclassto = models.ForeignKey(
Node,
db_column="resourceclassto",
blank=True,
null=True,
related_name="resxres_contstraint_classes_to",
on_delete=models.SET_NULL,
on_delete=models.CASCADE,
)

def __init__(self, *args, **kwargs):
Expand Down

0 comments on commit ccc1ff6

Please sign in to comment.