-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat (push notifications): adds api, some bug fixes and additional tasks
- Loading branch information
1 parent
574d9cc
commit 399aaf5
Showing
12 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
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 decorators as auth_decorators | ||
from django.urls import include, path | ||
from drf_spectacular import views as drf_views | ||
|
||
from . import v1 | ||
|
||
schema_view = drf_views.SpectacularAPIView.as_view() | ||
swagger_view = drf_views.SpectacularSwaggerView.as_view(url_name="schema") | ||
redoc_view = drf_views.SpectacularRedocView.as_view(url_name="schema") | ||
|
||
app_name = "api" | ||
urlpatterns = [ | ||
path("v1/", include((v1.urls, "api"), namespace="v1")), | ||
path("schema/", auth_decorators.login_required(schema_view), name="schema"), | ||
path("docs/", auth_decorators.login_required(swagger_view), name="docs"), | ||
path("redoc/", auth_decorators.login_required(redoc_view), name="redoc"), | ||
] |
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 @@ | ||
from . import urls |
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 @@ | ||
from . import urls |
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,53 @@ | ||
from rest_framework import serializers | ||
|
||
from app.consts import push_notification as push_notification_consts | ||
from app.drf.fields import create_choice_human_field | ||
from app.drf.serializers import inline_serializer | ||
from push_notifications import models | ||
|
||
|
||
class PushNotificationInputSchema(serializers.Serializer): | ||
only_read = serializers.BooleanField(required=False, allow_null=True) | ||
|
||
|
||
PushNotificationKindField = create_choice_human_field(constant_class=push_notification_consts.Kind) | ||
PushNotificationStatusField = create_choice_human_field( | ||
constant_class=push_notification_consts.Status | ||
) | ||
|
||
|
||
class PushNotificationOutputSchema(serializers.ModelSerializer): | ||
kind = PushNotificationKindField() | ||
status = PushNotificationStatusField() | ||
data = inline_serializer( | ||
name="PushNotificationEnrichedDataOutputSchema", | ||
fields={ | ||
"id": serializers.CharField(), | ||
"createdAt": serializers.DateTimeField(), | ||
"readAt": serializers.DateTimeField(), | ||
"timeSinceCreated": serializers.DateTimeField(), | ||
"kind": serializers.CharField(), | ||
"meta": serializers.JSONField(), | ||
}, | ||
source="enriched_data", | ||
) | ||
|
||
class Meta: | ||
model = models.PushNotification | ||
fields = ( | ||
"id", | ||
"title", | ||
"description", | ||
"read_at", | ||
"kind", | ||
"status", | ||
"data", | ||
) | ||
|
||
|
||
class PushNotificationReadManyInputSchema(serializers.Serializer): | ||
ids = serializers.ListField(required=False, allow_null=True) | ||
|
||
|
||
class PushNotificationReadManyOutputSchema(serializers.Serializer): | ||
read = serializers.IntegerField() |
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,8 @@ | ||
from rest_framework.routers import SimpleRouter | ||
|
||
from . import views | ||
|
||
router = SimpleRouter(trailing_slash=False) | ||
router.register("notifications", views.PushNotificationViewSet, basename="push_notifications") | ||
|
||
urlpatterns = router.urls |
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,69 @@ | ||
from rest_framework.decorators import action | ||
from rest_framework.generics import get_object_or_404 | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.request import Request | ||
from rest_framework.response import Response | ||
|
||
from app.drf.openapi import limit_offset_openapi_schema, openapi_schema | ||
from app.drf.viewsets import AppViewSet | ||
from push_notifications import selectors, services | ||
|
||
from . import schemas | ||
|
||
|
||
class PushNotificationViewSet(AppViewSet): | ||
permission_classes = [IsAuthenticated] | ||
|
||
@limit_offset_openapi_schema( | ||
wrapped_schema=schemas.PushNotificationOutputSchema, | ||
operation_id="push-notification-list", | ||
summary="Notifications list", | ||
description="Returns a list of notifications", | ||
request=None, | ||
tags=["push notifications"], | ||
add_unauthorized_response=True, | ||
parameter_serializer=schemas.PushNotificationInputSchema, | ||
) | ||
def list(self, request: Request) -> Response: | ||
params = self.get_valid_query_params(srlzr_class=schemas.PushNotificationInputSchema) | ||
qs = selectors.push_notification_get_viewable_qs(user=request.user, filters=params) | ||
return self.get_paginated_response( | ||
queryset=qs, srlzr_class=schemas.PushNotificationOutputSchema | ||
) | ||
|
||
@openapi_schema( | ||
summary="Read many notifications", | ||
description="Reads a list of notifications", | ||
request=schemas.PushNotificationReadManyInputSchema, | ||
responses={200: schemas.PushNotificationReadManyOutputSchema}, | ||
tags=["push notifications"], | ||
operation_id="push-notifications-read", | ||
add_bad_request_response=True, | ||
add_unauthorized_response=True, | ||
) | ||
@action(methods=["PATCH"], detail=False, url_path="read") | ||
def read_many(self, request: Request) -> Response: | ||
data = self.get_valid_data(srlzr_class=schemas.PushNotificationReadManyInputSchema) | ||
updated = services.push_notification_read_many(reader=request.user, ids=data.get("ids")) | ||
return Response(data={"read": updated}) | ||
|
||
@openapi_schema( | ||
summary="Read notification", | ||
description="Reads a single notification", | ||
request=None, | ||
responses={200: schemas.PushNotificationOutputSchema}, | ||
tags=["push notifications"], | ||
operation_id="push-notification-read", | ||
add_not_found_response=True, | ||
add_bad_request_response=True, | ||
add_unauthorized_response=True, | ||
) | ||
@action(methods=["PATCH"], detail=True) | ||
def read(self, request: Request, id: int) -> Response: | ||
notification = get_object_or_404( | ||
selectors.push_notification_get_viewable_qs(user=request.user), | ||
pk=id, | ||
) | ||
notification = services.push_notification_read(push_notification=notification) | ||
out_srlzr = schemas.PushNotificationOutputSchema(instance=notification) | ||
return Response(data=out_srlzr.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from django.urls import include, path | ||
|
||
from . import push_notifications | ||
|
||
urlpatterns = [ | ||
path( | ||
"notifications/", | ||
include((push_notifications.urls, "push_notifications"), namespace="push_notifications"), | ||
), | ||
] |
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
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