Skip to content

Commit

Permalink
reduced DB requests on mark as compliant.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzovagliano authored and pamfilos committed Oct 7, 2024
1 parent 47dbc15 commit babcf5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
19 changes: 7 additions & 12 deletions scoap3/articles/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,14 @@ def get_identifiers(self, obj):
)


def make_compliant(obj):
article = obj
def make_compliant(article_queryset):
article_ids = article_queryset.values_list("id", flat=True)

reports = article.report.all()
reports = ComplianceReport.objects.filter(article_id__in=article_ids)
reports.update(compliant=True)

for report in reports:
report.compliant = True
report.save()

return f"Articles marked as compliant {article.id}"
ids = map(str, reports.values_list("article_id", flat=True))
return ids


class ArticleAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -323,10 +321,7 @@ def make_compliance_check(self, request, queryset):

@admin.action(description="Mark as compliant")
def mark_as_compliant(self, request, queryset):
ids = []
for obj in queryset:
make_compliant(obj)
ids.append(str(obj.id))
ids = make_compliant(queryset)
messages.success(
request,
f"""
Expand Down
10 changes: 3 additions & 7 deletions scoap3/articles/tests/test_article_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,15 @@ def test_create_article_published_before_2023(self):

def test_mark_article_with_reports_as_compliant(self):
compliance_checks(self.article.id)
make_compliant(self.article)
make_compliant(Article.objects.all())
report = self.article.report.first()

self.assertEqual(report.compliant, True)

def test_mark_article_without_reports_as_compliant(self):
make_compliant(self.article)
reports = self.article.report.all()
ids = make_compliant(Article.objects.all())

for report in reports:
self.assertEqual(
report.compliant, "No reports available for selected articles"
)
self.assertEqual(list(ids), [])

def tearDown(self):
ArticleIdentifier.objects.all().delete()
Expand Down

0 comments on commit babcf5b

Please sign in to comment.