diff --git a/meinberlin/apps/cms/migrations/0040_storefront_add_numbers_as_fields.py b/meinberlin/apps/cms/migrations/0040_storefront_add_numbers_as_fields.py new file mode 100644 index 0000000000..02009c66b2 --- /dev/null +++ b/meinberlin/apps/cms/migrations/0040_storefront_add_numbers_as_fields.py @@ -0,0 +1,27 @@ +# Generated by Django 3.2.17 on 2023-02-09 12:31 + +from django.db import migrations, models +import meinberlin.apps.cms.models.storefronts + + +class Migration(migrations.Migration): + dependencies = [ + ("meinberlin_cms", "0039_alter_homepage_body"), + ] + + operations = [ + migrations.AddField( + model_name="storefront", + name="num_entries", + field=models.PositiveIntegerField( + default=meinberlin.apps.cms.models.storefronts.get_num_entries_count + ), + ), + migrations.AddField( + model_name="storefront", + name="num_projects", + field=models.PositiveIntegerField( + default=meinberlin.apps.cms.models.storefronts.get_num_projects_count + ), + ), + ] diff --git a/meinberlin/apps/cms/migrations/0041_storefrontitem_district_project_count.py b/meinberlin/apps/cms/migrations/0041_storefrontitem_district_project_count.py new file mode 100644 index 0000000000..39ca383ce6 --- /dev/null +++ b/meinberlin/apps/cms/migrations/0041_storefrontitem_district_project_count.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.17 on 2023-02-09 12:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("meinberlin_cms", "0040_storefront_add_numbers_as_fields"), + ] + + operations = [ + migrations.AddField( + model_name="storefrontitem", + name="district_project_count", + field=models.PositiveIntegerField(default=0), + preserve_default=False, + ), + ] diff --git a/meinberlin/apps/cms/models/storefronts.py b/meinberlin/apps/cms/models/storefronts.py index e796e44422..4b67d1069e 100644 --- a/meinberlin/apps/cms/models/storefronts.py +++ b/meinberlin/apps/cms/models/storefronts.py @@ -43,6 +43,7 @@ class StorefrontItem(models.Model): limit_choices_to=project_choice_limit, ) quote = models.TextField(blank=True, max_length=150) + district_project_count = models.PositiveIntegerField() def __str__(self): return str(self.pk) @@ -62,8 +63,7 @@ def project_url(self): return self.project.externalproject.url return self.project.get_absolute_url() - @cached_property - def district_project_count(self): + def get_district_project_count(self): projects = Project.objects.filter( Q(access=Access.PUBLIC) | Q(access=Access.SEMIPUBLIC), administrative_district=self.district, @@ -77,6 +77,11 @@ def district_project_count(self): active_project_count += 1 return active_project_count + def save(self, *args, **kwargs): + if not self.district_project_count: + self.district_project_count = self.get_district_project_count() + super().save(*args, **kwargs) + panels = [ FieldPanel("district"), FieldPanel("project"), @@ -84,6 +89,25 @@ def district_project_count(self): ] +def get_num_entries_count(): + num_comments = Comment.objects.all().count() + num_items = Item.objects.all().count() + return num_comments + num_items + + +def get_num_projects_count(): + projects = Project.objects.all().filter( + Q(access=Access.PUBLIC) | Q(access=Access.SEMIPUBLIC), + is_draft=False, + is_archived=False, + ) + active_project_count = 0 + for project in projects: + if project.active_phase or project.future_phases: + active_project_count += 1 + return active_project_count + + @register_snippet class Storefront(ClusterableModel): title = models.CharField(max_length=255, null=False, blank=False) @@ -95,29 +119,12 @@ class Storefront(ClusterableModel): related_name="+", ) teaser = models.CharField(max_length=100) + num_entries = models.PositiveIntegerField(default=get_num_entries_count) + num_projects = models.PositiveIntegerField(default=get_num_projects_count) def __str__(self): return self.title - @cached_property - def num_entries(self): - num_comments = Comment.objects.all().count() - num_items = Item.objects.all().count() - return num_comments + num_items - - @cached_property - def num_projects(self): - projects = Project.objects.all().filter( - Q(access=Access.PUBLIC) | Q(access=Access.SEMIPUBLIC), - is_draft=False, - is_archived=False, - ) - active_project_count = 0 - for project in projects: - if project.active_phase or project.future_phases: - active_project_count += 1 - return active_project_count - @cached_property def random_items(self): items = self.items.all()