Skip to content

Commit 9cc1a83

Browse files
authored
Merge pull request #228 from PROCOLLAB-github/dev
yet another release
2 parents 4e641c2 + 227ded3 commit 9cc1a83

6 files changed

Lines changed: 69 additions & 17 deletions

File tree

chats/views.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,14 @@ def get(self, request, *args, **kwargs):
219219
user = request.user
220220
# get all user chats
221221
direct_messages = user.direct_chats.all().prefetch_related("messages")
222-
project_messages = user.get_project_chats().prefetch_related("messages")
223-
224-
has_direct_messages_unread = direct_messages.filter(
225-
messages__is_read=False
226-
).exists()
227-
has_project_messages_unread = project_messages.filter(
228-
messages__is_read=False
229-
).exists()
222+
project_chats = user.get_project_chats().prefetch_related("messages")
223+
224+
has_direct_messages_unread = (
225+
direct_messages.filter(messages__is_read=False).distinct().exists()
226+
)
227+
has_project_messages_unread = (
228+
project_chats.filter(messages__is_read=False).distinct().exists()
229+
)
230230
return Response(
231231
{"has_unreads": has_direct_messages_unread or has_project_messages_unread}
232232
)

projects/pagination.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ class ProjectNewsPagination(pagination.LimitOffsetPagination):
1313
default_limit = 5
1414
limit_query_param = "limit"
1515
offset_query_param = "offset"
16+
17+
18+
class ProjectsPagination(pagination.LimitOffsetPagination):
19+
"""
20+
Pagination for Users
21+
22+
For example:
23+
/projects/?limit=10&offset=10
24+
gets the next 10 news after the first 10 news.
25+
"""
26+
27+
default_limit = 10
28+
limit_query_param = "limit"
29+
offset_query_param = "offset"

projects/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
update_partner_program,
1919
)
2020
from projects.models import Project, Achievement, ProjectNews
21-
from projects.pagination import ProjectNewsPagination
21+
from projects.pagination import ProjectNewsPagination, ProjectsPagination
2222
from projects.permissions import (
2323
IsProjectLeaderOrReadOnlyForNonDrafts,
2424
HasInvolvementInProjectOrReadOnly,
@@ -48,6 +48,7 @@ class ProjectList(generics.ListCreateAPIView):
4848
permission_classes = [IsAuthenticated, permissions.IsAuthenticatedOrReadOnly]
4949
filter_backends = (filters.DjangoFilterBackend,)
5050
filterset_class = ProjectFilter
51+
pagination_class = ProjectsPagination
5152

5253
def create(self, request, *args, **kwargs):
5354
serializer = self.get_serializer(data=request.data)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{% extends "admin/change_form.html" %}
2+
{% load i18n %}
3+
4+
{% block submit_buttons_bottom %}
5+
6+
7+
{{ block.super }}
8+
<div class="submit-row">
9+
<input type="button" class="mailing-btn" value="Рассылка" onclick="mailing()"/>
10+
</div>
11+
<script>
12+
function mailing() {
13+
{% if object_id %}
14+
window.open("{% url 'admin:user_mailing' object_id %}", '_blank').focus()
15+
{% endif %}
16+
}
17+
</script>
18+
19+
{% endblock %}
20+

users/admin.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.conf import settings
22
from django.contrib import admin
3+
from django.urls import path
34

5+
from mailing.views import MailingTemplateRender
46
from .helpers import send_verification_completed_email
57
from .models import (
68
CustomUser,
@@ -106,6 +108,7 @@ class CustomUserAdmin(admin.ModelAdmin):
106108
)
107109

108110
readonly_fields = ("ordering_score",)
111+
change_form_template = "users/admin/users_change_form.html"
109112

110113
def save_model(self, request, obj, form, change):
111114
# if user_type changed, then delete all related fields
@@ -146,6 +149,22 @@ def save_model(self, request, obj, form, change):
146149

147150
super().save_model(request, obj, form, change)
148151

152+
def get_urls(self):
153+
default_urls = super(CustomUserAdmin, self).get_urls()
154+
custom_urls = [
155+
path(
156+
"mailing/<int:user_object>/",
157+
self.admin_site.admin_view(self.mailing),
158+
name="user_mailing",
159+
),
160+
]
161+
return custom_urls + default_urls
162+
163+
def mailing(self, request, user_object):
164+
user = CustomUser.objects.get(pk=user_object)
165+
users = [user]
166+
return MailingTemplateRender().render_template(request, None, users, None)
167+
149168

150169
@admin.register(UserAchievement)
151170
class UserAchievementAdmin(admin.ModelAdmin):

users/models.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib.auth.models import AbstractUser
22
from django.db import models
3+
from django.db.models import QuerySet
34

45
from users.constants import (
56
ADMIN,
@@ -133,14 +134,11 @@ def calculate_ordering_score(self) -> int:
133134
score += 7
134135
return score
135136

136-
def get_project_chats(self) -> list:
137-
collaborations = self.collaborations.prefetch_related(
138-
"project__project_chats"
139-
).all()
140-
projects = []
141-
for collaboration in collaborations:
142-
projects.extend(list(collaboration.project.project_chats.all()))
143-
return projects
137+
def get_project_chats(self) -> QuerySet:
138+
from chats.models import ProjectChat
139+
140+
user_project_ids = self.collaborations.all().values_list("project_id", flat=True)
141+
return ProjectChat.objects.filter(project__in=user_project_ids)
144142

145143
def get_key_skills(self) -> list[str]:
146144
return [skill.strip() for skill in self.key_skills.split(",") if skill.strip()]

0 commit comments

Comments
 (0)