Skip to content

Commit

Permalink
feat(Entreprises): nouveau champ user_tender_count, nouvelle commande…
Browse files Browse the repository at this point in the history
… pour mettre les count à jour (#1132)
  • Loading branch information
raphodn authored Mar 20, 2024
1 parent 7438756 commit a58f4af
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ fi
# $APP_HOME is set by default by clever cloud.
cd $APP_HOME

# django-admin set_company_users --with-count
django-admin set_company_users --only-add --with-count
django-admin set_company_users --only-add
django-admin update_company_count_fields
2 changes: 1 addition & 1 deletion clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"20 7 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh",
"25 7 * * 1 $ROOT/clevercloud/siaes_update_count_fields.sh",
"30 7 * * 1 $ROOT/clevercloud/siaes_update_super_badge_field.sh",
"50 7 * * 1 $ROOT/clevercloud/companies_update_users.sh",
"50 7 * * 1 $ROOT/clevercloud/companies_update_users_and_count_fields.sh",
"55 7 * * 1 $ROOT/clevercloud/crm_brevo_sync.sh",
"0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh",
"0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh",
Expand Down
12 changes: 0 additions & 12 deletions lemarche/companies/management/commands/set_company_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class Command(BaseCommand):
Usage:
- poetry run python manage.py set_company_users --dry-run
- poetry run python manage.py set_company_users --only-add
- poetry run python manage.py set_company_users --only-add --with-count
- poetry run python manage.py set_company_users
"""

Expand All @@ -21,9 +20,6 @@ def add_arguments(self, parser):
parser.add_argument(
"--only-add", dest="only_add", action="store_true", help="Only add new users, don't delete existing"
)
parser.add_argument(
"--with-count", dest="with_count", action="store_true", help="Update user_count at the end"
)

def handle(self, *args, **options):
self.stdout_info("-" * 80)
Expand Down Expand Up @@ -72,13 +68,5 @@ def handle(self, *args, **options):
f"Companies with user: before {old_companies_with_user_count} / after {new_companies_with_user_count} / {new_companies_with_user_count-old_companies_with_user_count}", # noqa
]

if options["with_count"]:
self.stdout_info("-" * 80)
self.stdout_info("Updating Company.user_count fields")
for company in Company.objects.prefetch_related("users").all():
company.user_count = company.users.count()
company.save()
msg_success.append("Also updated Company.user_count field")

self.stdout_messages_success(msg_success)
api_slack.send_message_to_channel("\n".join(msg_success))
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 lemarche/companies/migrations/0004_company_user_tender_count.py
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"),
),
]
3 changes: 2 additions & 1 deletion lemarche/companies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def with_user_stats(self):


class Company(models.Model):
FIELDS_STATS_COUNT = ["user_count"]
FIELDS_STATS_COUNT = ["user_count", "user_tender_count"]
FIELDS_STATS_TIMESTAMPS = ["created_at", "updated_at"]
READONLY_FIELDS = FIELDS_STATS_COUNT + FIELDS_STATS_TIMESTAMPS

Expand All @@ -40,6 +40,7 @@ class Company(models.Model):

# stats
user_count = models.IntegerField("Nombre d'utilisateurs", default=0)
user_tender_count = models.IntegerField("Nombre de besoins déposés par les utilisateurs", default=0)

created_at = models.DateTimeField(verbose_name="Date de création", default=timezone.now)
updated_at = models.DateTimeField(verbose_name="Date de modification", auto_now=True)
Expand Down

0 comments on commit a58f4af

Please sign in to comment.