Create custom renditions from django url fields
Project description
# django-url-renditions
django-url-renditions allows to define rendition process to django url fields. This works pretty well with https://github.com/bradleyg/django-s3direct and Heroku.
It comes with a Graphene `Query` that enable renditions in your GraphQL schema.
## Install
```
pip install django-url-renditions
```
Add `url_renditions` to your `INSTALLED_APPS`
## Define your model
```python
from django.db import models
from url_renditions.fields import FileUrlWithRenditions
# django-url-renditions comes with a simple image resizer method.
# Look at the implementation if you need something more specific
from url_renditions.resize_image import ResizeImage
class Track(models.Model):
original_artwork = models.URLField()
artwork = FileUrlWithRenditions(
source='original_artwork',
use_job_runner=True, # if we want to queue the job with django_rq
renditions={
'small': ResizeImage('80x80'),
'medium': ResizeImage('300x300'),
}
)
```
That way, when a `Track` model get created with an `original_artwork`, `artwork` will be automatically polulated with two renditions: `small` and `medium`.
To access them, use:
```python
r = my_track.artwork.rendition_set.get(name='small')
print('url:', r.href, 'width:', r.width, 'height:', r.height)
```
## Graphql with Graphene
Add `url_renditions.graphql_schema.Query` to your root query.
```python
import graphene
import url_renditions.graphql_schema # noqa
class Query(
...
url_renditions.graphql_schema.Query,
graphene.ObjectType):
pass
schema = graphene.Schema(query=Query)
```
Then when you ask for
```graphql
{
track(id: "VHJhY2s6OA==") {
artwork {
renditions {
medium {
href
width
height
}
}
}
}
}
```
You get
```json
{
"data": {
"track": {
"artwork": {
"renditions": {
"medium": {
"href": "https://images.unsplash.com/photo-1474314170901-f351b68f544f",
"width": 300,
"height": 300
}
}
}
}
}
}
```
django-url-renditions allows to define rendition process to django url fields. This works pretty well with https://github.com/bradleyg/django-s3direct and Heroku.
It comes with a Graphene `Query` that enable renditions in your GraphQL schema.
## Install
```
pip install django-url-renditions
```
Add `url_renditions` to your `INSTALLED_APPS`
## Define your model
```python
from django.db import models
from url_renditions.fields import FileUrlWithRenditions
# django-url-renditions comes with a simple image resizer method.
# Look at the implementation if you need something more specific
from url_renditions.resize_image import ResizeImage
class Track(models.Model):
original_artwork = models.URLField()
artwork = FileUrlWithRenditions(
source='original_artwork',
use_job_runner=True, # if we want to queue the job with django_rq
renditions={
'small': ResizeImage('80x80'),
'medium': ResizeImage('300x300'),
}
)
```
That way, when a `Track` model get created with an `original_artwork`, `artwork` will be automatically polulated with two renditions: `small` and `medium`.
To access them, use:
```python
r = my_track.artwork.rendition_set.get(name='small')
print('url:', r.href, 'width:', r.width, 'height:', r.height)
```
## Graphql with Graphene
Add `url_renditions.graphql_schema.Query` to your root query.
```python
import graphene
import url_renditions.graphql_schema # noqa
class Query(
...
url_renditions.graphql_schema.Query,
graphene.ObjectType):
pass
schema = graphene.Schema(query=Query)
```
Then when you ask for
```graphql
{
track(id: "VHJhY2s6OA==") {
artwork {
renditions {
medium {
href
width
height
}
}
}
}
}
```
You get
```json
{
"data": {
"track": {
"artwork": {
"renditions": {
"medium": {
"href": "https://images.unsplash.com/photo-1474314170901-f351b68f544f",
"width": 300,
"height": 300
}
}
}
}
}
}
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Close
Hashes for django-url-renditions-0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | da66a6351100eb92a3b3d73acb6ad119160d357aa656c30541dae30d3463ea41 |
|
MD5 | 99e3da22535118ece887146b1630e147 |
|
BLAKE2b-256 | 77f08d8d75067ad7295f67547e123aaccf7d3208cfaa9891aa47c69e7fa7a6fd |