refactored a little bit

This commit is contained in:
Anatoly 2019-12-19 16:39:42 +03:00
parent 68b8aa427c
commit 5a6af6a2a7
3 changed files with 18 additions and 8 deletions

View File

@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations = [
migrations.AddField(
model_name='guide',
name='count_objects_during_init',
name='count_related_objects',
field=models.PositiveIntegerField(default=0, help_text='* after rebuild guide, refresh count of related guide elements', verbose_name='count of related guide elements during initialization'),
),
]

View File

@ -271,7 +271,7 @@ class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin):
verbose_name=_('site settings'))
state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES,
verbose_name=_('state'))
count_objects_during_init = models.PositiveIntegerField(
count_related_objects = models.PositiveIntegerField(
default=0,
help_text=_('* after rebuild guide, refresh count of related guide elements'),
verbose_name=_('count of related guide elements during initialization'))
@ -328,6 +328,16 @@ class Guide(ProjectBaseMixin, CollectionNameMixin, CollectionDateMixin):
# parent_id__isnull=True).count()
# return counter
def update_count_related_objects(self, nodes: set = ('EstablishmentNode', 'WineNode')):
"""Update count of related guide element objects."""
descendants = GuideElement.objects.get_root_node(self) \
.get_descendants()
if descendants:
updated_count = descendants.filter(
guide_element_type__name__in=nodes).count()
self.count_related_objects = updated_count
self.save()
class AdvertorialQuerySet(models.QuerySet):
"""QuerySet for model Advertorial."""

View File

@ -254,12 +254,12 @@ def transfer_guide_elements_bulk():
for guide in tqdm(Guide.objects.all(),
desc='update count_of_initial_objects field values'):
count_of_initial_objects = GuideElement.objects.get_root_node(guide) \
.get_descendants() \
.filter(guide_element_type__name__in=['EstablishmentNode', 'WineNode']) \
.count()
guide.count_objects_during_init = count_of_initial_objects
objects_to_update.append(guide)
descendants = GuideElement.objects.get_root_node(guide).get_descendants()
if descendants:
count_of_initial_objects = descendants.filter(
guide_element_type__name__in=['EstablishmentNode', 'WineNode']).count()
guide.count_related_objects = count_of_initial_objects
objects_to_update.append(guide)
# update count_of_initial_objects field values
Guide.objects.bulk_update(objects_to_update, ['count_objects_during_init', ])