Skip to content

Commit

Permalink
Fix ThumbnailerSerializer error
Browse files Browse the repository at this point in the history
  • Loading branch information
modbender committed Sep 12, 2020
1 parent c84f945 commit 5903fe9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 114 deletions.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Easy Thumbnails Rest

[![Downloads](https://pepy.tech/badge/easy-thumbnails-rest)](https://pepy.tech/project/easy-thumbnails-rest)
[![Downloads](https://pepy.tech/badge/easy-thumbnails-rest/month)](https://pepy.tech/project/easy-thumbnails-rest/month)
[![Downloads](https://pepy.tech/badge/easy-thumbnails-rest/week)](https://pepy.tech/project/easy-thumbnails-rest/week)

Easy Thumbnails Fields for Django Rest API Framework

## Installation

`pip install easy-thumbnails-rest`

## Usage
Remember that this app needs `THUMBNAIL_ALIASES` to be defined in `settings.py`

If not yet added, please check [Easy Thumbnails Docs](https://easy-thumbnails.readthedocs.io/en/latest/usage/#thumbnail-aliases) to add it.

Example `settings.THUMBNAIL_ALIASES`

```python
THUMBNAIL_ALIASES = {
'': {
'avatar': {'size': (50, 50), 'crop': True},
},
}
```


## Fields

- ThumbnailerSerializer
- ThumbnailerListSerializer
- ThumbnailerJSONSerializer

### ThumbnailerSerializer

You can use `ThumbnailerSerializer` to get image's predefined alias. You need to pass argument `alias` with value as one of the aliases name defined in `THUMBNAIL_ALIASES`

Example:

```python
from rest_framework import serializers
from easy_thumbnails_rest.serializers import ThumbnailerSerializer

class ExampleSerializer(serializers.ModelSerializer):
image = ThumbnailerSerializer(alias='avatar')

class Meta:
model = ExampleModel
fields = '__all__'
```

From the above example the field `image` will contain string value of alias image url.

### ThumbnailerListSerializer

You can use `ThumbnailerListSerializer` to get image's predefined alias image list. You need to pass argument `alias` with value as one of the target's in `THUMBNAIL_ALIASES`.

If you don't understand where to find target, please see the structure of the `THUMBNAIL_ALIASES` in [Easy Thumbnails Docs](https://easy-thumbnails.readthedocs.io/en/latest/usage/#thumbnail-aliases)

Example:

```python
from rest_framework import serializers
from easy_thumbnails_rest.serializers import ThumbnailerListSerializer

class ExampleSerializer(serializers.ModelSerializer):
image = ThumbnailerListSerializer(alias='target')

class Meta:
model = ExampleModel
fields = '__all__'
```

From the above example the field `image` will contain list of all aliased image urls under the given target.

### ThumbnailerJSONSerializer

You can use `ThumbnailerJSONSerializer` to get image's predefined alias image list. You need to pass argument `alias` with value as one of the target's in `THUMBNAIL_ALIASES`.

If you don't understand where to find target, please see the structure of the `THUMBNAIL_ALIASES` in [Easy Thumbnails Docs](https://easy-thumbnails.readthedocs.io/en/latest/usage/#thumbnail-aliases)

Example:

```python
from rest_framework import serializers
from easy_thumbnails_rest.serializers import ThumbnailerJSONSerializer

class ExampleSerializer(serializers.ModelSerializer):
image = ThumbnailerJSONSerializer(alias='target')

class Meta:
model = ExampleModel
fields = '__all__'
```
From the above example the field `image` will contain list of key-value pair where key's are the alias under the given target and values are the respective image url.
109 changes: 0 additions & 109 deletions README.rst

This file was deleted.

2 changes: 1 addition & 1 deletion easy_thumbnails_rest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, **kwargs):

def to_representation(self, instance):
if self.alias or self.alias == '':
return get_url(request, instance, self.alias)
return get_url(self.context['request'], instance, self.alias)
return super().to_representation(instance)

# class ThumbnailerFilterSerializer(ImageField):
Expand Down
7 changes: 3 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import setuptools
from readme_renderer import rst

README = open('README.rst').read()
README = open('README.md').read()

setuptools.setup(
name='easy-thumbnails-rest',
version='1.0.6',
version='1.1',
url='https://github.com/modbender/easy-thumbnails-rest',
description='Easy Thumbnails Fields for Django Rest API',
long_description=README,
long_description_content_type='text/x-rst',
long_description_content_type='text/markdown',
author='Yashas H R',
author_email='[email protected]',
install_requires=[
Expand Down

0 comments on commit 5903fe9

Please sign in to comment.