Skip to content

Commit

Permalink
Add a ComponentInterfaceValuePostRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvanrun committed Nov 15, 2024
1 parent fb34862 commit fe43515
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions gcapi/create_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
ArchiveItem,
ArchiveItemPost,
ComponentInterface,
ComponentInterfaceValuePostRequest,
DisplaySet,
DisplaySetPost,
HyperlinkedComponentInterfaceValue,
Expand Down Expand Up @@ -79,13 +80,9 @@ def prepare(self):

def __call__(self):
"""
If applicable, upload any contents and returns a dict
If applicable, upload any contents and returns an item
that can be used in POSTs that would use the created objects.
"""

# TODO: convert this to use the proper PostRequest models;
# requires non-trivial in-depth changes to client

if not self.prepared:
yield from self.prepare()

Expand Down Expand Up @@ -135,7 +132,14 @@ def __init__(
def __call__(self):
yield from super().__call__()

return {"interface": self.interface.slug} # noqa: B901
return ComponentInterfaceValuePostRequest( # noqa: B901
interface=self.interface.slug,
value=None,
file=None,
image=None,
upload_session=None,
user_upload=None,
)


class FileCIVCreateStrategy(CIVCreateStrategy):
Expand Down Expand Up @@ -165,15 +169,15 @@ def prepare(self):
self.content_name = self.interface.relative_path

def __call__(self):
item = yield from super().__call__()
post_request = yield from super().__call__()

with open(self.content, "rb") as f:
user_upload = yield from self.client_api.uploads.upload_fileobj(
fileobj=f, filename=self.content_name
)
item["user_upload"] = user_upload.api_url
post_request.user_upload = user_upload.api_url

return item
return post_request


class ImageCIVCreateStrategy(CIVCreateStrategy):
Expand All @@ -194,12 +198,12 @@ def prepare(self):
self.content = clean_file_source(self.source)

def __call__(self):
item = yield from super().__call__()
post_request = yield from super().__call__()

if isinstance(self.content, HyperlinkedImage):
# Reuse the existing image
item["image"] = self.content.api_url
return item
post_request.image = self.content.api_url
return

# Upload the image
if self.parent is None:
Expand Down Expand Up @@ -238,8 +242,8 @@ def __call__(self):
)

if self.parent is None:
item["upload_session"] = raw_image_upload_session.api_url
return item
post_request.upload_session = raw_image_upload_session.api_url
return post_request
else:
return Empty

Expand Down

0 comments on commit fe43515

Please sign in to comment.