diff --git a/addon_service/authorized_storage_account/models.py b/addon_service/authorized_storage_account/models.py index 450ac982..7ada099e 100644 --- a/addon_service/authorized_storage_account/models.py +++ b/addon_service/authorized_storage_account/models.py @@ -26,3 +26,7 @@ class Meta: class JSONAPIMeta: resource_name = "authorized-storage-accounts" + + @property + def account_owner(self): + return self.external_account.owner # TODO: prefetch/select_related diff --git a/addon_service/authorized_storage_account/serializers.py b/addon_service/authorized_storage_account/serializers.py index 5a945ca5..aa47cf30 100644 --- a/addon_service/authorized_storage_account/serializers.py +++ b/addon_service/authorized_storage_account/serializers.py @@ -2,7 +2,6 @@ from rest_framework_json_api.relations import ( HyperlinkedRelatedField, ResourceRelatedField, - SerializerMethodResourceRelatedField, ) from rest_framework_json_api.utils import get_resource_type_from_model @@ -19,10 +18,9 @@ class AuthorizedStorageAccountSerializer(serializers.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField(view_name=f"{RESOURCE_NAME}-detail") - account_owner = SerializerMethodResourceRelatedField( - model=InternalUser, + account_owner = HyperlinkedRelatedField( many=False, - method_name="_get_account_owner", + queryset=InternalUser.objects.all(), related_link_view_name=f"{RESOURCE_NAME}-related", ) external_storage_service = ResourceRelatedField( @@ -55,6 +53,3 @@ class Meta: "default_root_folder", "external_storage_service", ] - - def _get_account_owner(self, instance: AuthorizedStorageAccount): - return instance.external_account.owner # TODO: prefetch/select_related diff --git a/addon_service/internal_user/models.py b/addon_service/internal_user/models.py index fd8e3514..18b231b9 100644 --- a/addon_service/internal_user/models.py +++ b/addon_service/internal_user/models.py @@ -1,11 +1,18 @@ from django.db import models +from addon_service.authorized_storage_account.models import AuthorizedStorageAccount from addon_service.common.base_model import AddonsServiceBaseModel class InternalUser(AddonsServiceBaseModel): user_uri = models.URLField(unique=True, db_index=True, null=False) + @property + def authorized_storage_accounts(self): + return AuthorizedStorageAccount.objects.filter( + external_account__owner=self, + ) + class Meta: verbose_name = "Internal User" verbose_name_plural = "Internal Users" diff --git a/addon_service/urls.py b/addon_service/urls.py index bc167c5f..c80c6240 100644 --- a/addon_service/urls.py +++ b/addon_service/urls.py @@ -6,7 +6,7 @@ def _urls_for_viewsets(*viewsets): - """returns urlpatterns for a viewset that corresponds to a resource type + """returns urlpatterns for viewsets that each correspond to a resource type includes patterns for jsonapi-style relationships """