|
1 | 1 | """ |
2 | 2 | Declare and configure the models for the impress core application |
3 | 3 | """ |
| 4 | + |
4 | 5 | import hashlib |
5 | 6 | import os |
6 | 7 | import tempfile |
@@ -316,6 +317,29 @@ class Meta: |
316 | 317 | def __str__(self): |
317 | 318 | return self.title |
318 | 319 |
|
| 320 | + def save(self, *args, **kwargs): |
| 321 | + """Write content to object storage only if _content has changed.""" |
| 322 | + super().save(*args, **kwargs) |
| 323 | + |
| 324 | + if self._content: |
| 325 | + file_key = self.file_key |
| 326 | + bytes_content = self._content.encode("utf-8") |
| 327 | + |
| 328 | + if default_storage.exists(file_key): |
| 329 | + response = default_storage.connection.meta.client.head_object( |
| 330 | + Bucket=default_storage.bucket_name, Key=file_key |
| 331 | + ) |
| 332 | + has_changed = ( |
| 333 | + response["ETag"].strip('"') |
| 334 | + != hashlib.md5(bytes_content).hexdigest() # noqa |
| 335 | + ) |
| 336 | + else: |
| 337 | + has_changed = True |
| 338 | + |
| 339 | + if has_changed: |
| 340 | + content_file = ContentFile(bytes_content) |
| 341 | + default_storage.save(file_key, content_file) |
| 342 | + |
319 | 343 | @property |
320 | 344 | def key_base(self): |
321 | 345 | """Key base of the location where the document is stored in object storage.""" |
@@ -356,28 +380,6 @@ def get_content_response(self, version_id=""): |
356 | 380 | Bucket=default_storage.bucket_name, Key=self.file_key, VersionId=version_id |
357 | 381 | ) |
358 | 382 |
|
359 | | - def save(self, *args, **kwargs): |
360 | | - """Write content to object storage only if _content has changed.""" |
361 | | - super().save(*args, **kwargs) |
362 | | - |
363 | | - if self._content: |
364 | | - file_key = self.file_key |
365 | | - bytes_content = self._content.encode("utf-8") |
366 | | - |
367 | | - if default_storage.exists(file_key): |
368 | | - response = default_storage.connection.meta.client.head_object( |
369 | | - Bucket=default_storage.bucket_name, Key=file_key |
370 | | - ) |
371 | | - has_changed = ( |
372 | | - response["ETag"].strip('"') |
373 | | - != hashlib.md5(bytes_content).hexdigest() # noqa |
374 | | - ) |
375 | | - else: |
376 | | - has_changed = True |
377 | | - if has_changed: |
378 | | - content_file = ContentFile(bytes_content) |
379 | | - default_storage.save(file_key, content_file) |
380 | | - |
381 | 383 | def get_versions_slice( |
382 | 384 | self, from_version_id="", from_datetime=None, page_size=None |
383 | 385 | ): |
|
0 commit comments