Skip to content

Commit

Permalink
apps/cms: use model fields for storefront counts, add management comm…
Browse files Browse the repository at this point in the history
…and to update numbers
  • Loading branch information
fuzzylogic2000 committed Feb 10, 2023
1 parent 929aee9 commit 2151bd6
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 22 deletions.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django.core.management.base import BaseCommand
from django.db.models import Q

from adhocracy4.comments.models import Comment
from adhocracy4.modules.models import Item
from adhocracy4.projects.enums import Access
from adhocracy4.projects.models import Project
from meinberlin.apps.cms.models import storefronts
from meinberlin.apps.plans.models import Plan


class Command(BaseCommand):
help = "Update item and project counts for the storefront."

def handle(self, *args, **options):
for storefront in storefronts.Storefront.objects.all():
storefront.num_entries = self.get_num_entries_count()
storefront.num_projects = self.get_num_projects_count()
storefront.save()
for item in storefronts.StorefrontItem.objects.filter(district__isnull=False):
district = item.district
item.district_project_count = self.get_district_project_count(district)
item.save()

def get_num_entries_count(self):
num_comments = Comment.objects.all().count()
num_items = Item.objects.all().count()
return num_comments + num_items

def get_num_projects_count(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

def get_district_project_count(self, district):
projects = Project.objects.filter(
Q(access=Access.PUBLIC) | Q(access=Access.SEMIPUBLIC),
administrative_district=district,
is_draft=False,
is_archived=False,
)
plans = Plan.objects.filter(
district=district,
status=0,
is_draft=False,
)
active_project_count = plans.count()
for project in projects:
if project.active_phase or project.future_phases:
active_project_count += 1
return active_project_count
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,
),
]
55 changes: 33 additions & 22 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,28 +63,55 @@ 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,
is_draft=False,
is_archived=False,
)
plans = Plan.objects.filter(district=self.district, status=0)
plans = Plan.objects.filter(
district=self.district,
status=0,
is_draft=False,
)
active_project_count = plans.count()
for project in projects:
if project.active_phase or project.future_phases:
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 +123,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 2151bd6

Please sign in to comment.