Skip to content

Commit

Permalink
Update models.py
Browse files Browse the repository at this point in the history
performance update proposed by copilot
  • Loading branch information
pep-un authored Jan 13, 2025
1 parent bd1ba22 commit 1c80177
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions conformity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,15 @@ def get_frameworks(self):
def remove_conformity(self, pid):
"""Cascade deletion of conformity"""
with set_actor('system'):
requirement_set = Requirement.objects.filter(framework=pid)
for requirement in requirement_set:
Conformity.objects.filter(requirement=requirement.id).filter(organization=self.id).delete()

requirement_set = Requirement.objects.filter(framework=pid).values_list('id', flat=True)
Conformity.objects.filter(requirement__in=requirement_set, organization=self.id).delete()

def add_conformity(self, pid):
"""Automatic creation of conformity"""
with set_actor('system'):
requirement_set = Requirement.objects.filter(framework=pid)
for requirement in requirement_set:
conformity = Conformity(organization=self, requirement=requirement)
conformity.save()
conformities = [Conformity(organization=self, requirement=requirement) for requirement in requirement_set]
Conformity.objects.bulk_create(conformities)


class RequirementManager(models.Manager):
Expand Down Expand Up @@ -233,16 +231,13 @@ def set_responsible(self, resp):
child.set_responsible(resp)

def update(self):
"""Update conformity to recursivly update conformity when change"""
with set_actor('system'):
children = self.get_children().filter(applicable=True)

if children.exists():
children_stat = [child.status for child in children]
self.status = mean(children_stat)
self.status = children.aggregate(mean_status=models.Avg('status'))['mean_status']
self.applicable = True

self.save()

parent = self.get_parent()
if parent:
parent.update()
Expand Down

0 comments on commit 1c80177

Please sign in to comment.