From 2c0804d3901ec6bb4f2b74b6386910b49ebc4857 Mon Sep 17 00:00:00 2001 From: jackson ng Date: Tue, 1 Feb 2022 22:28:28 -0800 Subject: [PATCH 1/9] Created new Public API router and viewset for External Site use --- kerckhoff/packages/models.py | 11 ++++---- kerckhoff/packages/serializers.py | 1 + kerckhoff/packages/views.py | 36 ++++++++++++++++++++++--- kerckhoff/urls.py | 45 +++++++++++++++++++++++++++---- 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/kerckhoff/packages/models.py b/kerckhoff/packages/models.py index fc7d78e..6dc26a9 100644 --- a/kerckhoff/packages/models.py +++ b/kerckhoff/packages/models.py @@ -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) @@ -301,3 +301,4 @@ def create_from_google_drive_item( ) pi.save() return pi + diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index b1c9d1b..8c0eb12 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -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",) + diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index 20869a0..c998158 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -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 @@ -14,7 +17,7 @@ RetrievePackageSerializer, PackageVersionSerializer, CreatePackageVersionSerializer, - PackageSetDetailedSerializer, + PackageSetDetailedSerializer ) @@ -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"]) @@ -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 + + + + + diff --git a/kerckhoff/urls.py b/kerckhoff/urls.py index d1aa887..b93cf2a 100755 --- a/kerckhoff/urls.py +++ b/kerckhoff/urls.py @@ -18,6 +18,7 @@ PackageSetCreateAndListViewSet, PackageViewSet, PackageCreateAndListViewSet, + PublicPackageViewSet ) from .comments.views import CommentViewSet from .integrations.views import IntegrationOAuthView @@ -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( @@ -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), From 189cd15993da88ac4a47b2ae03f8612155e57023 Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Sat, 19 Mar 2022 18:59:10 -0700 Subject: [PATCH 2/9] initial commit --- kerckhoff/packages/models.py | 14 +++--- kerckhoff/packages/serializers.py | 38 ++++++++++++++ kerckhoff/packages/views.py | 84 ++++++++++++++++++++++++++++++- 3 files changed, 126 insertions(+), 10 deletions(-) diff --git a/kerckhoff/packages/models.py b/kerckhoff/packages/models.py index 6dc26a9..3b1cdbf 100644 --- a/kerckhoff/packages/models.py +++ b/kerckhoff/packages/models.py @@ -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 __str__(self): - return self.title + 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) + ops = GoogleDriveOperations(self.created_by) items, _ = ops.list_folder(gdrive_info.folder_id) @@ -194,7 +194,6 @@ def create_version( updated_package_item_titles: List[str], ): """Creates new PackageVersion object - Arguments: user {User} -- User object, required argument package_version {PackageVersion} -- the package version to be added @@ -300,5 +299,4 @@ def create_from_google_drive_item( mime_type=google_drive_file.mimeType, ) pi.save() - return pi - + return pi \ No newline at end of file diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 8c0eb12..eb29e19 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -1,3 +1,4 @@ +from dataclasses import field from rest_framework import serializers from rest_framework.validators import UniqueTogetherValidator @@ -173,3 +174,40 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) +class TestSerializer(serializers.ModelSerializer): + package_set = serializers.StringRelatedField() + created_by = SimpleUserSerializer(read_only=True) + latest_version = serializers.StringRelatedField() + tags = TagListSerializerField() + + class Meta: + model = Package + fields = ( + # "id", + "slug", + "package_set", + "metadata", + "cached", + "state", + "last_fetched_date", + "created_by", + "created_at", + "updated_at", + "tags", + "latest_version", + ) + read_only_fields = ( + # "id", + "package_set", + "cached", + "last_fetched_date", + "created_by", + "created_at", + "updated_at", + "latest_version", + ) + validators = [ + UniqueTogetherValidator( + queryset=Package.objects.all(), fields=("slug", "package_set") + ) + ] diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index c998158..4865a6e 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -1,5 +1,7 @@ from multiprocessing import log_to_stderr from typing import List +import os +import json from importlib_metadata import packages_distributions from rest_framework import mixins, viewsets, filters from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly @@ -10,14 +12,16 @@ from kerckhoff.integrations.serializers import IntegrationSerializer from .tasks import sync_gdrive_task -from .models import PackageSet, Package +from .models import PackageSet, Package, PackageVersion, PackageItem from .serializers import ( PackageSetSerializer, PackageSerializer, RetrievePackageSerializer, PackageVersionSerializer, CreatePackageVersionSerializer, - PackageSetDetailedSerializer + PackageSetDetailedSerializer, + PackageItemSerializer, + TestSerializer ) @@ -167,7 +171,29 @@ class PublicPackageViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixin # 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): + package = Package.objects.get(package_set__slug=self.kwargs["package_set_slug"], slug=self.kwargs["slug"]) + package_items = package.get_version(package.latest_version.id_num).packageitem_set.all() + # Get items from latest package + img_urls = {} + supported_image_types = [".jpg", ".jpeg", ".png"] + json_data = {"content_rich":{"data":{}}} + for file in package_items: + file_ext = os.path.splitext(file.file_name)[-1] + if(file.file_name == "data.aml"): + json_data = file.data + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] + aml_data = json_data["content_rich"]["data"] + print(aml_data) + aml_data["slug"] = self.kwargs["slug"] + + # package_set = Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) + # package_set.update(metadata=img_urls) + + # return package_set return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) + serializer_class = PackageSerializer permission_classes = (IsAuthenticatedOrReadOnly,) @@ -176,6 +202,60 @@ def get_queryset(self): # 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 + @action(methods=["get"], detail=True) + def versions(self, request, **kwargs): + package: Package = self.get_object() + serializer = PackageVersionSerializer(package.get_all_versions(), many=True) + return Response({"results": serializer.data}) + + @action(methods=["get"], detail=True, serializer_class=Serializer) + def preview(self, request, **kwargs): + package = self.get_object() + package.fetch_cache() + serializer = PackageSerializer(package, many=False) + return Response(serializer.data) + + + + @action(methods=["get"], detail=True, serializer_class=Serializer) + def details(self, request, **kwargs): + package = self.get_object() + package.fetch_cache() + versionSerializer = PackageVersionSerializer(package.get_version(package.latest_version.id_num)) + version_description = versionSerializer.data["version_description"] + folder_id = package.metadata["google_drive"]["folder_id"] + folder_url = package.metadata["google_drive"]["folder_url"] + + package_items = package.get_version(package.latest_version.id_num).packageitem_set.all() + + img_urls = {} + supported_image_types = [".jpg", ".jpeg", ".png"] + for file in package_items: + file_ext = os.path.splitext(file.file_name)[-1] + if(file.file_name == "article.aml"): + aml_data = file.data["content_rich"]["data"] + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] + + cached = package.cached + for item in cached: + if(item["title"] == "article.aml"): + cached_article_preview = file.data["content_plain"]["raw"] + + return Response({"slug": package.slug, + "description": version_description, + "folder_id": folder_id, + "folder_url": folder_url, + "metadata": {}, + "data": {"article": aml_data}, + "processing": False if package.state == "pub" else True, + "cached_article_preview": cached_article_preview, + "last_fetched_date": package.last_fetched_date, + "package_set": str(package.package_set), + "latest_version": str(package.latest_version) + }) + From 6a77e8d0383591f6f08c17a6e9c6c9aacb4de72a Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Tue, 5 Apr 2022 13:59:08 -0700 Subject: [PATCH 3/9] New Serializer Create a new serializer: PackageInfoSerializer. A new API to return similar package info as the old kerckhoff. --- kerckhoff/packages/serializers.py | 78 ++++++++++++++++++++++++------- kerckhoff/packages/views.py | 77 +----------------------------- 2 files changed, 62 insertions(+), 93 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index eb29e19..2b33548 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -1,3 +1,4 @@ +import os from dataclasses import field from rest_framework import serializers from rest_framework.validators import UniqueTogetherValidator @@ -174,40 +175,81 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class TestSerializer(serializers.ModelSerializer): +class PackageInfoSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() - created_by = SimpleUserSerializer(read_only=True) latest_version = serializers.StringRelatedField() - tags = TagListSerializerField() + description = serializers.SerializerMethodField() + folder_id = serializers.SerializerMethodField() + folder_url = serializers.SerializerMethodField() + metadata = serializers.SerializerMethodField() + data = serializers.SerializerMethodField() + cached_article_preview = serializers.SerializerMethodField() + + def get_description(self, obj: Package): + obj.fetch_cache() + versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) + return versionSerializer.data["version_description"] + + def get_folder_id(self, obj: Package): + return obj.metadata["google_drive"]["folder_id"] + + def get_folder_url(self, obj: Package): + return obj.metadata["google_drive"]["folder_url"] + + def get_metadata(self, obj: Package): + return {} + + def get_data(self, obj: Package): + obj.fetch_cache() + package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() + img_urls = {} + supported_image_types = [".jpg", ".jpeg", ".png"] + for file in package_items: + file_ext = os.path.splitext(file.file_name)[-1] + if(file.file_name == "article.aml"): + aml_data = file.data["content_rich"]["data"] + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] + return {"article": aml_data} + + def get_cached_article_preview(self, obj: Package): + obj.fetch_cache() + cached = obj.cached + for item in cached: + if(item["title"] == "article.aml"): + cached_article_preview = item["content_plain"]["raw"] + return cached_article_preview + class Meta: model = Package fields = ( - # "id", "slug", - "package_set", + "description", + "folder_id", + "folder_url", "metadata", - "cached", - "state", + "data", + "cached_article_preview", "last_fetched_date", - "created_by", - "created_at", - "updated_at", - "tags", + "package_set", "latest_version", + ) read_only_fields = ( - # "id", - "package_set", - "cached", + "description", + "folder_id", + "folder_url", + "metadata", + "data", + "cached_article_preview", "last_fetched_date", - "created_by", - "created_at", - "updated_at", + "package_set", "latest_version", ) validators = [ UniqueTogetherValidator( queryset=Package.objects.all(), fields=("slug", "package_set") ) - ] + ] \ No newline at end of file diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index 4865a6e..a7d5f09 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -21,7 +21,7 @@ CreatePackageVersionSerializer, PackageSetDetailedSerializer, PackageItemSerializer, - TestSerializer + PackageInfoSerializer ) @@ -171,90 +171,17 @@ class PublicPackageViewSet(viewsets.GenericViewSet, mixins.ListModelMixin, mixin # 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): - package = Package.objects.get(package_set__slug=self.kwargs["package_set_slug"], slug=self.kwargs["slug"]) - package_items = package.get_version(package.latest_version.id_num).packageitem_set.all() - # Get items from latest package - img_urls = {} - supported_image_types = [".jpg", ".jpeg", ".png"] - json_data = {"content_rich":{"data":{}}} - for file in package_items: - file_ext = os.path.splitext(file.file_name)[-1] - if(file.file_name == "data.aml"): - json_data = file.data - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] - aml_data = json_data["content_rich"]["data"] - print(aml_data) - aml_data["slug"] = self.kwargs["slug"] - - # package_set = Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) - # package_set.update(metadata=img_urls) - # return package_set return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) - serializer_class = PackageSerializer + serializer_class = PackageInfoSerializer 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 - @action(methods=["get"], detail=True) - def versions(self, request, **kwargs): - package: Package = self.get_object() - serializer = PackageVersionSerializer(package.get_all_versions(), many=True) - return Response({"results": serializer.data}) - - @action(methods=["get"], detail=True, serializer_class=Serializer) - def preview(self, request, **kwargs): - package = self.get_object() - package.fetch_cache() - serializer = PackageSerializer(package, many=False) - return Response(serializer.data) - - - - @action(methods=["get"], detail=True, serializer_class=Serializer) - def details(self, request, **kwargs): - package = self.get_object() - package.fetch_cache() - versionSerializer = PackageVersionSerializer(package.get_version(package.latest_version.id_num)) - version_description = versionSerializer.data["version_description"] - folder_id = package.metadata["google_drive"]["folder_id"] - folder_url = package.metadata["google_drive"]["folder_url"] - - package_items = package.get_version(package.latest_version.id_num).packageitem_set.all() - - img_urls = {} - supported_image_types = [".jpg", ".jpeg", ".png"] - for file in package_items: - file_ext = os.path.splitext(file.file_name)[-1] - if(file.file_name == "article.aml"): - aml_data = file.data["content_rich"]["data"] - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] - - cached = package.cached - for item in cached: - if(item["title"] == "article.aml"): - cached_article_preview = file.data["content_plain"]["raw"] - - return Response({"slug": package.slug, - "description": version_description, - "folder_id": folder_id, - "folder_url": folder_url, - "metadata": {}, - "data": {"article": aml_data}, - "processing": False if package.state == "pub" else True, - "cached_article_preview": cached_article_preview, - "last_fetched_date": package.last_fetched_date, - "package_set": str(package.package_set), - "latest_version": str(package.latest_version) - }) From 5f1e299e9ceb408006ebda2e2a2229de2792b9f4 Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Tue, 10 May 2022 14:29:10 -0700 Subject: [PATCH 4/9] PR Modified --- kerckhoff/packages/serializers.py | 31 ++++++++----------------------- kerckhoff/packages/views.py | 16 ++++++++++++---- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 2b33548..9f9ac7a 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -175,42 +175,31 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class PackageInfoSerializer(serializers.ModelSerializer): +class PublicPackageSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() latest_version = serializers.StringRelatedField() description = serializers.SerializerMethodField() - folder_id = serializers.SerializerMethodField() - folder_url = serializers.SerializerMethodField() - metadata = serializers.SerializerMethodField() + data = serializers.SerializerMethodField() cached_article_preview = serializers.SerializerMethodField() def get_description(self, obj: Package): + obj.fetch_cache() + if not obj.latest_version: + return "Has Not Created A Version Yet" versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) return versionSerializer.data["version_description"] - - def get_folder_id(self, obj: Package): - return obj.metadata["google_drive"]["folder_id"] - - def get_folder_url(self, obj: Package): - return obj.metadata["google_drive"]["folder_url"] - - def get_metadata(self, obj: Package): - return {} - + def get_data(self, obj: Package): obj.fetch_cache() + if not obj.latest_version: + return "Has Not Created A Version Yet" package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() - img_urls = {} - supported_image_types = [".jpg", ".jpeg", ".png"] for file in package_items: file_ext = os.path.splitext(file.file_name)[-1] if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] return {"article": aml_data} def get_cached_article_preview(self, obj: Package): @@ -227,8 +216,6 @@ class Meta: fields = ( "slug", "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", @@ -239,8 +226,6 @@ class Meta: ) read_only_fields = ( "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index a7d5f09..0ff1af3 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -1,7 +1,5 @@ from multiprocessing import log_to_stderr from typing import List -import os -import json from importlib_metadata import packages_distributions from rest_framework import mixins, viewsets, filters from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly @@ -21,7 +19,7 @@ CreatePackageVersionSerializer, PackageSetDetailedSerializer, PackageItemSerializer, - PackageInfoSerializer + PublicPackageSerializer ) @@ -175,13 +173,23 @@ def get_queryset(self): return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) - serializer_class = PackageInfoSerializer + serializer_class = PublicPackageSerializer 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 + # @action(methods=["get"], detail=True, serializer_class=Serializer, url_name='version', + # url_path='version/(?P[^/.]+)') + # def version(self, request, **kwargs): + # for key, value in kwargs.items(): + # print("{0} = {1}".format(key, value)) + # package = self.get_object() + # package.fetch_cache() + # serializer = PackageSerializer(package, many=False) + # return Response(serializer.data) + From aa214e9f513df3fa5785e5a7f6ca114832c821fc Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Thu, 26 May 2022 21:40:41 -0700 Subject: [PATCH 5/9] Specific Version Retrieval Allows for retrieving data and images form a specified version. --- kerckhoff/packages/serializers.py | 3 +-- kerckhoff/packages/views.py | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 9f9ac7a..6cf76d8 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -195,9 +195,8 @@ def get_data(self, obj: Package): obj.fetch_cache() if not obj.latest_version: return "Has Not Created A Version Yet" - package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() + package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() for file in package_items: - file_ext = os.path.splitext(file.file_name)[-1] if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] return {"article": aml_data} diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index 0ff1af3..240b258 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -1,4 +1,5 @@ from multiprocessing import log_to_stderr +import os from typing import List from importlib_metadata import packages_distributions from rest_framework import mixins, viewsets, filters @@ -133,6 +134,21 @@ def retrieve(self, request, **kwargs): ) response = serializer.data return Response(response) + + @action(methods=["get"], detail=True, serializer_class=Serializer, + url_path='version/(?P[^/.]+)') + def version(self, request, **kwargs): + package_items = self.get_object().get_version(kwargs['ver']).packageitem_set.all() + img_urls = {} + supported_image_types = [".jpg", ".jpeg", ".png"] + for file in package_items: + file_ext = os.path.splitext(file.file_name)[-1] + if(file.file_name == "article.aml"): + aml_data = file.data["content_rich"]["data"] + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] + return Response({"data": aml_data, "images": img_urls} ) class PackageCreateAndListViewSet( @@ -180,16 +196,6 @@ def get_queryset(self): # 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 - # @action(methods=["get"], detail=True, serializer_class=Serializer, url_name='version', - # url_path='version/(?P[^/.]+)') - # def version(self, request, **kwargs): - # for key, value in kwargs.items(): - # print("{0} = {1}".format(key, value)) - # package = self.get_object() - # package.fetch_cache() - # serializer = PackageSerializer(package, many=False) - # return Response(serializer.data) - From 182dec5f33c339dbc35da11d2d13be85f4c1aacd Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Thu, 26 May 2022 23:14:05 -0700 Subject: [PATCH 6/9] resolve conflict --- kerckhoff/packages/serializers.py | 28 ++++++++++++++++++++++------ kerckhoff/packages/views.py | 6 ++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 6cf76d8..3212e84 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -175,30 +175,42 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class PublicPackageSerializer(serializers.ModelSerializer): +class PackageInfoSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() latest_version = serializers.StringRelatedField() description = serializers.SerializerMethodField() - + folder_id = serializers.SerializerMethodField() + folder_url = serializers.SerializerMethodField() + metadata = serializers.SerializerMethodField() data = serializers.SerializerMethodField() cached_article_preview = serializers.SerializerMethodField() def get_description(self, obj: Package): - obj.fetch_cache() - if not obj.latest_version: - return "Has Not Created A Version Yet" versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) return versionSerializer.data["version_description"] - + + def get_folder_id(self, obj: Package): + return obj.metadata["google_drive"]["folder_id"] + + def get_folder_url(self, obj: Package): + return obj.metadata["google_drive"]["folder_url"] + + def get_metadata(self, obj: Package): + return {} + def get_data(self, obj: Package): obj.fetch_cache() if not obj.latest_version: return "Has Not Created A Version Yet" package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() + for file in package_items: if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] return {"article": aml_data} def get_cached_article_preview(self, obj: Package): @@ -215,6 +227,8 @@ class Meta: fields = ( "slug", "description", + "folder_id", + "folder_url", "metadata", "data", "cached_article_preview", @@ -225,6 +239,8 @@ class Meta: ) read_only_fields = ( "description", + "folder_id", + "folder_url", "metadata", "data", "cached_article_preview", diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index 240b258..c999679 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -1,6 +1,8 @@ from multiprocessing import log_to_stderr import os from typing import List +import os +import json from importlib_metadata import packages_distributions from rest_framework import mixins, viewsets, filters from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly @@ -20,7 +22,7 @@ CreatePackageVersionSerializer, PackageSetDetailedSerializer, PackageItemSerializer, - PublicPackageSerializer + PackageInfoSerializer ) @@ -189,7 +191,7 @@ def get_queryset(self): return Package.objects.filter(package_set__slug=self.kwargs["package_set_slug"]) - serializer_class = PublicPackageSerializer + serializer_class = PackageInfoSerializer 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" From ac4372bb77124a40f3bb1c9bb20e3e0f7d07a134 Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Thu, 26 May 2022 23:22:56 -0700 Subject: [PATCH 7/9] back to prev --- kerckhoff/packages/serializers.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 3212e84..9f9ac7a 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -175,42 +175,31 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class PackageInfoSerializer(serializers.ModelSerializer): +class PublicPackageSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() latest_version = serializers.StringRelatedField() description = serializers.SerializerMethodField() - folder_id = serializers.SerializerMethodField() - folder_url = serializers.SerializerMethodField() - metadata = serializers.SerializerMethodField() + data = serializers.SerializerMethodField() cached_article_preview = serializers.SerializerMethodField() def get_description(self, obj: Package): + obj.fetch_cache() + if not obj.latest_version: + return "Has Not Created A Version Yet" versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) return versionSerializer.data["version_description"] - - def get_folder_id(self, obj: Package): - return obj.metadata["google_drive"]["folder_id"] - - def get_folder_url(self, obj: Package): - return obj.metadata["google_drive"]["folder_url"] - - def get_metadata(self, obj: Package): - return {} - + def get_data(self, obj: Package): obj.fetch_cache() if not obj.latest_version: return "Has Not Created A Version Yet" - package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() - + package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() for file in package_items: + file_ext = os.path.splitext(file.file_name)[-1] if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] return {"article": aml_data} def get_cached_article_preview(self, obj: Package): @@ -227,8 +216,6 @@ class Meta: fields = ( "slug", "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", @@ -239,8 +226,6 @@ class Meta: ) read_only_fields = ( "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", From af0c3fff695e77655b2317a29d2f048d2808d68e Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Thu, 26 May 2022 23:23:07 -0700 Subject: [PATCH 8/9] Revert "PR Modified" This reverts commit 5f1e299e9ceb408006ebda2e2a2229de2792b9f4. --- kerckhoff/packages/serializers.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 9f9ac7a..2b33548 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -175,31 +175,42 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class PublicPackageSerializer(serializers.ModelSerializer): +class PackageInfoSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() latest_version = serializers.StringRelatedField() description = serializers.SerializerMethodField() - + folder_id = serializers.SerializerMethodField() + folder_url = serializers.SerializerMethodField() + metadata = serializers.SerializerMethodField() data = serializers.SerializerMethodField() cached_article_preview = serializers.SerializerMethodField() def get_description(self, obj: Package): - obj.fetch_cache() - if not obj.latest_version: - return "Has Not Created A Version Yet" versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) return versionSerializer.data["version_description"] - + + def get_folder_id(self, obj: Package): + return obj.metadata["google_drive"]["folder_id"] + + def get_folder_url(self, obj: Package): + return obj.metadata["google_drive"]["folder_url"] + + def get_metadata(self, obj: Package): + return {} + def get_data(self, obj: Package): obj.fetch_cache() - if not obj.latest_version: - return "Has Not Created A Version Yet" package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() + img_urls = {} + supported_image_types = [".jpg", ".jpeg", ".png"] for file in package_items: file_ext = os.path.splitext(file.file_name)[-1] if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] + if(file_ext in supported_image_types): + # Don't worry about images for now + img_urls[file.file_name] = file.data["src_large"] return {"article": aml_data} def get_cached_article_preview(self, obj: Package): @@ -216,6 +227,8 @@ class Meta: fields = ( "slug", "description", + "folder_id", + "folder_url", "metadata", "data", "cached_article_preview", @@ -226,6 +239,8 @@ class Meta: ) read_only_fields = ( "description", + "folder_id", + "folder_url", "metadata", "data", "cached_article_preview", From 14bc31ddffb14b8ec15231dc9a21300a3b1bc12e Mon Sep 17 00:00:00 2001 From: Emery <56237263+EmeryYU@users.noreply.github.com> Date: Thu, 26 May 2022 23:25:01 -0700 Subject: [PATCH 9/9] revert --- kerckhoff/packages/serializers.py | 31 ++++++++----------------------- kerckhoff/packages/views.py | 15 +-------------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/kerckhoff/packages/serializers.py b/kerckhoff/packages/serializers.py index 2b33548..9f9ac7a 100644 --- a/kerckhoff/packages/serializers.py +++ b/kerckhoff/packages/serializers.py @@ -175,42 +175,31 @@ class Meta(PackageSerializer.Meta): fields = PackageSerializer.Meta.fields + ("version_data",) read_only_fields = PackageSerializer.Meta.read_only_fields + ("version_data",) -class PackageInfoSerializer(serializers.ModelSerializer): +class PublicPackageSerializer(serializers.ModelSerializer): package_set = serializers.StringRelatedField() latest_version = serializers.StringRelatedField() description = serializers.SerializerMethodField() - folder_id = serializers.SerializerMethodField() - folder_url = serializers.SerializerMethodField() - metadata = serializers.SerializerMethodField() + data = serializers.SerializerMethodField() cached_article_preview = serializers.SerializerMethodField() def get_description(self, obj: Package): + obj.fetch_cache() + if not obj.latest_version: + return "Has Not Created A Version Yet" versionSerializer = PackageVersionSerializer(obj.get_version(obj.latest_version.id_num)) return versionSerializer.data["version_description"] - - def get_folder_id(self, obj: Package): - return obj.metadata["google_drive"]["folder_id"] - - def get_folder_url(self, obj: Package): - return obj.metadata["google_drive"]["folder_url"] - - def get_metadata(self, obj: Package): - return {} - + def get_data(self, obj: Package): obj.fetch_cache() + if not obj.latest_version: + return "Has Not Created A Version Yet" package_items = obj.get_version(obj.latest_version.id_num).packageitem_set.all() - img_urls = {} - supported_image_types = [".jpg", ".jpeg", ".png"] for file in package_items: file_ext = os.path.splitext(file.file_name)[-1] if(file.file_name == "article.aml"): aml_data = file.data["content_rich"]["data"] - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] return {"article": aml_data} def get_cached_article_preview(self, obj: Package): @@ -227,8 +216,6 @@ class Meta: fields = ( "slug", "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", @@ -239,8 +226,6 @@ class Meta: ) read_only_fields = ( "description", - "folder_id", - "folder_url", "metadata", "data", "cached_article_preview", diff --git a/kerckhoff/packages/views.py b/kerckhoff/packages/views.py index c999679..1572eeb 100644 --- a/kerckhoff/packages/views.py +++ b/kerckhoff/packages/views.py @@ -137,20 +137,7 @@ def retrieve(self, request, **kwargs): response = serializer.data return Response(response) - @action(methods=["get"], detail=True, serializer_class=Serializer, - url_path='version/(?P[^/.]+)') - def version(self, request, **kwargs): - package_items = self.get_object().get_version(kwargs['ver']).packageitem_set.all() - img_urls = {} - supported_image_types = [".jpg", ".jpeg", ".png"] - for file in package_items: - file_ext = os.path.splitext(file.file_name)[-1] - if(file.file_name == "article.aml"): - aml_data = file.data["content_rich"]["data"] - if(file_ext in supported_image_types): - # Don't worry about images for now - img_urls[file.file_name] = file.data["src_large"] - return Response({"data": aml_data, "images": img_urls} ) + class PackageCreateAndListViewSet(