Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
pix666 committed Nov 7, 2023
2 parents c70d639 + 9da83f7 commit a4c8674
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 19 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Change Log

## [0.17.1](https://github.com/dldevinc/paper-streamfield/tree/v0.17.1) - 2023-11-07

### Features

- Added the `srcset` field for image resources.
- Removed the automatic selection of the "[All]" option in the dialogs for
the `recreate_variations` and `remove_variations` management commands.

### Bug Fixes

- Fixed a bug where the number of selected items in a collection was not properly
included in the deletion confirmation message.

## [0.17.0](https://github.com/dldevinc/paper-streamfield/tree/v0.17.0) - 2023-10-08

### ⚠ BREAKING CHANGES
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Page(models.Model):
В следующих таблицах перечислены общие поля и свойства обеих моделей:

| Поле | Описание |
| ------------- | -------------------------------------------------------------------------------------- |
|---------------|----------------------------------------------------------------------------------------|
| resource_name | Имя файла без пути, суффикса и расширения.<br>Пример: `report2020`. |
| extension | Расширение файла в нижнем регистре без точки.<br>Пример: `pdf`. |
| size | Размер файла в байтах. |
Expand All @@ -153,30 +153,31 @@ class Page(models.Model):
| created_at | Дата и время создания экземпляра модели. |
| modified_at | Дата и время изменения экземпляра модели. |

| Свойство | Описание |
| -------- | ------------------------------------------------------------------------------------------ |
| name | Полное имя файла.<br>Пример: `files/report2020_19sc2Kj.pdf`. |
| url | URL-адрес файла.<br>Пример: `/media/files/report2020_19sc2Kj.pdf`. |
| path | Абсолютный путь к файлу.<br>Пример: `/home/www/django/media/files/report2020_19sc2Kj.pdf`. |
| Свойство | Описание |
|----------|---------------------------------------------------------------------------------------------|
| name | Полное имя файла.<br>Пример: `files/report2020_19sc2Kj.pdf`. |
| url | URL-адрес файла.<br>Пример: `/media/files/report2020_19sc2Kj.pdf`. |
| path | Абсолютный путь к файлу.<br>Пример: `/home/www/django/media/files/report2020_19sc2Kj.pdf`. |

Ниже перечислены поля и свойства, специфичные для каждой модели.

Специфичные поля `UploadedFile`:

| Поле | Описание |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
|--------------|-------------------------------------------------------------------------------------------------------------------------------------------|
| display_name | Удобочитаемое название файла для вывода на сайте.<br>Заполняется в диалоговом окне редактирования файла.<br>Пример: `Annual report 2020`. |

Специфичные поля `UploadedImage`:

| Поле | Описание |
| ----------- | ---------------------------------------------------------------------------- |
|-------------|------------------------------------------------------------------------------|
| title | Название изображения, которое можно вставить в атрибут `title` тэга `<img>`. |
| description | Описание изображения, которое можно вставить в атрибут `alt` тэга `<img>`. |
| width | Ширина загруженного изображения. |
| height | Высота загруженного изображения. |
| ratio | Отношение ширины изображения к высоте в формате `Decimal`. |
| hw_ratio | Отношение высоте изображения к ширине в формате `Decimal`. |
| srcset | Строка формата `[URL] [WIDTH]w`. |

Большинство полей заполняются автоматически при загрузке файла и предназначены
только для чтения. Но такие поля, как `display_name` или `title`, заполняются
Expand Down Expand Up @@ -521,7 +522,7 @@ PAPER_UPLOADS = {
но связанная с ним модель `UploadedSVGFile` включает несколько дополнительных полей:

| Поле | Описание |
| ----------- | ---------------------------------------------------------------------------- |
|-------------|------------------------------------------------------------------------------|
| width | Ширина изображения в формате `Decimal`. Может быть вещественным числом. |
| height | Высота изображения в формате `Decimal`. Может быть вещественным числом. |
| title | Название изображения, которое можно вставить в атрибут `title` тэга `<img>`. |
Expand Down
2 changes: 1 addition & 1 deletion paper_uploads/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.17.0"
__version__ = "0.17.1"
default_app_config = "paper_uploads.apps.Config"
7 changes: 7 additions & 0 deletions paper_uploads/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def hw_ratio(self) -> Decimal:
round(operator.truediv(*reversed(self._get_image_dimensions())), 8)
))

@property
def srcset(self) -> str:
return "{} {}w".format(
self.url,
self.width
)

@cached_method("_dimensions_cache")
def _get_image_dimensions(self):
return self.variation.get_output_size(
Expand Down
6 changes: 2 additions & 4 deletions paper_uploads/management/commands/recreate_variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ def get_collection_variations(self):
self._field_name,
multiple=True,
prepend_choices=["[All]"],
append_choices=["[Back]", "[Exit]"],
default="[All]"
append_choices=["[Back]", "[Exit]"]
)

if "[Exit]" in variations:
Expand All @@ -200,8 +199,7 @@ def get_resource_variations(self):
self._field_name,
multiple=True,
prepend_choices=["[All]"],
append_choices=["[Back]", "[Exit]"],
default="[All]"
append_choices=["[Back]", "[Exit]"]
)

if "[Exit]" in variations:
Expand Down
6 changes: 2 additions & 4 deletions paper_uploads/management/commands/remove_variations.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ def get_collection_variations(self):
self._field_name,
multiple=True,
prepend_choices=["[All]"],
append_choices=["[Back]", "[Exit]"],
default="[All]"
append_choices=["[Back]", "[Exit]"]
)

if "[Exit]" in variations:
Expand All @@ -195,8 +194,7 @@ def get_resource_variations(self):
self._field_name,
multiple=True,
prepend_choices=["[All]"],
append_choices=["[Back]", "[Exit]"],
default="[All]"
append_choices=["[Back]", "[Exit]"]
)

if "[Exit]" in variations:
Expand Down
14 changes: 14 additions & 0 deletions paper_uploads/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,13 @@ def hw_ratio(self) -> Decimal:
"""
return Decimal(str(round(self.height / self.width, 8)))

@property
def srcset(self) -> str:
return "{} {}w".format(
self.url,
round(self.width)
)

def as_dict(self) -> Dict[str, Any]:
return {
**super().as_dict(), # noqa
Expand Down Expand Up @@ -788,6 +795,13 @@ def hw_ratio(self) -> Decimal:
"""
return Decimal(str(round(self.height / self.width, 8)))

@property
def srcset(self) -> str:
return "{} {}w".format(
self.url,
self.width
)

def as_dict(self) -> Dict[str, Any]:
return {
**super().as_dict(), # noqa
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,8 @@ class Collection extends EventEmitter {
),
{
count: selectedItems.length
}
},
true
),
buttons: [
{
Expand Down
12 changes: 12 additions & 0 deletions tests/tests/models/test_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,12 @@ def test_ratio(self, storage):
def test_hw_ratio(self, storage):
assert storage.resource.hw_ratio == Decimal("0.72122251")

def test_srcset(self, storage):
assert utils.match_path(
storage.resource.srcset,
"/media/{} 3501w".format(self.resource_name),
)

def test_prepare_file(self, storage):
obj = DummyImageFieldResource()
with open(CALLIPHORA_FILEPATH, "rb") as fp:
Expand Down Expand Up @@ -1079,6 +1085,12 @@ def test_ratio(self, storage):
def test_hw_ratio(self, storage):
assert storage.resource.hw_ratio == Decimal("0.734375")

def test_srcset(self, storage):
assert utils.match_path(
storage.resource.srcset,
"/media/{} 1024w".format(self.resource_name),
)

def test_as_dict(self, storage):
utils.compare_dicts(
storage.resource.as_dict(),
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/models/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_ratio(self, storage):
def test_hw_ratio(self, storage):
assert storage.resource.hw_ratio == Decimal("1.49004975")

def test_srcset(self, storage):
assert utils.match_path(
storage.resource.srcset,
"/media/{} 804w".format(self.resource_name),
)

def test_calculate_max_size(self, storage):
assert storage.resource.calculate_max_size((3000, 2000)) == (1800, 1200)
assert storage.resource.calculate_max_size((2000, 3000)) == (800, 1200)
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/models/test_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def test_ratio(self, storage):
def test_hw_ratio(self, storage):
assert storage.resource.hw_ratio == Decimal("1.05439808")

def test_srcset(self, storage):
assert utils.match_path(
storage.resource.srcset,
"/media/{} 626w".format(self.resource_name),
)

def test_as_dict(self, storage):
utils.compare_dicts(
storage.resource.as_dict(),
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def test_ratio(self, storage):
def test_hw_ratio(self, storage):
assert storage.file.hw_ratio == Decimal("0.72125")

def test_srcset(self, storage):
assert utils.match_path(
storage.file.srcset,
"/media/{}/milky-way-nasa{{suffix}}.desktop.jpg 800w".format(self.resource_folder),
)

def test_read(self, storage):
assert storage.file.closed is True
with storage.file.open():
Expand Down

0 comments on commit a4c8674

Please sign in to comment.