-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/tasks_api
- Loading branch information
Showing
18 changed files
with
365 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.contrib.auth import get_user_model | ||
from django_filters.rest_framework import BooleanFilter, FilterSet | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CustomFilter(FilterSet): | ||
no_ipr = BooleanFilter(method="filter_no_ipr", label="No IPR") | ||
|
||
class Meta: | ||
model = User | ||
fields = ("team",) | ||
|
||
def filter_no_ipr(self, queryset, name, value): | ||
if value: | ||
return queryset.filter(ipr=None) | ||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from notifications.models import Notification | ||
from rest_framework import serializers | ||
|
||
from api.v1.serializers.api.users_serializer import CustomUserSerializer | ||
|
||
|
||
class NotificationSerializer(serializers.ModelSerializer): | ||
"""Сериализатор уведомлений.""" | ||
|
||
recipient = CustomUserSerializer(read_only=True, many=False) | ||
actor = CustomUserSerializer(read_only=True, many=False) | ||
|
||
class Meta: | ||
model = Notification | ||
fields = ( | ||
"id", | ||
"verb", | ||
"unread", | ||
"timestamp", | ||
"recipient", | ||
"actor", | ||
) | ||
|
||
# TODO: Добавить ссылку на объект уведомления. | ||
# url = reverse('your_nested_detail_view_name', | ||
# kwargs={'content_type': contentType, 'pk': objectId}) | ||
# >>> reverse("users-detail", args=[user.id]) | ||
# >>> '/api/v1/users/5/' | ||
# request.build_absolute_uri('/some-relative-path/') | ||
# т.е. всего 3 варианта, можно if-ами пройти. | ||
# if isinstance(notification.target, IPR): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from rest_framework import serializers | ||
|
||
|
||
class DummySerializer(serializers.Serializer): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from drf_spectacular.utils import extend_schema, extend_schema_view | ||
from rest_framework import status, viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.response import Response | ||
|
||
from api.v1.serializers.api.notifications_serializer import ( | ||
NotificationSerializer, | ||
) | ||
from api.v1.serializers.internal.dummy_serializer import DummySerializer | ||
|
||
|
||
@extend_schema(tags=["Уведомления"]) | ||
@extend_schema_view( | ||
list=extend_schema( | ||
summary="Список уведомлений", | ||
description=( | ||
"Список уведомлений, " | ||
"уведомления именно для пользователя делающего запрос." | ||
), | ||
), | ||
retrieve=extend_schema( | ||
summary="Уведомление", | ||
), | ||
partial_update=extend_schema( | ||
summary="Отметить уведомление как прочитанное", | ||
description=( | ||
"Чтобы отметить уведомление как прочитанное, " | ||
"нужно изменить значение unread." | ||
), | ||
), | ||
mark_all_as_read=extend_schema( | ||
methods=["get"], | ||
summary="Отметить все уведомления пользователя как прочтенные", | ||
responses={status.HTTP_200_OK: DummySerializer}, | ||
), | ||
) | ||
class NotificationViewSet(viewsets.ModelViewSet): | ||
serializer_class = NotificationSerializer | ||
http_method_names = ["get", "patch", "head", "options"] | ||
|
||
def partial_update(self, request, pk): | ||
"""Отметить уведомление как прочитанное.""" | ||
notification = self.get_object() | ||
if request.user != notification.recipient: | ||
return Response( | ||
{"message": "Уведомление не принадлежит пользователю."}, | ||
status=status.HTTP_403_FORBIDDEN, | ||
) | ||
notification.mark_as_read() | ||
serializer = self.get_serializer(notification) | ||
return Response(serializer.data) | ||
|
||
def get_queryset(self): | ||
"""Персонализированная выдача списка уведомлений.""" | ||
return self.request.user.notifications.unread() | ||
|
||
@action(methods=["get"], detail=False) | ||
def mark_all_as_read(self, request): | ||
"""Отметить все уведомления пользователя как прочтенные.""" | ||
request.user.notifications.mark_all_as_read() | ||
return Response( | ||
{"message": "Уведомления отмечены как прочтенные."}, | ||
status=status.HTTP_200_OK, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,90 @@ | ||
from django.conf import settings | ||
from django.db.models import CharField, ExpressionWrapper, F, Value | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from djoser.views import UserViewSet as UserViewSetFromDjoser | ||
from drf_spectacular.utils import extend_schema | ||
from rest_framework.pagination import PageNumberPagination | ||
from drf_spectacular.utils import extend_schema, extend_schema_view | ||
from rest_framework import filters | ||
from rest_framework.decorators import action | ||
from rest_framework.filters import OrderingFilter | ||
from rest_framework.pagination import LimitOffsetPagination | ||
from rest_framework.response import Response | ||
|
||
from api.v1.serializers.api.users_serializer import CustomUserSerializer | ||
from api.v1.filters import CustomFilter | ||
from api.v1.serializers.api.users_serializer import ( | ||
CustomUserSerializer, | ||
PositionsSerializer, | ||
) | ||
from users.models import Position | ||
|
||
|
||
@extend_schema( | ||
responses=CustomUserSerializer, | ||
description="Пользователи.", | ||
@extend_schema(tags=["Пользователи"]) | ||
@extend_schema_view( | ||
list=extend_schema( | ||
summary=("Список пользователей."), | ||
description=( | ||
"<ul><h3>Поддерживается:</h3><li>Сортировка по имени " | ||
"<code>./?ordering=full_name</code> " | ||
"и должности <code>./?ordering=-position_name</code></li>" | ||
"<li>Поиск по ФИО и должности <code>./?search=Мирон</code></li>" | ||
"<li>Ограничение pagination <code>./?limit=5</code>.</li>" | ||
"<li>Фильтр по id команды <code>./?team=1</code></li>" | ||
"<li>Фильтр по id команды и отсутствию ИПР " | ||
"<code>./?team=1&no_ipr=true</code></li></ul>" | ||
), | ||
), | ||
retrieve=extend_schema(summary="Профиль пользователя"), | ||
me=extend_schema(summary="Текущий пользователь"), | ||
) | ||
class UserViewSet(UserViewSetFromDjoser): | ||
"""Пользователи.""" | ||
|
||
filterset_class = CustomFilter | ||
filter_backends = ( | ||
DjangoFilterBackend, | ||
filters.SearchFilter, | ||
OrderingFilter, | ||
) | ||
if settings.USE_POSTGRESQL: # TODO: Проверить. | ||
search_fields = ( | ||
"@last_name", | ||
"@first_name", | ||
"@patronymic", | ||
"@position__name", | ||
) | ||
else: | ||
search_fields = ( | ||
"last_name", | ||
"first_name", | ||
"patronymic", | ||
"position__name", | ||
) | ||
serializer_class = CustomUserSerializer | ||
pagination_class = PageNumberPagination | ||
pagination_class = LimitOffsetPagination | ||
http_method_names = ["get", "head", "options"] | ||
ordering_fields = ("full_name", "position_name") | ||
|
||
def get_queryset(self): | ||
queryset = super().get_queryset() | ||
|
||
return queryset.annotate( | ||
full_name=ExpressionWrapper( | ||
F("last_name") + F("first_name") + F("patronymic"), | ||
output_field=CharField(), | ||
), | ||
position_name=ExpressionWrapper( | ||
F("position__name") if F("position__name") else Value(""), | ||
output_field=CharField(), | ||
), | ||
) | ||
|
||
@extend_schema( | ||
summary="Список должностей", | ||
) | ||
@action( | ||
methods=["get"], detail=False, serializer_class=PositionsSerializer | ||
) | ||
def positions(self, request): | ||
"""Список должностей.""" | ||
queryset = Position.objects.all() | ||
serializer = PositionsSerializer(queryset, many=True) | ||
return Response(serializer.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CoreConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "core" | ||
|
||
def ready(self): | ||
"""Добавление signals.py.""" | ||
try: | ||
import core.signals # noqa | ||
except ImportError: | ||
pass |
Oops, something went wrong.