| 1 |
from locale import strcoll |
|---|
| 2 |
from Products.CMFCore import CMFCorePermissions |
|---|
| 3 |
from Products.CMFCore.utils import getToolByName |
|---|
| 4 |
from AccessControl import ClassSecurityInfo |
|---|
| 5 |
from Acquisition import aq_parent, aq_inner |
|---|
| 6 |
|
|---|
| 7 |
from Products.ATContentTypes.types.ATContentType import updateActions |
|---|
| 8 |
from Products.ATContentTypes.types.ATTopic import ATTopic, IGNORED_FIELDS |
|---|
| 9 |
from Products.ATContentTypes.interfaces.IATTopic import IATTopic |
|---|
| 10 |
from Products.ATContentTypes.types.criteria import CriterionRegistry |
|---|
| 11 |
from Products.ATContentTypes.Permissions import ChangeTopics, AddTopics |
|---|
| 12 |
from Products.ATContentTypes.types.schemata import ATTopicSchema |
|---|
| 13 |
from Products.ATContentTypes.interfaces.IATTopic import IATTopicSearchCriterion, IATTopicSortCriterion |
|---|
| 14 |
from Products.Archetypes.public import * |
|---|
| 15 |
from config import * |
|---|
| 16 |
|
|---|
| 17 |
qTopic_schema = ATTopicSchema.copy() + Schema(( |
|---|
| 18 |
StringField('catalog', |
|---|
| 19 |
default = 'portal_catalog', |
|---|
| 20 |
vocabulary = 'getCatalogList', |
|---|
| 21 |
widget = SelectionWidget(label="Catalog", |
|---|
| 22 |
label_msgid="label_catalog", |
|---|
| 23 |
description="Select catalog to query", |
|---|
| 24 |
description_msgid="description_catalog") |
|---|
| 25 |
), |
|---|
| 26 |
LinesField('exportFields', |
|---|
| 27 |
vocabulary = 'getFieldsList', |
|---|
| 28 |
schemata = 'export', |
|---|
| 29 |
widget = MultiSelectionWidget(label="Fields to be exported", |
|---|
| 30 |
label_msgid="label_export_fields", |
|---|
| 31 |
description="Select fileds to be exported", |
|---|
| 32 |
description_msgid="description_export_fields") |
|---|
| 33 |
), |
|---|
| 34 |
BooleanField('showHeader', |
|---|
| 35 |
schemata = 'export', |
|---|
| 36 |
default = 1, |
|---|
| 37 |
widget = BooleanWidget(label="Print field headers", |
|---|
| 38 |
label_msgid="label_print_headers", |
|---|
| 39 |
description="Check if headers need to be printed", |
|---|
| 40 |
description_msgid="description_print_headers") |
|---|
| 41 |
), |
|---|
| 42 |
StringField('delimiter', |
|---|
| 43 |
default = ';', |
|---|
| 44 |
schemata = 'export', |
|---|
| 45 |
widget = StringWidget(label="Values delimiter", |
|---|
| 46 |
label_msgid="label_delimiter", |
|---|
| 47 |
description="Select delimiter to be used in CSV", |
|---|
| 48 |
description_msgid="description_delimiter", |
|---|
| 49 |
size = 4) |
|---|
| 50 |
), |
|---|
| 51 |
)) |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
class qTopic(ATTopic): |
|---|
| 55 |
"""A topic folder""" |
|---|
| 56 |
meta_type = 'qTopic' |
|---|
| 57 |
portal_type = 'qTopic' |
|---|
| 58 |
archetype_name = 'qTopic' |
|---|
| 59 |
typeDescription= ("qTopic is the same topic but with "+ |
|---|
| 60 |
"option of catlog selection") |
|---|
| 61 |
typeDescMsgId = 'description_edit_topic' |
|---|
| 62 |
|
|---|
| 63 |
schema = qTopic_schema |
|---|
| 64 |
security = ClassSecurityInfo() |
|---|
| 65 |
actions = updateActions(ATTopic, |
|---|
| 66 |
( |
|---|
| 67 |
{ |
|---|
| 68 |
'id' : 'export_csv', |
|---|
| 69 |
'name' : 'Export in CSV', |
|---|
| 70 |
'action' : 'string:${folder_url}/result_csv', |
|---|
| 71 |
'permissions' : (CMFCorePermissions.View,) |
|---|
| 72 |
}, |
|---|
| 73 |
) |
|---|
| 74 |
) |
|---|
| 75 |
|
|---|
| 76 |
def getCatalogList(self): |
|---|
| 77 |
""" return list of catalog ids |
|---|
| 78 |
""" |
|---|
| 79 |
at_tool = getToolByName(self, 'archetype_tool') |
|---|
| 80 |
catalogs = at_tool.getCatalogsInSite() |
|---|
| 81 |
return DisplayList(zip(catalogs, catalogs)) |
|---|
| 82 |
|
|---|
| 83 |
def getFieldsList(self): |
|---|
| 84 |
""" return DisplayList of fields |
|---|
| 85 |
""" |
|---|
| 86 |
pcatalog = getToolByName( self, self.getCatalog() ) |
|---|
| 87 |
available = pcatalog.schema() |
|---|
| 88 |
val = [ field |
|---|
| 89 |
for field in available |
|---|
| 90 |
if field not in IGNORED_FIELDS |
|---|
| 91 |
] |
|---|
| 92 |
val.sort(lambda x,y: strcoll(self.translate(x),self.translate( y))) |
|---|
| 93 |
return DisplayList(zip(val, val)) |
|---|
| 94 |
|
|---|
| 95 |
security.declareProtected(ChangeTopics, 'criteriaByIndexId') |
|---|
| 96 |
def criteriaByIndexId(self, indexId): |
|---|
| 97 |
catalog_tool = getToolByName(self, self.getCatalog()) |
|---|
| 98 |
indexObj = catalog_tool.Indexes[indexId] |
|---|
| 99 |
results = CriterionRegistry.criteriaByIndex(indexObj.meta_type) |
|---|
| 100 |
return results |
|---|
| 101 |
|
|---|
| 102 |
security.declareProtected(ChangeTopics, 'listFields') |
|---|
| 103 |
def listFields(self): |
|---|
| 104 |
"""Return a list of fields from portal_catalog. |
|---|
| 105 |
""" |
|---|
| 106 |
pcatalog = getToolByName( self, self.getCatalog() ) |
|---|
| 107 |
available = pcatalog.indexes() |
|---|
| 108 |
val = [ field |
|---|
| 109 |
for field in available |
|---|
| 110 |
if field not in IGNORED_FIELDS |
|---|
| 111 |
] |
|---|
| 112 |
val.sort(lambda x,y: strcoll(self.translate(x),self.translate( y))) |
|---|
| 113 |
return val |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
security.declareProtected(CMFCorePermissions.View, 'queryCatalog') |
|---|
| 117 |
def queryCatalog(self, REQUEST=None, **kw): |
|---|
| 118 |
"""Invoke the catalog using our criteria to augment any passed |
|---|
| 119 |
in query before calling the catalog. |
|---|
| 120 |
""" |
|---|
| 121 |
q = self.buildQuery() |
|---|
| 122 |
if q is None: |
|---|
| 123 |
|
|---|
| 124 |
return [] |
|---|
| 125 |
kw.update(q) |
|---|
| 126 |
pcatalog = getToolByName(self, self.getCatalog()) |
|---|
| 127 |
limit = self.getLimitNumber() |
|---|
| 128 |
max_items = self.getItemCount() |
|---|
| 129 |
if limit and self.hasSortCriterion(): |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
kw.setdefault('sort_limit', max_items) |
|---|
| 134 |
results = pcatalog.searchResults(REQUEST, **kw) |
|---|
| 135 |
if limit: |
|---|
| 136 |
return results[:max_items] |
|---|
| 137 |
return results |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
registerType(qTopic, PROJECTNAME) |
|---|
| 141 |
|
|---|
| 142 |
def modify_fti(fti): |
|---|
| 143 |
"""Remove folderlisting action |
|---|
| 144 |
""" |
|---|
| 145 |
actions = [] |
|---|
| 146 |
for action in fti['actions']: |
|---|
| 147 |
if action['id'] == 'folderlisting': |
|---|
| 148 |
action['visible'] = False |
|---|
| 149 |
|
|---|
| 150 |
|
|---|