Skip to main content

Creating a snippet images on Wagtail for sharing for social media.

Project description

Package for creating a snippet images for sharing in social networks and etc. Based on django-snippet-image and snippet-image. But for storage of images used Wagtail Images.

Installation

pip3 install wagtail-snippet-image

How use it

from django.db.models import (
    CharField,
    ForeignKey,
    SET_NULL,
    CASCADE,
)
from wagtail_snippet_image import SnippetImageField
from wagtail.core.models import Page
from wagtail.admin.edit_handlers import FieldPanel
from wagtail.images.edit_handlers import ImageChooserPanel
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase


class PageTag(TaggedItemBase):
    content_object = ParentalKey(
        'home.HomePage',
        on_delete=CASCADE,
        related_name='tagged_items',
    )


class Statuses:
    DRAFT = 'draft'
    PUBLISH = 'publish'

    CHOICES = (
        (DRAFT, 'Draft'),
        (PUBLISH, 'Publish'),
    )


class HomePage(Page):
    background = ForeignKey(
        'wagtailimages.Image',
        verbose_name='Изображение для обложки',
        related_name='cover_images',
        on_delete=SET_NULL,
        blank=True,
        null=True,
    )

    snippet_image_field = SnippetImageField(
        verbose_name='Example snippet image field',
        null=True,
    )

    status = CharField(
        max_length=20,
        choices=Statuses.CHOICES,
        default=Statuses.DRAFT,
    )

    tags = ClusterTaggableManager(through=PageTag, blank=True)

    content_panels = Page.content_panels + [
        ImageChooserPanel('background'),
        ImageChooserPanel('snippet_image_field'),
        FieldPanel('status'),
        FieldPanel('tags'),
    ]

    def get_snippet_image_background(self, snippet_type):
        return self.background and self.background.file and self.background.file.path \
            if snippet_type == 'default' else None

    def get_snippet_image_center(self, snippet_type):
        return (self.background.focal_point_x, self.background.focal_point_y) \
            if snippet_type == 'default' and self.background \
               and self.background.focal_point_x and self.background.focal_point_y \
            else None

    def get_snippet_image_text(self, snippet_type):
        return self.title if snippet_type == 'default' else None

    def snippet_image_should_be_created(self):
        return self.status == Statuses.PUBLISH

    # Wagtail custom methods for create wagtail images for sharing snippet image
    def get_snippet_image_title(self, snippet_type):
        return self.title if snippet_type == 'default' else None

    def get_snippet_image_tags(self, snippet_type):
        return self.tags.names() if snippet_type == 'default' else None

And use it in template:

<meta property="og:image" content="{{ image(page.snippet_image_field, 'original') }}" />

Read more in home.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for wagtail-snippet-image, version 0.1.4
Filename, size File type Python version Upload date Hashes
Filename, size wagtail_snippet_image-0.1.4-py3-none-any.whl (4.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size wagtail-snippet-image-0.1.4.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page