-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Entreprises): nouveau champ user_tender_count, nouvelle commande…
… pour mettre les count à jour (#1132)
- Loading branch information
Showing
6 changed files
with
66 additions
and
16 deletions.
There are no files selected for viewing
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
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
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
44 changes: 44 additions & 0 deletions
44
lemarche/companies/management/commands/update_company_count_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,44 @@ | ||
from lemarche.companies.models import Company | ||
from lemarche.utils.apis import api_slack | ||
from lemarche.utils.commands import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Goal: update the '_count' fields of each Company | ||
Usage: | ||
- poetry run python manage.py update_company_count_fields | ||
""" | ||
|
||
def handle(self, *args, **options): | ||
self.stdout_messages_info("Updating Company count fields...") | ||
|
||
# Step 1a: build the queryset | ||
company_queryset = Company.objects.has_email_domain().with_user_stats() | ||
self.stdout_messages_info(f"Found {company_queryset.count()} companies with an email_domain") | ||
|
||
# Step 1b: init fields to update | ||
update_fields = Company.FIELDS_STATS_COUNT | ||
self.stdout_messages_info(f"Fields to update: {update_fields}") | ||
|
||
# Step 2: loop on each Company with an email domain | ||
progress = 0 | ||
for index, company in enumerate(company_queryset): | ||
company.user_count = company.user_count_annotated | ||
company.user_tender_count = company.user_tender_count_annotated | ||
|
||
# Step 3: update count fields | ||
company.save(update_fields=update_fields) | ||
|
||
progress += 1 | ||
if (progress % 50) == 0: | ||
self.stdout_info(f"{progress}...") | ||
|
||
msg_success = [ | ||
"----- Company count fields -----", | ||
f"Done! Processed {company_queryset.count()} companies with an email_domain", | ||
f"Fields updated: {update_fields}", | ||
] | ||
self.stdout_messages_success(msg_success) | ||
api_slack.send_message_to_channel("\n".join(msg_success)) |
17 changes: 17 additions & 0 deletions
17
lemarche/companies/migrations/0004_company_user_tender_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,17 @@ | ||
# Generated by Django 4.2.9 on 2024-03-20 09:03 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("companies", "0003_company_user_count"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="company", | ||
name="user_tender_count", | ||
field=models.IntegerField(default=0, verbose_name="Nombre de besoins déposés par les utilisateurs"), | ||
), | ||
] |
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