Skip to content

Commit edcec65

Browse files
authored
Merge pull request #492 from PROCOLLAB-github/fix/download_user_statistic
Fix export users activity
2 parents a8c48f8 + 6310bfc commit edcec65

1 file changed

Lines changed: 43 additions & 10 deletions

File tree

users/services/users_activity.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
from django.db.models import (
77
Count,
8+
Value,
89
QuerySet,
910
OuterRef,
1011
Subquery,
1112
IntegerField,
1213
)
14+
from django.db.models.functions import Coalesce
1315

1416
from users.models import CustomUser
1517
from news.models import News
@@ -90,18 +92,33 @@ def __prepare_user_data(self, user: CustomUser) -> dict[str, Any]:
9092

9193
def __get_user_queryset(self) -> QuerySet[CustomUser]:
9294
user_content_type = ContentType.objects.get_for_model(CustomUser)
93-
projects_in_program_subqury = (
95+
projects_in_program_subquery = (
9496
PartnerProgramUserProfile.objects
9597
.filter(user_id=OuterRef("id"))
9698
.exclude(project=None)
97-
.annotate(count=Count("id"))
98-
.values("count")
99+
.values("user_id")
100+
.annotate(total=Count("id"))
101+
.values("total")
99102
)
100-
posts_count_subqury = (
103+
posts_count_subquery = (
101104
News.objects
102105
.filter(content_type=user_content_type, object_id=OuterRef("id"))
103-
.annotate(count=Count("id"))
104-
.values("count")
106+
.values("object_id")
107+
.annotate(total=Count("id"))
108+
.values("total")
109+
)
110+
likes_count_subquery = (
111+
CustomUser.objects
112+
.filter(likes__user_id=OuterRef("id"))
113+
.annotate(total_likes=Count("likes"))
114+
.values("total_likes")
115+
)
116+
117+
projects_in_program_subquery = (
118+
CustomUser.objects
119+
.filter(partner_program_profiles__user_id=OuterRef("id"))
120+
.annotate(total_proj=Count("id"))
121+
.values("total_proj")
105122
)
106123

107124
users: QuerySet[CustomUser] = (
@@ -116,11 +133,27 @@ def __get_user_queryset(self) -> QuerySet[CustomUser]:
116133
"partner_program_profiles__partner_program",
117134
)
118135
.annotate(
119-
likes_count=Count("likes"),
120136
projects_count=Count("leaders_projects"),
121-
program_profiles_count=Count("partner_program_profiles"),
122-
projects_in_program=Subquery(projects_in_program_subqury, output_field=IntegerField()),
123-
posts_count=Subquery(posts_count_subqury, output_field=IntegerField()),
137+
likes_count=Coalesce(
138+
Subquery(likes_count_subquery, output_field=IntegerField()),
139+
Value(0),
140+
output_field=IntegerField(),
141+
),
142+
program_profiles_count=Coalesce(
143+
Subquery(projects_in_program_subquery, output_field=IntegerField()),
144+
Value(0),
145+
output_field=IntegerField(),
146+
),
147+
projects_in_program=Coalesce(
148+
Subquery(projects_in_program_subquery, output_field=IntegerField()),
149+
Value(0),
150+
output_field=IntegerField(),
151+
),
152+
posts_count=Coalesce(
153+
Subquery(posts_count_subquery, output_field=IntegerField()),
154+
Value(0),
155+
output_field=IntegerField(),
156+
),
124157
)
125158
)
126159
return users

0 commit comments

Comments
 (0)