<commit_before># -*- coding: utf-8 -*-
#
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © <NAME>. See NOTICE for more information.
#
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from catalogue.helpers import cached_in_field
from catalogue.tasks import refresh_instance
from dvcs import models as dvcs_models


class Image(dvcs_models.Document):
    """ An editable chunk of text. Every Book text is divided into chunks. """
    REPO_PATH = settings.CATALOGUE_IMAGE_REPO_PATH

    image = models.FileField(_('image'), upload_to='catalogue/images')
    title = models.CharField(_('title'), max_length=255, blank=True)
    slug = models.SlugField(_('slug'), unique=True)
    public = models.BooleanField(_('public'), default=True, db_index=True)

    # cache
    _short_html = models.TextField(null=True, blank=True, editable=False)
    _new_publishable = models.NullBooleanField(editable=False)
    _published = models.NullBooleanField(editable=False)
    _changed = models.NullBooleanField(editable=False)

    class Meta:
        app_label = 'catalogue'
        ordering = ['title']
        verbose_name = _('image')
        verbose_name_plural = _('images')
        permissions = [('can_pubmark_image', 'Can mark images for publishing')]

    # Representing
    # ============

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ("catalogue_image", [self.slug])

    def correct_about(self):
        return "http://%s%s" % (
            Site.objects.get_current().domain,
            self.get_absolute_url()
        )

    # State & cache
    # =============

    def last_published(self):
        try:
            return self.publish_log.all()[0].timestamp
        except IndexError:
            return None

    def assert_publishable(self):
        from librarian.picture import WLPicture
        from librarian import NoDublinCore, ParseError, ValidationError

        class SelfImageStore(object):
            def path(self_, slug, mime_type):
                """Returns own file object. Ignores slug ad mime_type."""
                return open(self.image.path)

        picture_xml = self.publishable().materialize()

        try:
            picture = WLPicture.from_string(picture_xml.encode('utf-8'),
                    image_store=SelfImageStore)
        except ParseError, e:
            raise AssertionError(_('Invalid XML') + ': ' + str(e))
        except NoDublinCore:
            raise AssertionError(_('No Dublin Core found.'))
        except ValidationError, e:
            raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))

        valid_about = self.correct_about()
        assert (picture.picture_info.about == valid_about,
                _("rdf:about is not") + " " + valid_about)

    def accessible(self, request):
        return self.public or request.user.is_authenticated()

    def is_new_publishable(self):
        change = self.publishable()
        if not change:
            return False
        return not change.publish_log.exists()
    new_publishable = cached_in_field('_new_publishable')(is_new_publishable)

    def is_published(self):<commit_msg>bugfix<commit_after># -*- coding: utf-8 -*-
#
# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © <NAME>. See NOTICE for more information.
#
from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
from catalogue.helpers import cached_in_field
from catalogue.tasks import refresh_instance
from dvcs import models as dvcs_models


class Image(dvcs_models.Document):
    """ An editable chunk of text. Every Book text is divided into chunks. """
    REPO_PATH = settings.CATALOGUE_IMAGE_REPO_PATH

    image = models.FileField(_('image'), upload_to='catalogue/images')
    title = models.CharField(_('title'), max_length=255, blank=True)
    slug = models.SlugField(_('slug'), unique=True)
    public = models.BooleanField(_('public'), default=True, db_index=True)

    # cache
    _short_html = models.TextField(null=True, blank=True, editable=False)
    _new_publishable = models.NullBooleanField(editable=False)
    _published = models.NullBooleanField(editable=False)
    _changed = models.NullBooleanField(editable=False)

    class Meta:
        app_label = 'catalogue'
        ordering = ['title']
        verbose_name = _('image')
        verbose_name_plural = _('images')
        permissions = [('can_pubmark_image', 'Can mark images for publishing')]

    # Representing
    # ============

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ("catalogue_image", [self.slug])

    def correct_about(self):
        return "http://%s%s" % (
            Site.objects.get_current().domain,
            self.get_absolute_url()
        )

    # State & cache
    # =============

    def last_published(self):
        try:
            return self.publish_log.all()[0].timestamp
        except IndexError:
            return None

    def assert_publishable(self):
        from librarian.picture import WLPicture
        from librarian import NoDublinCore, ParseError, ValidationError

        class SelfImageStore(object):
            def path(self_, slug, mime_type):
                """Returns own file object. Ignores slug ad mime_type."""
                return open(self.image.path)

        publishable = self.publishable()
        assert publishable, _("There is no publishable revision")
        picture_xml = publishable.materialize()

        try:
            picture = WLPicture.from_string(picture_xml.encode('utf-8'),
                    image_store=SelfImageStore)
        except ParseError, e:
            raise AssertionError(_('Invalid XML') + ': ' + str(e))
        except NoDublinCore:
            raise AssertionError(_('No Dublin Core found.'))
        except ValidationError, e:
            raise AssertionError(_('Invalid Dublin Core') + ': ' + str(e))

        valid_about = self.correct_about()
        assert (picture.picture_info.about == valid_about,
                _("rdf:about is not") + " " + valid_about)

    def accessible(self, request):
        return self.public or request.user.is_authenticated()

    def is_new_publishable(self):
        change = self.publishable()
        if not change:
            return False
        return not change.publish_log.exists()
    new_publishable = cached_in_field('_new_publishable')(is_new_publishable)

    def is_published(self):
{'fraction_non_alphanumeric': 0.10747051114023591, 'fraction_numerical': 0.0020387359836901123, 'mean_word_length': 2.931310818546079, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '7033453', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1914, 'n_words': 474}