<commit_before>from zeit.cms.i18n import MessageFactory as _
from zeit.content.image.browser.interfaces import IMasterImageUploadSchema
from zeit.content.image.interfaces import INFOGRAPHIC_DISPLAY_TYPE
from zope.formlib.widget import CustomWidgetFactory
import gocept.form.grouped
import itertools
import re
import zc.table.column
import zeit.cms.browser.form
import zeit.cms.browser.listing
import zeit.cms.browser.view
import zeit.content.image.browser.form
import zeit.content.image.image
import zeit.content.image.imagegroup
import zeit.content.image.interfaces
import zeit.ghost.ghost
import zope.app.appsetup.appsetup
import zope.formlib.form
import zope.publisher.interfaces


class FormBase(object):

    field_groups = zeit.content.image.browser.form.ImageFormBase.field_groups

    form_fields = zope.formlib.form.FormFields(
        zeit.content.image.interfaces.IImageGroup,
        zeit.content.image.interfaces.IImageMetadata,
        zeit.content.image.interfaces.IReferences).omit(
            'acquire_metadata', 'variants')


class AddForm(FormBase,
              zeit.cms.repository.browser.file.FormBase,
              zeit.cms.browser.form.AddForm):

    title = _('Add image group')
    factory = zeit.content.image.imagegroup.ImageGroup
    checkout = False
    form_fields = (
        FormBase.form_fields.omit(
            'references', 'master_images', 'external_id') +
        zope.formlib.form.FormFields(IMasterImageUploadSchema))

    form_fields['master_image_blobs'].custom_widget = (
        CustomWidgetFactory(
            zope.formlib.sequencewidget.SequenceWidget,
            zeit.cms.repository.browser.file.BlobWidget))

    def create(self, data):
        # Must remove master_image_blobs from data before creating the images,
        # since `zeit.cms.browser.form.apply_changes_with_setattr` breaks on
        # fields that are not actually part of the interface.
        blobs = data.pop('master_image_blobs')

        # Create ImageGroup with remaining data.
        group = super(AddForm, self).create(data)

        # Create images from blobs. Skip missing blobs, i.e. None.
        self.images = [self.create_image(blob, data) for blob in blobs if blob]

        # Prefill `master_images` with uploaded images and configure viewport.
        # Viewports should be prefilled sequentially, i.e. primary master image
        # is configured with first viewport of source, secondary master image
        # with second viewport etc.
        viewports = zeit.content.image.interfaces.VIEWPORT_SOURCE(group)
        for image, viewport in itertools.izip(self.images, viewports):
            group.master_images += ((viewport, image.__name__),)

        return group

    def add(self, group):
        super(AddForm, self).add(group)
        group = self._created_object  # We need IRepositoryContent.

        # Add images to ImageGroup container.
        for image in self.images:
            if image is not None:
                super(AddForm, self).add(image, group)

        self._created_object = group  # Additional add() calls overwrote this.
        zeit.ghost.ghost.create_ghost(group)

    def create_image(self, blob, data):
        image = zeit.content.image.image.LocalImage()
        self.update_file(image, blob)
        name = zeit.cms.interfaces.normalize_filename(
            getattr(blob, 'filename', ''))
        zeit.cms.browser.form.apply_changes_with_setattr(
            image,
            self.form_fields.omit('__name__', 'display_type'), data)
        image.__name__ = name
        return image

    @property
    def next_view(self):
        if self._created_object.display_type == INFOGRAPHIC_DISPLAY_TYPE:
            return 'view.html'
        return 'variant.html'

    def setUpWidgets(self, *args, **kw):
        super(AddForm, self).setUpWidgets(*args, **kw)
        self.widgets['master_image_blobs'].addButtonLabel = _('Add motif')
        self.widgets['master_image_blobs'].subwidget.extra = 'accept="%s"' % (
            ','.join(zeit.content.image.interfaces.AVAILABLE_MIME_TYPES))


class EditForm(FormBase, zeit.cms.browser.form.EditForm):

    title = _('Edit image group')
    form_fields = FormBase.form_fields.omit('__name__')

    def setUpWidgets(self, *args, **kw):
        super(EditForm, self).setUpWidgets(*args, **kw)
        self.widgets['master_images'].addButtonLabel = _(
            'Add viewport to master image mapping')


class DisplayForm(FormBase, zeit.cms.browser.form.DisplayForm):

    title = _('Image group metadata')


class ImageColumn(zc.table.column.GetterColumn):

    def getter(self, item, formatter):
        return item.context

    def cell_formatter(self, value, item, formatter):
        img = zope.component.getMultiAdapter(
            (value, formatter.request),
            name='preview').tag()
        master = ''<commit_msg>ZON-4106: Add css class to determine ressource state<commit_after>from zeit.cms.i18n import MessageFactory as _
from zeit.content.image.browser.interfaces import IMasterImageUploadSchema
from zeit.content.image.interfaces import INFOGRAPHIC_DISPLAY_TYPE
from zope.formlib.widget import CustomWidgetFactory
import gocept.form.grouped
import itertools
import re
import zc.table.column
import zeit.cms.browser.form
import zeit.cms.browser.listing
import zeit.cms.browser.view
import zeit.content.image.browser.form
import zeit.content.image.image
import zeit.content.image.imagegroup
import zeit.content.image.interfaces
import zeit.ghost.ghost
import zope.app.appsetup.appsetup
import zope.formlib.form
import zope.publisher.interfaces


class FormBase(object):

    field_groups = zeit.content.image.browser.form.ImageFormBase.field_groups

    form_fields = zope.formlib.form.FormFields(
        zeit.content.image.interfaces.IImageGroup,
        zeit.content.image.interfaces.IImageMetadata,
        zeit.content.image.interfaces.IReferences).omit(
            'acquire_metadata', 'variants')


class AddForm(FormBase,
              zeit.cms.repository.browser.file.FormBase,
              zeit.cms.browser.form.AddForm):

    title = _('Add image group')
    factory = zeit.content.image.imagegroup.ImageGroup
    checkout = False
    form_fields = (
        FormBase.form_fields.omit(
            'references', 'master_images', 'external_id') +
        zope.formlib.form.FormFields(IMasterImageUploadSchema))

    form_fields['master_image_blobs'].custom_widget = (
        CustomWidgetFactory(
            zope.formlib.sequencewidget.SequenceWidget,
            zeit.cms.repository.browser.file.BlobWidget))

    def create(self, data):
        # Must remove master_image_blobs from data before creating the images,
        # since `zeit.cms.browser.form.apply_changes_with_setattr` breaks on
        # fields that are not actually part of the interface.
        blobs = data.pop('master_image_blobs')

        # Create ImageGroup with remaining data.
        group = super(AddForm, self).create(data)

        # Create images from blobs. Skip missing blobs, i.e. None.
        self.images = [self.create_image(blob, data) for blob in blobs if blob]

        # Prefill `master_images` with uploaded images and configure viewport.
        # Viewports should be prefilled sequentially, i.e. primary master image
        # is configured with first viewport of source, secondary master image
        # with second viewport etc.
        viewports = zeit.content.image.interfaces.VIEWPORT_SOURCE(group)
        for image, viewport in itertools.izip(self.images, viewports):
            group.master_images += ((viewport, image.__name__),)

        return group

    def add(self, group):
        super(AddForm, self).add(group)
        group = self._created_object  # We need IRepositoryContent.

        # Add images to ImageGroup container.
        for image in self.images:
            if image is not None:
                super(AddForm, self).add(image, group)

        self._created_object = group  # Additional add() calls overwrote this.
        zeit.ghost.ghost.create_ghost(group)

    def create_image(self, blob, data):
        image = zeit.content.image.image.LocalImage()
        self.update_file(image, blob)
        name = zeit.cms.interfaces.normalize_filename(
            getattr(blob, 'filename', ''))
        zeit.cms.browser.form.apply_changes_with_setattr(
            image,
            self.form_fields.omit('__name__', 'display_type'), data)
        image.__name__ = name
        return image

    @property
    def next_view(self):
        if self._created_object.display_type == INFOGRAPHIC_DISPLAY_TYPE:
            return 'view.html'
        return 'variant.html'

    def setUpWidgets(self, *args, **kw):
        super(AddForm, self).setUpWidgets(*args, **kw)
        self.widgets['master_image_blobs'].addButtonLabel = _('Add motif')
        self.widgets['master_image_blobs'].subwidget.extra = 'accept="%s"' % (
            ','.join(zeit.content.image.interfaces.AVAILABLE_MIME_TYPES))
        self.widgets['copyrights'].vivi_css_class = 'fieldname-copyrights--add'


class EditForm(FormBase, zeit.cms.browser.form.EditForm):

    title = _('Edit image group')
    form_fields = FormBase.form_fields.omit('__name__')

    def setUpWidgets(self, *args, **kw):
        super(EditForm, self).setUpWidgets(*args, **kw)
        self.widgets['master_images'].addButtonLabel = _(
            'Add viewport to master image mapping')
        self.widgets['copyrights'].vivi_css_class = (
            'fieldname-copyrights--edit')


class DisplayForm(FormBase, zeit.cms.browser.form.DisplayForm):

    title = _('Image group metadata')

    def setUpWidgets(self, *args, **kw):
        super(DisplayForm, self).setUpWidgets(*args, **kw)
        self.widgets['copyrights'].vivi_css_class = (
            'fieldname-copyrights--display')


class ImageColumn(zc.table.column.GetterColumn):

    def getter(self, item, formatter):
        return item.context

    def cell_formatter(self, value, item, formatter):
        img = zope.component.getMultiAdapter(
            (value, formatter.request),
            name='preview').tag()
        master = ''
{'fraction_non_alphanumeric': 0.10232098765432099, 'fraction_numerical': 0.0007901234567901234, 'mean_word_length': 3.0504, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4033014', 'n_tokens_mistral': 3076, 'n_tokens_neox': 2937, 'n_words': 630}