Skip to content

Commit

Permalink
apps/cms: use model fields for storefront counts
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzylogic2000 committed Feb 9, 2023
1 parent 929aee9 commit b20189f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
),
),
]
Original file line number Diff line number Diff line change
@@ -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,
),
]
49 changes: 28 additions & 21 deletions meinberlin/apps/cms/models/storefronts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -77,13 +77,37 @@ 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"),
FieldPanel("quote"),
]


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)
Expand All @@ -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()
Expand Down

0 comments on commit b20189f

Please sign in to comment.