diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index 14d51120b..b2a6e06e8 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -431,12 +431,20 @@ type SharedResourceNodeEdge { type SharedResourceNode implements Node { id: ID! + shareUuid: UUID! shareAccess: CoreSharedResourceShareAccessChoices! source: SourceNode target: TargetNode + downloadUrl: String shareUrl: String } +""" +Leverages the internal Python implementation of UUID (uuid.UUID) to provide native UUID objects +in fields, resolvers and input. +""" +scalar UUID + """An enumeration.""" enum CoreSharedResourceShareAccessChoices { """No share access""" @@ -522,12 +530,6 @@ type VisualizationConfigNodeEdge { cursor: String! } -""" -Leverages the internal Python implementation of UUID (uuid.UUID) to provide native UUID objects -in fields, resolvers and input. -""" -scalar UUID - union OwnerNode = GroupNode | LandscapeNode union TargetNode = GroupNode | LandscapeNode diff --git a/terraso_backend/apps/graphql/schema/shared_resources.py b/terraso_backend/apps/graphql/schema/shared_resources.py index d7e81397c..2e2457a06 100644 --- a/terraso_backend/apps/graphql/schema/shared_resources.py +++ b/terraso_backend/apps/graphql/schema/shared_resources.py @@ -48,11 +48,12 @@ class SharedResourceNode(DjangoObjectType): id = graphene.ID(source="pk", required=True) source = graphene.Field(SourceNode) target = graphene.Field(TargetNode) + download_url = graphene.String() share_url = graphene.String() class Meta: model = SharedResource - fields = ["id", "share_access"] + fields = ["id", "share_access", "share_uuid"] interfaces = (relay.Node,) connection_class = TerrasoConnection @@ -62,9 +63,16 @@ def resolve_source(self, info, **kwargs): def resolve_target(self, info, **kwargs): return self.target - def resolve_share_url(self, info, **kwargs): + def resolve_download_url(self, info, **kwargs): return f"{settings.API_ENDPOINT}/shared-data/download/{self.share_uuid}" + def resolve_share_url(self, info, **kwargs): + target = self.target + entity = "groups" if isinstance(target, Group) else "landscapes" + slug = target.slug + share_uuid = self.share_uuid + return f"{settings.WEB_CLIENT_URL}/{entity}/{slug}/shared-resource/download/{share_uuid}" + class SharedResourceRelayNode: @classmethod diff --git a/terraso_backend/tests/graphql/test_shared_data.py b/terraso_backend/tests/graphql/test_shared_data.py index b598aceff..70c26da60 100644 --- a/terraso_backend/tests/graphql/test_shared_data.py +++ b/terraso_backend/tests/graphql/test_shared_data.py @@ -399,6 +399,7 @@ def test_data_entries_from_parent_query_by_resource_field( } } shareUrl + downloadUrl shareAccess } } @@ -417,16 +418,21 @@ def test_data_entries_from_parent_query_by_resource_field( { "name": resource["node"]["source"]["name"], "share_url": resource["node"]["shareUrl"], + "download_url": resource["node"]["downloadUrl"], "share_access": resource["node"]["shareAccess"], } for resource in resources ] for data_entry in data_entries: - share_uuid = data_entry.shared_resources.all()[0].share_uuid - share_url = f"{settings.API_ENDPOINT}/shared-data/download/{share_uuid}" + uuid = data_entry.shared_resources.all()[0].share_uuid + download_url = f"{settings.API_ENDPOINT}/shared-data/download/{uuid}" + slug = parent_entity.slug + share_url = f"{settings.WEB_CLIENT_URL}/{parent}/{slug}/shared-resource/download/{uuid}" + assert { "name": data_entry.name, + "download_url": download_url, "share_url": share_url, "share_access": data_entry.shared_resources.all()[0].share_access.upper(), } in entries_result