Skip to content

Instantly share code, notes, and snippets.

@brunobbbs
Last active August 29, 2015 14:02
Show Gist options
  • Save brunobbbs/2c17155872ff27819b35 to your computer and use it in GitHub Desktop.
Save brunobbbs/2c17155872ff27819b35 to your computer and use it in GitHub Desktop.
Query portal_catalog filtering tags with plone.api in Plone 4
<browser:page
for="*"
name="getdocument_view"
class=".getdocument.GetDocument"
template="getdocument.pt"
allowed_interface=".getdocument.IGetDocument"
permission="zope.Public"
/>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt-br"
lang="pt-br"
metal:use-macro="here/main_template/macros/master"
i18n:domain="plone">
<body>
<div metal:fill-slot="main">
<dl>
<tal:items repeat="obj view/get">
<dt tal:content="obj/titulo">Titulo</dt>
<dd>
<p tal:content="obj/descricao">Descrição</p>
<p tal:content="obj/tags">Tags</p>
</dd>
</tal:items>
</dl>
</div>
</body>
</html>
#-*- coding: utf-8 -*-
from plone import api
from Products.Five import BrowserView
from zope.interface import implements, Interface
class IGetDocument(Interface):
""" """
class GetDocument(BrowserView):
""" """
def __init__(self, context, request):
self.context = context
self.request = request
def brain(self):
path = "%s/diretorio" % ('/'.join(api.portal.get().getPhysicalPath())) # Chama o path a partir da raiz do portal
#path = "%s/diretorio" % ('/'.join(self.context.getPhysicalPath())) # Chama o path a partir do contexto
query = {
'path': path,
'portal_type': ('Document', ),
'review_state': 'published',
'sort_on': 'getObjPositionInParent', # Ordena os resultados de acordo com a posição deles no diretório que se encontram
}
catalog = api.portal.get_tool('portal_catalog')
brains = catalog(**query)
return brains
def get(self):
result = []
brains = self.brain()
for brain in brains:
obj = brain.getObject()
if 'tag1' in obj.subject:
result.append({
'titulo': brain.Title,
'descricao': brain.Description,
'tags': obj.subject, # Tags são uma turpla, podem ser iteradas através de um laço 'for'
})
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment