Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a search in ProjectListPage view #233

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions estela-api/api/serializers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,12 @@ class ProjectActivitySerializer(serializers.Serializer):
count = serializers.IntegerField(
required=True, help_text="Project activities count."
)


class ProjectSearchSerializer(serializers.Serializer):
results = ProjectSerializer(
many=True, required=True, help_text="Project search results."
)
count = serializers.IntegerField(
required=True, help_text="Project search results count."
)
48 changes: 48 additions & 0 deletions estela-api/api/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
ActivitySerializer,
ProjectActivitySerializer,
ProjectSerializer,
ProjectSearchSerializer,
ProjectUpdateSerializer,
ProjectUsageSerializer,
UsageRecordSerializer,
Expand Down Expand Up @@ -59,6 +60,53 @@ def get_queryset(self):
else self.request.user.project_set.filter(deleted=False)
)

@swagger_auto_schema(
methods=["GET"],
manual_parameters=[
openapi.Parameter(
"page",
openapi.IN_QUERY,
description="A page number within the paginated result set.",
type=openapi.TYPE_NUMBER,
required=False,
),
openapi.Parameter(
"page_size",
openapi.IN_QUERY,
description="Number of results to return per page.",
type=openapi.TYPE_NUMBER,
required=False,
),
openapi.Parameter(
"search",
openapi.IN_QUERY,
description="Search for a project by name.",
type=openapi.TYPE_STRING,
required=False,
),
],
responses={status.HTTP_200_OK: ProjectSearchSerializer()},
)
@action(methods=["GET"], detail=False)
def search(self, request, *args, **kwargs):
page, page_size = self.get_parameters(request)
if page_size > self.MAX_PAGINATION_SIZE or page_size < self.MIN_PAGINATION_SIZE:
raise ParseError({"error": errors.INVALID_PAGE_SIZE})
if page < 1:
raise ParseError({"error": errors.INVALID_PAGE_SIZE})
search = request.query_params.get("search", None)
projects = self.get_queryset()
if search:
projects = projects.filter(name__icontains=search)

paginator_result = Paginator(projects, page_size)
page_result = paginator_result.page(page)
serializer = ProjectSerializer(page_result, many=True)
return Response(
{"results": serializer.data, "count": projects.count()},
status=status.HTTP_200_OK,
)

def perform_create(self, serializer):
instance = serializer.save()
instance.users.add(
Expand Down
43 changes: 43 additions & 0 deletions estela-api/docs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,34 @@ paths:
tags:
- api
parameters: []
/api/projects/search:
get:
operationId: api_projects_search
description: ''
parameters:
- name: page
in: query
description: A page number within the paginated result set.
required: false
type: number
- name: page_size
in: query
description: Number of results to return per page.
required: false
type: number
- name: search
in: query
description: Search for a project by name.
required: false
type: string
responses:
'200':
description: ''
schema:
$ref: '#/definitions/ProjectSearch'
tags:
- api
parameters: []
/api/projects/{pid}:
get:
operationId: api_projects_read
Expand Down Expand Up @@ -1797,6 +1825,21 @@ definitions:
type: integer
maximum: 65535
minimum: 0
ProjectSearch:
required:
- results
- count
type: object
properties:
results:
description: Project search results.
type: array
items:
$ref: '#/definitions/Project'
count:
title: Count
description: Project search results count.
type: integer
ProjectUpdate:
type: object
properties:
Expand Down
Loading
Loading