Skip to content

Commit

Permalink
Add management command dedicated to updating counts
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Mar 20, 2024
1 parent ae28fef commit 32c6ed6
Show file tree
Hide file tree
Showing 5 changed files with 49 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,45 @@
from lemarche.companies.models import Company
from lemarche.utils.apis import api_slack
from lemarche.utils.commands import BaseCommand


class Command(BaseCommand):
"""
Command to attach users to their company, depending on their e-mail address
Only for companies that have their 'email_domain_list' field set
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))
2 changes: 1 addition & 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 Down

0 comments on commit 32c6ed6

Please sign in to comment.