|
18 | 18 |
|
19 | 19 | from datetime import datetime
|
20 | 20 |
|
| 21 | +from django.conf import settings |
| 22 | +from django.contrib.postgres.aggregates import ArrayAgg |
| 23 | +from django.core.management import BaseCommand, CommandError, call_command |
| 24 | +from django.db import transaction |
| 25 | +from django.db.models import Exists, OuterRef, Q, Subquery |
| 26 | + |
21 | 27 | from arches import __version__
|
22 | 28 | from arches.app.const import IntegrityCheck
|
23 | 29 | from arches.app.models import models
|
24 |
| -from django.core.management.base import BaseCommand, CommandError |
25 |
| -from django.db import transaction |
26 |
| -from django.db.models import Exists, OuterRef |
27 | 30 |
|
28 | 31 | # Command modes
|
29 | 32 | FIX = "fix"
|
30 | 33 | VALIDATE = "validate"
|
31 | 34 |
|
32 | 35 | # Fix actions
|
33 | 36 | DELETE_QUERYSET = "delete queryset"
|
| 37 | +UPDATE_GRAPH_PUBLICATIONS = "manage.py graph --update" |
34 | 38 |
|
35 | 39 |
|
36 | 40 | class Command(BaseCommand):
|
@@ -116,6 +120,36 @@ def handle(self, *args, **options):
|
116 | 120 | ),
|
117 | 121 | fix_action=DELETE_QUERYSET,
|
118 | 122 | )
|
| 123 | + self.check_integrity( |
| 124 | + check=IntegrityCheck.PUBLICATION_MISSING_FOR_LANGUAGE, # 1014 |
| 125 | + queryset=( |
| 126 | + models.GraphModel.objects.filter( |
| 127 | + isresource=True, |
| 128 | + publication__isnull=False, |
| 129 | + source_identifier=None, |
| 130 | + ) |
| 131 | + .annotate( |
| 132 | + publications_in_system_languages=ArrayAgg( |
| 133 | + Subquery( |
| 134 | + models.PublishedGraph.objects.filter( |
| 135 | + pk=OuterRef("publication__publishedgraph"), |
| 136 | + ) |
| 137 | + .values("language") |
| 138 | + .distinct() |
| 139 | + ), |
| 140 | + filter=Q( |
| 141 | + publication__publishedgraph__language__in=[ |
| 142 | + lang[0] for lang in settings.LANGUAGES |
| 143 | + ] |
| 144 | + ), |
| 145 | + ) |
| 146 | + ) |
| 147 | + .filter( |
| 148 | + publications_in_system_languages__len__lt=len(settings.LANGUAGES) |
| 149 | + ) |
| 150 | + ), |
| 151 | + fix_action=UPDATE_GRAPH_PUBLICATIONS, |
| 152 | + ) |
119 | 153 |
|
120 | 154 | def check_integrity(self, check, queryset, fix_action):
|
121 | 155 | # 500 not set as a default earlier: None distinguishes whether verbose output implied
|
@@ -146,10 +180,24 @@ def check_integrity(self, check, queryset, fix_action):
|
146 | 180 | elif queryset.exists():
|
147 | 181 | fix_status = self.style.ERROR("No") # until actually fixed below
|
148 | 182 | # Perform fix action
|
149 |
| - if fix_action is DELETE_QUERYSET: |
| 183 | + if fix_action == DELETE_QUERYSET: |
150 | 184 | with transaction.atomic():
|
151 | 185 | queryset.delete()
|
152 | 186 | fix_status = self.style.SUCCESS("Yes")
|
| 187 | + elif fix_action == UPDATE_GRAPH_PUBLICATIONS: |
| 188 | + call_command( |
| 189 | + "graph", |
| 190 | + "publish", |
| 191 | + "--update", |
| 192 | + "-g", |
| 193 | + ",".join( |
| 194 | + str(pk) for pk in queryset.values_list("pk", flat=True) |
| 195 | + ), |
| 196 | + verbosity=self.options["verbosity"], |
| 197 | + stdout=self.stdout, |
| 198 | + stderr=self.stderr, |
| 199 | + ) |
| 200 | + fix_status = self.style.SUCCESS("Yes") |
153 | 201 | else:
|
154 | 202 | raise NotImplementedError
|
155 | 203 | else:
|
|
0 commit comments