Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Entreprises] Tâche pour rattacher automatiquement les utilisateurs tous les lundi #979

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions clevercloud/companies_update_users.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash -l

# Update company users

# Do not run if this env var is not set:
if [[ -z "$CRON_UPDATE_COMPANY_USERS_ENABLED" ]]; then
echo "CRON_UPDATE_COMPANY_USERS_ENABLED not set. Exiting..."
exit 0
fi

# About clever cloud cronjobs:
# https://www.clever-cloud.com/doc/tools/crons/

if [[ "$INSTANCE_NUMBER" != "0" ]]; then
echo "Instance number is ${INSTANCE_NUMBER}. Stop here."
exit 0
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
11 changes: 6 additions & 5 deletions clevercloud/cron.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"30 0 * * * $ROOT/clevercloud/stats_export_user_search_list_to_file.sh",
"0 1 * * * $ROOT/clevercloud/tenders_update_count_fields.sh",
"0 7 * * 1 $ROOT/clevercloud/siaes_sync_c1_c4.sh",
"10 7 * * 1 $ROOT/clevercloud/siaes_sync_c2_c4.sh",
"20 7 * * 1 $ROOT/clevercloud/siaes_update_api_entreprise_fields.sh",
"30 7 * * 1 $ROOT/clevercloud/siaes_update_api_qpv_fields.sh",
"40 7 * * 1 $ROOT/clevercloud/siaes_update_api_zrr_fields.sh",
"50 7 * * 1 $ROOT/clevercloud/siaes_update_count_fields.sh",
"5 7 * * 1 $ROOT/clevercloud/siaes_sync_c2_c4.sh",
"10 7 * * 1 $ROOT/clevercloud/siaes_update_api_entreprise_fields.sh",
"15 7 * * 1 $ROOT/clevercloud/siaes_update_api_qpv_fields.sh",
"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/companies_update_users.sh",
"0 7 * * 2 $ROOT/clevercloud/siaes_send_completion_reminder_emails.sh",
"0 8 * * * $ROOT/clevercloud/siaes_send_user_request_reminder_emails.sh",
"30 8 * * * $ROOT/clevercloud/tenders_send_author_transactioned_question_emails.sh",
Expand Down
12 changes: 12 additions & 0 deletions lemarche/companies/management/commands/set_company_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 @@ -19,6 +20,9 @@ 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 @@ -64,3 +68,11 @@ def handle(self, *args, **options):
self.stdout_info(f"Users with company count: {users_with_company.count()}")
self.stdout_info(f"Companies with email_domain_list field count: {companies_with_email_domain_list.count()}")
self.stdout_info(f"Companies with users count: {companies_with_users.count()}")

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()
self.stdout_info("Finished updating Company.user_count!")
17 changes: 17 additions & 0 deletions lemarche/companies/migrations/0003_company_user_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.2 on 2023-11-16 08:54

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("companies", "0002_company_email_domain_list"),
]

operations = [
migrations.AddField(
model_name="company",
name="user_count",
field=models.IntegerField(default=0, verbose_name="Nombre d'utilisateurs"),
),
]
3 changes: 3 additions & 0 deletions lemarche/companies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Company(models.Model):
default=list,
)

# stats
user_count = models.IntegerField("Nombre d'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