Skip to content

Commit

Permalink
Merge branch 'feature/organization-ordering-api-filters' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
zMardone committed Dec 19, 2023
2 parents 04a12f3 + b3190e5 commit 192cd6e
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
20 changes: 16 additions & 4 deletions connect/api/v2/organizations/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pendulum
from rest_framework import mixins, status
from rest_framework.decorators import action
from rest_framework.viewsets import GenericViewSet
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import ValidationError
from drf_yasg2.utils import swagger_auto_schema
from django.utils.translation import ugettext_lazy as _

Expand All @@ -17,7 +19,6 @@
OrganizationHasPermission,
)
from connect.api.v2.paginations import CustomCursorPagination

from connect.api.v2.organizations.serializers import (
OrganizationSeralizer,
NestedAuthorizationOrganizationSerializer
Expand All @@ -26,9 +27,7 @@
from connect.api.v2.organizations.api_schemas import (
create_organization_schema,
)
from rest_framework.exceptions import ValidationError
from connect.api.v2.paginations import CustomCursorPagination
import pendulum



class OrganizationViewSet(
Expand Down Expand Up @@ -58,6 +57,19 @@ def get_queryset(self, *args, **kwargs):

return self.queryset.filter(pk__in=auth)

def get_ordering(self):
valid_fields = (org_fields.name for org_fields in Organization._meta.get_fields())
ordering = []
for param in self.request.query_params.getlist('ordering'):
if param.startswith('-'):
field = param[1:]
else:
field = param
if field in valid_fields:
ordering.append(param)
return ordering or ["created_at"]


@swagger_auto_schema(request_body=create_organization_schema)
def create(self, request, *args, **kwargs):
org_data = request.data.get("organization")
Expand Down
12 changes: 11 additions & 1 deletion connect/api/v2/paginations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@
class CustomCursorPagination(CursorPagination):
page_size_query_param = 'page_size'
page_size = 20
ordering = "created_at"

def paginate_queryset(self, queryset, request, view=None):
self.ordering = view.get_ordering()
return super().paginate_queryset(queryset, request, view)


class OpenedProjectCustomCursorPagination(CursorPagination):
def paginate_queryset(self, queryset, request, view=None):
self.ordering = view.get_ordering()
self.queryset = queryset.order_by(*self.ordering).distinct('project')
return super().paginate_queryset(queryset, request, view)
27 changes: 27 additions & 0 deletions connect/api/v2/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,30 @@ def get_pending_rocketchat_role(self, email):
if rocket_authorization:
return rocket_authorization.role
return None


class OpenedProjectSerializer(serializers.ModelSerializer):

class Meta:
model = OpenedProject
fields = [
"day",
"project",
"user",
]

user = serializers.PrimaryKeyRelatedField(
queryset=User.objects,
required=True,
style={"show": False},
)
day = serializers.DateTimeField(required=False)
project = serializers.SerializerMethodField()

def get_project(self, obj):
data = ProjectSerializer(
obj.project,
context=self.context
).data

return data
52 changes: 51 additions & 1 deletion connect/api/v2/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
ProjectSerializer,
ProjectUpdateSerializer,
ProjectListAuthorizationSerializer,
OpenedProjectSerializer
)

from django.utils import timezone
from connect.api.v2.paginations import CustomCursorPagination
from connect.api.v2.paginations import CustomCursorPagination, OpenedProjectCustomCursorPagination


class ProjectViewSet(
Expand All @@ -41,6 +42,20 @@ def get_queryset(self, **kwargs):

return super().get_queryset().filter(organization__uuid=self.kwargs["organization_uuid"])

def get_ordering(self):
valid_fields = (
org_fields.name for org_fields in Project._meta.get_fields()
)
ordering = []
for param in self.request.query_params.getlist('ordering'):
if param.startswith('-'):
field = param[1:]
else:
field = param
if field in valid_fields:
ordering.append(param)
return ordering or ["created_at"]

@action(
detail=True,
methods=["GET"],
Expand Down Expand Up @@ -104,3 +119,38 @@ class ProjectAuthorizationViewSet(
serializer_class = ProjectListAuthorizationSerializer
permission_classes = [IsAuthenticated, ProjectHasPermission, Has2FA]
lookup_field = "uuid"


class OpenedProjectViewSet(
mixins.ListModelMixin,
GenericViewSet
):

queryset = OpenedProject.objects.select_related("project", "user")
serializer_class = OpenedProjectSerializer
permission_classes = [IsAuthenticated, ProjectHasPermission, Has2FA]
lookup_field = "uuid"
pagination_class = OpenedProjectCustomCursorPagination

def get_queryset(self, **kwargs):
if getattr(self, "swagger_fake_view", False):
return OpenedProject.objects.none() # pragma: no cover

organization__uuid = self.kwargs["organization_uuid"]
projects = Project.objects.filter(organization__uuid=organization__uuid)
opened_projects = super().get_queryset().filter(project__in=projects)
return opened_projects

def get_ordering(self):
valid_fields = (
opened_project.name for opened_project in OpenedProject._meta.get_fields()
)
ordering = ["project"]
for param in self.request.query_params.getlist('ordering'):
if param.startswith('-'):
field = param[1:]
else:
field = param
if field in valid_fields:
ordering.append(param)
return ordering or ["project", "day"]
3 changes: 3 additions & 0 deletions connect/api/v2/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
projects_router.register(
"projects", project_views.ProjectViewSet, basename="organization-projects"
)
projects_router.register(
"opened-projects", project_views.OpenedProjectViewSet, basename="opened-projects"
)

urlpatterns = [
path("", include(router.urls)),
Expand Down

0 comments on commit 192cd6e

Please sign in to comment.