Beispiel #1
0
 def remove(self):
     tag = self.request.form.get('tag')
     catalog = api.portal.get_tool('portal_catalog')
     for brain in catalog(Subject=tag)[:self.remove_size]:
         obj = brain.getObject()
         bdata = ICategorization(obj, None)
         if bdata:
             if tag in bdata.subjects:
                 tags = list(bdata.subjects)
                 tags.remove(tag)
                 bdata.subjects = tuple(tags)
                 obj.reindexObject(idxs=['Subject'])
     try:
         total = len(catalog._catalog.getIndex('Subject')._index[tag])
     except Exception:
         total = 0
     return {'success': True, 'tag': tag, 'total': total}
Beispiel #2
0
    def dx_action(self, obj):
        if self.effectiveDate and hasattr(obj, 'effective_date'):
            obj.effective_date = DateTime(self.effectiveDate)
        if self.expirationDate and hasattr(obj, 'expiration_date'):
            obj.expiration_date = DateTime(self.expirationDate)
        if self.copyright and hasattr(obj, 'rights'):
            obj.rights = self.copyright
        if self.contributors and hasattr(obj, 'contributors'):
            obj.contributors = tuple(self.contributors)
        if self.creators and hasattr(obj, 'creators'):
            obj.creators = tuple(self.creators)
        if self.exclude and hasattr(obj, 'exclude_from_nav'):
            obj.exclude_from_nav = self.exclude == 'yes'

        behavior_categorization = ICategorization(obj)
        if self.language and behavior_categorization:
            behavior_categorization.language = self.language
Beispiel #3
0
    def test_categorization_language(self):
        """The vocabulary of the language field in the ICategorization
        behavior should only allow to set, what was configured in
        ``plone.available_languages`` registry setting.
        """
        # set available languages
        registry = getUtility(IRegistry)
        registry['plone.available_languages'] = ['hu', 'sl']

        self.portal.invokeFactory('Folder', 'test')
        ob = self.portal.test
        cat = ICategorization(ob)

        cat.language = 'hu'
        self.assertEqual(ob.language, 'hu')

        cat.language = 'sl'
        self.assertEqual(ob.language, 'sl')

        with self.assertRaises(ConstraintNotSatisfied):
            cat.language = 'de'

        with self.assertRaises(ConstraintNotSatisfied):
            cat.language = 'en'
Beispiel #4
0
    def post_creation(self, obj):
        field_data = self.field_data
        bdata = ILayoutAware(obj, None)
        if bdata:
            bdata.contentLayout = '++contentlayout++default/document.html'
        bdata = IRichText(obj, None)
        if bdata:
            bdata.text = RichTextValue(field_data['text'], 'text/html',
                                       'text/html')

        bdata = IBasic(obj, None)
        if bdata:
            bdata.title = field_data['title']
            bdata.description = field_data['description']
        else:
            obj.title = field_data['title']
            obj.description = field_data['description']

        bdata = ICategorization(obj, None)
        if bdata:
            bdata.subjects = field_data['subject']

            bdata = IPublication(obj)
            if field_data['effectiveDate']:
                bdata.effective = field_data['effectiveDate'].asdatetime()

        ldata = ILocation(obj, None)
        if ldata:
            if field_data.get('location'):
                ldata.locations = [field_data['location']]

            if field_data.get('newsLocation'):
                if ldata.locations:
                    ldata.locations.append(field_data['newsLocation'])
                else:
                    ldata.locations = [field_data['newsLocation']]

        obj.modification_date = field_data['modification_date']
        obj.creation_date = field_data['creation_date']

        bdata = ILayoutAware(obj, None)
        if bdata:
            if self.data[
                    'portal_type'] == 'Folder' and 'text' in self.field_data:
                bdata.content = FOLDER_DEFAULT_PAGE_LAYOUT % self.field_data[
                    'text']
            elif self.layout:
                bdata.contentLayout = self.layout
    def test_categorization_language(self):
        """The vocabulary of the language field in the ICategorization
        behavior should only allow to set, what was configured in
        ``plone.available_languages`` registry setting.
        """
        # set available languages
        registry = getUtility(IRegistry)
        registry['plone.available_languages'] = ['hu', 'sl']

        self.portal.invokeFactory('Folder', 'test')
        ob = self.portal.test
        cat = ICategorization(ob)

        cat.language = 'hu'
        self.assertEqual(ob.language, 'hu')

        cat.language = 'sl'
        self.assertEqual(ob.language, 'sl')

        with self.assertRaises(ConstraintNotSatisfied):
            cat.language = 'de'

        with self.assertRaises(ConstraintNotSatisfied):
            cat.language = 'en'
# -*- coding: utf-8 -*-
from zope.i18nmessageid import MessageFactory
from plone.app.dexterity.behaviors.metadata import ICategorization
from plone.autoform.interfaces import WIDGETS_KEY
from plone.directives.form.schema import TEMP_KEY
from zope import schema as _schema


_ = MessageFactory('collective.z3cform.widgets')

widget = 'collective.z3cform.widgets.token_input_widget.TokenInputFieldWidget'
_directives_values = ICategorization.queryTaggedValue(TEMP_KEY)
if _directives_values:
    # groked form
    _directives_values.setdefault(WIDGETS_KEY, {})
    _directives_values[WIDGETS_KEY]['subjects'] = widget
else:
    # plone 4.3 not groked form
    _widget_values = ICategorization.queryTaggedValue(WIDGETS_KEY, {})
    _widget_values['subjects'] = widget
    ICategorization.setTaggedValue(WIDGETS_KEY, _widget_values)

_schema.getFields(ICategorization)['subjects'].index_name = 'Subject'
Beispiel #7
0
    def post_creation(self,
                      obj,
                      pdb_if_exception=False,
                      post_creation_data=None):
        if obj is None:
            return
        field_data = self.field_data
        bdata = ILayoutAware(obj, None)
        if bdata:
            try:
                bdata.contentLayout = self.layout
            except Exception:
                bdata.contentLayout = '++contentlayout++default/document.html'
        bdata = IRichText(obj, None)
        if bdata:
            try:
                bdata.text = RichTextValue(field_data['text'], 'text/html',
                                           'text/html')
            except Exception:
                try:
                    bdata.text = RichTextValue(
                        field_data[
                            'plone.app.contenttypes.behaviors.richtext.IRichText']
                        ['text'],  # noqa
                        'text/html',
                        'text/html').raw
                except Exception:
                    bdata.text = ''

        bdata = IBasic(obj, None)
        if bdata:
            try:
                bdata.title = field_data['title']
            except Exception:
                try:
                    bdata.title = field_data[
                        'plone.app.content.interfaces.INameFromTitle']['title']
                except Exception:
                    bdata.description = field_data[dublin]['title']
            try:
                bdata.description = field_data['description']
            except Exception:
                try:
                    bdata.description = field_data[dublin]['description']
                except Exception:
                    bdata.description = field_data[basic]['description']
        else:
            try:
                obj.title = field_data['title']
                obj.description = field_data['description']
            except Exception:
                obj.title = field_data[dublin]['title']
                obj.description = field_data[dublin]['description']

        bdata = ICategorization(obj, None)
        if bdata:
            try:
                bdata.subjects = field_data['subject']
            except Exception:
                try:
                    bdata.subjects = self.field_data[dublin]['subjects']
                except Exception:
                    try:
                        bdata.subjects = self.field_data[categorization][
                            'subjects']
                    except Exception:
                        pass  # no keywords found

            bdata = IPublication(obj)
            try:
                if field_data['effectiveDate']:
                    bdata.effective = pydt(field_data['effectiveDate'])
            except Exception:
                try:
                    if field_data[dublin]['effective']:
                        bdata.effective = pydt(field_data[dublin]['effective'])
                except Exception:
                    try:
                        if field_data[publication]['effective']:
                            bdata.effective = pydt(
                                field_data[publication]['effective'])
                    except Exception:
                        bdata.effective = None

        ldata = ILocation(obj, None)
        if ldata:
            if field_data.get('location'):
                ldata.locations = [field_data['location']]

            if field_data.get('newsLocation'):
                if ldata.locations:
                    ldata.locations.append(field_data['newsLocation'])
                else:
                    ldata.locations = [field_data['newsLocation']]

        try:
            obj.modification_date = field_data['modification_date']
        except Exception:
            try:
                obj.modification_date = obj.modified()
            except Exception:
                obj.modification_date = None
        try:
            obj.creation_date = field_data['creation_date']
        except Exception:
            try:
                obj.creation_date = obj.created()
            except Exception:
                obj.creation_date = None

        bdata = IDublinCore(obj, None)
        if bdata:
            if IDublinCore.__identifier__ in field_data:
                dublin_core = field_data[IDublinCore.__identifier__]
                bdata.expires = dublin_core['expires']
                bdata.rights = dublin_core['rights']
                bdata.creators = tuple(dublin_core['creators'])
                bdata.language = dublin_core['language']
                bdata.effective = pydt(dublin_core['effective'])
                bdata.subjects = dublin_core['subjects']
                bdata.contributors = tuple(dublin_core['contributors'])
            else:
                bdata.expires = pydt(field_data.get('expirationDate'))
                bdata.rights = field_data.get('rights')
                creators = field_data.get('creators')
                bdata.creators = tuple(creators) if creators else ()
                language = field_data.get('language')
                bdata.language = language if language is not None else ""
                bdata.effective = pydt(field_data.get('effectiveDate'))
                bdata.subjects = field_data.get('subject')
                contributors = field_data.get('contributors')
                bdata.contributors = tuple(contributors) if contributors else (
                )

        bdata = ILayoutAware(obj, None)
        if bdata:
            if self.data['portal_type'] == 'Folder' and (
                    self.field_data.get('text') or '').strip():
                bdata.content = FOLDER_DEFAULT_PAGE_LAYOUT % self.field_data[
                    'text']
                # need to explicitly reset contentLayout value because this data
                # could be overwritten
                bdata.contentLayout = None
            elif self.layout:
                if layoutaware in field_data and 'contentLayout' in field_data[
                        layoutaware]:
                    bdata.contentLayout = field_data[layoutaware][
                        'contentLayout']
                if layoutaware in field_data and 'content' in field_data[
                        layoutaware]:
                    bdata.content = field_data[
                        'plone.app.blocks.layoutbehavior.ILayoutAware'][
                            'content']
                if 'rendered_layout' in self.data['data']:
                    bdata.rendered_layout = self.data['data'][
                        'rendered_layout']

        inv_field_mapping = {v: k for k, v in self.fields_mapping.iteritems()}
        for IBehavior, field_name in self.behavior_data_mappers:

            original_field_name = inv_field_mapping.get(field_name, field_name)

            if original_field_name not in self.field_data:
                # data not here...
                continue

            behavior = IBehavior(obj, None)
            if behavior is None:
                # behavior not valid for obj type
                continue

            val = self.field_data[original_field_name]

            if field_name in self.data_converters:
                val = self.data_converters[field_name](val)

            setattr(behavior, field_name, val)

        # handle lead images
        for field_name in self.lead_image_field_names:
            if self.field_data.get(field_name):
                if field_name == 'plone.app.contenttypes.behaviors.leadimage.ILeadImage':
                    im_obj = self.field_data.get(field_name)['image']
                else:
                    im_obj = self.field_data.get(field_name)
                if hasattr(im_obj, 'read'):
                    im_data = im_obj.read()
                else:
                    im_data = im_obj

                if not im_data:
                    continue

                filename = self.field_data.get('image_filename')
                if not filename:
                    if hasattr(im_obj, 'filename'):
                        filename = im_obj.filename
                    else:
                        filename = self.field_data['id']
                obj.image = im_data

                if not isinstance(obj.image, NamedBlobImage):
                    is_stringio = isinstance(im_obj, StringIO)
                    if is_stringio:
                        namedblobimage_data = im_data
                    elif isinstance(im_obj, Image):
                        namedblobimage_data = im_data.data
                    else:
                        if pdb_if_exception:
                            pdb.set_trace()
                        logger.info("    lead image is type %s" % type(im_obj))
                    obj.image = NamedBlobImage(data=namedblobimage_data,
                                               contentType='',
                                               filename=filename)

                if hasattr(obj.image, 'contentType') and isinstance(
                        obj.image.contentType, unicode):
                    obj.image.contentType = obj.image.contentType.encode(
                        'ascii')
                else:
                    if isinstance(im_obj, Image):
                        data = im_obj.data
                    elif hasattr(im_obj, 'buf'):
                        data = im_obj.buf
                    elif hasattr(im_obj, '_blob'):
                        if hasattr(im_obj._blob, '_p_blob_uncommitted'):
                            f = open(im_obj._blob._p_blob_uncommitted, 'r')
                            data = f.read()
                            f.close()
                        else:
                            raise Exception(
                                "no _p_blob_uncommitted attr in im_obj._blob")
                    else:
                        raise Exception("no _blob attr in im_obj")

                    if data == '' or data is None:
                        data = base64.b64decode(
                            'R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
                        )

                    image_type = what('', h=data)
                    if image_type in [
                            'png', 'bmp', 'jpeg', 'xbm', 'tiff', 'gif'
                    ]:
                        obj.image.contentType = 'image/%s' % image_type
                    elif image_type == 'rast':
                        obj.image.contentType = 'image/cmu-raster'
                    elif image_type == 'ppm':
                        obj.image.contentType = 'image/x-portable-pixmap'
                    elif image_type == 'pgm':
                        obj.image.contentType = 'image/x-portable-greymap'
                    elif image_type == 'pbm':
                        obj.image.contentType = 'image/x-portable-bitmap'
                    elif image_type == 'rgb':
                        obj.image.contentType = 'image/x-rgb'
                    else:
                        # look at filename extension
                        contentType, encoding = guess_type(obj.image.filename,
                                                           strict=False)
                        if contentType:
                            obj.image.contentType = contentType
                        else:
                            logger.info(
                                "Unknown image type {};"
                                " defaulting to jpeg".format(image_type))
                            pdb.set_trace()
                            obj.image.contentType = 'image/jpeg'  # default
                for caption_field_name in self.lead_image_caption_field_names:
                    if caption_field_name in self.field_data:
                        obj.imageCaption = self.field_data.get(
                            caption_field_name)
Beispiel #8
0
# -*- coding: utf-8 -*-

from zope.i18nmessageid import MessageFactory

_ = MessageFactory('collective.nitf')

from plone.autoform.interfaces import WIDGETS_KEY
from plone.directives.form.schema import TEMP_KEY
from plone.app.dexterity.behaviors.metadata import ICategorization
from zope import schema as _schema

_directives_values = ICategorization.queryTaggedValue(TEMP_KEY)
_directives_values.setdefault(WIDGETS_KEY, {})
widget = 'collective.z3cform.widgets.token_input_widget.TokenInputFieldWidget'
_directives_values[WIDGETS_KEY]['subjects'] = widget
_schema.getFields(ICategorization)['subjects'].index_name = 'Subject'
# -*- coding: utf-8 -*-
from zope.i18nmessageid import MessageFactory
from plone.app.dexterity.behaviors.metadata import ICategorization
from plone.autoform.interfaces import WIDGETS_KEY
from plone.directives.form.schema import TEMP_KEY
from zope import schema as _schema

_ = MessageFactory('collective.z3cform.widgets')

widget = 'collective.z3cform.widgets.token_input_widget.TokenInputFieldWidget'
_directives_values = ICategorization.queryTaggedValue(TEMP_KEY)
if _directives_values:
    # groked form
    _directives_values.setdefault(WIDGETS_KEY, {})
    _directives_values[WIDGETS_KEY]['subjects'] = widget
else:
    # plone 4.3 not groked form
    _widget_values = ICategorization.queryTaggedValue(WIDGETS_KEY, {})
    _widget_values['subjects'] = widget
    ICategorization.setTaggedValue(WIDGETS_KEY, _widget_values)

_schema.getFields(ICategorization)['subjects'].index_name = 'Subject'
Beispiel #10
0
                value = values[field]
                if field == 'image':
                    try:
                        filename = "{}/{}".format(json_dir, value)
                        value = NamedBlobImage(open(filename).read())
                    except IOError, msg:
                        print "Invalid path for image: {}".format(str(msg))
                        continue
                fields[field] = value
        item = createContentInContainer(container, 'isaw.exhibitions.object',
                                        **fields)

        # Work around for but in dexterity createContent kwargs handling of
        # list values
        if 'subjects' in fields:
            ICategorization(item).subjects = fields['subjects']
        if 'pleiades_url' in fields:
            url = fields['pleiades_url']
            try:
                resp = requests.get(url,
                                    headers={'accept': 'application/json'})
                data = resp.json()
                if data.get('reprPoint'):
                    loc = data['reprPoint']
                    item.geolocation = Geolocation(loc[1], loc[0])
                else:
                    print "No data in pleiades repsonse from: {}".format(url)
            except requests.exceptions.RequestException:
                print "Error fetching pleiades data from: {}".format(url)
            except ValueError:
                print "Error decoding json response from: {}".format(url)
 def get_language(self):
     metadata = ICategorization(self.context, None)
     if metadata and metadata.language:
         return metadata.language
     return self.get_language_from_portal()
Beispiel #12
0
from zope.interface import Interface
from zope.interface import Invalid
from zope.interface import directlyProvides
from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleVocabulary

from gwopa.core import _
from gwopa.core import utils
from gwopa.core.widgets.fieldset_widget import FieldsetFieldWidget

import datetime
import unicodedata

ICategorization.setTaggedValue(OMITTED_KEY, [(Interface, 'language', 'true')])

grok.templatedir("templates")


class InvalidCoordinateError(schema.ValidationError):
    __doc__ = _(u'Please enter a valid coordinate format (-75.2509766).')


def isCoordinate(value):
    try:
        Point((float(value), float(0)))
        return True
    except:
        raise InvalidCoordinateError
Beispiel #13
0
    def post_creation(self, obj):
        field_data = self.field_data
        bdata = ILayoutAware(obj, None)
        if bdata:
            bdata.contentLayout = '++contentlayout++default/document.html'
        bdata = IRichText(obj, None)
        if bdata:
            bdata.text = RichTextValue(field_data['text'], 'text/html',
                                       'text/html')

        bdata = IBasic(obj, None)
        if bdata:
            bdata.title = field_data['title']
            bdata.description = field_data['description']
        else:
            obj.title = field_data['title']
            obj.description = field_data['description']

        bdata = ICategorization(obj, None)
        if bdata:
            bdata.subjects = field_data['subject']

            bdata = IPublication(obj)
            if field_data['effectiveDate']:
                bdata.effective = pydt(field_data['effectiveDate'])

        ldata = ILocation(obj, None)
        if ldata:
            if field_data.get('location'):
                ldata.locations = [field_data['location']]

            if field_data.get('newsLocation'):
                if ldata.locations:
                    ldata.locations.append(field_data['newsLocation'])
                else:
                    ldata.locations = [field_data['newsLocation']]

        obj.modification_date = field_data['modification_date']
        obj.creation_date = field_data['creation_date']

        bdata = ILayoutAware(obj, None)
        if bdata:
            if self.data[
                    'portal_type'] == 'Folder' and 'text' in self.field_data:
                bdata.content = FOLDER_DEFAULT_PAGE_LAYOUT % self.field_data[
                    'text']
                # need to explicitly reset contentLayout value because this data
                # could be overwritten
                bdata.contentLayout = None
            elif self.layout:
                bdata.contentLayout = self.layout

        inv_field_mapping = {v: k for k, v in self.fields_mapping.iteritems()}
        for IBehavior, field_name in self.behavior_data_mappers:

            original_field_name = inv_field_mapping.get(field_name, field_name)

            if original_field_name not in self.field_data:
                # data not here...
                continue

            behavior = IBehavior(obj, None)
            if behavior is None:
                # behavior not valid for obj type
                continue

            val = self.field_data[original_field_name]

            if field_name in self.data_converters:
                val = self.data_converters[field_name](val)

            setattr(behavior, field_name, val)

        # handle lead images
        for field_name in self.lead_image_field_names:
            if self.field_data.get(field_name):
                im_obj = self.field_data.get(field_name)
                if hasattr(im_obj, 'read'):
                    im_data = im_obj.read()
                else:
                    im_data = im_obj

                if not im_data:
                    continue

                filename = self.field_data.get('image_filename')
                if not filename:
                    if hasattr(im_obj, 'filename'):
                        filename = im_obj.filename
                    else:
                        filename = self.field_data['id']
                obj.image = NamedBlobImage(data=decode_file_data(im_data),
                                           filename=to_unicode(filename))
                if not obj.image.contentType:
                    obj.image.contentType = 'image/jpeg'
                for caption_field_name in self.lead_image_caption_field_names:
                    if caption_field_name in self.field_data:
                        obj.imageCaption = self.field_data.get(
                            caption_field_name)