diff --git a/BackEnd/administration/filters.py b/BackEnd/administration/filters.py index a025bca6..926ef6e3 100644 --- a/BackEnd/administration/filters.py +++ b/BackEnd/administration/filters.py @@ -21,8 +21,8 @@ class UsersFilter(FilterSet): company_name = filters.CharFilter( field_name="profile__name", lookup_expr="icontains" ) - registration_date = filters.CharFilter( - field_name="profile__created_at", lookup_expr="icontains" + registration_date = filters.DateFilter( + field_name="profile__created_at", ) def is_deleted_filter(self, queryset, name, value): @@ -45,6 +45,40 @@ def is_deleted_filter(self, queryset, name, value): ) +class ProfilesFilter(FilterSet): + """ + Filters + /?name= , /?representative= , /?official_name= , /?phone= , /?address= , + /?created_at= , /?updated_at= , + Ordering sample: + /?ordering=id asc or /?ordering=-id desc + """ + + name = filters.CharFilter(lookup_expr="icontains") + representative = filters.CharFilter( + field_name="representative", lookup_expr="icontains" + ) + official_name = filters.CharFilter(lookup_expr="icontains") + phone = filters.CharFilter(lookup_expr="icontains") + address = filters.CharFilter(lookup_expr="icontains") + created_at = filters.DateFilter() + updated_at = filters.DateFilter() + + ordering = filters.OrderingFilter( + fields=( + ("name", "name"), + ("is_registered", "is_registered"), + ("representative", "representative"), + ("official_name", "official_name"), + ("phone", "phone"), + ("address", "address"), + ("status", "status"), + ("created_at", "created_at"), + ("updated_at", "updated_at"), + ) + ) + + class CategoriesFilter(FilterSet): id = filters.NumberFilter(lookup_expr="contains") name = filters.CharFilter(lookup_expr="icontains") diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index ca0c6a8b..4c23de29 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -6,8 +6,13 @@ from profiles.models import ( Profile, Region, + Activity, Category, ) +from utils.administration.profiles.profiles_functions import ( + format_company_type, + format_business_entity, +) from utils.administration.create_password import generate_password from utils.administration.send_email import send_email_about_admin_registration from .models import AutoModeration, ModerationEmail @@ -24,6 +29,15 @@ class Meta: ) +class ActivitiesSerializer(serializers.ModelSerializer): + class Meta: + model = Activity + fields = ( + "id", + "name", + ) + + class AdminRegistrationSerializer(serializers.Serializer): email = serializers.EmailField( write_only=True, @@ -107,25 +121,35 @@ def get_company_name(self, obj) -> bool: class AdminCompanyListSerializer(serializers.ModelSerializer): person = AdminUserDetailSerializer(read_only=True) - regions = AdminRegionSerializer(many=True, read_only=True) + company_type = serializers.SerializerMethodField(read_only=True) + activities = ActivitiesSerializer(many=True, read_only=True) + representative = serializers.CharField(read_only=True) + business_entity = serializers.SerializerMethodField(read_only=True) class Meta: model = Profile fields = ( "id", "name", - "is_registered", - "is_startup", "person", - "person_position", - "regions", - "official_name", + "business_entity", "phone", - "edrpou", "address", + "status", + "updated_at", + "created_at", "is_deleted", + "company_type", + "activities", + "representative", ) + def get_company_type(self, obj) -> str: + return format_company_type(obj) + + def get_business_entity(self, obj) -> str: + return format_business_entity(obj) + class AdminCompanyDetailSerializer(serializers.ModelSerializer): person = AdminUserDetailSerializer(read_only=True) diff --git a/BackEnd/administration/tests/test_admin_profiles.py b/BackEnd/administration/tests/test_admin_profiles.py index 24bb4a27..6034f47d 100644 --- a/BackEnd/administration/tests/test_admin_profiles.py +++ b/BackEnd/administration/tests/test_admin_profiles.py @@ -55,8 +55,6 @@ def test_get_profiles_structure_json(self): { "id": AnyInt(), "name": "Test person", - "is_registered": True, - "is_startup": True, "person": { "name": AnyStr(), "surname": AnyStr(), @@ -66,11 +64,14 @@ def test_get_profiles_structure_json(self): "is_superuser": False, "company_name": True, }, - "person_position": "Test person position", - "regions": [], - "official_name": "Test official name", + "business_entity": AnyStr(), + "status": AnyStr(), + "updated_at": AnyStr(), + "created_at": AnyStr(), + "company_type": AnyStr(), + "activities": [], + "representative": AnyStr(), "phone": "380112909099", - "edrpou": "10000003", "address": "Test Country, Test City, St. Test, 1", "is_deleted": False, } diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 6b246324..af6dfb65 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -1,3 +1,5 @@ +from django.db.models.functions import Concat +from django.db.models import F, Value, CharField from django.http import JsonResponse from django.views import View from django.db.models import Count, Q @@ -39,7 +41,7 @@ from utils.administration.send_email_feedback import send_email_feedback from django_filters.rest_framework import DjangoFilterBackend -from .filters import UsersFilter, CategoriesFilter +from .filters import UsersFilter, ProfilesFilter, CategoriesFilter from utils.administration.send_email_notification import send_email_to_user from .filters import UsersFilter @@ -88,16 +90,26 @@ def destroy(self, request, *args, **kwargs): class ProfilesListView(ListAPIView): """ - List of profiles. + View to list profiles with optional filtering and ordering. """ permission_classes = [IsStaffUser] pagination_class = ListPagination serializer_class = AdminCompanyListSerializer + filter_backends = [DjangoFilterBackend] + filterset_class = ProfilesFilter queryset = ( Profile.objects.select_related("person") .prefetch_related("regions", "categories", "activities") .order_by("id") + .annotate( + representative=Concat( + F("person__name"), + Value(" "), + F("person__surname"), + output_field=CharField(), + ) + ) ) diff --git a/BackEnd/utils/administration/profiles/profiles_functions.py b/BackEnd/utils/administration/profiles/profiles_functions.py new file mode 100644 index 00000000..fb855233 --- /dev/null +++ b/BackEnd/utils/administration/profiles/profiles_functions.py @@ -0,0 +1,14 @@ +def format_company_type(obj): + if obj.is_startup and obj.is_registered: + return "Компанія і стартап" + if obj.is_registered: + return "Компанія" + if obj.is_startup: + return "Стартап" + return None + + +def format_business_entity(obj): + if obj.is_fop: + return "ФОП" + return "Юридична особа"