<commit_before>from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import get_language

from cms.models import Page
from cms.models.pluginmodel import CMSPlugin
from djangocms_text_ckeditor.fields import HTMLField
from filer.fields.image import FilerImageField

#from htmlcutstring import cutHtmlString
import re


class CreatePageMixin(object):
    
    def ensure_is_in_page(self, user, parent_page_id):
        try:
            self.placeholder.page.pk
        except AttributeError:
            page = self.create_page(user, parent_page_id)
            self.assign_to_page(page)
            
    
    def create_page(self, user, parent_page_id):
        from cms import api # Import here because not available before model creation
        try:
            parent = Page.objects.get(
                reverse_id=parent_page_id,
                publisher_is_draft=True
            )
        except Page.DoesNotExist:
            parent = self.get_parent_page()
        page = api.create_page(
            self.name,
            self.PAGE_TEMPLATE,
            get_language(),
            created_by=user,
            parent=parent,
            position='first-child'
        )
        return page
    
    def assign_to_page(self, page):
        placeholder = page.placeholders.get(slot=self.PLACEHOLDER)
        self.placeholder = placeholder
        self.language = get_language()
        self.position = 0
        self.plugin_type = self.PLUGIN_TYPE


class BlogPostManager(models.Manager):
    def pub_for_language(self, current_language):
        published = Q(placeholder__page__publisher_is_draft=False)
        language_fits = Q(language=current_language)
        exists_in_current_language = Q(placeholder__page__languages__contains=current_language)
        return BlogPost.objects.filter(
            published
            & (language_fits | ~exists_in_current_language)
        )


class BlogPost(CreatePageMixin, CMSPlugin):
    name = models.CharField(max_length=100)
    body = HTMLField(_('Text'))
    image = FilerImageField(verbose_name=_('Bild'))
    pad = models.BooleanField(
        "Pad instead crop image in teaser",
        default=False
    )
    
    objects = BlogPostManager()

    PAGE_TEMPLATE = 'blog-post.html'
    PLUGIN_TYPE = 'BlogPostPlugin'
    PLACEHOLDER = 'blogpost'
    
    def excerpt(self):
        text = re.sub(r'<[^>]*?>', '', self.body)
        return text[:225]<commit_msg>Add image padding option<commit_after>from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import get_language

from cms.models import Page
from cms.models.pluginmodel import CMSPlugin
from djangocms_text_ckeditor.fields import HTMLField
from filer.fields.image import FilerImageField

#from htmlcutstring import cutHtmlString
import re


class CreatePageMixin(object):
    
    def ensure_is_in_page(self, user, parent_page_id):
        try:
            self.placeholder.page.pk
        except AttributeError:
            page = self.create_page(user, parent_page_id)
            self.assign_to_page(page)
            
    
    def create_page(self, user, parent_page_id):
        from cms import api # Import here because not available before model creation
        try:
            parent = Page.objects.get(
                reverse_id=parent_page_id,
                publisher_is_draft=True
            )
        except Page.DoesNotExist:
            parent = self.get_parent_page()
        page = api.create_page(
            self.name,
            self.PAGE_TEMPLATE,
            get_language(),
            created_by=user,
            parent=parent,
            position='first-child'
        )
        return page
    
    def assign_to_page(self, page):
        placeholder = page.placeholders.get(slot=self.PLACEHOLDER)
        self.placeholder = placeholder
        self.language = get_language()
        self.position = 0
        self.plugin_type = self.PLUGIN_TYPE


class BlogPostManager(models.Manager):
    def pub_for_language(self, current_language):
        published = Q(placeholder__page__publisher_is_draft=False)
        language_fits = Q(language=current_language)
        exists_in_current_language = Q(placeholder__page__languages__contains=current_language)
        return BlogPost.objects.filter(
            published
            & (language_fits | ~exists_in_current_language)
        )


class BlogPost(CreatePageMixin, CMSPlugin):
    name = models.CharField(max_length=100)
    body = HTMLField(_('Text'))
    image = FilerImageField(verbose_name=_('Bild'))
    pad = models.BooleanField(
        _("Pad instead crop the image in the teaser."),
        default=False
    )
    
    objects = BlogPostManager()

    PAGE_TEMPLATE = 'blog-post.html'
    PLUGIN_TYPE = 'BlogPostPlugin'
    PLACEHOLDER = 'blogpost'
    
    def excerpt(self):
        text = re.sub(r'<[^>]*?>', '', self.body)
        return text[:225]
{'fraction_non_alphanumeric': 0.07962853191068958, 'fraction_numerical': 0.0027662517289073307, 'mean_word_length': 2.5926188786373316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3716704', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1446, 'n_words': 304}