collective / collective.plonetruegallery

Very customizable and sophisticated gallery for Plone
https://pypi.python.org/pypi/collective.plonetruegallery
9 stars 20 forks source link

Issues with dexterity collections and PTG #45

Open volkerjaenisch opened 9 years ago

volkerjaenisch commented 9 years ago

I noticed some problems with dexterity collections from plone.app.contenttypes in Plone4.3 and Plone5. Namely they do not work anymore. There are four problems: 1) The collections adapter is bound to plone.app.collections.interfaces.ICollection but dexterity collections have plone.app.contenttypes.interfaces.ICollection as marker interface. So the adapter does not fit the interface. 2) In dexterity the collections do no longer expose the getRawQuery function. See https://community.plone.org/t/where-is-defined-collection-getrawquery-and-how/1040 . This leads to a stack trace. 3) If this problem is overcome the wrong image type is used in the query. 4) And last but not least. The current code does not work with collections collecting subgalleries.

I wrote a replacement to collection.py that fixes most of the problems but does not address all the corner cases. The code is ugly and obtains a monkey patch to change the get_subgalleries function of the basic_adapter. But it may be clear enough to show my intentions. Also the code will not work with ATCollections - which is not a problem because this new adapter can be bound to the dexterity specific plone.app.contenttypes.interfaces.ICollection and live in friendly coexistence with the old adapter for ATCollections.

from zope.component import adapts, getMultiAdapter
from Products.CMFCore.utils import getToolByName
from collective.plonetruegallery.galleryadapters.basic import \
    BasicTopicImageInformationRetriever as BTIIR
from Products.ATContentTypes.interface.image import IImageContent
from plone.app.contenttypes.interfaces import ICollection, IImage
from collective.plonetruegallery.interfaces import IBasicAdapter
from plone.app.querystring import queryparser
from collective.plonetruegallery.interfaces import IGalleryAdapter, IGallery
import types

try:
    from plone.uuid.interfaces import IUUID
except:
    def IUUID(_, _2=None):
        return None

try:
    from plone.app.contenttypes.behaviors.leadimage import ILeadImage
except ImportError:
    ILeadImage = None

class BasicCollectionImageInformationRetriever(BTIIR):
    adapts(ICollection, IBasicAdapter)

    def __init__(self, context, gallery_adapter):
        self.pm = getToolByName(context, 'portal_membership')
        self.context = context
        self.gallery_adapter = gallery_adapter

        def get_subgalleries(self, **kwargs):
            query = queryparser.parseFormquery(
            self.gallery, self.gallery.query)
            catalog = getToolByName(self.gallery, 'portal_catalog')
            if 'Subject' in kwargs:
                if not 'Subject' in query:
                    query.update({'Subject': kwargs['Subject']})
                else:
                    query['Subject'] = {'operator': 'and', 'query': [kwargs['Subject']] + query['Subject']['query']}

            if not 'object_provides' in query:
                query.update({'object_provides': IGallery.__identifier__})
            else:
                query['object_provides'] = {'operator': 'and', 'query': [IGallery.__identifier__] + query['object_provides']['query']}

            results = catalog(query)
            uid = IUUID(self.gallery, None)
            if uid is None:
                uid = self.gallery.UID()

            def afilter(i):
                """prevent same object and multiple nested galleries"""
                return i.UID != uid and \
                    getMultiAdapter(
                    (i.getObject(), self.request),
                    name='plonetruegallery_util'
                    ).enabled()

            return filter(afilter, results)
        self.gallery_adapter.get_subgalleries = types.MethodType(get_subgalleries, self.gallery_adapter)

    def getImageInformation(self):
        limit = self.context.limit
        query = queryparser.parseFormquery(
            self.context, self.context.query)
        if ILeadImage:
            query.update({
                'object_provides': {
                    'query': [
                        IImage.__identifier__,
                        ILeadImage.__identifier__],
                    'operator': 'or'
                }
            })
        else:
            query.update({'object_provides': IImage.__identifier__})
        query['sort_limit'] = limit
        catalog = getToolByName(self.context, 'portal_catalog')
        images = catalog(query)
        images = images[:limit]
        return map(self.assemble_image_information, images)
mactrash commented 7 years ago

May I know how to use this file? @volkerjaenisch

volkerjaenisch commented 7 years ago

Just install collective.plonetruegallery . Then replace the collection.py file that lives in this package with the code shown above.

mactrash commented 7 years ago

@volkerjaenisch

Thanks for your reply, I am very new to this...

Do you mean after replay the file then I can use the gallery view in plone 5 collection?

volkerjaenisch commented 7 years ago

Yes. We are using PTG with collections in Plone 5. You only need to replace this one file. If you get into problems feel free to ask.

mactrash commented 7 years ago

@volkerjaenisch I have follow the step you mentioned, buy the PTG gallery view still not show up in the selection list of my collections... any idea?

volkerjaenisch commented 7 years ago

The alternative is to use my fork of PTG: https://github.com/volkerjaenisch/collective.plonetruegallery

mactrash commented 7 years ago

@volkerjaenisch thanks for your help

I try your method and now with collective.plonetruegallery 3.4.7.dev0 but there is no gallery view in the collection ... do i missed something my plone is Plone 5.0.6rc1 (5016) do I need to active "Archetypes Content Types for Plone" ?

mactrash commented 7 years ago

@volkerjaenisch and my plone.app.collection = 1.1.6 is that same as yours

volkerjaenisch commented 7 years ago

The Version number 1.1.6 should not be an issue. Please check the configuration for collections:

/dexterity-types/Collection/@@behaviors The entry: Plone True Gallery Enable Plone True Gallery support has to be enabled.
volkerjaenisch commented 7 years ago

Tested with plone.app.collection = 1.1.6, works flawlessly.

mactrash commented 7 years ago

@volkerjaenisch

Thanks Gallery view now show up in the collector and can be select but it got a error as below.

Traceback (innermost last): Module ZPublisher.Publish, line 138, in publish Module ZPublisher.mapply, line 77, in mapply Module ZPublisher.Publish, line 48, in call_object Module collective.plonetruegallery.browser.views.galleryview, line 29, in call Module Products.Five.browser.pagetemplatefile, line 125, in call Module Products.Five.browser.pagetemplatefile, line 59, in call Module zope.pagetemplate.pagetemplate, line 132, in pt_render Module five.pt.engine, line 98, in call Module z3c.pt.pagetemplate, line 163, in render Module chameleon.zpt.template, line 261, in render Module chameleon.template, line 191, in render Module chameleon.template, line 171, in render Module d0e9d24677fac041b6f34863811f414d.py, line 459, in render Module e3bbe0aa01b667bbf022121054b66185.py, line 1223, in render_master Module e3bbe0aa01b667bbf022121054b66185.py, line 420, in render_content Module d0e9d24677fac041b6f34863811f414d.py, line 447, in __fill_content_core Module d0e9d24677fac041b6f34863811f414d.py, line 95, in render_content_core Module collective.plonetruegallery.galleryadapters.base, line 100, in number_of_images Module plone.memoize.instance, line 51, in memogetter Module collective.plonetruegallery.galleryadapters.basic, line 155, in cooked_images Module collective.plonetruegallery.galleryadapters.basic, line 142, in retrieve_images Module zope.component._api, line 109, in getMultiAdapter ComponentLookupError: ((<Collection at /test/test>, <collective.plonetruegallery.galleryadapters.basic.BasicAdapter object at 0x7f775d31b1d0>), , u'')

volkerjaenisch commented 7 years ago

Are you sure that you have only images (Type Image) and No PDF-Files etc. in the collection? Have you any Images in the collection?

mactrash commented 7 years ago

Yes, 1 jpg can show with Album view.

volkerjaenisch commented 7 years ago

Can you please make sure that the alternative collectiv.plonetruegallery is really used:

mactrash commented 7 years ago

Ok here is the result,

I don't know why it error and not build

sudo -u plone_buildout bin/buildout Develop: '/opt/plone/zinstance/src/collective.plonetruegallery' warning: no files found matching '.rst' warning: no previously-included files found matching '.coveragerc' warning: no previously-included files found matching 'buildout.cfg' warning: no previously-included files found matching 'tox.ini' warning: no previously-included files found matching 'travis.cfg' warning: no previously-included files matching '.pyc' found anywhere in distribution Uninstalling precompiler. Uninstalling zopepy. Uninstalling instance.

volkerjaenisch commented 7 years ago

You have to enter the virtual environment first. in zeoserver dir:
source bin/activate ./bin/buildout

If this fails please post the complete output

mactrash commented 7 years ago

@volkerjaenisch

It is working now, thanks alot.

volkerjaenisch commented 7 years ago

you are welcome