Django field for create snippet image for sharing in social networks.
Project description
===========================
django-snippet-image
===========================
The python package provides a django field for automatic
generation of images for sharing in social networks.
django-snippet-image based on snippet-image_ package.
.. _snippet-image: https://github.com/acrius/snippet-image
Installation
-------------------------
`
pip3 install django-snippet-image
`
User guide
-------------------------
Import package field:
.. code-block:: python
from django_snippet_image import SnippetImageField
Use it in model:
.. code-block:: python
class ExampleModel(Model):
text = CharField(
max_length=200,
verbose_name='Text for snippet image',
)
background = ImageField(
verbose_name='Background for snippet image',
blank=True,
null=True,
)
snippet_image_field = SnippetImageField(
verbose_name='Example snippet image field',
null=True,
)
Set static params:
.. code-block:: python
snippet_image_field = SnippetImageField(
overlay='/user/python/app/assets/overlay.png',
background_color=(0, 75, 125),
font='/user/python/app/assets/OpenSans-Bold.ttf',
verbose_name='Example snippet image field',
null=True,
)
Set dynamic params. They are set using model methods:
.. code-block:: python
def get_snippet_image_text(self, snippet_type):
return self.text if snippet_type == 'default' and self.text else ''
def get_snippet_image_background(self, snippet_type):
if snippet_type == 'default' and self.background:
return self.background.path
Full example:
.. code-block:: python
from django.db.models import (
Model,
CharField,
ImageField,
)
from django_snippet_image import SnippetImageField
class Statuses:
DRAFT = 'draft'
PUBLISH = 'publish'
CHOICES = (
(DRAFT, 'Draft'),
(PUBLISH, 'Publish'),
)
class ExampleModel(Model):
text = CharField(
max_length=200,
verbose_name='Text for snippet image',
)
background = ImageField(
verbose_name='Background for snippet image',
blank=True,
null=True,
)
snippet_image_field = SnippetImageField(
verbose_name='Example snippet image field',
null=True,
)
status = CharField(
max_length=20,
choices=Statuses.CHOICES,
)
# Methods for collect data for snippet image.
def get_snippet_image_text(self, snippet_type):
return self.text if snippet_type == 'default' and self.text else ''
def get_snippet_image_background(self, snippet_type):
if snippet_type == 'default' and self.background:
return self.background.path
def snippet_image_should_be_created(self):
return self.status == Statuses.PUBLISH
class Meta:
verbose_name = 'example object'
verbose_name_plural = 'example objects'
And use in template:
.. code-block:: html
<meta property="og:image" content="{{ instance.snippet_image_field.url }}" />
Read more on home page_.
.. _page: https://github.com/acrius/django-snippet-image
django-snippet-image
===========================
The python package provides a django field for automatic
generation of images for sharing in social networks.
django-snippet-image based on snippet-image_ package.
.. _snippet-image: https://github.com/acrius/snippet-image
Installation
-------------------------
`
pip3 install django-snippet-image
`
User guide
-------------------------
Import package field:
.. code-block:: python
from django_snippet_image import SnippetImageField
Use it in model:
.. code-block:: python
class ExampleModel(Model):
text = CharField(
max_length=200,
verbose_name='Text for snippet image',
)
background = ImageField(
verbose_name='Background for snippet image',
blank=True,
null=True,
)
snippet_image_field = SnippetImageField(
verbose_name='Example snippet image field',
null=True,
)
Set static params:
.. code-block:: python
snippet_image_field = SnippetImageField(
overlay='/user/python/app/assets/overlay.png',
background_color=(0, 75, 125),
font='/user/python/app/assets/OpenSans-Bold.ttf',
verbose_name='Example snippet image field',
null=True,
)
Set dynamic params. They are set using model methods:
.. code-block:: python
def get_snippet_image_text(self, snippet_type):
return self.text if snippet_type == 'default' and self.text else ''
def get_snippet_image_background(self, snippet_type):
if snippet_type == 'default' and self.background:
return self.background.path
Full example:
.. code-block:: python
from django.db.models import (
Model,
CharField,
ImageField,
)
from django_snippet_image import SnippetImageField
class Statuses:
DRAFT = 'draft'
PUBLISH = 'publish'
CHOICES = (
(DRAFT, 'Draft'),
(PUBLISH, 'Publish'),
)
class ExampleModel(Model):
text = CharField(
max_length=200,
verbose_name='Text for snippet image',
)
background = ImageField(
verbose_name='Background for snippet image',
blank=True,
null=True,
)
snippet_image_field = SnippetImageField(
verbose_name='Example snippet image field',
null=True,
)
status = CharField(
max_length=20,
choices=Statuses.CHOICES,
)
# Methods for collect data for snippet image.
def get_snippet_image_text(self, snippet_type):
return self.text if snippet_type == 'default' and self.text else ''
def get_snippet_image_background(self, snippet_type):
if snippet_type == 'default' and self.background:
return self.background.path
def snippet_image_should_be_created(self):
return self.status == Statuses.PUBLISH
class Meta:
verbose_name = 'example object'
verbose_name_plural = 'example objects'
And use in template:
.. code-block:: html
<meta property="og:image" content="{{ instance.snippet_image_field.url }}" />
Read more on home page_.
.. _page: https://github.com/acrius/django-snippet-image
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-snippet-image-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b9c64f940e432ec2c3e8331343e304764228cd05f48c30d64c592b0ee1bdf10 |
|
MD5 | a56b851ecf768dfc3f0434554ee008e1 |
|
BLAKE2b-256 | e6a5a98ccbe2209673e9fcabaddb3f17c66d6eb7f985eb9e077effe048962642 |
Close
Hashes for django_snippet_image-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88124d177a22be466c85c935f2d78e03373b71dadde97f36a58cdf7c22c05c49 |
|
MD5 | 6a189c6201a9717b3b9c4336e84fdfad |
|
BLAKE2b-256 | 735fd8b1f525828b39c83e00584c12505ded2914295742779c9eabf15619e011 |