wbcrm 2.2.4__py2.py3-none-any.whl → 2.2.6__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of wbcrm might be problematic. Click here for more details.

@@ -0,0 +1,171 @@
1
+ from typing import Type
2
+
3
+ from django.db.models import Q
4
+ from django.db.models.expressions import F
5
+ from django.db.models.query import QuerySet
6
+ from django.utils.translation import gettext_lazy as _
7
+ from wbcore import serializers
8
+ from wbcore.contrib.directory.models import Person
9
+ from wbcore.serializers.serializers import Serializer
10
+ from wbcrm.models import Activity, ActivityParticipant, ActivityType
11
+ from wbcrm.serializers import ActivityTypeRepresentationSerializer
12
+ from wbhuman_resources.models.kpi import KPI, KPIHandler
13
+ from wbhuman_resources.serializers import KPIModelSerializer
14
+
15
+
16
+ class NumberOfActivityKPISerializer(KPIModelSerializer):
17
+ activity_type = serializers.PrimaryKeyRelatedField(
18
+ required=True, many=True, queryset=ActivityType.objects.all(), label=_("Activity Type")
19
+ )
20
+ _activity_type = ActivityTypeRepresentationSerializer(source="activity_type", many=True)
21
+
22
+ activity_area = serializers.ChoiceField(
23
+ default="all",
24
+ choices=[("only_internal", _("Only Internal")), ("only_external", _("Only External")), ("all", _("All"))],
25
+ )
26
+ person_participates = serializers.BooleanField(
27
+ default=True,
28
+ label=_("Participants of Activity"),
29
+ help_text=_("Activities considered are related to the participants"),
30
+ )
31
+ person_created = serializers.BooleanField(
32
+ default=True, label=_("Creator of activity"), help_text=_("Activities considered are related to the creator")
33
+ )
34
+ person_assigned = serializers.BooleanField(
35
+ default=True,
36
+ label=_("Person Assigned"),
37
+ help_text=_("Activities considered are related to the persons assigned"),
38
+ )
39
+
40
+ def update(self, instance, validated_data):
41
+ activity_type = validated_data.get(
42
+ "activity_type",
43
+ instance.additional_data["serializer_data"].get(
44
+ "activity_type", list(ActivityType.objects.values_list("id", flat=True))
45
+ ),
46
+ )
47
+ activity_area = validated_data.get(
48
+ "activity_area",
49
+ instance.additional_data["serializer_data"].get("activity_area", "all"),
50
+ )
51
+
52
+ person_participates = validated_data.get(
53
+ "person_participates",
54
+ instance.additional_data["serializer_data"].get("person_participates", True),
55
+ )
56
+ person_created = validated_data.get(
57
+ "person_created",
58
+ instance.additional_data["serializer_data"].get("person_created", True),
59
+ )
60
+ person_assigned = validated_data.get(
61
+ "person_assigned",
62
+ instance.additional_data["serializer_data"].get("person_assigned", True),
63
+ )
64
+
65
+ additional_data = instance.additional_data
66
+ additional_data["serializer_data"]["activity_type"] = (
67
+ [_type.id for _type in validated_data.get("activity_type")]
68
+ if validated_data.get("activity_type")
69
+ else activity_type
70
+ )
71
+ additional_data["serializer_data"]["activity_area"] = activity_area
72
+ additional_data["serializer_data"]["person_participates"] = person_participates
73
+ additional_data["serializer_data"]["person_created"] = person_created
74
+ additional_data["serializer_data"]["person_assigned"] = person_assigned
75
+
76
+ additional_data["list_data"] = instance.get_handler().get_list_data(additional_data["serializer_data"])
77
+ validated_data["additional_data"] = additional_data
78
+
79
+ return super().update(instance, validated_data)
80
+
81
+ class Meta(KPIModelSerializer.Meta):
82
+ fields = (
83
+ *KPIModelSerializer.Meta.fields,
84
+ "activity_type",
85
+ "_activity_type",
86
+ "activity_area",
87
+ "person_participates",
88
+ "person_created",
89
+ "person_assigned",
90
+ )
91
+
92
+
93
+ class NumberOfActivityKPI(KPIHandler):
94
+ def get_name(self) -> str:
95
+ return _("Number of Activities")
96
+
97
+ def get_serializer(self) -> Type[Serializer]:
98
+ return NumberOfActivityKPISerializer
99
+
100
+ def annotate_parameters(self, queryset: QuerySet[KPI]) -> QuerySet[KPI]:
101
+ return queryset.annotate(
102
+ activity_type=F("additional_data__serializer_data__activity_type"),
103
+ activity_area=F("additional_data__serializer_data__activity_area"),
104
+ person_participates=F("additional_data__serializer_data__person_participates"),
105
+ person_created=F("additional_data__serializer_data__person_created"),
106
+ person_assigned=F("additional_data__serializer_data__person_assigned"),
107
+ )
108
+
109
+ def get_list_data(self, serializer_data: dict) -> list[str]:
110
+ activity_types = list(
111
+ ActivityType.objects.filter(pk__in=serializer_data["activity_type"]).values_list("title", flat=True)
112
+ )
113
+ return [
114
+ _("Activity Type: {types}").format(types=activity_types),
115
+ _("Activity Area: {area}").format(area=serializer_data["activity_area"]),
116
+ _("Person Participates: {participating}").format(participating={serializer_data["person_participates"]}),
117
+ _("Person Created: {created}").format(created=serializer_data["person_created"]),
118
+ _("Person Assigned: {assigned}").format(assigned=serializer_data["person_assigned"]),
119
+ ]
120
+
121
+ def get_display_grid(self) -> list[list[str]]:
122
+ return [
123
+ ["activity_type"] * 3,
124
+ ["activity_area"] * 3,
125
+ ["person_created", "person_assigned", "person_participates"],
126
+ ]
127
+
128
+ def evaluate(self, kpi: "KPI", evaluated_person=None, evaluation_date=None) -> int:
129
+ persons = (
130
+ [evaluated_person.id] if evaluated_person else kpi.evaluated_persons.all().values_list("id", flat=True)
131
+ )
132
+ serializer_data = kpi.additional_data.get("serializer_data")
133
+ qs = Activity.objects.filter(
134
+ period__startswith__date__gte=kpi.period.lower,
135
+ period__endswith__date__lte=evaluation_date if evaluation_date else kpi.period.upper,
136
+ ).exclude(status=Activity.Status.CANCELLED)
137
+ qs = (
138
+ qs.filter(type__id__in=serializer_data.get("activity_type"))
139
+ if serializer_data.get("activity_type")
140
+ else qs
141
+ )
142
+
143
+ condition = None
144
+ if serializer_data.get("person_created") or serializer_data.get("person_created") is None:
145
+ condition = Q(creator__in=persons)
146
+ if serializer_data.get("person_assigned") or serializer_data.get("person_assigned") is None:
147
+ condition = (condition | Q(assigned_to__in=persons)) if condition else Q(assigned_to__in=persons)
148
+ if serializer_data.get("person_participates") or serializer_data.get("person_participates") is None:
149
+ condition = (condition | Q(participants__in=persons)) if condition else Q(participants__in=persons)
150
+ if condition:
151
+ qs = qs.filter(condition)
152
+
153
+ if (activity_area := serializer_data.get("activity_area")) and (activity_area != "all"):
154
+ participant_ids = set(
155
+ ActivityParticipant.objects.filter(activity__id__in=qs.values_list("id", flat=True)).values_list(
156
+ "participant", flat=True
157
+ )
158
+ )
159
+ employee_ids = Person.objects.filter_only_internal().values_list("id", flat=True)
160
+ externals = set(
161
+ Person.objects.filter(Q(id__in=participant_ids) & ~Q(id__in=employee_ids)).values_list("id", flat=True)
162
+ )
163
+ if activity_area == "only_internal":
164
+ qs = qs.exclude(
165
+ Q(creator__in=externals) | Q(assigned_to__in=externals) | Q(participants__id__in=externals)
166
+ )
167
+ elif activity_area == "only_external":
168
+ qs = qs.filter(
169
+ Q(creator__in=externals) | Q(assigned_to__in=externals) | Q(participants__id__in=externals)
170
+ )
171
+ return qs.distinct("id").count()