Skip to content

Commit

Permalink
[ENG-5257] Rename "internal foo" to "foo reference" (#13)
Browse files Browse the repository at this point in the history
internal_user to user_reference
internal_resource to resource_reference
  • Loading branch information
aaxelb authored Feb 8, 2024
1 parent 8c987b3 commit e3eddfe
Show file tree
Hide file tree
Showing 27 changed files with 141 additions and 135 deletions.
21 changes: 10 additions & 11 deletions addon_service/authorized_storage_account/serializers.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
from rest_framework_json_api import serializers
from rest_framework_json_api.relations import (
ResourceRelatedField,
HyperlinkedRelatedField,
ResourceRelatedField,
)
from rest_framework_json_api.utils import get_resource_type_from_model

from addon_service.models import (
AuthorizedStorageAccount,
ConfiguredStorageAddon,
ExternalStorageService,
ExternalCredentials,
ExternalAccount,
InternalUser,
ExternalCredentials,
ExternalStorageService,
UserReference,
)


RESOURCE_NAME = get_resource_type_from_model(AuthorizedStorageAccount)


class AccountOwnerField(ResourceRelatedField):
def to_internal_value(self, data):
internal_user, _ = InternalUser.objects.get_or_create(user_uri=data["id"])
return internal_user
user_reference, _ = UserReference.objects.get_or_create(user_uri=data["id"])
return user_reference


class ExternalStorageServiceField(ResourceRelatedField):
Expand All @@ -32,7 +33,6 @@ def to_internal_value(self, data):


class AuthorizedStorageAccountSerializer(serializers.HyperlinkedModelSerializer):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

Expand All @@ -41,12 +41,11 @@ def __init__(self, *args, **kwargs):
self.fields.pop("configured_storage_addons", None)

url = serializers.HyperlinkedIdentityField(
view_name=f"{RESOURCE_NAME}-detail",
required=False
view_name=f"{RESOURCE_NAME}-detail", required=False
)
account_owner = AccountOwnerField(
many=False,
queryset=InternalUser.objects.all(),
queryset=UserReference.objects.all(),
related_link_view_name=f"{RESOURCE_NAME}-related",
)
external_storage_service = ExternalStorageServiceField(
Expand All @@ -68,7 +67,7 @@ def __init__(self, *args, **kwargs):
) # placeholder for ExternalCredentials integrity only not auth

included_serializers = {
"account_owner": "addon_service.serializers.InternalUserSerializer",
"account_owner": "addon_service.serializers.UserReferenceSerializer",
"external_storage_service": "addon_service.serializers.ExternalStorageServiceSerializer",
"configured_storage_addons": "addon_service.serializers.ConfiguredStorageAddonSerializer",
}
Expand Down
2 changes: 1 addition & 1 deletion addon_service/configured_storage_addon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ConfiguredStorageAddon(AddonsServiceBaseModel):
related_name="configured_storage_addons",
)
authorized_resource = models.ForeignKey(
"addon_service.InternalResource",
"addon_service.ResourceReference",
on_delete=models.CASCADE,
related_name="configured_storage_addons",
)
Expand Down
11 changes: 6 additions & 5 deletions addon_service/configured_storage_addon/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
from addon_service.models import (
AuthorizedStorageAccount,
ConfiguredStorageAddon,
InternalResource,
ResourceReference,
)


RESOURCE_NAME = get_resource_type_from_model(ConfiguredStorageAddon)


class AuthorizedResourceField(ResourceRelatedField):
def to_internal_value(self, data):
internal_resource, _ = InternalResource.objects.get_or_create(
resource_reference, _ = ResourceReference.objects.get_or_create(
resource_uri=data["id"]
)
return internal_resource
return resource_reference


class ConfiguredStorageAddonSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -28,7 +29,7 @@ class ConfiguredStorageAddonSerializer(serializers.HyperlinkedModelSerializer):
related_link_view_name=f"{RESOURCE_NAME}-related",
)
authorized_resource = AuthorizedResourceField(
queryset=InternalResource.objects.all(),
queryset=ResourceReference.objects.all(),
many=False,
related_link_view_name=f"{RESOURCE_NAME}-related",
)
Expand All @@ -37,7 +38,7 @@ class ConfiguredStorageAddonSerializer(serializers.HyperlinkedModelSerializer):
"base_account": (
"addon_service.serializers.AuthorizedStorageAccountSerializer"
),
"authorized_resource": "addon_service.serializers.InternalResourceSerializer",
"authorized_resource": "addon_service.serializers.ResourceReferenceSerializer",
}

class Meta:
Expand Down
2 changes: 1 addition & 1 deletion addon_service/external_account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ExternalAccount(AddonsServiceBaseModel):
related_name="external_accounts",
)
owner = models.ForeignKey(
"addon_service.InternalUser",
"addon_service.UserReference",
on_delete=models.CASCADE,
related_name="external_accounts",
)
Expand Down
10 changes: 0 additions & 10 deletions addon_service/internal_resource/views.py

This file was deleted.

10 changes: 0 additions & 10 deletions addon_service/internal_user/views.py

This file was deleted.

4 changes: 2 additions & 2 deletions addon_service/management/commands/fill_garbage.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def handle_label(self, label, **options):
credentials_issuer=_ci,
)
for _i in range(3):
_iu, _ = db.InternalUser.objects.get_or_create(
_iu, _ = db.UserReference.objects.get_or_create(
user_uri=f"http://osf.example/u{label}{_i}",
)
_ec = db.ExternalCredentials.objects.create()
Expand All @@ -37,7 +37,7 @@ def handle_label(self, label, **options):
external_account=_ea,
)
for _j in range(5):
_ir, _ = db.InternalResource.objects.get_or_create(
_ir, _ = db.ResourceReference.objects.get_or_create(
resource_uri=f"http://osf.example/r{label}{_j}",
)
_csa = db.ConfiguredStorageAddon.objects.create(
Expand Down
16 changes: 8 additions & 8 deletions addon_service/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name="InternalResource",
name="ResourceReference",
fields=[
(
"id",
Expand All @@ -97,12 +97,12 @@ class Migration(migrations.Migration):
("resource_uri", models.URLField(db_index=True, unique=True)),
],
options={
"verbose_name": "Internal Resource",
"verbose_name_plural": "Internal Resources",
"verbose_name": "Resource Reference",
"verbose_name_plural": "Resource References",
},
),
migrations.CreateModel(
name="InternalUser",
name="UserReference",
fields=[
(
"id",
Expand All @@ -118,8 +118,8 @@ class Migration(migrations.Migration):
("user_uri", models.URLField(db_index=True, unique=True)),
],
options={
"verbose_name": "Internal User",
"verbose_name_plural": "Internal Users",
"verbose_name": "User Reference",
"verbose_name_plural": "User References",
},
),
migrations.CreateModel(
Expand Down Expand Up @@ -190,7 +190,7 @@ class Migration(migrations.Migration):
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="external_accounts",
to="addon_service.internaluser",
to="addon_service.userreference",
),
),
],
Expand Down Expand Up @@ -227,7 +227,7 @@ class Migration(migrations.Migration):
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="configured_storage_addons",
to="addon_service.internalresource",
to="addon_service.resourcereference",
),
),
],
Expand Down
8 changes: 4 additions & 4 deletions addon_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from addon_service.external_account.models import ExternalAccount
from addon_service.external_credentials.models import ExternalCredentials
from addon_service.external_storage_service.models import ExternalStorageService
from addon_service.internal_resource.models import InternalResource
from addon_service.internal_user.models import InternalUser
from addon_service.resource_reference.models import ResourceReference
from addon_service.user_reference.models import UserReference


__all__ = (
Expand All @@ -20,6 +20,6 @@
"ExternalCredentials",
"ExternalStorageService",
# 'ExternalComputeService',
"InternalResource",
"InternalUser",
"ResourceReference",
"UserReference",
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from addon_service.common.base_model import AddonsServiceBaseModel


class InternalResource(AddonsServiceBaseModel):
class ResourceReference(AddonsServiceBaseModel):
resource_uri = models.URLField(unique=True, db_index=True, null=False)

class Meta:
verbose_name = "Internal Resource"
verbose_name_plural = "Internal Resources"
verbose_name = "Resource Reference"
verbose_name_plural = "Resource References"
app_label = "addon_service"

class JSONAPIMeta:
resource_name = "internal-resources"
resource_name = "resource-references"
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

from addon_service.models import (
ConfiguredStorageAddon,
InternalResource,
ResourceReference,
)


RESOURCE_NAME = get_resource_type_from_model(InternalResource)
RESOURCE_NAME = get_resource_type_from_model(ResourceReference)


class InternalResourceSerializer(serializers.HyperlinkedModelSerializer):
class ResourceReferenceSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name=f"{RESOURCE_NAME}-detail")
configured_storage_addons = HyperlinkedRelatedField(
many=True,
Expand All @@ -25,7 +25,7 @@ class InternalResourceSerializer(serializers.HyperlinkedModelSerializer):
}

class Meta:
model = InternalResource
model = ResourceReference
fields = [
"url",
"resource_uri",
Expand Down
10 changes: 10 additions & 0 deletions addon_service/resource_reference/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework_json_api.views import ReadOnlyModelViewSet

from .models import ResourceReference
from .serializers import ResourceReferenceSerializer


class ResourceReferenceViewSet(ReadOnlyModelViewSet):
queryset = ResourceReference.objects.all()
serializer_class = ResourceReferenceSerializer
# TODO: permissions_classes
8 changes: 4 additions & 4 deletions addon_service/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
from addon_service.external_storage_service.serializers import (
ExternalStorageServiceSerializer,
)
from addon_service.internal_resource.serializers import InternalResourceSerializer
from addon_service.internal_user.serializers import InternalUserSerializer
from addon_service.resource_reference.serializers import ResourceReferenceSerializer
from addon_service.user_reference.serializers import UserReferenceSerializer


__all__ = (
"AuthorizedStorageAccountSerializer",
"ConfiguredStorageAddonSerializer",
"ExternalStorageServiceSerializer",
"InternalResourceSerializer",
"InternalUserSerializer",
"ResourceReferenceSerializer",
"UserReferenceSerializer",
)
12 changes: 6 additions & 6 deletions addon_service/tests/_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
from addon_service import models as db


class InternalUserFactory(DjangoModelFactory):
class UserReferenceFactory(DjangoModelFactory):
class Meta:
model = db.InternalUser
model = db.UserReference

user_uri = factory.Sequence(lambda n: f"http://osf.example/user{n}")


class InternalResourceFactory(DjangoModelFactory):
class ResourceReferenceFactory(DjangoModelFactory):
class Meta:
model = db.InternalResource
model = db.ResourceReference

resource_uri = factory.Sequence(lambda n: f"http://osf.example/thing{n}")

Expand All @@ -36,7 +36,7 @@ class Meta:
remote_account_display_name = factory.Faker("word")

credentials_issuer = factory.SubFactory(CredentialsIssuerFactory)
owner = factory.SubFactory(InternalUserFactory)
owner = factory.SubFactory(UserReferenceFactory)
credentials = factory.SubFactory(ExternalCredentialsFactory)


Expand Down Expand Up @@ -71,4 +71,4 @@ class Meta:

root_folder = "/"
base_account = factory.SubFactory(AuthorizedStorageAccountFactory)
authorized_resource = factory.SubFactory(InternalResourceFactory)
authorized_resource = factory.SubFactory(ResourceReferenceFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_unauthorized(self):

@unittest.expectedFailure # TODO
def test_wrong_user(self):
_another_user = _factories.InternalUserFactory()
_another_user = _factories.UserReferenceFactory()
_resp = self._view(
get_test_request(user=_another_user),
pk=self._user.pk,
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_post(self):
},
"account_owner": {
"data": {
"type": "internal-users",
"type": "user-references",
"id": self._csa.base_account.external_account.owner.user_uri,
}
},
Expand Down
Loading

0 comments on commit e3eddfe

Please sign in to comment.