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

Created new Public API router and viewset for External Site use #56

Open
wants to merge 1 commit into
base: master
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
11 changes: 6 additions & 5 deletions kerckhoff/packages/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def get_or_create_gdrive_meta(self) -> GoogleDriveMeta:
self.metadata[GOOGLE_DRIVE_META_KEY] = data
self.save()
return GoogleDriveMeta(**data)
# Test model
class Practice(models.Model):
title = models.CharField(max_length = 200)

def get_new_packages_from_gdrive(self) -> List["Package"]:
gdrive_info = self.get_or_create_gdrive_meta()
if not gdrive_info.folder_id:
raise GoogleDriveNotConfiguredException(self)

def __str__(self):
return self.title
ops = GoogleDriveOperations(self.created_by)

items, _ = ops.list_folder(gdrive_info.folder_id)
Expand Down Expand Up @@ -301,3 +301,4 @@ def create_from_google_drive_item(
)
pi.save()
return pi

1 change: 1 addition & 0 deletions kerckhoff/packages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,4 @@ def get_version_data(self, obj: Package):
class Meta(PackageSerializer.Meta):
fields = PackageSerializer.Meta.fields + ("version_data",)
read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",)

36 changes: 33 additions & 3 deletions kerckhoff/packages/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from multiprocessing import log_to_stderr
from typing import List
from importlib_metadata import packages_distributions
from rest_framework import mixins, viewsets, filters
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
from rest_framework.decorators import action
from rest_framework.serializers import Serializer
from rest_framework.response import Response
Expand All @@ -14,7 +17,7 @@
RetrievePackageSerializer,
PackageVersionSerializer,
CreatePackageVersionSerializer,
PackageSetDetailedSerializer,
PackageSetDetailedSerializer
)


Expand Down Expand Up @@ -80,7 +83,7 @@ class PackageViewSet(
):
"""
Updates and retrieves packages
"""
"""
def get_queryset(self):
return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"])

Expand Down Expand Up @@ -150,3 +153,30 @@ def perform_create(self, serializer):
lookup_value_regex = slug_with_dots
filter_backends = (filters.OrderingFilter,)
ordering_fields = ("slug", "last_fetched_date", "created_at", "updated_at")



# Public Package View set for External site Kerckhoff API

# mixins.ListModelMixin list out all packages in package set
# mixins.RetrieveModelMixin retrieves specific/individual package within the package set
class PublicPackageViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixins.RetrieveModelMixin):
"""
List and retrieve packages for external site
"""

# Retrieve only the packages from the package set that has the same name/ slug as the defined package set name/slug in the url
def get_queryset(self):
return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"])

serializer_class = PackageSerializer
permission_classes = (IsAuthenticatedOrReadOnly,)
# set slug as the lookup field so that we look up for packages in the package set with the same slug
lookup_field = "slug"
# verifies if the url slug is a valid slug and matches our valid slug format defined at the top of this file
lookup_value_regex = slug_with_dots





45 changes: 40 additions & 5 deletions kerckhoff/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PackageSetCreateAndListViewSet,
PackageViewSet,
PackageCreateAndListViewSet,
PublicPackageViewSet
)
from .comments.views import CommentViewSet
from .integrations.views import IntegrationOAuthView
Expand All @@ -28,16 +29,47 @@
router.register(r"package-sets", PackageSetViewSet)
router.register(r"package-sets", PackageSetCreateAndListViewSet)

package_set_router = NestedSimpleRouter(router, r"package-sets", lookup="package_set")
# API Router for Public External Site
public_package_set_router = NestedSimpleRouter(
router,
r"package-sets",
lookup="package_set")

public_package_set_router.register(
r"packages",
PublicPackageViewSet,
base_name="public-package"
)



# To Internal Kerckhoff Frontend Site
package_set_router = NestedSimpleRouter(
router,
r"package-sets",
lookup="package_set")

package_set_router.register(
r"packages", PackageViewSet, base_name="package-sets_packages"
r"packages",
PackageViewSet,
base_name="package-generate_packages"
)

package_set_router.register(
r"packages", PackageCreateAndListViewSet, base_name="package-sets_packages"
r"packages",
PackageCreateAndListViewSet,
base_name="package-sets_packages"
)

package_router = NestedSimpleRouter(package_set_router, r"packages", lookup="package")
package_router.register(r"comments", CommentViewSet, base_name="comments")
package_router = NestedSimpleRouter(
package_set_router,
r"packages",
lookup="package")

package_router.register(
r"comments",
CommentViewSet,
base_name="comments")

schema_view = get_schema_view(
openapi.Info(
Expand Down Expand Up @@ -73,6 +105,9 @@
path("api/v1/", include(router.urls)),
path("api/v1/", include(package_set_router.urls)),
path("api/v1/", include(package_router.urls)),
# path for external site api
path("api/v1/public/",include(public_package_set_router.urls)),

path("api/v1/integrations/", IntegrationOAuthView.as_view()),
path("api-oauth/", include(auth_urlpatterns)),
path("api-token-auth/", views.obtain_auth_token),
Expand Down