dev: filter by snoozed and response update for list and permissions

This commit is contained in:
pablohashescobar 2023-06-23 11:25:27 +05:30
parent be96ec47ba
commit e73b39fcdf
4 changed files with 100 additions and 49 deletions

View File

@ -21,6 +21,7 @@ from .project import (
ProjectIdentifierSerializer, ProjectIdentifierSerializer,
ProjectFavoriteSerializer, ProjectFavoriteSerializer,
ProjectLiteSerializer, ProjectLiteSerializer,
ProjectMemberLiteSerializer,
) )
from .state import StateSerializer, StateLiteSerializer from .state import StateSerializer, StateLiteSerializer
from .view import IssueViewSerializer, IssueViewFavoriteSerializer from .view import IssueViewSerializer, IssueViewFavoriteSerializer

View File

@ -96,7 +96,6 @@ class ProjectMemberSerializer(BaseSerializer):
workspace = WorkSpaceSerializer(read_only=True) workspace = WorkSpaceSerializer(read_only=True)
project = ProjectSerializer(read_only=True) project = ProjectSerializer(read_only=True)
member = UserLiteSerializer(read_only=True) member = UserLiteSerializer(read_only=True)
is_subscribed = serializers.BooleanField(read_only=True)
class Meta: class Meta:
model = ProjectMember model = ProjectMember
@ -135,3 +134,13 @@ class ProjectLiteSerializer(BaseSerializer):
model = Project model = Project
fields = ["id", "identifier", "name"] fields = ["id", "identifier", "name"]
read_only_fields = fields read_only_fields = fields
class ProjectMemberLiteSerializer(BaseSerializer):
member = UserLiteSerializer(read_only=True)
is_subscribed = serializers.BooleanField(read_only=True)
class Meta:
model = ProjectMember
fields = ["member", "id", "is_subscribed"]
read_only_fields = fields

View File

@ -45,11 +45,13 @@ from plane.api.serializers import (
IssueAttachmentSerializer, IssueAttachmentSerializer,
IssueSubscriberSerializer, IssueSubscriberSerializer,
ProjectMemberSerializer, ProjectMemberSerializer,
ProjectMemberLiteSerializer,
) )
from plane.api.permissions import ( from plane.api.permissions import (
ProjectEntityPermission, ProjectEntityPermission,
WorkSpaceAdminPermission, WorkSpaceAdminPermission,
ProjectMemberPermission, ProjectMemberPermission,
ProjectLitePermission,
) )
from plane.db.models import ( from plane.db.models import (
Project, Project,
@ -871,29 +873,17 @@ class IssueSubscriberViewSet(BaseViewSet):
ProjectEntityPermission, ProjectEntityPermission,
] ]
def list(self, request, slug, project_id, issue_id): def get_permissions(self):
try: if self.action in ["subscribe", "unsubscribe", "subscription_status"]:
members = ProjectMember.objects.filter( self.permission_classes = [
workspace__slug=slug, project_id=project_id ProjectLitePermission,
) ]
members = members.annotate( else:
is_subscribed=Exists( self.permission_classes = [
IssueSubscriber.objects.filter( ProjectEntityPermission,
workspace__slug=slug, ]
project_id=project_id,
issue_id=issue_id, return super(IssueSubscriberViewSet, self).get_permissions()
subscriber=OuterRef("member"),
)
)
)
serializer = ProjectMemberSerializer(members, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
capture_exception(e)
return Response(
{"error": e},
status=status.HTTP_400_BAD_REQUEST,
)
def perform_create(self, serializer): def perform_create(self, serializer):
serializer.save( serializer.save(
@ -913,6 +903,53 @@ class IssueSubscriberViewSet(BaseViewSet):
.distinct() .distinct()
) )
def list(self, request, slug, project_id, issue_id):
try:
members = ProjectMember.objects.filter(
workspace__slug=slug, project_id=project_id
).annotate(
is_subscribed=Exists(
IssueSubscriber.objects.filter(
workspace__slug=slug,
project_id=project_id,
issue_id=issue_id,
subscriber=OuterRef("member"),
)
)
).select_related("member")
serializer = ProjectMemberLiteSerializer(members, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
capture_exception(e)
return Response(
{"error": e},
status=status.HTTP_400_BAD_REQUEST,
)
def destroy(self, request, slug, project_id, issue_id, subscriber_id):
try:
issue_subscriber = IssueSubscriber.objects.get(
project=project_id,
subscriber=subscriber_id,
workspace__slug=slug,
issue=issue_id,
)
issue_subscriber.delete()
return Response(
status=status.HTTP_204_NO_CONTENT,
)
except IssueSubscriber.DoesNotExist:
return Response(
{"error": "User is not subscribed to this issue"},
status=status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def subscribe(self, request, slug, project_id, issue_id): def subscribe(self, request, slug, project_id, issue_id):
try: try:
if IssueSubscriber.objects.filter( if IssueSubscriber.objects.filter(
@ -979,27 +1016,3 @@ class IssueSubscriberViewSet(BaseViewSet):
{"error": "Something went wrong, please try again later"}, {"error": "Something went wrong, please try again later"},
status=status.HTTP_400_BAD_REQUEST, status=status.HTTP_400_BAD_REQUEST,
) )
def destroy(self, request, slug, project_id, issue_id, subscriber_id):
try:
issue_subscriber = IssueSubscriber.objects.get(
project=project_id,
subscriber=subscriber_id,
workspace__slug=slug,
issue=issue_id,
)
issue_subscriber.delete()
return Response(
status=status.HTTP_204_NO_CONTENT,
)
except IssueSubscriber.DoesNotExist:
return Response(
{"error": "User is not subscribed to this issue"},
status=status.HTTP_400_BAD_REQUEST,
)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)

View File

@ -1,3 +1,7 @@
# Django imports
from django.db.models import Q
from django.utils import timezone
# Third party imports # Third party imports
from rest_framework import status from rest_framework import status
from rest_framework.response import Response from rest_framework.response import Response
@ -24,6 +28,28 @@ class NotificationViewSet(BaseViewSet):
.select_related("workspace") .select_related("workspace")
) )
def list(self, request, slug):
try:
order_by = request.GET.get("ordeer_by", "-created_at")
snoozed = request.GET.get("snoozed", "false")
notifications = Notification.objects.filter(
workspace__slug=slug, receiver=request.user
).order_by(order_by)
if snoozed == "false":
notifications = notifications.filter(
Q(snoozed_till__gte=timezone.now()) | Q(snoozed_till__isnull=True),
)
serializer = NotificationSerializer(notifications, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
capture_exception(e)
return Response(
{"error": "Something went wrong please try again later"},
status=status.HTTP_400_BAD_REQUEST,
)
def partial_update(self, request, slug, pk): def partial_update(self, request, slug, pk):
try: try:
notification = Notification.objects.get( notification = Notification.objects.get(
@ -34,7 +60,9 @@ class NotificationViewSet(BaseViewSet):
"read_at": request.data.get("read_at", None), "read_at": request.data.get("read_at", None),
"snoozed_till": request.data.get("snoozed_till", None), "snoozed_till": request.data.get("snoozed_till", None),
} }
serializer = NotificationSerializer(notification, data=notification_data, partial=True) serializer = NotificationSerializer(
notification, data=notification_data, partial=True
)
if serializer.is_valid(): if serializer.is_valid():
serializer.save() serializer.save()