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

feat(Besoins): Admin : Connaitre le nombre de besoins déposés par chaque entreprise #1120

Merged
merged 2 commits into from
Mar 7, 2024
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
24 changes: 21 additions & 3 deletions lemarche/companies/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,22 @@ def queryset(self, request, queryset):

@admin.register(Company, site=admin_site)
class CompanyAdmin(admin.ModelAdmin, DynamicArrayMixin):
list_display = ["id", "name", "user_count_annotated_with_link", "created_at"]
list_display = [
"id",
"name",
"user_count_annotated_with_link",
"user_tender_count_annotated_with_link",
"created_at",
]
list_filter = [HasUserFilter, HasEmailDomainFilter]
search_fields = ["id", "name"]
search_help_text = "Cherche sur les champs : ID, Nom"

readonly_fields = ["logo_url_display", "user_count_annotated_with_link", "created_at", "updated_at"]
readonly_fields = [
"logo_url_display",
"user_count_annotated_with_link",
"user_tender_count_annotated_with_link",
] + Company.READONLY_FIELDS

fieldsets = (
(
Expand All @@ -57,6 +67,7 @@ class CompanyAdmin(admin.ModelAdmin, DynamicArrayMixin):
),
("Logo", {"fields": ("logo_url", "logo_url_display")}),
("Utilisateurs", {"fields": ("user_count_annotated_with_link",)}),
("Besoins", {"fields": ("user_tender_count_annotated_with_link",)}),
("Dates", {"fields": ("created_at", "updated_at")}),
)

Expand Down Expand Up @@ -93,5 +104,12 @@ def user_count_annotated_with_link(self, company):
url = reverse("admin:users_user_changelist") + f"?company__id__exact={company.id}"
return format_html(f'<a href="{url}">{company.user_count_annotated}</a>')

user_count_annotated_with_link.short_description = "Nombre d'utilisateurs rattachés"
user_count_annotated_with_link.short_description = "Utilisateurs rattachés"
user_count_annotated_with_link.admin_order_field = "user_count_annotated"

def user_tender_count_annotated_with_link(self, company):
url = reverse("admin:tenders_tender_changelist") + f"?author__company_id__exact={company.id}"
return format_html(f'<a href="{url}">{company.user_tender_count_annotated}</a>')

user_tender_count_annotated_with_link.short_description = "Besoins déposés par les utilisateurs"
user_tender_count_annotated_with_link.admin_order_field = "user_tender_count_annotated"
8 changes: 7 additions & 1 deletion lemarche/companies/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ def has_email_domain(self):
return self.exclude(email_domain_list=[])

def with_user_stats(self):
return self.annotate(user_count_annotated=Count("users", distinct=True))
return self.annotate(user_count_annotated=Count("users", distinct=True)).annotate(
user_tender_count_annotated=Count("users__tenders", distinct=True)
)


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

name = models.CharField(verbose_name="Nom", max_length=255)
slug = models.SlugField(verbose_name="Slug", max_length=255, unique=True)
description = models.TextField(verbose_name="Description", blank=True)
Expand Down
7 changes: 7 additions & 0 deletions lemarche/companies/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from lemarche.companies.factories import CompanyFactory
from lemarche.companies.models import Company
from lemarche.tenders.factories import TenderFactory
from lemarche.users.factories import UserFactory


Expand All @@ -22,10 +23,16 @@ class CompanyQuerysetTest(TestCase):
def setUpTestData(cls):
cls.user_1 = UserFactory()
cls.user_2 = UserFactory()
TenderFactory(author=cls.user_1)
TenderFactory(author=cls.user_1)
cls.company_with_users = CompanyFactory(users=[cls.user_1, cls.user_2])
cls.company = CompanyFactory()

def test_with_user_stats(self):
company_queryset = Company.objects.with_user_stats()
# user_count
self.assertEqual(company_queryset.get(id=self.company.id).user_count_annotated, 0)
self.assertEqual(company_queryset.get(id=self.company_with_users.id).user_count_annotated, 2)
# user_tender_count
self.assertEqual(company_queryset.get(id=self.company.id).user_tender_count_annotated, 0)
self.assertEqual(company_queryset.get(id=self.company_with_users.id).user_tender_count_annotated, 2)
Loading