Skip to content

Commit

Permalink
feat: Added share url and download url
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Feb 7, 2024
1 parent b9ef04b commit 18c4b50
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 8 additions & 6 deletions terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions terraso_backend/apps/graphql/schema/shared_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions terraso_backend/tests/graphql/test_shared_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def test_data_entries_from_parent_query_by_resource_field(
}
}
shareUrl
downloadUrl
shareAccess
}
}
Expand All @@ -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
Expand Down

0 comments on commit 18c4b50

Please sign in to comment.