Skip to content

Commit

Permalink
feat(Besoins): Admin : filtre de la liste par bizdev (#1134)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Mar 20, 2024
1 parent a58f4af commit 078b95a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
21 changes: 21 additions & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ def queryset(self, request, queryset):
return queryset


class UserAdminFilter(admin.SimpleListFilter):
title = "Suivi bizdev"
parameter_name = "admins"

def lookups(self, request, model_admin):
admins = User.objects.is_admin_bizdev().values("id", "first_name")
admins_choices = [(admin["id"], admin["first_name"]) for admin in admins]
admins_choices += [("None", "Sans bizdev")]
return admins_choices

def queryset(self, request, queryset):
lookup_value = self.value()
if lookup_value:
if lookup_value == "None":
queryset = queryset.filter(admins__isnull=True)
else:
queryset = queryset.filter(admins__id__exact=lookup_value)
return queryset


class TenderNoteInline(GenericTabularInline):
model = Note
fields = ["text", "author", "created_at", "updated_at"]
Expand Down Expand Up @@ -206,6 +226,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin):
"start_working_date",
ResponseKindFilter,
"siae_transactioned",
UserAdminFilter,
]
advanced_filter_fields = (
"kind",
Expand Down
6 changes: 6 additions & 0 deletions lemarche/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class UserQueryset(models.QuerySet):
Custom queryset with additional filtering methods for users.
"""

def is_admin_bizdev(self):
return self.filter(kind=user_constants.KIND_ADMIN, position__iexact="Bizdev", is_staff=True)

def has_company(self):
return self.filter(company__isnull=False).distinct()

Expand Down Expand Up @@ -88,6 +91,9 @@ def create_superuser(self, email, password, **extra_fields):

return self.create_user(email, password, **extra_fields)

def is_admin_bizdev(self):
return self.get_queryset().is_admin_bizdev()

def has_company(self):
return self.get_queryset().has_company()

Expand Down
20 changes: 16 additions & 4 deletions lemarche/users/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.test import TestCase

from lemarche.companies.factories import CompanyFactory
from lemarche.favorites.factories import FavoriteListFactory
from lemarche.siaes.factories import SiaeFactory
from lemarche.tenders.factories import TenderFactory
Expand Down Expand Up @@ -49,10 +50,22 @@ class UserModelQuerysetTest(TestCase):
def setUpTestData(cls):
cls.user = UserFactory()

def test_is_admin_bizdev(self):
UserFactory(kind=user_constants.KIND_ADMIN, is_staff=True)
UserFactory(kind=user_constants.KIND_ADMIN, is_staff=True, position="BizDev")
UserFactory(kind=user_constants.KIND_ADMIN, is_staff=True, position="Bizdev")
self.assertEqual(User.objects.count(), 1 + 3)
self.assertEqual(User.objects.is_admin_bizdev().count(), 2)

def test_has_company(self):
user_2 = UserFactory()
CompanyFactory(users=[user_2])
self.assertEqual(User.objects.count(), 1 + 1)
self.assertEqual(User.objects.has_company().count(), 1)

def test_has_siae(self):
user_2 = UserFactory()
siae = SiaeFactory()
siae.users.add(user_2)
SiaeFactory(users=[user_2])
self.assertEqual(User.objects.count(), 1 + 1)
self.assertEqual(User.objects.has_siae().count(), 1)

Expand All @@ -75,8 +88,7 @@ def test_has_api_key(self):

def test_with_siae_stats(self):
user_2 = UserFactory()
siae = SiaeFactory()
siae.users.add(user_2)
SiaeFactory(users=[user_2])
self.assertEqual(User.objects.count(), 1 + 1)
self.assertEqual(User.objects.with_siae_stats().filter(id=self.user.id).first().siae_count_annotated, 0)
self.assertEqual(User.objects.with_siae_stats().filter(id=user_2.id).first().siae_count_annotated, 1)
Expand Down

0 comments on commit 078b95a

Please sign in to comment.