Skip to content

Commit

Permalink
fix(Secteurs d'activités): Mise à jour du calcul du nombre de secteur…
Browse files Browse the repository at this point in the history
…s pour le déduire des activités associées (#1564)
  • Loading branch information
SebastienReuiller authored Dec 5, 2024
1 parent f37debf commit 7ab2f2a
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def handle(self, *args, **options):
for index, siae in enumerate(siae_queryset):
# M2M
siae.user_count = siae.users.count()
siae.sector_count = siae.sectors.count()
siae.sector_count = siae.activities.values_list("sector_group").distinct().count()
siae.network_count = siae.networks.count()
siae.group_count = siae.groups.count()
# FK
Expand Down
25 changes: 13 additions & 12 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,15 +1227,16 @@ def elasticsearch_index_metadata(self):

return metadata

def sectors_list_string(self, display_max=3):
sectors_name_list = self.sectors.form_filter_queryset().values_list("name", flat=True)
def sector_groups_list_string(self, display_max=3):
# Retrieve sectors from activities instead of directly from the sectors field
sectors_name_list = set(self.activities.values_list("sector_group__name", flat=True))
if display_max and len(sectors_name_list) > display_max:
sectors_name_list = sectors_name_list[:display_max]
sectors_name_list.append("…")
return ", ".join(sectors_name_list)

def sectors_full_list_string(self):
return self.sectors_list_string(display_max=None)
def sector_groups_full_list_string(self):
return self.sector_groups_list_string(display_max=None)

@cached_property
def stat_view_count_last_3_months(self):
Expand Down Expand Up @@ -1307,14 +1308,6 @@ def siae_users_changed(sender, instance, action, **kwargs):
instance.save()


@receiver(m2m_changed, sender=Siae.sectors.through)
def siae_sectors_changed(sender, instance, action, **kwargs):
if isinstance(instance, Siae):
if action in ("post_add", "post_remove", "post_clear"):
instance.sector_count = instance.sectors.count()
instance.save()


@receiver(m2m_changed, sender=Siae.networks.through)
def siae_networks_changed(sender, instance, action, **kwargs):
if isinstance(instance, Siae):
Expand Down Expand Up @@ -1614,6 +1607,14 @@ def __str__(self):
return self.name


@receiver(post_save, sender=SiaeActivity)
@receiver(post_delete, sender=SiaeActivity)
def siae_activity_post_save(sender, instance, **kwargs):
"""Update sector_count when SiaeActivity is created or updated."""
instance.siae.sector_count = instance.siae.activities.values("sector_group").distinct().count()
instance.siae.save()


class SiaeClientReference(models.Model):
name = models.CharField(verbose_name="Nom", max_length=255, blank=True)
description = models.TextField(verbose_name="Description", blank=True)
Expand Down
77 changes: 76 additions & 1 deletion lemarche/siaes/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import factory
from django.core.management import call_command
from django.db.models import signals
from django.test import TransactionTestCase

from lemarche.perimeters.factories import PerimeterFactory
from lemarche.perimeters.models import Perimeter
from lemarche.sectors.factories import SectorFactory
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.factories import SiaeFactory
from lemarche.siaes.factories import SiaeActivityFactory, SiaeFactory
from lemarche.siaes.models import SiaeActivity
from lemarche.users.factories import UserFactory


class SiaeActivitiesCreateCommandTest(TransactionTestCase):
Expand Down Expand Up @@ -153,3 +156,75 @@ def test_create_activities(self):
self.assertEqual(siae5_activities.first().presta_type, [siae_constants.PRESTA_DISP])
self.assertEqual(siae5_activities.first().geo_range, siae_constants.GEO_RANGE_ZONES)
self.assertEqual(siae5_activities.first().locations.count(), 0)


class SiaeUpdateCountFieldsCommandTest(TransactionTestCase):
@factory.django.mute_signals(signals.post_save, signals.m2m_changed)
def test_update_count_fields(self):
"""
Create two siaes with users and activities, and check that the count fields are updated correctly
"""
siae_1 = SiaeFactory()
for _ in range(3):
user = UserFactory()
siae_1.users.add(user)
SiaeActivityFactory.create_batch(2, siae=siae_1)

self.assertEqual(siae_1.user_count, 0)
self.assertEqual(siae_1.sector_count, 0)
self.assertEqual(siae_1.network_count, 0)
self.assertEqual(siae_1.group_count, 0)
self.assertEqual(siae_1.offer_count, 0)
self.assertEqual(siae_1.client_reference_count, 0)
self.assertEqual(siae_1.label_count, 0)
self.assertEqual(siae_1.image_count, 0)
self.assertEqual(siae_1.etablissement_count, 0)
self.assertEqual(siae_1.completion_rate, None)
self.assertEqual(siae_1.tender_count, 0)
self.assertEqual(siae_1.tender_email_send_count, 0)
self.assertEqual(siae_1.tender_email_link_click_count, 0)
self.assertEqual(siae_1.tender_detail_display_count, 0)
self.assertEqual(siae_1.tender_detail_contact_click_count, 0)

siae_2 = SiaeFactory()
for _ in range(2):
user = UserFactory()
siae_2.users.add(user)
SiaeActivityFactory.create_batch(4, siae=siae_2)

self.assertEqual(siae_2.user_count, 0)
self.assertEqual(siae_2.sector_count, 0)

call_command("update_siae_count_fields")
siae_1.refresh_from_db()
self.assertEqual(siae_1.user_count, 3)
self.assertEqual(siae_1.sector_count, 2)
siae_2.refresh_from_db()
self.assertEqual(siae_2.user_count, 2)
self.assertEqual(siae_2.sector_count, 4)

@factory.django.mute_signals(signals.post_save, signals.m2m_changed)
def test_update_count_fields_with_id(self):
"""
Create two siaes with users and activities, and check that the count fields are updated correctly
Only for the siae with id
"""
siae_to_update = SiaeFactory()
for _ in range(3):
user = UserFactory()
siae_to_update.users.add(user)
SiaeActivityFactory.create_batch(2, siae=siae_to_update)

siae_not_updated = SiaeFactory()
for _ in range(3):
user = UserFactory()
siae_not_updated.users.add(user)
SiaeActivityFactory.create_batch(2, siae=siae_not_updated)

call_command("update_siae_count_fields", id=siae_to_update.id)
siae_to_update.refresh_from_db()
self.assertEqual(siae_to_update.user_count, 3)
self.assertEqual(siae_to_update.sector_count, 2)
siae_not_updated.refresh_from_db()
self.assertEqual(siae_not_updated.user_count, 0)
self.assertEqual(siae_not_updated.sector_count, 0)
18 changes: 7 additions & 11 deletions lemarche/siaes/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from lemarche.networks.factories import NetworkFactory
from lemarche.perimeters.factories import PerimeterFactory
from lemarche.perimeters.models import Perimeter
from lemarche.sectors.factories import SectorFactory
from lemarche.siaes import constants as siae_constants, utils as siae_utils
from lemarche.siaes.factories import (
SiaeActivityFactory,
SiaeClientReferenceFactory,
SiaeFactory,
SiaeGroupFactory,
Expand Down Expand Up @@ -171,8 +171,7 @@ def test_is_missing_content_property(self):
)
self.assertTrue(score_completion_before < siae_full.completion_rate_calculated)
score_completion_before = siae_full.completion_rate_calculated
sector = SectorFactory()
siae_full.sectors.add(sector)
SiaeActivityFactory(siae=siae_full)
SiaeOfferFactory(siae=siae_full)
SiaeLabelOldFactory(siae=siae_full)
siae_full.save() # to update stats
Expand All @@ -186,7 +185,7 @@ def test_is_missing_content_property(self):
# contact_phone="0000000000",
description="test",
)
siae_full_2.sectors.add(sector)
SiaeActivityFactory(siae=siae_full_2)
SiaeOfferFactory(siae=siae_full_2)
SiaeLabelOldFactory(siae=siae_full_2)
siae_full_2.save() # to update stats
Expand Down Expand Up @@ -265,13 +264,11 @@ def test_update_related_offer_count_on_save(self):
siae.save()
self.assertEqual(siae.offer_count, 1)

def test_update_m2m_sector_count_on_save(self):
def test_update_o2m_sector_count_on_save(self):
siae = SiaeFactory()
sector = SectorFactory()
self.assertEqual(siae.sector_count, 0)
siae.sectors.add(sector)
self.assertEqual(siae.sectors.count(), 1)
# siae.save() # no need to run save(), m2m_changed signal was triggered above
SiaeActivityFactory(siae=siae)
self.assertEqual(siae.activities.count(), 1)
self.assertEqual(siae.sector_count, 1)

def test_update_m2m_network_count_on_save(self):
Expand Down Expand Up @@ -328,8 +325,7 @@ def test_update_content_fill_date_on_save(self):
siae = SiaeFactory(description="")
user = UserFactory()
siae.users.add(user)
sector = SectorFactory()
siae.sectors.add(sector)
SiaeActivityFactory(siae=siae)
self.assertIsNone(siae.content_filled_basic_date)
siae.description = "test"
siae.save()
Expand Down
2 changes: 1 addition & 1 deletion lemarche/templates/dashboard/siae_edit_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h3 class="fr-alert__title">Votre fiche n'est pas complète, et donc moins visib
<ol class="mb-0">
{% if not siae.sector_count %}
<li>
<a href="{% url 'dashboard_siaes:siae_edit_search' siae.slug %}">Ajoutez un ou plusieurs secteurs d'activité</a>
<a href="{% url 'dashboard_siaes:siae_edit_activities' siae.slug %}">Ajoutez un ou plusieurs secteurs d'activité</a>
</li>
{% endif %}
{% if not siae.description and not siae.logo_url %}
Expand Down
2 changes: 1 addition & 1 deletion lemarche/utils/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def generate_siae_row(siae: Siae, siae_field_list):
col_value = "Oui" if getattr(siae, field_name, None) else "Non"
# ManyToManyFields
elif field_name == "sectors":
col_value = siae.sectors_full_list_string()
col_value = siae.sector_groups_full_list_string()
# Complex fields
elif field_name == "contact_phone":
col_value = siae.contact_phone_display
Expand Down

0 comments on commit 7ab2f2a

Please sign in to comment.