Skip to content

Commit 1e5c0bf

Browse files
authored
Merge pull request #17 from octue/feature/add-object-metadata
Add content object metadata and custom time by default
2 parents 70e3717 + 0403aef commit 1e5c0bf

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

.devcontainer/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"esbonio.sphinx.confDir": "${workspaceFolder}/docs/source",
1616
"jupyter.widgetScriptSources": ["jsdelivr.com", "unpkg.com"],
1717
"prettier.prettierPath": "/usr/local/prettier",
18+
"python.defaultInterpreterPath": "/usr/local/bin/python",
1819
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
1920
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
2021
"python.formatting.provider": "black",

django_gcp/events/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def make_pubsub_message(
6060
https://stackoverflow.com/questions/73477187/why-do-gcp-pub-sub-messages-have-duplicated-message-id-and-publish-time-values
6161
6262
:param Union[dict, list] data: JSON-serialisable data to form the body of the message
63+
:param str subscription: a subscription path which indicates the subscription that caused this message to be delivered, eg 'projects/my-project/subscriptions/my-subscription-name'
6364
:param Union[dict, None] attributes: Dict of attributes to attach to the message. Contents must be flat, containing only string keys with string values.
6465
:param Union[str, None] message_id: An optional id for the message.
6566
:param Union[str, None] ordering_key: A string used to order messages.

django_gcp/storage/gcloud.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,24 @@ def _open(self, name, mode="rb"):
155155
return file_object
156156

157157
def _save(self, name, content):
158+
159+
# # cleaned_name = clean_name(name)
160+
# # name = self._normalize_name(cleaned_name)
161+
# # content.name = cleaned_name
162+
# # file = GoogleCloudFile(name, "rw", self)
163+
# file.blob.upload_from_file(
164+
# content, rewind=True, size=content.size, content_type=file.mime_type, predefined_acl=self.default_acl
165+
# )
166+
# return cleaned_name
167+
158168
cleaned_name = clean_name(name)
159169
name = self._normalize_name(cleaned_name)
160170

161171
content.name = cleaned_name
162172
file_object = GoogleCloudFile(name, "rw", self)
163173

164174
upload_params = {}
165-
blob_params = self.get_object_parameters(name)
175+
blob_params = self.get_object_parameters(content)
166176
upload_params["predefined_acl"] = blob_params.pop("acl", self.settings.default_acl)
167177
upload_params[CONTENT_TYPE] = blob_params.pop(CONTENT_TYPE, file_object.mime_type)
168178

@@ -180,12 +190,26 @@ def _save(self, name, content):
180190
file_object.blob.upload_from_file(content, rewind=True, size=getattr(content, "size", None), **upload_params)
181191
return cleaned_name
182192

183-
def get_object_parameters(self, name):
184-
"""Override this to return a dictionary of overwritable blob-property to value.
193+
def get_object_parameters(self, content):
194+
"""Add object-specific parameters to the uploaded blob.
195+
196+
Override this to return a dictionary of blob-specific parameters for the upload.
197+
198+
By default, this uses any object_parameters configured on the store, except:
199+
200+
- A `custom_time` attribute on the `content` object will override a `custom_time` property in the store settings
201+
- Any `metadata` on the `content` object will be merged with any `metadata` in the store settings, with fields
202+
from the `content` object taking precedence over the store metadata where duplicate fields exist.
185203
186-
Returns GS_OBJECT_PARAMETERS by default. See the docs for all possible options.
187204
"""
188-
return self.settings.object_parameters.copy()
205+
object_parameters = self.settings.object_parameters.copy()
206+
store_metadata = getattr(object_parameters, "metadata", dict())
207+
store_custom_time = getattr(object_parameters, "custom_time", None)
208+
object_metadata = getattr(content, "metadata", dict())
209+
object_parameters["custom_time"] = getattr(content, "custom_time", store_custom_time)
210+
object_parameters["metadata"] = {**store_metadata, **object_metadata}
211+
212+
return object_parameters
189213

190214
def delete(self, name):
191215
name = self._normalize_name(clean_name(name))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-gcp"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
description = "Utilities to run Django on Google Cloud Platform"
55
authors = ["Tom Clark"]
66
license = "MIT"

0 commit comments

Comments
 (0)