-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apps/cms: use model fields for storefront counts, add management comm…
…and to update numbers
- Loading branch information
1 parent
929aee9
commit 2151bd6
Showing
6 changed files
with
136 additions
and
22 deletions.
There are no files selected for viewing
Empty file.
Empty file.
58 changes: 58 additions & 0 deletions
58
meinberlin/apps/cms/management/commands/update_storefront_counts.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
27 changes: 27 additions & 0 deletions
27
meinberlin/apps/cms/migrations/0040_storefront_add_numbers_as_fields.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
meinberlin/apps/cms/migrations/0041_storefrontitem_district_project_count.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters