def __init__(self, id,title=''): '''Initialize an instance of the class''' self.id=id self.sampleImage = '' self.abstract = '' self.keywords = '' self.uniqueName = '' self.geometryField = '' self.geometryType = '' self.title = id self.wfsSRS = '' self.wmsStyleName = '' self.wfsBoundingBox = '' self.fields = '' self.source = '' self.error = '' self.wmsQueryable = '' self.wmsName = '' self.wfsName = '' self.wmsSRS = '' self.wmsTitle = '' self.hasWFSLayer = False self.wmsBoundingBox = '' self.describeFeatureResponse = '' self.availablePermissions = ['Render','Extract'] self.security = PersistentMapping() # e.g {'Manager':{'Render':0,'Extract':1}} self.wmsXML = "" self.wfsXML = "" self.organization = "" self.reindexObject() self._p_changed = 1
class TransformDataProvider(Implicit): """ Base class for data providers """ security = ClassSecurityInfo() security.declareObjectPublic() def __init__(self): self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({'inputs' : {} }) self.config_metadata.update({ 'inputs' : { 'key_label' : '', 'value_label' : '', 'description' : ''} }) security.declarePublic('setElement') def setElement(self, inputs): """ inputs - dictionary, but may be extended to new data types""" self.config['inputs'].update(inputs) def delElement(self, el): """ el - dictionary key""" del self.config['inputs'][el] security.declarePublic('getElements') def getElements(self): """ Returns mapping """ return self.config['inputs'] security.declarePublic('getConfigMetadata') def getConfigMetadata(self): """ Returns config metadata """ return self.config_metadata['inputs']
def __init__(self, id=None): BaseContent.__init__(self, 'portal_csvreplicatatool') self.setTitle('') ##code-section constructor-footer #fill in your manual code here self.setTitle('CSV Replicator tool') self.replicabletypes = PersistentMapping() self.handlers = {}
def __init__(self): self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({'inputs' : {} }) self.config_metadata.update({ 'inputs' : { 'key_label' : '', 'value_label' : '', 'description' : ''} })
def test_persistent_types_buglets(self): l = PersistentList([1, 2, 3]) self.assertTrue(isinstance(l, PersistentList)) self.assertFalse(isinstance(l, list)) # dangerous self.assertTrue(isinstance(l[:], PersistentList)) d = PersistentMapping({1: 2}) self.assertTrue(isinstance(d, PersistentMapping)) self.assertFalse(isinstance(d, dict)) # dangerous self.assertTrue(isinstance(d.copy(), PersistentMapping))
def __init__(self, id, title=''): '''Initialize an instance of the class''' self.id = id if title == '': self.title = id else: self.title = title self.type = '' self.availablePermissions = ['Render'] self.security = PersistentMapping() # e.g {'Manager':{'Render':0}} self.reindexObject() self._p_changed = 1
def test_mapping_copy(self): a = PersistentMapping(dict(a=3, b=5)) b = a.copy() c = copy.copy(a) assert a == b assert a == c assert a is not b assert a is not c assert b is not c del a["a"] assert b["a"] # NOT impacted assert c["a"] # NOT impacted
def __init__(self): """ We need to redefine config and config_metadata in base class. """ self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({ 'inputs' : self.defaultEmoticons()}) self.config_metadata.update({ 'inputs' : { 'key_label' : 'emoticon code', 'value_label' : 'image name', 'description' : 'Emoticons to images mapping'} })
def compute_broken_links(site): """ Script that will get called by cron once per day """ results = [] annot = IAnnotations(site)['broken_links_data'] now = DateTime() links = get_links(site) if isinstance(annot, list): # store old data old_data = annot annot = PersistentMapping() IAnnotations(site)['broken_links_data'] = annot annot['pre_nov7_data'] = old_data for info in links: res = check_link(info['link']) if res is not None: res['object_url'] = info['object_url'] results.append(res) annot[now] = results dates = annot.keys() if len(dates) >= 5: # maximum no. of dates stored # delete oldest data except 'pre_nov7_data' del annot[sorted(dates)[0]] IAnnotations(site)._p_changed = True transaction.commit()
def __init__(self, id): Content.__init__(self, id) Query.__init__(self) self.shownFields = PersistentMapping() self.shownResultsFields = PersistentMapping() # by default we only show fulltext search # and a couple of resultfields self.shownFields['fulltext'] = True self.shownResultsFields['link'] = True self.shownResultsFields['ranking'] = True self.shownResultsFields['resultcount'] = True self.shownResultsFields['icon'] = True self.shownResultsFields['date'] = True self.shownResultsFields['textsnippet'] = True self.shownResultsFields['thumbnail'] = True self.shownResultsFields['breadcrumbs'] = True
class Map(SimpleItem): def __init__(self, id): self.id = id self.channels = PersistentMapping() self.policy = defaultPolicy def setPolicy(self, name): self.policy = lookupPolicy(name, defaultPolicy) def setPriority(self, channel, pri): if not isinstance(channel, Channel): id = channel.id else: id = channel channel = self.channels[id] channel['priority'] = int(pri) def addChannel(self, channel, **kwargs): enabled = kwargs.get('enabled', 1) priority = kwargs.get('priority', 0) self.channels[channel.id] = {'channel' : channel, 'enabled' : enabled, 'priority': priority, } def Channels(self): return self.channels.values()
def _reassembleWorkingCopy(self, new_baseline, baseline): # reattach the source's workflow history, try avoid a dangling ref try: new_baseline.workflow_history = PersistentMapping( baseline.workflow_history.items()) except AttributeError: # No workflow apparently. Oh well. pass # reset wf state security directly workflow_tool = getToolByName(self.context, 'portal_workflow') wfs = workflow_tool.getWorkflowsFor(self.context) for wf in wfs: if not isinstance(wf, DCWorkflowDefinition): continue wf.updateRoleMappingsFor(new_baseline) # Reattach the source's uid, this will update wc refs to point # back to the new baseline. This may introduce duplicate # references, so we check that and fix them. self._recursivelyReattachUIDs(baseline, new_baseline) # reattach the source's history id, to get the previous # version ancestry histid_handler = getToolByName(self.context, 'portal_historyidhandler') huid = histid_handler.getUid(baseline) histid_handler.setUid(new_baseline, huid, check_uniqueness=False) return new_baseline
def convert(self, ob): # Creates a copy of ob without its children. ob = aq_base(ob) k = self._klass if hasattr(k, '__basicnew__'): newob = k.__basicnew__() else: newob = new.instance(k, {}) id = ob.id if callable(id): id = id() try: newob._setId(id) except AttributeError: newob.id = id newob.__dict__.update(ob.__dict__) if hasattr(newob, '_objects'): # Clear the children. for info in newob._objects: del newob.__dict__[info['id']] newob._objects = () if hasattr(newob, '_container'): # Clear the children. newob._container = PersistentMapping() return newob
def __init__(self,default_role,NoLocalRoles): self.id='nisAuthSource' self.default_role=default_role self.NoLocalRoles=NoLocalRoles self.data=PersistentMapping() self.manage_addUserForm=DTMLFile('manage_addNISUserForm',globals()) self.manage_editUserForm=DTMLFile('manage_editNISUserForm',globals()) #not used. No way to plug it into exUserFolder.py
def install(self): " Register the CMF Topic with portal_types and friends " out = StringIO() typestool = getToolByName(self, 'portal_types') skinstool = getToolByName(self, 'portal_skins') workflowtool = getToolByName(self, 'portal_workflow') # Borrowed from CMFDefault.Portal.PortalGenerator.setupTypes() # We loop through anything defined in the factory type information # and configure it in the types tool if it doesn't already exist for t in CMFWikiPage.factory_type_information: if t['id'] not in typestool.objectIds(): cfm = apply(ContentFactoryMetadata, (), t) typestool._setObject(t['id'], cfm) out.write('Registered %s with the types tool\n' % t['id']) else: out.write('Object "%s" already existed in the types tool\n' % (t['id'])) # Setup the skins # This is borrowed from CMFDefault/scripts/addImagesToSkinPaths.pys if 'wiki' not in skinstool.objectIds(): # We need to add Filesystem Directory Views for any directories # in our skins/ directory. These directories should already be # configured. addDirectoryViews(skinstool, 'skins', wiki_globals) out.write("Added 'wiki' directory view to portal_skins\n") # Now we need to go through the skin configurations and insert # 'wiki' into the configurations. Preferably, this should be # right before where 'content' is placed. Otherwise, we append # it to the end. skins = skinstool.getSkinSelections() for skin in skins: path = skinstool.getSkinPath(skin) path = map(string.strip, string.split(path, ',')) for dir in ('wiki', 'zpt_wiki'): if not dir in path: try: idx = path.index('custom') except ValueError: idx = 999 path.insert(idx + 1, dir) path = string.join(path, ', ') # addSkinSelection will replace existing skins as well. skinstool.addSkinSelection(skin, path) out.write("Added 'wiki' and 'zpt_wiki' to %s skin\n" % skin) # remove workflow for Wiki pages cbt = workflowtool._chains_by_type if cbt is None: cbt = PersistentMapping() cbt['CMF Wiki'] = [] cbt['CMF Wiki Page'] = [] workflowtool._chains_by_type = cbt out.write("Removed all workflow from CMF Wikis and CMF Wiki Pages") return out.getvalue()
def __init__(self, id=None): BaseContent.__init__(self,'portal_csvreplicatatool') self.setTitle('') ##code-section constructor-footer #fill in your manual code here self.setTitle('CSV Replicator tool') self.replicabletypes = PersistentMapping() self.handlers = {}
def test_mapping_copy(self): a = PersistentMapping(dict(a=3, b=5)) b = a.copy() c = copy.copy(a) d = copy.deepcopy(a) assert a == b assert a == c assert a == d assert a is not b assert a is not c assert b is not c assert a is not d del a["a"] assert b["a"] # NOT impacted assert "a" in c # NOT impacted assert d["a"] # NOT impacted
def __init__(self): """ We need to redefine config and config_metadata in base class. """ self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({"inputs": self.defaultEmoticons()}) self.config_metadata.update( { "inputs": { "key_label": "emoticon code", "value_label": "image name", "description": "Emoticons to images mapping", } } )
def handleApply(self, action): data, errors = self.extractData() workflow_history = self.context.workflow_history for line in workflow_history.values()[0]: state = line.get("review_state") if state in data: line["time"] = DateTime( datetime.combine(data[state], datetime.min.time())) self.context.workflow_history = PersistentMapping( workflow_history.items()) self.request.response.redirect(self.context.absolute_url())
def migrateDiscussionContainer(self, src_folder, dst_folder, place=()): self.visited_folders.append(join(place, '/')) dst_container = getattr(dst_folder, '_container', None) if dst_container is None: dst_container = dst_folder._container = PersistentMapping() for id, s_ob in src_folder._container.items(): d_ob = dst_container.get(id) to_store = self.migrateObject(id, s_ob, d_ob, dst_folder, place + (id, )) if to_store is not None: dst_container[id] = aq_base(to_store)
class EmoticonDataProvider(TransformDataProvider): def __init__(self): """ We need to redefine config and config_metadata in base class. """ self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({"inputs": self.defaultEmoticons()}) self.config_metadata.update( { "inputs": { "key_label": "emoticon code", "value_label": "image name", "description": "Emoticons to images mapping", } } ) def defaultEmoticons(): emoticons = { ":)": '<img src="smiley_smile.png" alt=":)" title="Smile" />', ":(": '<img src="smiley_sad.png" alt=":(" title="Sad" />', "8-)": '<img src="smiley_cool.png" alt="8)" title="Cool" />', ":D": '<img src="smiley_lol.png" alt=":D" title="Big grin" />', ":|": '<img src="smiley_skeptic.png" alt=":|" title="Skeptic" />', ":o": '<img src="smiley_surprised.png" alt=":o" title="Surprised" />', ":P": '<img src="smiley_tongue.png" alt=":P" title="Tongue-in-cheek" />', ";)": '<img src="smiley_wink.png" alt=";)" title="Wink" />', ":-)": '<img src="smiley_smile.png" alt=":)" title="Smile" />', ":-(": '<img src="smiley_sad.png" alt=":(" title="Sad" />', ":-D": '<img src="smiley_lol.png" alt=":D" title="Big grin" />', ":-|": '<img src="smiley_skeptic.png" alt=":|" title="Skeptic" />', ":-o": '<img src="smiley_surprised.png" alt=":o" title="Surprised" />', ":-P": '<img src="smiley_tongue.png" alt=":P" title="Tongue-in-cheek" />', ";-)": '<img src="smiley_wink.png" alt=";)" title="Wink" />', } return emoticons defaultEmoticons = staticmethod(defaultEmoticons)
def __init__(self, id): self.id = id self.dat = PersistentMapping() # automatical Persictence of Dictionary self.dat.update({ 'title': None, 'graphname': None, 'gridlines': None, 'height': None, 'width': None, 'data': TALESMethod(None), 'legend': TALESMethod(None), 'colnames': TALESMethod(None), 'stylesheet': None, 'convertername': None, 'fixcolumn': None, 'specialattrib': None, 'intcaption': None, 'fillgaps': None, 'taltitle': TALESMethod(None), }) self.current_version = self.version # set at creation & update
def _store(self, values, set, set_id, reindex=0): if not values: return False # Update acquirable values for element_id, element, value in values: if element.isAcquireable(): attr_name = encodeElement(set_id, element_id) if not (value == '' or value is None): setattr(self.content, attr_name, value) else: # Try and get rid of encoded attribute on the # annotatable object; this will get acquisition # of the value working again. try: delattr(self.content, attr_name) except (KeyError, AttributeError): pass # Save values in Annotations annotations = IAnnotations(aq_base(self.content)) if set.metadata_uri not in annotations: annotations[set.metadata_uri] = PersistentMapping({}) storage = annotations[set.metadata_uri] data = {} for element_id, element, value in values: if not (value == '' or value is None): storage[element_id] = value elif element_id in storage: del storage[element_id] # For the event data[element_id] = value # invalidate the cache version of the set if any # we do a check for cached acquired/non-acquired if (0, set_id) in self.cached_values: del self.cached_values[(0, set_id)] if (1, set_id) in self.cached_values: del self.cached_values[(1, set_id)] # mark both the content and the annotatable object as changed so # on txn commit bindings in other objectspaces get invalidated as well self.content._p_changed = 1 # reindex object if reindex and not getattr(self.content, '__initialization__', False): ICataloging(self.content).reindex() notify(MetadataModifiedEvent(self.content, data)) return True
class EmoticonDataProvider(TransformDataProvider): def __init__(self): """ We need to redefine config and config_metadata in base class. """ self.config = PersistentMapping() self.config_metadata = PersistentMapping() self.config.update({ 'inputs' : self.defaultEmoticons()}) self.config_metadata.update({ 'inputs' : { 'key_label' : 'emoticon code', 'value_label' : 'image name', 'description' : 'Emoticons to images mapping'} }) def defaultEmoticons(): emoticons = { ':)' : '<img src="smiley_smile.png" alt=":)" title="Smile" />' , ':(' : '<img src="smiley_sad.png" alt=":(" title="Sad" />' , '8-)' : '<img src="smiley_cool.png" alt="8)" title="Cool" />' , ':D' : '<img src="smiley_lol.png" alt=":D" title="Big grin" />' , ':|' : '<img src="smiley_skeptic.png" alt=":|" title="Skeptic" />' , ':o' : '<img src="smiley_surprised.png" alt=":o" title="Surprised" />' , ':P' : '<img src="smiley_tongue.png" alt=":P" title="Tongue-in-cheek" />' , ';)' : '<img src="smiley_wink.png" alt=";)" title="Wink" />' , ':-)' : '<img src="smiley_smile.png" alt=":)" title="Smile" />' , ':-(' : '<img src="smiley_sad.png" alt=":(" title="Sad" />' , ':-D' : '<img src="smiley_lol.png" alt=":D" title="Big grin" />' , ':-|' : '<img src="smiley_skeptic.png" alt=":|" title="Skeptic" />' , ':-o' : '<img src="smiley_surprised.png" alt=":o" title="Surprised" />' , ':-P' : '<img src="smiley_tongue.png" alt=":P" title="Tongue-in-cheek" />' , ';-)' : '<img src="smiley_wink.png" alt=";)" title="Wink" />' } return emoticons defaultEmoticons=staticmethod(defaultEmoticons)
def _reassembleWorkingCopy( self, new_baseline, baseline ): # reattach the source's workflow history, try avoid a dangling ref try: new_baseline.workflow_history = PersistentMapping( baseline.workflow_history.items() ) except AttributeError: # No workflow apparently. Oh well. pass # reset wf state security directly workflow_tool = getToolByName(self.context, 'portal_workflow') wfs = workflow_tool.getWorkflowsFor( self.context ) for wf in wfs: if not isinstance( wf, DCWorkflowDefinition ): continue wf.updateRoleMappingsFor( new_baseline ) return new_baseline
class BdrAuthorizationMiddleware(SimpleItem): recheck_interval = 300 def __init__(self, url): self.recheck_interval = 300 self.lockedDownCollections = PersistentMapping() def setServiceRecheckInterval(self, seconds): self.recheck_interval = seconds @ram.cache(lambda *args, **kwargs: args[2] + str(time() // kwargs['recheck_interval'])) def getUserCollectionPaths(self, username, recheck_interval=recheck_interval): logger.debug("Get companies from middleware for ecas user: %s" % username) accessiblePaths = self.FGASRegistryAPI.getCollectionPaths(username) return accessiblePaths def authorizedUser(self, username, path): if self.lockedCollection(path): logger.warning("This collection is locked down: %s!" % path) return False accessiblePaths = self.getUserCollectionPaths(username, recheck_interval=self.recheck_interval) if path in accessiblePaths.get('paths'): return "RW" if path in accessiblePaths.get('prev_paths'): return "RO" def lockDownCollection(self, path, user): if path not in self.lockedDownCollections: self.lockedDownCollections[path] = None self.lockedDownCollections[path] = {'state': 'locked', 'ts': time(), 'user': user} def unlockCollection(self, path, user): if path not in self.lockedDownCollections: # log unlock without lock self.lockedDownCollections[path] = None self.lockedDownCollections[path] = {'state': 'unlocked', 'ts': time(), 'user': user} def lockedCollection(self, path): lockedItem = self.lockedDownCollections.get(path) return lockedItem and lockedItem['state'] == 'locked'
def _reassembleWorkingCopy(self, new_baseline, baseline): # NOTE: we rewrite this method because it is broken. It should check # for workflows not on the self.context (as it is in original), but on # the new baseline try: new_baseline.workflow_history = PersistentMapping( baseline.workflow_history.items()) except AttributeError: # No workflow apparently. Oh well. pass # reset wf state security directly workflow_tool = getToolByName(self.context, 'portal_workflow') wfs = workflow_tool.getWorkflowsFor(new_baseline) for wf in wfs: if not isinstance(wf, DCWorkflowDefinition): continue wf.updateRoleMappingsFor(new_baseline) return new_baseline
class Query(object): def __init__(self): self.searchValues = PersistentMapping() def getSearchSchema(self): return getUtility(IFindService).getSearchSchema() def getResultsSchema(self): return getUtility(IFindService).getResultsSchema() def getSearchFields(self): return self.getSearchSchema().getFields() def getResultFields(self): return self.getResultsSchema().getFields() def getCriterionValue(self, name): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): return self.searchValues.get(name, None) else: raise ValueError( u'No field named %s defined in search schema' % name) def deleteCriterionValue(self, name): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): if name in self.searchValues: del self.searchValues[name] def setCriterionValue(self, name, value): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): self.searchValues[name] = value else: raise ValueError( u'No field named %s defined in search schema' % name)
class Query(object): def __init__(self): self.searchValues = PersistentMapping() def getSearchSchema(self): return getUtility(IFindService).getSearchSchema() def getResultsSchema(self): return getUtility(IFindService).getResultsSchema() def getSearchFields(self): return self.getSearchSchema().getFields() def getResultFields(self): return self.getResultsSchema().getFields() def getCriterionValue(self, name): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): return self.searchValues.get(name, None) else: raise ValueError('No field named %s defined in search schema' % name) def deleteCriterionValue(self, name): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): if name in self.searchValues: del self.searchValues[name] def setCriterionValue(self, name, value): searchSchema = self.getSearchSchema() if searchSchema.hasField(name): self.searchValues[name] = value else: raise ValueError('No field named %s defined in search schema' % name)
def _reassembleWorkingCopy(self, new_baseline, baseline): # reattach the source's workflow history, try avoid a dangling ref try: new_baseline.workflow_history = PersistentMapping( baseline.workflow_history.items()) except AttributeError: # No workflow apparently. Oh well. pass # reset wf state security directly workflow_tool = getToolByName(self.context, 'portal_workflow') wfs = workflow_tool.getWorkflowsFor(self.context) for wf in wfs: if not isinstance(wf, DCWorkflowDefinition): continue wf.updateRoleMappingsFor(new_baseline) # reattach the source's uid, this will update wc refs to point back to # the new baseline original_refs = len(new_baseline.getRefs()) original_back_refs = len(new_baseline.getBRefs()) new_baseline._setUID(baseline.UID()) new_refs = len(new_baseline.getRefs()) new_back_refs = len(new_baseline.getBRefs()) if original_refs != new_refs: self._removeDuplicateReferences(new_baseline, backrefs=False) if original_back_refs != new_back_refs: self._removeDuplicateReferences(new_baseline, backrefs=True) # reattach the source's history id, to get the previous version # ancestry histid_handler = getToolByName(self.context, 'portal_historyidhandler') huid = histid_handler.getUid(baseline) histid_handler.setUid(new_baseline, huid, check_uniqueness=False) return new_baseline
def __init__(self, elemContainer): self._container = OOBTree() self._pointers = PersistentMapping() self._elemContainer = elemContainer
def __init__(self): self._libraries = PersistentList() self._res_types = PersistentMapping() self.linkbyuid = False
class PloneKupuLibraryTool(UniqueObject, SimpleItem, KupuLibraryTool, plonedrawers.PloneDrawers): """Plone specific version of the kupu library tool""" id = TOOLNAME meta_type = "Kupu Library Tool" title = TOOLTITLE security = ClassSecurityInfo() implements(IPloneKupuLibraryTool) # protect methods provided by super class KupuLibraryTool security.declareProtected(permissions.QueryLibraries, "getLibraries", "getPortalTypesForResourceType") security.declareProtected(permissions.ManageLibraries, "addLibrary", "deleteLibraries", "updateLibraries", "moveUp", "moveDown") security.declareProtected(permissions.ManageLibraries, "addResourceType", "updateResourceTypes", "deleteResourceTypes") def __init__(self): self._libraries = PersistentList() self._res_types = PersistentMapping() self.linkbyuid = False def manage_afterAdd(self, item, container): # We load default values here, so __init__ can still be used # in unit tests. Plus, it only makes sense to load these if # we're being added to a Plone site anyway if not len(self._libraries): for lib in _default_libraries: self.addLibrary(**lib) self._res_types.update(_default_resource_types) security.declareProtected('View', "getLinkbyuid") def getLinkbyuid(self): """Returns 'is linking by UID enabled'?""" try: return bool(self.linkbyuid) except AttributeError: return False security.declareProtected('View', "getRefBrowser") def getRefBrowser(self): """Returns True if kupu_references is in all skin layers""" return util.layer_installed(self, 'kupu_references') security.declareProtected(permissions.ManageLibraries, 'ensureReferencesLayer') def ensureReferencesLayer(self, add=False): """Called from the link tab code: we must have the kupu_references directory view at least present for the link tab to work.""" out = StringIO() util.register_layer(self, 'plone/kupu_references', 'kupu_references', out, add) security.declareProtected('View', "getCaptioning") def getCaptioning(self): """Returns True if captioning is enabled""" try: return bool(self.captioning) except AttributeError: return False security.declareProtected('View', "getTableClassnames") def getTableClassnames(self): """Return a list of classnames supported in tables""" try: return self.table_classnames except AttributeError: return ('plain', 'listing', 'vertical listing', 'listing nosort|unsorted listing') security.declareProtected('View', "getParagraphStyles") def getParagraphStyles(self): """Return a list of classnames supported by paragraphs""" try: return self.paragraph_styles except AttributeError: return _default_paragraph_styles security.declareProtected('View', "getStyleList") def getStyleList(self, field=None): """Return the styles for a field.""" gstyles = self.getParagraphStyles() if field: widget = field.widget redefine = getattr(widget, 'redefine_parastyles', False) lstyles = getattr(widget, 'parastyles', ()) else: redefine = False lstyles = [] result = [] __traceback_info__ = (gstyles, lstyles) if redefine: styles = lstyles else: styles = list(gstyles) + list(lstyles) for style in styles: parts = style.split('|',1)+['',''] value = parts[1] content = parts[0] result.append({'value':value, 'content':content}) return result security.declareProtected('View', "filterToolbar") def filterToolbar(self, context, field=None): return helpers.ButtonFilter(self, context, field) security.declareProtected('View', "getFilterOptions") def getFilterOptions(self, field=None): filters = helpers.FILTERS config = self._getToolbarFilterOptions() res = [] for (id, title, default, klass) in filters: cfg = config.get(id, {}) visible = cfg.get('visible', default) expr = cfg.get('override', None) if expr is not None: expr = expr.text res.append(dict(id=id, title=title, visible=visible, override=expr, classname=klass)) return res security.declareProtected(permissions.ManageLibraries, "set_toolbar_filters") def set_toolbar_filters(self, filters, globalfilter, REQUEST=None): """Set the toolbar filtering filter is a list of records with: id, checked, expression""" DEFAULTS = helpers.FILTERDICT def nonstandard(f): expr = f['override'] id = f['id'] visible = bool(f.get('visible', False)) return expr != '' or visible != DEFAULTS.get(id, False) cleaned = [ f for f in filters if nonstandard(f) ] self._setToolbarFilters(cleaned, globalfilter) if REQUEST: REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_toolbar') security.declareProtected("View", "getGlobalButtonFilter") def getGlobalButtonFilter(self): gfilter = getattr(self, '_global_toolbar_filter', None) if gfilter is not None: return gfilter.text return '' security.declareProtected('View', "getHtmlExclusions") def getHtmlExclusions(self): try: excl = self.html_exclusions except AttributeError: excl = self.html_exclusions = _excluded_html res = [] for (t,a) in excl: if t and t[0]=='': t = [] if a and a[0]=='': a = [] res.append((t,a)) return res security.declareProtected('View', "getStyleWhitelist") def getStyleWhitelist(self): try: return self.style_whitelist except AttributeError: self.style_whitelist = _style_whitelist return self.style_whitelist security.declareProtected('View', "getClassBlacklist") def getClassBlacklist(self): return getattr(self, 'class_blacklist', []) security.declareProtected('View', "getDefaultResource") def getDefaultResource(self): return getattr(self, 'default_resource', 'linkable') security.declareProtected(permissions.ManageLibraries, "setDefaultResource") def setDefaultResource(self, resource_type): self.default_resource = resource_type security.declareProtected('View', "installBeforeUnload") def installBeforeUnload(self): return getattr(self, 'install_beforeunload', False) security.declareProtected('View', "getFiltersourceedit") def getFiltersourceedit(self): return getattr(self, 'filtersourceedit', True) security.declareProtected('View', "getAllowOriginalImageSize") def getAllowOriginalImageSize(self): return getattr(self, 'allowOriginalImageSize', False) security.declareProtected('View', 'isKupuEnabled') def isKupuEnabled(self, useragent='', allowAnonymous=False, REQUEST=None, context=None, fieldName=None): if not REQUEST: REQUEST = self.REQUEST def numerics(s): '''Convert a string into a tuple of all digit sequences ''' seq = [''] for c in s: if c.isdigit(): seq[-1] = seq[-1] + c elif seq[-1]: seq.append('') return tuple([ int(val) for val in seq if val]) # First check whether the user actually wants kupu pm = getToolByName(self, 'portal_membership') if pm.isAnonymousUser() and not allowAnonymous: return False user = pm.getAuthenticatedMember() if not pm.isAnonymousUser(): editor = user.getProperty('wysiwyg_editor') if editor and editor.lower() != 'kupu': return False # Then check whether the current content allows html if context is not None and fieldName and hasattr(context, 'getWrappedField'): field = context.getWrappedField(fieldName) if field: allowedTypes = getattr(field, 'allowable_content_types', None) if allowedTypes is not None and not 'text/html' in [t.lower() for t in allowedTypes]: return False # Then check whether their browser supports it. if not useragent: useragent = REQUEST['HTTP_USER_AGENT'] if 'BEOS' in useragent: return False def getver(s): """Extract a version number given the string which precedes it""" pos = useragent.find(s) if pos >= 0: tail = useragent[pos+len(s):].strip() verno = numerics(tail.split(' ')[0]) return verno return None try: v = getver('Opera/') if not v: v = getver('Opera ') if v: return v >= (9,0) mozillaver = getver('Mozilla/') if mozillaver > (5,0): return True elif mozillaver == (5,0): verno = getver(' rv:') if verno: return verno >= (1,3,1) verno = getver(' AppleWebKit/') if verno: return verno >= (525,1) verno = getver(' Safari/') if verno: return verno >= (522,12) verno = getver('MSIE') if verno: return verno >= (5,5) except: # In case some weird browser makes the test code blow up. pass return False security.declarePublic("getWysiwygmacros") def getWysiwygmacros(self): """Find the appropriate template to use for the kupu widget""" pm = getToolByName(self, 'portal_membership') user = pm.getAuthenticatedMember() editor = user.getProperty('wysiwyg_editor', '') if not editor: ptool = getToolByName(self, 'portal_properties') site_props = getattr(ptool, 'site_properties', None) if site_props is not None: default_editor = site_props.getProperty('default_editor', '') if default_editor: editor = default_editor if editor: editor = editor.lower() if editor=='fck editor': editor = 'editor_fck' portal = getToolByName(self, 'portal_url').getPortalObject() for path in ('%s_wysiwyg_support' % editor, '%s/wysiwyg_support' % editor, 'portal_skins/plone_wysiwyg/wysiwyg_support'): template = portal.restrictedTraverse(path, None) if template: break return template.macros security.declarePublic("forcekupu_url") def forcekupu_url(self, fieldName): args = {'kupu.convert':fieldName, 'kupu.suppress':None, 'portal_status_message':None } qs = self.query_string(args); return "%s?%s" % (self.REQUEST.URL0, qs) security.declarePublic("query_string") def query_string(self, replace={}, original=None): """ Updates 'original' dictionary by the values in the 'replace' dictionary and returns the result as url quoted query string. The 'original' dictionary defaults to 'REQUEST.form' if no parameter is passed to it. Keys in the 'replace' dictionary with a value of 'None' (or _.None in DTML) will be deleted from 'original' dictionary before being quoted. The original 'REQUEST.form' will remain unchanged. """ # Based on code by Grégoire Weber if original is None: query = self.REQUEST.form.copy() else: query = original.copy() # delete key/value pairs if value is None for k,v in replace.items(): if v is None: if query.has_key(k): del query[k] del replace[k] # update dictionary query.update(replace) qs = '&'.join(["%s=%s" % (quote_plus(str(k)), quote_plus(str(v))) for k,v in query.items()]) return qs security.declareProtected("View", "url_plus_query") def url_plus_query(self, url, query=None): """Adds query segment to an existing URL. Existing query parameters are may be overridden by query, otherwise they are preserved. """ if query is None: query = {} parts = url.split('?', 1) oldargs = {} if len(parts) > 1: for arg in parts[1].split('&'): k,v = [unquote_plus(s) for s in arg.split('=',1)] oldargs[k] = v return "%s?%s" % (parts[0], self.query_string(query, oldargs)) security.declareProtected('View', 'kupuUrl') def kupuUrl(self, url, query=None): """Generate a url which includes resource_type and instance""" request = self.REQUEST resource_type = request.get('resource_type', 'mediaobject') instance = request.get('instance', None) newquery = { 'instance':instance, 'resource_type':resource_type } if query is not None: newquery.update(query) return self.url_plus_query(url, newquery) security.declareProtected('View', "getCookedLibraries") def getCookedLibraries(self, context): """Return a list of libraries with our own parameters included. The library with id 'search' is excluded from this list.""" libraries = [l for l in self.getLibraries(context) if not l['id'].startswith('_')] default_library = getattr(self, '_default_library', '') for l in libraries: l['src'] = self.kupuUrl(l['src']) l['selected'] = l['id']==default_library or None return libraries security.declareProtected('View', "getSingleLibrary") def getSingleLibrary(self, context, id): """Return the library with id=search or None""" libraries = [l for l in self.getLibraries(context) if l['id']==id] for l in libraries: l['src'] = self.kupuUrl(l['src']) if libraries: return libraries[0] return None # ZMI views manage_options = (SimpleItem.manage_options[1:] + ( dict(label='Config', action='kupu_config'), dict(label='Libraries', action='zmi_libraries'), dict(label='Resource types', action='zmi_resource_types'), dict(label='Documentation', action='zmi_docs'), )) security.declarePublic('scanIds') def scanIds(self): """Finds the relevant source files and the doller/Id/dollar strings they contain""" return scanner.scanIds() security.declarePublic('scanKWS') def scanKWS(self): """Check that kupu_wysiwyg_support is up to date""" return scanner.scanKWS() security.declarePublic('docs') def docs(self): """Returns Kupu docs formatted as HTML""" docpath = os.path.join(package_home(kupu_globals), 'doc') f = open(os.path.join(docpath, 'PLONE2.txt'), 'r') _docs = f.read() return _docs security.declareProtected(permissions.ManageLibraries, "link_migration") def link_migration(self, action=None): """Do link checking or conversion, a little bit at a time""" if action is None: action = self.REQUEST.form.get('button', '') commit = self.REQUEST.form.get('commit', False) migrator = html2captioned.Migration(self) if action=='continue': migrator.restoreState() res = migrator.docontinue() return migrator.getInfo() elif action=='status': try: migrator.restoreState() except KeyError: return "state cleared" return migrator.status() elif action=='query': migrator.initFromRequest() return migrator.mkQuery() elif commit: migrator.initCommit() return migrator.getInfo() else: migrator.initFromRequest() return migrator.getInfo() security.declareProtected(permissions.ManageLibraries, "zmi_links") zmi_links = PageTemplateFile("zmi_links.pt", globals()) zmi_links.title = 'kupu link maintenance' security.declareProtected(permissions.ManageLibraries, "zmi_toolbar") zmi_toolbar = PageTemplateFile("zmi_toolbar.pt", globals()) zmi_toolbar.title = 'kupu toolbar customisation' security.declareProtected(permissions.ManageLibraries, "zmi_docs") zmi_docs = PageTemplateFile("zmi_docs.pt", globals()) zmi_docs.title = 'kupu configuration documentation' security.declareProtected(permissions.ManageLibraries, "kupu_config") kupu_config = PageTemplateFile("kupu_config.pt", globals()) kupu_config.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_libraries") zmi_libraries = PageTemplateFile("libraries.pt", globals()) zmi_libraries.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_resource_types") zmi_resource_types = PageTemplateFile("resource_types.pt", globals()) zmi_resource_types.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_get_libraries") def zmi_get_libraries(self): """Return the libraries sequence for the ZMI view""" #return () def text(value): return getattr(value, 'text', value) return [dict([(key, text(value)) for key, value in lib.items()]) for lib in self._libraries] security.declareProtected(permissions.ManageLibraries, "zmi_add_library") def zmi_add_library(self, id, title, uri, src, icon, REQUEST): """Add a library through the ZMI""" self.addLibrary(id, title, uri, src, icon) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_update_libraries") def zmi_update_libraries(self, libraries, REQUEST): """Update libraries through the ZMI""" self.updateLibraries(libraries) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_delete_libraries") def zmi_delete_libraries(self, indices, REQUEST): """Delete libraries through the ZMI""" self.deleteLibraries(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_move_up") def zmi_move_up(self, indices, REQUEST): """Move libraries up through the ZMI""" self.moveUp(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_move_down") def zmi_move_down(self, indices, REQUEST): """Move libraries down through the ZMI""" self.moveDown(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declarePublic("zmi_get_default_library") def zmi_get_default_library(self): """Return the default selected library for the ZMI view""" return getattr(self, '_default_library', '') security.declareProtected(permissions.ManageLibraries, "zmi_set_default_library") def zmi_set_default_library(self, defid=''): """Return the libraries sequence for the ZMI view""" self._default_library = defid security.declareProtected(permissions.ManageLibraries, "zmi_get_type_mapping") def zmi_get_type_mapping(self): """Return the type mapping for the ZMI view: Old version of code. Returns name,types pairs plus a dummy""" return [(t.name, t.types) for t in self.zmi_get_resourcetypes()] + [('',())] security.declareProtected(permissions.ManageLibraries, "export_resource_types") def export_resource_types(self): """Build a list of resource types formatted for export. 'blacklist' type lists are inverted so the listed types are the ones we don't want. """ types = self.get_resourcetypes() typetool = getToolByName(self, 'portal_types') portal_types = dict([ (t.id, 1) for t in typetool.listTypeInfo()]) for t in types: if t.newtype: t.types = self.invertTypeList(t.types) t.mode = 'blacklist' else: t.mode = 'whitelist' return types security.declareProtected("View", "get_resourcetypes") def get_resourcetypes(self): """Return the type mapping, but without the ZMI dummy entry""" keys = self._res_types.keys() keys.sort() real = [] for name in keys: value = self._res_types[name] wrapped = Object(name=name, types=tuple(value), newtype=self.getNewTypeHandler(name)) real.append(wrapped) return real security.declareProtected("View", "zmi_get_resourcetypes") def zmi_get_resourcetypes(self): """Return the type mapping for the ZMI view""" real = self.get_resourcetypes() real.append(Object(name='', types=())) return real security.declareProtected(permissions.ManageLibraries, "zmi_update_resource_types") def zmi_update_resource_types(self, type_info=None, preview_action=None, default_resource=None, REQUEST=None): """Update resource types through the ZMI""" if type_info: self.updateResourceTypes(type_info) if preview_action: self.updatePreviewActions(preview_action) if default_resource is not None: self.default_resource = default_resource if REQUEST: REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_resource_types') security.declareProtected(permissions.ManageLibraries, "zmi_delete_resource_types") def zmi_delete_resource_types(self, resource_types=None, preview_types=None, REQUEST=None): """Delete resource types through the ZMI""" if resource_types: self.deleteResourceTypes(resource_types) if preview_types: self.deletePreviewActions(preview_types) if (REQUEST): REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_resource_types') security.declareProtected("View", "getPreviewForType") def getPreviewForType(self, portal_type): action_map = getattr(self, '_preview_actions', {}) expr = action_map.get(portal_type, {}).get('expression', '') return getattr(expr, 'text', expr) security.declareProtected("View", "getNormalViewForType") def getNormalViewForType(self, portal_type): action_map = getattr(self, '_preview_actions', {}) expr = action_map.get(portal_type, {}).get('normal', '') return getattr(expr, 'text', expr) security.declareProtected("View", "getScaleFieldForType") def getScaleFieldForType(self, portal_type): action_map = getattr(self, '_preview_actions', {}) value = action_map.get(portal_type, {}).get('scalefield', 'image') return value security.declareProtected("View", "getDefaultImageType") def getDefaultImageType(self): return 'Image' security.declareProtected("View", "getDefaultScaleForType") def getDefaultScaleForType(self, portal_type = None): if not portal_type: portal_type = self.getDefaultImageType() action_map = getattr(self, '_preview_actions', {}) value = action_map.get(portal_type, {}).get('defscale', 'image_preview') return value security.declareProtected("View", "getClassesForType") def getClassesForType(self, portal_type): action_map = getattr(self, '_preview_actions', {}) return action_map.get(portal_type, {}).get('classes', ()) security.declareProtected("View", "getMediaForType") def getMediaForType(self, portal_type): action_map = getattr(self, '_preview_actions', {}) value = action_map.get(portal_type, {}).get('mediatype', 'image') return value security.declareProtected(permissions.ManageLibraries, "set_html_exclusions") def set_html_exclusions(self, exclusions): """Set the html_exclusions. Expects a list/tuple of 2-tuples [(tags,attrs),...] """ excl = [] for (tags,attrs) in exclusions: if len(tags)==1 and tags[0]=="": tags = [] if len(attrs)==1 and attrs[0]=="": attrs = [] excl.append((tags, attrs)) self.html_exclusions = excl security.declareProtected("View", "get_stripped_tags") def get_stripped_tags(self): """Returns a list of tags to be stripped""" stripped = [] for (tags, attrs) in self.getHtmlExclusions(): if not attrs: stripped.extend(tags) return stripped security.declareProtected(permissions.ManageLibraries, "set_stripped_tags") def set_stripped_tags(self, stripped): """Sets a list of tags to be stripped""" exclusions = [(tags, attrs) for (tags, attrs) in self.getHtmlExclusions() if attrs] exclusions.append((tuple(stripped), ())) self.set_html_exclusions(exclusions) security.declareProtected('View', "get_stripped_attributes") def get_stripped_attributes(self): """Returns a list of attributes to be stripped""" stripped = [] for (tags, attrs) in self.getHtmlExclusions(): if not tags: stripped.extend(attrs) return stripped security.declareProtected(permissions.ManageLibraries, "set_stripped_attributes") def set_stripped_attributes(self, stripped): """Sets a list of attributes to be stripped""" exclusions = [(tags, attrs) for (tags, attrs) in self.html_exclusions if tags] exclusions.append(((), tuple(stripped))) self.set_html_exclusions(exclusions) security.declareProtected('View', "get_stripped_combinations") def get_stripped_combinations(self): """Returns a list of tag/attribute combinations to be stripped""" stripped = [(tags, attrs) for (tags, attrs) in self.getHtmlExclusions() if tags and attrs] return stripped security.declareProtected(permissions.ManageLibraries, "set_stripped_combinations") def set_stripped_combinations(self, stripped): """Sets a list of tag/attribute pairs to be stripped""" exclusions = [(tags, attrs) for (tags, attrs) in self.getHtmlExclusions() if not (tags and attrs)] self.set_html_exclusions(stripped + exclusions) security.declareProtected('View', "have_plone_filtering") def have_plone_filtering(self): return HAVE_PLONE_FILTERING security.declareProtected(permissions.ManageLibraries, "configure_kupu") def configure_kupu(self, linkbyuid=None, table_classnames=None, html_exclusions=None, style_whitelist=None, class_blacklist=None, installBeforeUnload=None, parastyles=None, refbrowser=None, captioning=None, filterSourceEdit=None, allowOriginalImageSize=None, REQUEST=None): """Delete resource types through the ZMI""" if linkbyuid is not None: self.linkbyuid = bool(linkbyuid) if table_classnames is not None: self.table_classnames = [t for t in table_classnames if t] if installBeforeUnload is not None: self.install_beforeunload = bool(installBeforeUnload) if filterSourceEdit is not None: self.filtersourceedit = bool(filterSourceEdit) if allowOriginalImageSize is not None: self.allowOriginalImageSize = bool(allowOriginalImageSize) if parastyles is not None: self.paragraph_styles = [ line.strip() for line in parastyles if line.strip() ] if html_exclusions is not None: newex = html_exclusions[-1] html_exclusions = html_exclusions[:-1] html_exclusions = [ (tuple(h.get('tags', ())), tuple(h.get('attributes', ()))) for h in html_exclusions if h.get('keep')] tags = newex.get('tags', '').replace(',',' ').split() attr = newex.get('attributes', '').replace(',',' ').split() if tags or attr: html_exclusions.append((tuple(tags), tuple(attr))) self.set_html_exclusions(html_exclusions) if style_whitelist is not None: self.style_whitelist = list(style_whitelist) if class_blacklist is not None: self.class_blacklist = list(class_blacklist) if refbrowser is not None: out = StringIO() if refbrowser: self.ensureReferencesLayer(True); else: util.unregister_layers(self, ['kupu_references'], out) # Force compressed javascript to be recomputed. try: self.portal_javascripts.cookResources() except AttributeError: pass if captioning is not None: self.captioning = bool(captioning) if REQUEST: REQUEST.RESPONSE.redirect(self.absolute_url() + '/kupu_config')
class Concepts(SimpleItem, session_manager): """ Concepts """ meta_type = CONCEPTS_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = 'misc_/NaayaThesaurus/concepts.gif' manage_options = ( {'label':'Properties', 'action':'properties_html'}, {'label':'Management', 'action':'concepts_html'}, {'label':'Import', 'action':'import_html'}, {'label':'Statistics', 'action':'statistics_html'}, {'label':'Undo', 'action':'manage_UndoForm'},) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.concepts = PersistentMapping() #basic properties security.declareProtected(view_management_screens, 'manageBasicProperties') def manageBasicProperties(self, title='', REQUEST=None): """ manage basic properties for Concepts """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans('Saved changes.') return REQUEST.RESPONSE.redirect('properties_html') #concepts management def __add_concept(self, concept_id): #create a new item item = ConceptItem(concept_id) self.concepts[concept_id] = item self.catalog.CatalogObject(item) def __update_concept(self, concept_id, old_concept_id): #modify an item item = self.concepts.get(old_concept_id) if item is not None: self.__delete_concept(old_concept_id) self.__add_concept(concept_id) def __delete_concept(self, ids): #delete 1 or more items ids = th_utils().utConvertToList(ids) collection = self.get_concepts() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] #concept constraints security.declareProtected(view_management_screens, 'getIdsList') def getIdsList(self, ids, all=0): """ """ if all: return self.concepts.keys() return th_utils().getIdsList(ids) #concepts getters def get_concepts(self): #get all concepts return self.concepts def get_concepts_sorted(self): #get all concepts sorted return th_utils().utSortObjsListByAttr(self.concepts.values(), 'concept_id', 0) def get_concept_by_id(self, concept_id): #get an item try: return self.concepts[concept_id] except: return None def get_concept_item_data(self, concept_id): #get an item data item = self.get_concept_by_id(concept_id) if item is not None: return ['update', item.concept_id] else: return ['add', ''] #concepts api security.declareProtected(view_management_screens, 'manage_add_concept') def manage_add_concept(self, concept_id='', REQUEST=None): """ manage concepts """ if not concept_id: concept_id = th_utils().utGenRandomId() self.__add_concept(concept_id) if REQUEST: self.setSessionInfoTrans('Record added.') REQUEST.RESPONSE.redirect('concepts_html') security.declareProtected(view_management_screens, 'manage_update_concept') def manage_update_concept(self, concept_id='', old_concept_id='', REQUEST=None): """ update concept """ self.__update_concept(concept_id, old_concept_id) if REQUEST: self.setSessionInfoTrans('Record updated.') REQUEST.RESPONSE.redirect('concepts_html') security.declareProtected(view_management_screens, 'manage_delete_concepts') def manage_delete_concepts(self, ids=[], delete_all='', REQUEST=None): """ delete concepts """ del_count = 0 #TODO: uncomment when Groups will be implemented # if delete_all: ids = self.getIdsList(ids, 1) # else: ids = self.getIdsList(ids) if delete_all: ids = self.getIdsList(ids, 1) ids = th_utils().utConvertToList(ids) self.__delete_concept(ids) #delete all related data for concept_id in ids: #delete all related terms query = [('meta_type',TERM_ITEM_METATYPE), ('concept_id',concept_id)] term_list = self.catalog.searchCatalog(query) term_folder = self.getTermsFolder() for term_ob in term_list: term_folder.manage_delete_terms(['%s###%s' % (term_ob.concept_id, term_ob.langcode)]) del_count += 1 #delete all related definitions query = [('meta_type',DEFINITION_ITEM_METATYPE), ('concept_id',concept_id)] def_list = self.catalog.searchCatalog(query) def_folder = self.getDefinitionsFolder() for def_ob in def_list: def_folder.manage_delete_definitions(['%s###%s' % (def_ob.concept_id, def_ob.langcode)]) del_count += 1 #delete all related altterms query = [('meta_type',ALTTERM_ITEM_METATYPE), ('concept_id',concept_id)] alt_list = self.catalog.searchCatalog(query) alt_folder = self.getAltTermsFolder() for alt_ob in alt_list: alt_folder.manage_delete_altterms(['%s###%s' % (alt_ob.concept_id, alt_ob.langcode)]) del_count += 1 #delete all related scopes query = [('meta_type',SCOPE_ITEM_METATYPE), ('concept_id',concept_id)] scope_list = self.catalog.searchCatalog(query) scope_folder = self.getScopeNotesFolder() for scope_ob in scope_list: scope_folder.manage_delete_scopes(['%s###%s' % (scope_ob.concept_id, scope_ob.langcode)]) del_count += 1 #delete all related theme relations query = [('meta_type',THEME_RELATION_ITEM_METATYPE), ('concept_id',concept_id)] threl_list = self.catalog.searchCatalog(query) threl_folder = self.getThemeRelationsFolder() for threl_ob in threl_list: threl_folder.manage_delete_threlations(['%s###%s' % (threl_ob.concept_id, threl_ob.theme_id)]) del_count += 1 #delete all related concept relations query = [('meta_type',CONCEPT_RELATION_ITEM_METATYPE), ('concept_id',concept_id)] cprel_list = self.catalog.searchCatalog(query) cprel_folder = self.getConceptRelationsFolder() for cprel_ob in cprel_list: cprel_folder.manage_delete_relations(['%s###%s###%s' % (cprel_ob.concept_id, cprel_ob.relation_id, cprel_ob.relation_type)]) del_count += 1 query = [('meta_type',CONCEPT_RELATION_ITEM_METATYPE), ('relation_id',concept_id)] cprel_list = self.catalog.searchCatalog(query) cprel_folder = self.getConceptRelationsFolder() for cprel_ob in cprel_list: cprel_folder.manage_delete_relations(['%s###%s###%s' % (cprel_ob.concept_id, cprel_ob.relation_id, cprel_ob.relation_type)]) del_count += 1 if REQUEST: self.setSessionInfoTrans('Selected records deleted.', ('${del_count} related records were deleted.', {'del_count': del_count}, )) REQUEST.RESPONSE.redirect('concepts_html') security.declareProtected(view_management_screens, 'getConceptItemData') def getConceptItemData(self): """ return a concept based on its ID """ return self.get_concept_item_data(self.REQUEST.get('concept_id', None)) #import related def skos_import(self, file, REQUEST=None): """ """ parser = concept_parser() #parse the SKOS information chandler = parser.parseHeader(file) if chandler is None: if REQUEST: self.setSessionErrorsTrans('Parsing error. The file could not be parsed.') return REQUEST.RESPONSE.redirect('import_html') #get data concept_info = chandler.getConcepts() theme_relation_info = chandler.getThemes() #info count_concepts = 0 count_themes_rel = 0 #set Concepts for id, data in concept_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id != '': count_concepts += 1 self.__add_concept(concept_id) #set ThemeRelations for id, data in theme_relation_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] theme_id = data['theme_id'].encode('utf-8').split('/')[-1] if concept_id and theme_id: count_themes_rel += 1 theme_relations_folder = self.getThemeRelationsFolder() theme_relations_folder.manage_add_threlation(concept_id, theme_id) if REQUEST: self.setSessionInfoTrans(['File imported successfully.', ('Concepts added: ${count_concepts}', {'count_concepts': count_concepts}, ), ('ThemeRelations added: ${count_themes_rel}', {'count_themes_rel': count_themes_rel}, )]) return REQUEST.RESPONSE.redirect('import_html') #management tabs security.declareProtected(view_management_screens, 'properties_html') properties_html = PageTemplateFile("%s/zpt/Concepts/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'concepts_html') concepts_html = PageTemplateFile("%s/zpt/Concepts/concepts" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'statistics_html') statistics_html = PageTemplateFile("%s/zpt/Concepts/statistics" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'import_html') import_html = PageTemplateFile("%s/zpt/Concepts/import" % NAAYATHESAURUS_PATH, globals())
class Definitions(SimpleItem, session_manager): """ Definitions """ meta_type = DEFINITIONS_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = 'misc_/NaayaThesaurus/definitions.gif' manage_options = ( {'label':'Properties', 'action':'properties_html'}, {'label':'Management', 'action':'definitions_html'}, {'label':'Statistics', 'action':'statistics_html'}, {'label':'Undo', 'action':'manage_UndoForm'},) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.definitions = PersistentMapping() #basic properties security.declareProtected(view_management_screens, 'manageBasicProperties') def manageBasicProperties(self, title='', REQUEST=None): """ manage basic properties for Definitions """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans('Saved changes.') return REQUEST.RESPONSE.redirect('properties_html') #definitions management def __add_definition(self, concept_id, langcode, definition, source_id): #create a new item item = DefinitionItem(concept_id, langcode, definition, source_id) self.definitions[(concept_id, langcode)] = item self.catalog.CatalogObject(item) def __update_definition(self, concept_id, old_concept_id, langcode, old_langcode, definition, source_id): #modify an item item = self.definitions.get((old_concept_id, old_langcode)) if item is not None: self.__delete_definition((old_concept_id, old_langcode)) self.__add_definition(concept_id, langcode, definition, source_id) security.declareProtected(view_management_screens, 'update_source_id') def update_source_id(self, concept_id, langcode, source_id): """ update the source_id list """ definition_ob = self.get_definition_by_id((concept_id, langcode)) if definition_ob.source_id: upd_source_id = "%s %s" % (definition_ob.source_id, source_id) else: upd_source_id = source_id self.__update_definition(concept_id, concept_id, langcode, langcode, definition_ob.definition, upd_source_id) def __delete_definition(self, ids): #delete 1 or more items if type(ids) != type((1,1)): ids = th_utils().utConvertToList(ids) else: ids = [ids] collection = self.get_definitions() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] #definition constraints security.declareProtected(view_management_screens, 'checkDefinition') def checkDefinition(self, concept_id): """ """ if self.getConceptsFolder().get_concept_by_id(concept_id): return 1 return 0 security.declareProtected(view_management_screens, 'getIdsList') def getIdsList(self, ids, all=0): """ """ if all: return self.definitions.keys() return th_utils().getIdsList(ids) #definitions getters def get_definitions(self): #get all definitions return self.definitions def get_definitions_sorted(self): #get all definitions sorted return th_utils().utSortObjsListByAttr(self.definitions.values(), 'concept_id', 0) def get_definition_by_id(self, id): #get an item try: return self.definitions[id] except: return None def get_definition_item_data(self, concept_id, langcode, orig_concept_id, orig_langcode, definition, source_id): #get an item data item = self.get_definition_by_id((orig_concept_id, orig_langcode)) if item is not None: if definition is None: definition = item.definition if source_id is None: source_id = item.source_id return ['update', concept_id, langcode, definition, source_id, orig_concept_id, orig_langcode] else: return ['add', concept_id, langcode, definition, source_id, '', ''] #definitions api security.declareProtected(view_management_screens, 'manage_add_definition') def manage_add_definition(self, concept_id='', langcode='', definition='', source_id='', REQUEST=None): """ manage definitions """ err = 0 if self.checkDefinition(concept_id): self.__add_definition(concept_id, langcode, definition, source_id) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionDefinition(definition) self.setSessionSourceId(source_id) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) else: self.setSessionInfoTrans('Record added.') REQUEST.RESPONSE.redirect('definitions_html') security.declareProtected(view_management_screens, 'manage_update_definition') def manage_update_definition(self, concept_id='', old_concept_id='', langcode='', old_langcode='', definition='', source_id='', REQUEST=None): """ update definition """ err = 0 if self.checkDefinition(concept_id): self.__update_definition(concept_id, old_concept_id, langcode, old_langcode, definition, source_id) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionDefinition(definition) self.setSessionSourceId(source_id) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) REQUEST.RESPONSE.redirect('definitions_html?concept_id=%s&langcode=%s' % (old_concept_id, old_langcode)) else: self.setSessionInfoTrans('Record updated.') REQUEST.RESPONSE.redirect('definitions_html') security.declareProtected(view_management_screens, 'manage_delete_definitions') def manage_delete_definitions(self, ids=[], delete_all='', REQUEST=None): """ delete definitions """ if delete_all: ids = self.getIdsList(ids, 1) else: ids = self.getIdsList(ids) self.__delete_definition(ids) if REQUEST: self.setSessionInfoTrans('Selected records deleted.') REQUEST.RESPONSE.redirect('definitions_html') security.declareProtected(view_management_screens, 'getDefinitionItemData') def getDefinitionItemData(self): """ return a definition based on its ID """ if self.isSessionConceptId(): concept_id = self.getSessionConceptId() langcode = self.getSessionLangcode() definition = self.getSessionDefinition() source_id = self.getSessionSourceId() orig_concept_id = self.REQUEST.get('concept_id', None) orig_langcode = self.REQUEST.get('langcode', None) else: concept_id = self.REQUEST.get('concept_id', self.getSessionConceptId()) langcode = self.REQUEST.get('langcode', self.getSessionLangcode()) definition = self.getSessionDefinition() source_id = self.getSessionSourceId() orig_concept_id = concept_id orig_langcode = langcode self.delSessionConceptId() self.delSessionLangcode() self.delSessionDefinition() self.delSessionSourceId() return self.get_definition_item_data(concept_id, langcode, orig_concept_id, orig_langcode, definition, source_id) return self.get_definition_item_data(self.REQUEST.get('concept_id', None), self.REQUEST.get('langcode', None)) #statistics def getAllDef(self): query = [('meta_type',DEFINITION_ITEM_METATYPE)] return self.catalog.searchCatalog(query) def getDefNumber(self): return len(self.getAllDef()) def getDefTransNumber(self): results = {} for def_ob in self.getAllDef(): try: tr_count = results[def_ob.langcode][0] except: tr_count = 0 tr_count += 1 try: src_count = results[def_ob.langcode][1] except: src_count = 0 if def_ob.source_id: src_count += 1 results[def_ob.langcode] = (tr_count, src_count) return results def getDefWithSource(self): count = 0 for def_ob in self.getAllDef(): if len(def_ob.source_id): count += 1 return count def getEmptyDefs(self): empty_count = 0 for def_ob in self.getAllDef(): if not def_ob.definition: empty_count += 1 return empty_count #management tabs security.declareProtected(view_management_screens, 'properties_html') properties_html = PageTemplateFile("%s/zpt/Definitions/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'definitions_html') definitions_html = PageTemplateFile("%s/zpt/Definitions/definitions" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'statistics_html') statistics_html = PageTemplateFile("%s/zpt/Definitions/statistics" % NAAYATHESAURUS_PATH, globals())
class Terms(SimpleItem, session_manager): """ Terms """ meta_type = TERMS_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = 'misc_/NaayaThesaurus/terms.gif' manage_options = ( {'label':'Properties', 'action':'properties_html'}, {'label':'Management', 'action':'terms_html'}, {'label':'Import', 'action':'import_html'}, {'label':'Statistics', 'action':'statistics_html'}, {'label':'Undo', 'action':'manage_UndoForm'},) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.terms = PersistentMapping() #basic properties security.declareProtected(view_management_screens, 'manageBasicProperties') def manageBasicProperties(self, title='', REQUEST=None): """ manage basic properties for Terms """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans('Saved changes.') return REQUEST.RESPONSE.redirect('properties_html') #terms management def __add_term(self, concept_id, langcode, concept_name, source_id): #create a new item item = TermItem(concept_id, langcode, concept_name, source_id) self.terms[(concept_id, langcode)] = item self.catalog.CatalogObject(item) def __update_term(self, concept_id, old_concept_id, langcode, old_langcode, concept_name, source_id): #modify an item item = self.terms.get((old_concept_id, old_langcode)) if item is not None: self.__delete_term((old_concept_id, old_langcode)) self.__add_term(concept_id, langcode, concept_name, source_id) security.declareProtected(view_management_screens, 'update_source_id') def update_source_id(self, concept_id, langcode, source_id): """ update the source_id list """ term_ob = self.get_term_by_id((concept_id, langcode)) if term_ob.source_id: upd_source_id = "%s %s" % (term_ob.source_id, source_id) else: upd_source_id = source_id self.__update_term(concept_id, concept_id, langcode, langcode, term_ob.concept_name, upd_source_id) def __delete_term(self, ids): #delete 1 or more items if type(ids) != type((1,1)): ids = th_utils().utConvertToList(ids) else: ids = [ids] collection = self.get_terms() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] #term constraints security.declareProtected(view_management_screens, 'checkTerm') def checkTerm(self, concept_id): """ """ if self.getConceptsFolder().get_concept_by_id(concept_id): return 1 return 0 security.declareProtected(view_management_screens, 'getIdsList') def getIdsList(self, ids, all=0): """ """ if all: return self.terms.keys() return th_utils().getIdsList(ids) #terms getters def get_terms(self): #get all terms return self.terms def get_terms_sorted(self): #get all terms sorted return th_utils().utSortObjsListByAttr(self.terms.values(), 'concept_id', 0) def get_term_by_id(self, id): #get an item try: return self.terms[id] except: return None def get_term_item_data(self, concept_id, langcode, orig_concept_id, orig_langcode, concept_name, source_id): #get an item data item = self.get_term_by_id((orig_concept_id, orig_langcode)) if item is not None: if concept_name is None: concept_name = item.concept_name if source_id is None: source_id = item.source_id return ['update', concept_id, langcode, concept_name, source_id, orig_concept_id, orig_langcode] else: return ['add', concept_id, langcode, concept_name, source_id, '', ''] #terms api security.declareProtected(view_management_screens, 'manage_add_term') def manage_add_term(self, concept_id='', langcode='', concept_name='', source_id='', REQUEST=None): """ manage terms """ err = 0 if self.checkTerm(concept_id): self.__add_term(concept_id, langcode, concept_name, source_id) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionConceptName(concept_name) self.setSessionSourceId(source_id) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) else: self.setSessionInfoTrans('Record added.') REQUEST.RESPONSE.redirect('terms_html') security.declareProtected(view_management_screens, 'manage_update_term') def manage_update_term(self, concept_id='', old_concept_id='', langcode='', old_langcode='', concept_name='', source_id='', REQUEST=None): """ update term """ err = 0 if self.checkTerm(concept_id): self.__update_term(concept_id, old_concept_id, langcode, old_langcode, concept_name, source_id) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionConceptName(concept_name) self.setSessionSourceId(source_id) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) REQUEST.RESPONSE.redirect('terms_html?concept_id=%s&langcode=%s' % (old_concept_id, old_langcode)) else: self.setSessionInfoTrans('Record updated.') REQUEST.RESPONSE.redirect('terms_html') security.declareProtected(view_management_screens, 'manage_delete_terms') def manage_delete_terms(self, ids=[], delete_all='', REQUEST=None): """ delete terms """ if delete_all: ids = self.getIdsList(ids, 1) else: ids = self.getIdsList(ids) self.__delete_term(ids) if REQUEST: self.setSessionInfoTrans('Selected records deleted.') REQUEST.RESPONSE.redirect('terms_html') security.declareProtected(view_management_screens, 'getTermItemData') def getTermItemData(self): """ return a term based on its ID """ if self.isSessionConceptId(): concept_id = self.getSessionConceptId() langcode = self.getSessionLangcode() concept_name = self.getSessionConceptName() source_id = self.getSessionSourceId() orig_concept_id = self.REQUEST.get('concept_id', None) orig_langcode = self.REQUEST.get('langcode', None) else: concept_id = self.REQUEST.get('concept_id', self.getSessionConceptId()) langcode = self.REQUEST.get('langcode', self.getSessionLangcode()) concept_name = self.getSessionConceptName() source_id = self.getSessionSourceId() orig_concept_id = concept_id orig_langcode = langcode self.delSessionConceptId() self.delSessionLangcode() self.delSessionConceptName() self.delSessionSourceId() return self.get_term_item_data(concept_id, langcode, orig_concept_id, orig_langcode, concept_name, source_id) #import related def skos_import(self, file, langcode, REQUEST=None): """ """ parser = term_parser() #parse the SKOS information chandler = parser.parseHeader(file) if chandler is None: if REQUEST: self.setSessionErrorsTrans('Parsing error. The file could not be parsed.') return REQUEST.RESPONSE.redirect('import_html') #get the target language skos_lang = chandler.getLanguage() if skos_lang: langcode = skos_lang.encode('utf-8') #get data pref_info = chandler.getPref() alt_info = chandler.getAlt() def_info = chandler.getDef() scope_info = chandler.getScope() src_info = chandler.getSrc() def_src_info = chandler.getDefSrc() #info count_terms = 0 err_terms = [] count_altterms = 0 err_altterms = [] count_def = 0 err_defs = [] count_scope = 0 err_scope = [] count_src = 0 err_src = [] count_def_src = 0 err_def_src = [] #set Terms for id, data in pref_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: concept_name = data['concept_name'] source_id = '' if self.checkTerm(concept_id): self.__add_term(concept_id, langcode, concept_name, source_id) count_terms += 1 else: err_terms.append(concept_id) else: err_terms.append('None') #set Alternatives terms for id, data in alt_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: alt_terms_folder = self.getAltTermsFolder() alt_name = data['alt_name'] if self.checkTerm(concept_id): alt_terms_folder.manage_add_altterm(concept_id, langcode, alt_name) count_altterms += 1 else: err_altterms.append(concept_id) else: err_altterms.append('None') #set Definitions for id, data in def_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: definitions_folder = self.getDefinitionsFolder() definition = data['definition'] if self.checkTerm(concept_id): #TODO: implement source definitions_folder.manage_add_definition(concept_id, langcode, definition, '') count_def += 1 else: err_defs.append(concept_id) else: err_defs.append('None') #set ScopeNotes for id, data in scope_info.items(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: scopenotes_folder = self.getScopeNotesFolder() scope_note = data['scope_note'] if self.checkTerm(concept_id): scopenotes_folder.manage_add_scope(concept_id, langcode, scope_note) count_scope += 1 else: err_scope.append(concept_id) else: err_scope.append('None') #set Terms sources for id, data in src_info.items(): source_id = data['source_id'] source_name = data['source_name'] concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: if self.checkTerm(concept_id): sources_folder = self.getSourceFolder() #add source record sources_folder.manage_add_source(source_id, source_name) #update term record self.update_source_id(concept_id, langcode, source_id) count_src += 1 else: err_src.append(concept_id) else: err_src.append('None') #set Definitions sources for id, data in def_src_info.items(): source_id = data['source_id'] source_name = data['source_name'] concept_id = data['concept_id'].encode('utf-8').split('/')[-1] if concept_id: if self.checkTerm(concept_id): sources_folder = self.getSourceFolder() definitions_folder = self.getDefinitionsFolder() #add source record sources_folder.manage_add_source(source_id, source_name) #update definition record definitions_folder.update_source_id(concept_id, langcode, source_id) count_def_src += 1 else: err_def_src.append(concept_id) else: err_def_src.append('None') if REQUEST: self.setSessionInfoTrans(['File imported successfully.', ('Translations added: ${count_terms}', {'count_terms': count_terms}, ), ('Alternative terms added: ${count_altterms}', {'count_altterms': count_altterms}, ), ('Definitions added: ${count_def}', {'count_def': count_def}, ), ('ScopeNotes added: ${count_scope}', {'count_scope': count_scope}, ), ('Terms Sources added: ${count_src}', {'count_src': count_src}, ), ('Definitions Sources added: ${count_def_src}', {'count_def_src': count_def_src}, ), ]) msg_err = [] if err_terms: msg_err.append(('Translations not imported because the specified concept_id does not exist: ${err_terms}', {'err_terms': th_utils().utJoinToString(err_terms, ', ')}, )) if err_altterms: msg_err.append(('Alternative terms not imported because the specified concept_id does not exist: ${err_altterms}', {'err_altterms': th_utils().utJoinToString(err_altterms, ', ')}, )) if err_defs: msg_err.append(('Definitions not imported because the specified concept_id does not exist: ${err_defs}', {'err_defs': th_utils().utJoinToString(err_defs, ', ')}, )) if err_scope: msg_err.append(('ScopeNotes not imported because the specified concept_id does not exist: ${err_scope}', {'err_scope': th_utils().utJoinToString(err_scope, ', ')}, )) if err_src: msg_err.append(('Term sources not imported because the specified concept_id does not exist: ${err_src}', {'err_src': th_utils().utJoinToString(err_src, ', ')}, )) if err_def_src: msg_err.append(('Definition sources not imported because the specified concept_id does not exist: ${err_def_src}', {'err_def_src': th_utils().utJoinToString(err_def_src, ', ')}, )) if msg_err: self.setSessionErrorsTrans(msg_err) return REQUEST.RESPONSE.redirect('import_html') #statistics def getAllTerms(self): query = [('meta_type',TERM_ITEM_METATYPE)] return self.catalog.searchCatalog(query) def getTermsNumber(self): return len(self.getAllTerms()) def getTermsTransNumber(self): results = {} for term_ob in self.getAllTerms(): try: tr_count = results[term_ob.langcode][0] except: tr_count = 0 tr_count += 1 try: src_count = results[term_ob.langcode][1] except: src_count = 0 if term_ob.source_id: src_count += 1 results[term_ob.langcode] = (tr_count, src_count) return results def getTermsWithSource(self): count = 0 for term_ob in self.getAllTerms(): if len(term_ob.source_id): count += 1 return count def getEmptyTrans(self): empty_count = 0 for term_ob in self.getAllTerms(): if not term_ob.concept_name: empty_count += 1 return empty_count #management tabs security.declareProtected(view_management_screens, 'properties_html') properties_html = PageTemplateFile("%s/zpt/Terms/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'terms_html') terms_html = PageTemplateFile("%s/zpt/Terms/terms" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'statistics_html') statistics_html = PageTemplateFile("%s/zpt/Terms/statistics" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'import_html') import_html = PageTemplateFile("%s/zpt/Terms/import" % NAAYATHESAURUS_PATH, globals())
class AltTerms(SimpleItem, session_manager): """ AltTerms """ meta_type = ALTTERMS_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = 'misc_/NaayaThesaurus/alt_terms.gif' manage_options = ( {'label':'Properties', 'action':'properties_html'}, {'label':'Management', 'action':'altterms_html'}, {'label':'Statistics', 'action':'statistics_html'}, {'label':'Undo', 'action':'manage_UndoForm'},) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.altterms = PersistentMapping() #basic properties security.declareProtected(view_management_screens, 'manageBasicProperties') def manageBasicProperties(self, title='', REQUEST=None): """ manage basic properties for AltTerms """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans('Saved changes.') return REQUEST.RESPONSE.redirect('properties_html') #alt_terms management def __add_altterm(self, concept_id, langcode, alt_name): #create a new item item = AltTermItem(concept_id, langcode, alt_name) self.altterms[(concept_id, langcode)] = item self.catalog.CatalogObject(item) def __update_altterm(self, concept_id, old_concept_id, langcode, old_langcode, alt_name): #modify an item item = self.altterms.get((old_concept_id, old_langcode)) if item is not None: self.__delete_altterm((old_concept_id, old_langcode)) self.__add_altterm(concept_id, langcode, alt_name) def __delete_altterm(self, ids): #delete 1 or more items if type(ids) != type((1,1)): ids = th_utils().utConvertToList(ids) else: ids = [ids] collection = self.get_altterms() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] #altterm constraints security.declareProtected(view_management_screens, 'checkAltTerm') def checkAltTerm(self, concept_id): """ """ if self.getConceptsFolder().get_concept_by_id(concept_id): return 1 return 0 security.declareProtected(view_management_screens, 'getIdsList') def getIdsList(self, ids, all=0): """ """ if all: return self.altterms.keys() return th_utils().getIdsList(ids) #terms getters def get_altterms(self): #get all alt_terms return self.altterms def get_altterms_sorted(self): #get all alt_terms sorted return th_utils().utSortObjsListByAttr(self.altterms.values(), 'concept_id', 0) def get_altterm_by_id(self, id): #get an item try: return self.altterms[id] except: return None def get_altterm_item_data(self,concept_id, langcode, orig_concept_id, orig_langcode, alt_name): #get an item data item = self.get_altterm_by_id((orig_concept_id, orig_langcode)) if item is not None: if alt_name is None: alt_name = item.alt_name return ['update', concept_id, langcode, alt_name, orig_concept_id, orig_langcode] else: return ['add', concept_id, langcode, alt_name, '', ''] #alt_terms api security.declareProtected(view_management_screens, 'manage_add_altterm') def manage_add_altterm(self, concept_id='', langcode='', alt_name='', REQUEST=None): """ manage alt_terms """ err = 0 if self.checkAltTerm(concept_id): self.__add_altterm(concept_id, langcode, alt_name) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionAltName(alt_name) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) else: self.setSessionInfoTrans('Record added.') REQUEST.RESPONSE.redirect('altterms_html') security.declareProtected(view_management_screens, 'manage_update_altterm') def manage_update_altterm(self, concept_id='', old_concept_id='', langcode='', old_langcode='', alt_name='', REQUEST=None): """ update alt_term """ err = 0 if self.checkAltTerm(concept_id): self.__update_altterm(concept_id, old_concept_id, langcode, old_langcode, alt_name) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionLangcode(langcode) self.setSessionAltName(alt_name) self.setSessionErrorsTrans('${concept_id} is not a valid concept ID.', concept_id=concept_id) REQUEST.RESPONSE.redirect('altterms_html?concept_id=%s&langcode=%s' % (old_concept_id, old_langcode)) else: self.setSessionInfoTrans('Record updated.') REQUEST.RESPONSE.redirect('altterms_html') security.declareProtected(view_management_screens, 'manage_delete_altterms') def manage_delete_altterms(self, ids=[], delete_all='', REQUEST=None): """ delete alt_terms """ if delete_all: ids = self.getIdsList(ids, 1) else: ids = self.getIdsList(ids) self.__delete_altterm(ids) if REQUEST: self.setSessionInfoTrans('Selected records deleted.') REQUEST.RESPONSE.redirect('altterms_html') security.declareProtected(view_management_screens, 'getAltTermItemData') def getAltTermItemData(self): """ return a term based on its ID """ if self.isSessionConceptId(): concept_id = self.getSessionConceptId() langcode = self.getSessionLangcode() alt_name = self.getSessionAltName() orig_concept_id = self.REQUEST.get('concept_id', None) orig_langcode = self.REQUEST.get('langcode', None) else: concept_id = self.REQUEST.get('concept_id', self.getSessionConceptId()) langcode = self.REQUEST.get('langcode', self.getSessionLangcode()) alt_name = self.getSessionAltName() orig_concept_id = concept_id orig_langcode = langcode self.delSessionConceptId() self.delSessionLangcode() self.delSessionAltName() return self.get_altterm_item_data(concept_id, langcode, orig_concept_id, orig_langcode, alt_name) #statistics def getAllAltTerms(self): query = [('meta_type',ALTTERM_ITEM_METATYPE)] return self.catalog.searchCatalog(query) def getAltTermsNumber(self): return len(self.getAllAltTerms()) def getAltTermsTransNumber(self): results = {} for altt_ob in self.getAllAltTerms(): try: tr_count = results[altt_ob.langcode] except: tr_count = 0 results[altt_ob.langcode] = tr_count + 1 return results def getEmptyTrans(self): empty_count = 0 for altterm_ob in self.getAllAltTerms(): if not altterm_ob.alt_name: empty_count += 1 return empty_count #management tabs security.declareProtected(view_management_screens, 'properties_html') properties_html = PageTemplateFile("%s/zpt/AltTerms/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'altterms_html') altterms_html = PageTemplateFile("%s/zpt/AltTerms/altterms" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'statistics_html') statistics_html = PageTemplateFile("%s/zpt/AltTerms/statistics" % NAAYATHESAURUS_PATH, globals())
class ConceptRelations(SimpleItem, session_manager): """ ConceptRelations """ meta_type = CONCEPT_RELATIONS_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = 'misc_/NaayaThesaurus/concept_relations.gif' manage_options = ( { 'label': 'Properties', 'action': 'properties_html' }, { 'label': 'Management', 'action': 'concept_relations_html' }, { 'label': 'Import', 'action': 'import_html' }, { 'label': 'Statistics', 'action': 'statistics_html' }, { 'label': 'Undo', 'action': 'manage_UndoForm' }, ) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.concept_relations = PersistentMapping() #basic properties security.declareProtected(view_management_screens, 'manageBasicProperties') def manageBasicProperties(self, title='', REQUEST=None): """ manage basic properties for ConceptRelations """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans('Saved changes.') return REQUEST.RESPONSE.redirect('properties_html') #concept relations management def __add_relation(self, concept_id, relation_id, relation_type): #create a new item item = ConceptRelationsItem(concept_id, relation_id, relation_type) self.concept_relations[(concept_id, relation_id, relation_type)] = item self.catalog.CatalogObject(item) def __update_relation(self, concept_id, old_concept_id, relation_id, old_relation_id, relation_type, old_relation_type): #modify an item l_old_id = (old_concept_id, old_relation_id, old_relation_type) item = self.concept_relations.get(l_old_id) if item is not None: self.__delete_relation(l_old_id) self.__add_relation(concept_id, relation_id, relation_type) def __delete_relation(self, ids): #delete 1 or more items if type(ids) != type((1, 1)): ids = th_utils().utConvertToList(ids) else: ids = [ids] collection = self.get_relations() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] #concept relations constraints security.declareProtected(view_management_screens, 'checkCpRel') def checkCpRel(self, concept_id): """ """ if self.getConceptsFolder().get_concept_by_id(concept_id): return 1 return 0 security.declareProtected(view_management_screens, 'getIdsList') def getIdsList(self, ids, all=0): """ """ if all: return self.concept_relations.keys() return th_utils().getIdsList(ids) #relations getters def get_relations(self): #get all relations return self.concept_relations def get_relations_sorted(self): #get all relations sorted return th_utils().utSortObjsListByAttr(self.concept_relations.values(), 'concept_id', 0) def get_relation_by_id(self, id): #get an item try: return self.concept_relations[id] except: return None def get_relations_item_data(self, concept_id, relation_id, relation_type, orig_concept_id, orig_relation_id, orig_relation_type): #get an item data item = self.get_relation_by_id( (orig_concept_id, orig_relation_id, orig_relation_type)) if item is not None: return [ 'update', concept_id, relation_id, relation_type, orig_concept_id, orig_relation_id, orig_relation_type ] else: return ['add', concept_id, relation_id, relation_type, '', '', ''] #concept relations api security.declareProtected(view_management_screens, 'manage_add_relation') def manage_add_relation(self, concept_id='', relation_id='', relation_type='', REQUEST=None): """ manage relations """ err = 0 if self.checkCpRel(concept_id) and self.checkCpRel(relation_id): self.__add_relation(concept_id, relation_id, relation_type) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionRelationId(relation_id) self.setSessionRelationType(relation_type) self.setSessionErrorsTrans( '${concept_id} is not a valid concept ID.', concept_id=concept_id) else: self.setSessionInfoTrans('Record added.') REQUEST.RESPONSE.redirect('concept_relations_html') security.declareProtected(view_management_screens, 'manage_update_relation') def manage_update_relation(self, concept_id='', old_concept_id='', relation_id='', old_relation_id='', relation_type='', old_relation_type='', REQUEST=None): """ update relation """ err = 0 if self.checkCpRel(concept_id) and self.checkCpRel(relation_id): self.__update_relation(concept_id, old_concept_id, relation_id, old_relation_id, relation_type, old_relation_type) else: err = 1 if REQUEST: if err: self.setSessionConceptId(concept_id) self.setSessionRelationId(relation_id) self.setSessionRelationType(relation_type) self.setSessionErrorsTrans( '${concept_id} is not a valid concept ID.', concept_id=concept_id) REQUEST.RESPONSE.redirect( 'concept_relations_html?concept_id=%s&relation_id=%s&relation_type=%s' % (old_concept_id, old_relation_id, old_relation_type)) else: self.setSessionInfoTrans('Record updated.') REQUEST.RESPONSE.redirect('concept_relations_html') security.declareProtected(view_management_screens, 'manage_delete_relations') def manage_delete_relations(self, ids=[], delete_all='', REQUEST=None): """ delete relations """ if delete_all: ids = self.getIdsList(ids, 1) else: ids = self.getIdsList(ids) self.__delete_relation(ids) if REQUEST: self.setSessionInfoTrans('Selected records deleted.') REQUEST.RESPONSE.redirect('concept_relations_html') security.declareProtected(view_management_screens, 'getRelationItemData') def getRelationItemData(self): """ return a relation based on its ID """ if self.isSessionConceptId(): concept_id = self.getSessionConceptId() relation_id = self.getSessionRelationId() relation_type = self.getSessionRelationType() orig_concept_id = self.REQUEST.get('concept_id', None) orig_relation_id = self.REQUEST.get('relation_id', None) orig_relation_type = self.REQUEST.get('relation_type', None) else: concept_id = self.REQUEST.get('concept_id', self.getSessionConceptId()) relation_id = self.REQUEST.get('relation_id', self.getSessionRelationId()) relation_type = self.REQUEST.get('relation_type', self.getSessionRelationType()) orig_concept_id = concept_id orig_relation_id = relation_id orig_relation_type = relation_type self.delSessionConceptId() self.delSessionRelationId() self.delSessionRelationType() return self.get_relations_item_data(concept_id, relation_id, relation_type, orig_concept_id, orig_relation_id, orig_relation_type) #import related def skos_import(self, file, REQUEST=None): """ """ parser = relation_parser() #parse the SKOS information chandler = parser.parseHeader(file) if chandler is None: if REQUEST: self.setSessionErrorsTrans( 'Parsing error. The file could not be parsed.') return REQUEST.RESPONSE.redirect('import_html') #info count_rel = 0 err_list = [] #set data for data in chandler.getBody(): concept_id = data['concept_id'].encode('utf-8').split('/')[-1] relation_id = data['relation_id'].encode('utf-8').split('/')[-1] relation_type = data['relation_type'] if concept_id: if self.checkCpRel(concept_id) and self.checkCpRel( relation_id): self.__add_relation(concept_id, relation_id, relation_type) count_rel += 1 else: if not relation_id: err_list.append(( '(${content_id}, None) - relation_id not specified', { 'concept_id': concept_id }, )) else: err_list.append(( '(${concept_id}, ${relation_id}) - at least one of the concept_id or relation_id does not exist', { 'concept_id': concept_id, 'relation_id': relation_id })) else: err_list.append('None - concept_id not specified') if REQUEST: self.setSessionInfoTrans([ 'File imported successfully.', ( 'Translations added: ${count_rel}', { 'count_rel': count_rel }, ) ]) if err_list: self.setSessionErrorsTrans([ 'Relations not imported (by its (concept_id, relation_id)):', err_list, ]) return REQUEST.RESPONSE.redirect('import_html') #relation type related def getRelTypes(self): """ """ return RELATION_TYPES def getRelTypesIDs(self): """ """ return self.getRelTypes().keys() def getRelTypeByID(self, id): """ """ return self.getRelTypes()[id] #statistics def getAllConRel(self): query = [('meta_type', CONCEPT_RELATION_ITEM_METATYPE)] return self.catalog.searchCatalog(query) def getConRelNumber(self): return len(self.getAllConRel()) def getRelationsNumber(self): results = {} for conrel_ob in self.getAllConRel(): try: br_count = results[1] except: br_count = 0 try: nr_count = results[2] except: nr_count = 0 try: re_count = results[3] except: re_count = 0 if conrel_ob.relation_type == '1': results[1] = br_count + 1 elif conrel_ob.relation_type == '2': results[2] = nr_count + 1 elif conrel_ob.relation_type == '3': results[3] = re_count + 1 return results #management tabs security.declareProtected(view_management_screens, 'properties_html') properties_html = PageTemplateFile( "%s/zpt/ConceptRelations/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'concept_relations_html') concept_relations_html = PageTemplateFile( "%s/zpt/ConceptRelations/concept_relations" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'statistics_html') statistics_html = PageTemplateFile( "%s/zpt/ConceptRelations/statistics" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, 'import_html') import_html = PageTemplateFile( "%s/zpt/ConceptRelations/import" % NAAYATHESAURUS_PATH, globals())
class PloneboardTool(UniqueObject, Folder, ActionProviderBase): implements(IPloneboardTool) id = PLONEBOARD_TOOL meta_type = 'Ploneboard Tool' security = ClassSecurityInfo() def __init__(self): self.transforms = PersistentMapping() self.enable_anon_name = False security.declarePrivate('registerTransform') def registerTransform(self, name, module, friendlyName=None): tr_tool = getToolByName(self, 'portal_transforms') if name not in tr_tool.objectIds(): tr_tool.manage_addTransform(name, module) wasAdded = True else: wasAdded = False if not friendlyName: friendlyName = name if name not in self.transforms: self.transforms[name] = { 'enabled': True, 'friendlyName': friendlyName, 'wasAdded': wasAdded } security.declarePrivate('unregisterTransform') def unregisterTransform(self, name): tr_tool = getToolByName(self, 'portal_transforms') if self.transforms[name]['wasAdded']: try: tr_tool._delObject(name) except AttributeError as e: pass del self.transforms[name] security.declareProtected(ManagePortal, 'enableTransform') def enableTransform(self, name, enabled=True): """Change the activity status for a transform.""" self.transforms[name]['enabled'] = enabled security.declarePrivate('unregisterAllTransforms') def unregisterAllTransforms(self): tr_tool = getToolByName(self, 'portal_transforms') for transform_name in self.getTransforms(): if not self.transforms[transform_name] or \ self.transforms[transform_name].get('wasAdded', False): try: tr_tool._delObject(transform_name) except AttributeError: pass self.transforms.clear() security.declareProtected(ManagePortal, 'getTransforms') def getTransforms(self): """Returns list of transform names.""" return list(self.transforms.keys()) security.declareProtected(ManagePortal, 'getTransformFriendlyName') def getTransformFriendlyName(self, name): """Returns a friendly name for the given transform.""" return self.transforms[name]['friendlyName'] security.declareProtected(View, 'getEnabledTransforms') def getEnabledTransforms(self): """Returns list of names for enabled transforms""" return [ name for name in list(self.transforms.keys()) if self.transforms[name]['enabled'] ] security.declareProtected(View, 'performCommentTransform') def performCommentTransform(self, orig, **kwargs): """This performs the comment transform - also used for preview.""" transform_tool = getToolByName(self, 'portal_transforms') content_type = kwargs.get("content_type", "text/plain") # This one is very important, because transform object has no # acquisition context inside it, so we need to pass it our one context = kwargs.get('context', self) data = transform_tool._wrap(content_type) for transform in self.getEnabledTransforms(): data = transform_tool.convert(transform, orig, data, context) orig = data.getData() return orig # File upload - should be in a View once we get formcontroller support in Views security.declareProtected(View, 'getUploadedFiles') def getUploadedFiles(self): request = self.REQUEST result = [] files = request.get('files', []) if not files: return [] sdm = getToolByName(self, 'session_data_manager', None) if sdm is not None: pt = getToolByName(self, 'plone_utils') hassession = sdm.hasSessionData() for file in files: if isinstance(file, str) and hassession: # Look it up from session oldfile = request.SESSION.get(file, None) if oldfile is not None: result.append(oldfile) if isinstance(file, FileUpload): if file: filename = file.filename.split('\\')[-1] id = pt.normalizeString(filename) ct = file.headers.getheader('content-type') if ct is None: ct = '' newfile = File(id, id, file, ct) request.SESSION[id] = newfile result.append(newfile) # delete files form session if not referenced new_filelist = [x.getId() for x in result] old_filelist = hassession and request.SESSION.get( 'ploneboard_uploads', []) or [] for removed in [f for f in old_filelist if f not in new_filelist]: del request.SESSION[f] if hassession or new_filelist: request.SESSION['ploneboard_uploads'] = new_filelist return result security.declareProtected(View, 'clearUploadedFiles') def clearUploadedFiles(self): # Get previously uploaded files with a reference in request # + files uploaded in this request # XXX Add variable to keep track of filenames? request = self.REQUEST sdm = getToolByName(self, 'session_data_manager', None) if sdm is not None: if sdm.hasSessionData(): old_filelist = request.SESSION.get('ploneboard_uploads', None) if old_filelist is not None: for file in old_filelist: if file in request.SESSION: del request.SESSION[file] del request.SESSION['ploneboard_uploads'] security.declareProtected(View, 'getEnableAnonName') def getEnableAnonName(self): """Returns if anonymous can insert a name in their comments.""" return self.enable_anon_name security.declareProtected(ManagePortal, 'setEnableAnonName') def setEnableAnonName(self, value): """Set if anonymous can insert a name in their comments.""" self.enable_anon_name = value
class csvreplicataTool(UniqueObject, BaseContent, BrowserDefaultMixin): """ """ security = ClassSecurityInfo() implements(interfaces.IcsvreplicataTool) meta_type = 'csvreplicataTool' _at_rename_after_creation = True schema = csvreplicataTool_schema handlers = PersistentMapping ##code-section class-header #fill in your manual code here ##/code-section class-header # tool-constructors have no id argument, the id is fixed def __init__(self, id=None): BaseContent.__init__(self,'portal_csvreplicatatool') self.setTitle('') ##code-section constructor-footer #fill in your manual code here self.setTitle('CSV Replicator tool') self.replicabletypes = PersistentMapping() self.handlers = {} ##/code-section constructor-footer @property def dreplicabletypes(self): return dict(self.replicabletypes) def manage_afterAdd(self, item, container): """ initialize handlers with appconfig HANDLERS values""" self.handlers = HANDLERS # tool should not appear in portal_catalog def at_post_edit_script(self): self.unindexObject() ##code-section post-edit-method-footer #fill in your manual code here ##/code-section post-edit-method-footer # Methods # Manually created methods def setCSVsettings(self, REQUEST): """ """ self.setEncoding(REQUEST.get('encoding')); self.setDelimiter(REQUEST.get('delimiter')); self.setServerfilepath(REQUEST.get('serverfilepath')); self.setDateTimeFormat(REQUEST.get('datetimeformat')); self.setTempPath(REQUEST.get('tempPath')) self.setPlainFormat(REQUEST.get('is_plain_format', '') == 'on') self.setPartialCommitNumber(int(REQUEST.get('partial_commit_number', ''))) # Redirection of the page now that the treatment is done REQUEST.RESPONSE.redirect(self.absolute_url()+'/csv_settings') def setCSVHandledTypes(self, REQUEST): """ """ types_tool = getToolByName(self, 'archetype_tool') # Get of the various replicabletypes current and new) newreplicabletypes = REQUEST.get('csvhandledtypes') if (not type(newreplicabletypes) is list): newreplicabletypes = [newreplicabletypes] currentreplicablestypes = self.replicabletypes # Addition of the new replicable types, by default a new empty list # and creation of a temp variable to hold the new csv handled types newreplicabletypestempdict = {} for t in newreplicabletypes : newreplicabletypestempdict[t] = [] if (not currentreplicablestypes.has_key(t)): currentreplicablestypes[t] = ['default', ] # removal of the types that are not anymore replicables for k, v in currentreplicablestypes.items(): if (not newreplicabletypestempdict.has_key(k)): del currentreplicablestypes[k] # save of the new values self.replicabletypes.update(currentreplicablestypes) # Redirection of the page now that the treatment is done REQUEST.RESPONSE.redirect(self.absolute_url()+'/csv_settings') def setExcludedFields(self, REQUEST): """ """ self.setExcludedfieldsclasses( REQUEST.get('excludedfieldsclasses').split('\n')); self.setExcludedfields(REQUEST.get('excludedfields').split('\n')); # Redirection of the page now that the treatment is done REQUEST.RESPONSE.redirect(self.absolute_url()+'/csv_settings') def getPortalTypeNames(self): """ """ l = getPortalTypes(self).keys() l.sort() return [(k, k) for k in l] def getReplicableTypesSorted(self): """ """ l = self.replicabletypes.keys() l.sort() return l def getTypeSchematas(self, type): """ """ attool = getToolByName(self, 'archetype_tool') pt = getPortalTypes(self) if pt.has_key(type): (package, name) = pt[type] t = attool.lookupType(package, name) return t['klass'].schema.getSchemataNames() else: return [] def setCSVHandledTypesSchematas(self, REQUEST): """ """ currentreplicablestypes = self.replicabletypes i = 0 for ptn in self.getReplicableTypesSorted(): i += 1 if (type(ptn) is tuple): t = ptn[0] else: t = ptn r = REQUEST.get('csvhandledschematas-'+str(i)) if r: if (not type(r) is list): r = [r] currentreplicablestypes[t] = r else : if currentreplicablestypes.has_key(t): del currentreplicablestypes[t] self.replicabletypes.update(currentreplicablestypes) # Redirection of the page now that the treatment is done REQUEST.RESPONSE.redirect(self.absolute_url()+'/csv_settings') def clearReplicableTypes(self): """ """ self.replicabletypes.clear() def printReplicableTypes(self): """ """ return self.replicabletypes def getHandlers(self): """ """ if not getattr(self, 'handlers', None): setattr(self, 'handlers', {}) # migrate persistent mapping with possible new values for h in HANDLERS: if not h in self.handlers: self.handlers[h] = HANDLERS[h] return self.handlers def setHandler(self, key, value): """ """ self.handlers[key] = value def delHandler(self, key): """ """ del(self.handlers[key]) def getNonExportableFields(self): return (self.getExcludedfields() + ('tableContents',)) def fullactivation(self): types = getPortalTypes(self) self.replicabletypes.update( dict( [(t, self.getTypeSchematas(t)) for t in types]) )
class SilvaFind(Query, Content, SimpleItem): __doc__ = _("""Silva Find is a powerful search feature that allows easy creation of search forms and result pages. Users can add a Find anywhere and define which fields to make searchable by site visitors and/or which fields to limit to a preset value. Users also can determine which fields should be displayed in the search results. All metadata sets/fields are supported.""") security = ClassSecurityInfo() meta_type = "Silva Find" grok.implements(IFind) silvaconf.icon('SilvaFind.png') def __init__(self, id): Content.__init__(self, id) Query.__init__(self) self.shownFields = PersistentMapping() self.shownResultsFields = PersistentMapping() # by default we only show fulltext search # and a couple of resultfields self.shownFields['fulltext'] = True self.shownResultsFields['link'] = True self.shownResultsFields['ranking'] = True self.shownResultsFields['resultcount'] = True self.shownResultsFields['icon'] = True self.shownResultsFields['date'] = True self.shownResultsFields['textsnippet'] = True self.shownResultsFields['thumbnail'] = True self.shownResultsFields['breadcrumbs'] = True # ACCESSORS security.declareProtected(SilvaPermissions.View, 'getPublicResultFields') def getPublicResultFields(self): return filter(lambda field: self.isResultShown(field.getName()), self.getResultFields()) security.declareProtected(SilvaPermissions.View, 'getPublicSearchFields') def getPublicSearchFields(self): return filter(lambda field: self.isCriterionShown(field.getName()), self.getSearchFields()) security.declareProtected(SilvaPermissions.View, 'isCriterionShown') def isCriterionShown(self, fieldName): return self.shownFields.get(fieldName, False) security.declareProtected(SilvaPermissions.View, 'isResultShown') def isResultShown(self, fieldName): return self.shownResultsFields.get(fieldName, False) security.declareProtected(SilvaPermissions.View, 'havePublicSearchFields') def havePublicSearchFields(self): # BBB map(bool) is here for previously non-boolean stored values return reduce(operator.or_, map(bool, self.shownFields.values())) security.declareProtected(SilvaPermissions.View, 'searchResults') def searchResults(self, request={}, validate=True): options = self.getSearchCriterias(request) if validate: queryEmpty = True for key, value in options.items(): if key in ['path', 'meta_type']: # these fields do not count as a real search query # they are always there to filter unwanted results continue if type(value) is unicode and value.strip(): queryEmpty = False break elif type(value) is list: queryEmpty = False break query = options.get('fulltext', '').strip() if query and query[0] in ['?', '*']: raise ValueError( _(u'Search query can not start with wildcard character.')) if queryEmpty: raise ValueError( _(u'You need to fill at least one field in the search form.')) options['publication_status'] = ['public'] catalog = self.get_root().service_catalog try: results = catalog.searchResults(options) except ParseError: raise ValueError( _(u'Search query contains only common or reserved words.')) return results def getSearchCriterias(self, request): options = {} for field in self.getSearchFields(): name = field.getName() if (self.shownFields.get(name, False) or name == 'path'): queryPart = getMultiAdapter((field, self, request), IQueryPart) value = queryPart.getIndexValue() if value is None: value = '' options[queryPart.getIndexId()] = value return options
class Themes(SimpleItem, session_manager): """ Themes """ meta_type = THEMES_METATYPE product_name = NAAYATHESAURUS_PRODUCT_NAME icon = "misc_/NaayaThesaurus/themes.gif" manage_options = ( {"label": "Basic properties", "action": "properties_html"}, {"label": "Management", "action": "themes_html"}, {"label": "Import", "action": "import_html"}, {"label": "Statistics", "action": "statistics_html"}, {"label": "Undo", "action": "manage_UndoForm"}, ) security = ClassSecurityInfo() def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.themes = PersistentMapping() # basic properties security.declareProtected(view_management_screens, "manageBasicProperties") def manageBasicProperties(self, title="", REQUEST=None): """ manage basic properties for Themes """ self.title = title self._p_changed = 1 if REQUEST: self.setSessionInfoTrans("Saved changes.") return REQUEST.RESPONSE.redirect("properties_html") # themes management def __add_theme(self, theme_id, langcode, name): # create a new item item = ThemeItem(theme_id, langcode, name) self.themes[(theme_id, langcode)] = item self.catalog.CatalogObject(item) def __update_theme(self, theme_id, old_theme_id, langcode, old_langcode, name): # modify an item item = self.themes.get((old_theme_id, old_langcode)) if item is not None: self.__delete_theme((old_theme_id, old_langcode)) self.__add_theme(theme_id, langcode, name) def __delete_theme(self, ids): # delete 1 or more items if type(ids) != type((1, 1)): ids = th_utils().utConvertToList(ids) else: ids = [ids] collection = self.get_themes() for id in ids: self.catalog.UncatalogObject(collection[id]) del collection[id] # theme constraints security.declareProtected(view_management_screens, "checkTheme") def checkTheme(self, theme_id): """ """ if theme_id in self.getThemeRelationsFolder().getDistinctThemes(): return 1 return 0 security.declareProtected(view_management_screens, "getIdsList") def getIdsList(self, ids, all=0): """ """ if all: return self.themes.keys() return th_utils().getIdsList(ids) # themes getters def get_themes(self): # get all themes return self.themes def get_themes_sorted(self): # get all themes sorted return th_utils().utSortObjsListByAttr(self.themes.values(), "langcode", 0) def get_theme_by_id(self, id): # get an item try: return self.themes[id] except: return None def get_theme_item_data(self, theme_id, langcode, orig_theme_id, orig_langcode, theme_name): # get an item data item = self.get_theme_by_id((orig_theme_id, orig_langcode)) if item is not None: if theme_name is None: theme_name = item.theme_name return ["update", theme_id, langcode, theme_name, orig_theme_id, orig_langcode] else: return ["add", theme_id, langcode, theme_name, "", ""] # themes api security.declareProtected(view_management_screens, "manage_add_theme") def manage_add_theme(self, theme_id="", langcode="", theme_name="", REQUEST=None): """ manage themes """ err = 0 if self.checkTheme(theme_id): self.__add_theme(theme_id, langcode, theme_name) else: err = 1 if REQUEST: if err: self.setSessionThemeId(theme_id) self.setSessionLangcode(langcode) self.setSessionThemeName(theme_name) self.setSessionErrorsTrans("${theme_id} is not a valid theme ID.", theme_id=theme_id) else: self.setSessionInfoTrans("Record added.") REQUEST.RESPONSE.redirect("themes_html") security.declareProtected(view_management_screens, "manage_update_theme") def manage_update_theme( self, theme_id="", old_theme_id="", langcode="", old_langcode="", theme_name="", REQUEST=None ): """ update theme """ err = 0 if self.checkTheme(theme_id): self.__update_theme(theme_id, old_theme_id, langcode, old_langcode, theme_name) else: err = 1 if REQUEST: if err: self.setSessionThemeId(theme_id) self.setSessionLangcode(langcode) self.setSessionThemeName(theme_name) self.setSessionErrorsTrans("${theme_id} is not a valid theme ID.", theme_id=theme_id) REQUEST.RESPONSE.redirect("themes_html?theme_id=%s&langcode=%s" % (old_theme_id, old_langcode)) else: self.setSessionInfoTrans("Record updated.") REQUEST.RESPONSE.redirect("themes_html") security.declareProtected(view_management_screens, "manage_delete_themes") def manage_delete_themes(self, ids=[], delete_all="", REQUEST=None): """ delete themes """ if delete_all: ids = self.getIdsList(ids, 1) else: ids = self.getIdsList(ids) self.__delete_theme(ids) if REQUEST: self.setSessionInfoTrans("Selected records deleted.") REQUEST.RESPONSE.redirect("themes_html") security.declareProtected(view_management_screens, "getThemeItemData") def getThemeItemData(self): """ return a theme based on its ID """ if self.isSessionThemeId(): theme_id = self.getSessionThemeId() langcode = self.getSessionLangcode() theme_name = self.getSessionThemeName() orig_theme_id = self.REQUEST.get("theme_id", None) orig_langcode = self.REQUEST.get("langcode", None) else: theme_id = self.REQUEST.get("theme_id", self.getSessionThemeId()) langcode = self.REQUEST.get("langcode", self.getSessionLangcode()) theme_name = self.getSessionThemeName() orig_theme_id = theme_id orig_langcode = langcode self.delSessionThemeId() self.delSessionLangcode() self.delSessionThemeName() return self.get_theme_item_data(theme_id, langcode, orig_theme_id, orig_langcode, theme_name) # import related def skos_import(self, file, langcode, REQUEST=None): """ """ parser = theme_parser() # parse the SKOS information chandler = parser.parseHeader(file) if chandler is None: if REQUEST: self.setSessionErrorsTrans("Parsing error. The file could not be parsed.") return REQUEST.RESPONSE.redirect("import_html") # get the target language skos_lang = chandler.getLanguage() if skos_lang: target_language = skos_lang.encode("utf-8") else: target_language = langcode # get data body_info = chandler.getBody() # info count_themes = 0 err_themes = [] # set data for id, data in body_info.items(): theme_id = id.encode("utf-8").split("/")[-1] if theme_id: if self.checkTheme(theme_id): theme_name = data["name"] self.__add_theme(theme_id, target_language, theme_name) count_themes += 1 else: err_themes.append(theme_id) else: err_themes.append("None") if REQUEST: self.setSessionInfoTrans( ["File imported successfully.", ("Translations added: ${count_themes}", {"count_themes": count_themes})] ) if err_themes: self.setSessionErrorsTrans( [ "Translations not imported (by its theme_id):", ("Errors: ${err_themes}", {"err_themes": th_utils().utJoinToString(err_themes, ", ")}), ] ) return REQUEST.RESPONSE.redirect("import_html?msg=done") # statistics def getAllThemes(self): query = [("meta_type", THEME_ITEM_METATYPE)] return self.catalog.searchCatalog(query) def getThemesNumber(self): return len(self.getAllThemes()) def getThemesTransNumber(self): results = {} for theme_ob in self.getAllThemes(): try: tr_count = results[theme_ob.langcode] except: tr_count = 0 results[theme_ob.langcode] = tr_count + 1 return results def getEmptyTrans(self): empty_count = 0 for theme_ob in self.getAllThemes(): if not theme_ob.theme_name: empty_count += 1 return empty_count # management tabs security.declareProtected(view_management_screens, "properties_html") properties_html = PageTemplateFile("%s/zpt/Themes/properties" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, "themes_html") themes_html = PageTemplateFile("%s/zpt/Themes/themes" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, "statistics_html") statistics_html = PageTemplateFile("%s/zpt/Themes/statistics" % NAAYATHESAURUS_PATH, globals()) security.declareProtected(view_management_screens, "import_html") import_html = PageTemplateFile("%s/zpt/Themes/import" % NAAYATHESAURUS_PATH, globals())
class AnzECASClient(AnzCASClient): ''' Anz eCAS client extends anz.casclient to support European Council CAS''' implements( IAnzCASClient ) meta_type = 'Anz eCAS Client' casServerValidationUrl = '' security = ClassSecurityInfo() # Session variable use to save assertion CAS_ASSERTION = '__ecas_assertion' _properties = AnzCASClient._properties + ( { 'id': 'casServerValidationUrl', 'label': 'eCAS Server Validation URL', 'type': 'string', 'mode': 'w' }, ) def __init__( self, id, title ): super(AnzECASClient, self).__init__(id, title) self._ecas_id = PersistentMapping() def getEcasUserId(self, username): userdb = getattr(self, '_ecas_id', None) if userdb: for ecas_id, user in self._ecas_id.iteritems(): if isEmail(username): if user.email and user.email.lower() == username.lower(): return ecas_id else: if user.username == username: return ecas_id def getEcasIDUser(self, ecas_id): """Return internal user mapping for the ecas_id. """ userdb = getattr(self, '_ecas_id', {}) return userdb.get(ecas_id) def getEcasIDEmail(self, ecas_id): """Return ecas user's email.""" ecas_user = self.getEcasIDUser(ecas_id) if ecas_user: return ecas_user.email def getEcasIDUsername(self, ecas_id): """Return ecas user's username.""" ecas_user = self.getEcasIDUser(ecas_id) if ecas_user: return ecas_user.username security.declarePrivate( 'challenge' ) def challenge( self, request, response, **kw ): if request['QUERY_STRING']: url = request['ACTUAL_URL'] + "?" + request['QUERY_STRING'] else: url = request['ACTUAL_URL'] came_from = urllib2.quote(url) response.setCookie('challenged', True, path='/') response.redirect( '/Login/unauthorized?came_from=%s' % came_from, lock=1 ) return 1 def validateServiceTicket(self, service, ticket): if self.ticketValidationSpecification == 'CAS 1.0': validator = Cas10TicketValidator( self.casServerUrlPrefix, self.renew ) else: if self.acceptAnyProxy or self.allowedProxyChains: validator = Cas20ProxyTicketValidator( self.casServerUrlPrefix, self._pgtStorage, acceptAnyProxy=self.acceptAnyProxy, allowedProxyChains=self.allowedProxyChains, renew=self.renew ) else: validator = ECas20ServiceTicketValidator( self.casServerUrlPrefix, self.casServerValidationUrl, self._pgtStorage, self.renew ) return validator.validate(ticket, service, self.getProxyCallbackUrl() ) security.declarePrivate( 'authenticateCredentials' ) def authenticateCredentials( self, credentials ): user_and_info = super(AnzECASClient, self).authenticateCredentials(credentials) if not user_and_info: return None user, info = user_and_info """ # this code should not be here, but in an assignLocalRolesPlugin # make sure the code following will not start a transaction without committing this one # else we shall loose the session stored by casclient.py:extractCredentials # and the next auth plugin will try to validate the ticket himself and fail # because this one was provided by a different sso service than what the next plugin is bound to try: engine = self.unrestrictedTraverse('/ReportekEngine') authMiddleware = engine.authMiddlewareApi if authMiddleware: authMiddleware.updateLocalRoles(user) #from Products.Reportek.BdrAuthorizationMiddleware import updateLocalRoles2 #updateLocalRoles2(user) # make sure any relavant exception are caught before this point. # We wouldn't know what to do with them here... except: LOG.warning("Error while contacting Satelite Registry for authorization info. " "Using the know roles assignments") """ return user, info def invalidateOlderMapping(self, c_ecas_id, username): """Invalidate older mapping.""" userdb = getattr(self, '_ecas_id', None) res = [] if userdb: for ecas_id, user in self._ecas_id.iteritems(): if ecas_id != c_ecas_id: if isEmail(username): if user.email and user.email.lower() == username.lower(): user._email = None res.append(ecas_id) else: if user.username == username: user._username = None res.append(ecas_id) return res def mapUser(self, ecas, ecas_id, username): ecas_user = ecas._ecas_id.get(ecas_id) if not ecas_user: ecas_user = EcasClient(ecas_id, username) ecas._ecas_id[ecas_id] = ecas_user res = self.invalidateOlderMapping(ecas_id, username) if res: LOG.debug("Invalidated data for ecas id: {}".format(res)) elif not ecas_user.email and isEmail(username): ecas_user._email = username elif isEmail(username) and ecas_user.email != username: ecas_user._email = username elif not ecas_user.username and not isEmail(username): ecas_user._username = username else: LOG.debug("User %s already mapped in %s app" % (username, ECAS_ID)) def extractCredentials(self, request): creds = super(AnzECASClient, self).extractCredentials(request) sdm = getattr( self, 'session_data_manager', None ) assert sdm is not None, 'No session data manager found!' session = sdm.getSessionData( create=0 ) assertion = self.getAssertion( session ) if assertion: try: ecas = self.unrestrictedTraverse('/'+ENGINE_ID+'/acl_users/'+ECAS_ID) username = assertion.principal.id ecas_id = assertion.principal.ecas_id if not hasattr(ecas, '_ecas_id'): ecas._ecas_id = PersistentMapping() old_mapping = getattr(ecas, '_user2ecas_id', None) if old_mapping: for user, ecas_user_id in old_mapping.iteritems(): self.mapUser(ecas, ecas_user_id, user) del ecas._user2ecas_id self.mapUser(ecas, ecas_id, username) except Exception as e: LOG.warning("Error getting username: {}".format(str(e))) return creds
class MapLayer(BaseFolder):#(BaseFolder): '''Map Layer for CoGIS''' #schema = extra_schema#BaseFolderSchema #schema = BaseFolderSchema actions = ({'id': 'details', 'name': 'Details', 'action': 'string:${object_url}/MapLayer_Details', 'permissions': (permissions.ViewManagementScreens,)}, {'id': 'security', 'name': 'Security', 'action': 'string:${object_url}/Map_Layer_Security', 'permissions': (permissions.ViewManagementScreens,)}, {'id': 'pingLayer', 'name': 'Ping Layer', 'action': 'string:${object_url}/Map_Layer_Ping', 'permissions': (permissions.ViewManagementScreens,)}, ) schema = BaseSchema + Schema([ StringField('description', required=False, searchable = 1, widget=StringWidget(label='Description',description="Enter project description")), ReferenceField( 'relatedItems', relationship = 'relatesTo', multiValued = True, isMetadata = True, languageIndependent = False, index = 'KeywordIndex', write_permission = ModifyPortalContent, widget = ReferenceBrowserWidget( allow_search = True, allow_browse = True, show_indexes = False, force_close_on_insert = True, label = _(u'label_related_items', default=u'Related Items'), description = '', visible = {'edit' : 'visible', 'view' : 'invisible' } ) ) ]) #schema = schema + extra_schema security = ClassSecurityInfo() archetype_name = 'MapLayer' meta_type = 'MapLayer' portal_type = 'MapLayer' allowed_content_types = [] #['LayerField'] filter_content_types = 1 global_allow = 0 allow_discussion = 0 content_icon = "mapService_icon.gif" def __init__(self, id,title=''): '''Initialize an instance of the class''' self.id=id self.sampleImage = '' self.abstract = '' self.keywords = '' self.uniqueName = '' self.geometryField = '' self.geometryType = '' self.title = id self.wfsSRS = '' self.wmsStyleName = '' self.wfsBoundingBox = '' self.fields = '' self.source = '' self.error = '' self.wmsQueryable = '' self.wmsName = '' self.wfsName = '' self.wmsSRS = '' self.wmsTitle = '' self.hasWFSLayer = False self.wmsBoundingBox = '' self.describeFeatureResponse = '' self.availablePermissions = ['Render','Extract'] self.security = PersistentMapping() # e.g {'Manager':{'Render':0,'Extract':1}} self.wmsXML = "" self.wfsXML = "" self.organization = "" self.reindexObject() self._p_changed = 1 ## def manage_afterAdd(self,item, container): ## """ ## """ ## self.manage_permission("View", roles=["Owner"], acquire=False) ## self.manage_permission("List folder contents", roles=["Owner"], acquire=False) security.declarePublic('userIsOwner') def userIsOwner(self): """ @summary: this is a zope specific method to get logged in user name @return: boolean, true if logged in user is the owner """ try: ownerName = self.owner_info()['id'] member = self.portal_membership.getAuthenticatedMember() userName = member.getUserName() if userName == ownerName: return True else: return False except: logger.exception('error') return False def getSecurity(self): """ @summary: returns the current security definitions dict @return: dict containing security info for roles defined in plone """ return dict(self.security) def getMetadata(self): """ @summary: gets the metadata template for the layer @return: an html interface with metadata information """ return self.Map_Layer_Details_Stripped(self) def setSecurityVarOnly(self,securityDict): """ """ try: tmpDict = {} for k in securityDict.keys(): if k == 'fields': continue cDict = {} if securityDict[k]['Render'] in ['false',0]: cDict['Render'] = 0 if securityDict[k]['Render'] in ['true',1]: cDict['Render'] = 1 if securityDict[k]['Extract'] in ['false',0]: cDict['Extract'] = 0 if securityDict[k]['Extract'] in ['true',1]: cDict['Extract'] = 1 tmpDict[k] = cDict # get a diff between current security settings and the passed security settings changed = {} for k in tmpDict.keys(): if not k in self.security.keys(): changed[k] = tmpDict[k] # its a new key if k in self.security.keys(): if self.security[k]['Render'] != tmpDict[k]['Render']: changed[k] = tmpDict[k] self.security = tmpDict # only let the changes be propagated to children # get all fields fields = [] items = self.objectItems() for i in items: if i[1].meta_type == 'LayerField': fields.append(i[1]) for field in fields: tmpSec = field.getSecurity() for k in changed.keys(): if not tmpSec.has_key(k): tmpSec[k] = {'Render':0} # add the key if it does not exist tmpSec[k]['Render'] = changed[k]['Render'] field.setSecurityVarOnly(tmpSec) return 1 except: logger.exception('error') def setSecurity(self,securityDict): """ @summary: @param securityDict: a dictionary containing permissions defined for certain roles defined in plone """ try: tmpDict = {} for k in securityDict.keys(): if k == 'fields': continue cDict = {} if securityDict[k]['Render'] in ['false',0]: cDict['Render'] = 0 if securityDict[k]['Render'] in ['true',1]: cDict['Render'] = 1 if securityDict[k]['Extract'] in ['false',0]: cDict['Extract'] = 0 if securityDict[k]['Extract'] in ['true',1]: cDict['Extract'] = 1 tmpDict[k] = cDict # get a diff between current security settings and the passed security settings changed = {} for k in tmpDict.keys(): if not k in self.security.keys(): changed[k] = tmpDict[k] # its a new key if k in self.security.keys(): if self.security[k]['Render'] != tmpDict[k]['Render']: changed[k] = tmpDict[k] self.security = tmpDict # only let the changes be propagated to children # get all fields fields = [] items = self.objectItems() for i in items: if i[1].meta_type == 'LayerField': fields.append(i[1]) for field in fields: tmpSec = field.getSecurity() for k in changed.keys(): if not tmpSec.has_key(k): tmpSec[k] = {'Render':0} # add the key if it does not exist tmpSec[k]['Render'] = changed[k]['Render'] field.setSecurity(tmpSec) self.aq_parent.updateSecurityForSingleLayer(self.uniqueName) return 1 except: logger.exception('error') def getSampleImage(self,REQUEST=None): """ @summary: converts the hex encoded image to binary @return: sample image in binary format """ try: data = binascii.a2b_hex(self.sampleImage) REQUEST.RESPONSE.setHeader("Content-type","image/png") return data except: logger.exception('error') def pingWMSLayer(self,extent=['23.00','23.00','23.01','23.01']): """ """ try: startTime = time.time() layerName = self.wmsName source = self.source[1] layerName = layerName.replace(" ","%20") strEnv = "%s,%s,%s,%s" %(extent[0],extent[1],extent[2],extent[3]) theURL = source + WMS_GETMAP_EXTENSION + "&bbox=%s&styles=&Format=image/png&width=2&height=2&srs=EPSG:4326&layers=%s" %(strEnv,layerName) #"&request=GetLegendGraphic&version=1.0.0&format=%s&width=%s&height=%s&layer=%s" %(format,width,height,layer['wmsName']) data = self._getURLContent(theURL,{}) if data.find("ServiceExceptionReport") != -1: return "Layer Unavailable" endTime = time.time() return str(endTime - startTime) except: import traceback sio = cStringIO.StringIO() traceback.print_exc(file=sio) sio.seek(0) trace = sio.read() logger.exception('error') return MapServerTemplates.ogcServiceException %("Exception occured with _getSampleImage request, check log for details %s" %trace) def pingWFSLayer(self): """ """ if self.hasWFSLayer: try: startTime = time.time() layerName = self.wfsName source = self.source[0] baseURL = source + WFS_DESCRIBEFEATURETYPE_EXTENSION baseURL += "&typename=" + layerName.replace(" ","%20") data = self._getURLContent(baseURL) if (data.find("ServiceException") != -1) or (data.find("ServiceExceptionReport") != -1) or (data.strip() == ""): return "Layer Unavailable" endTime = time.time() return str(endTime - startTime) except: import traceback sio = cStringIO.StringIO() traceback.print_exc(file=sio) sio.seek(0) trace = sio.read() logger.exception('error') return MapServerTemplates.ogcServiceException %("Exception occured with pingWFSLayer request, check log for details %s" %trace) else: return "No WFS Source given." ## if self.hasWFSLayer: ## try: ## startTime = time.time() ## layerName = self.wfsName ## source = self.source[0] ## data = '' ## baseURL = source + WFS_DESCRIBEFEATURETYPE_EXTENSION ## baseURL += "&typename=" + layerName.replace(" ","%20") ## data = self._getURLContent(baseURL) ## if data.find("ServiceExceptionReport") != -1: ## return "Layer Unavailable" ## ## endTime = time.time() ## return str(endTime - startTime) ## except: ## import traceback ## sio = cStringIO.StringIO() ## traceback.print_exc(file=sio) ## sio.seek(0) ## trace = sio.read() ## ## return MapServerTemplates.ogcServiceException %("Exception occured with pingWFSLayer request, check log for details %s" %trace) ## ## else: ## return "No WFS Source given." ## ## return 1 def _getURLContent(self,url,data={}): """ """ try: if data: if type(data) == unicode: data = str(urllib.unquote(data)) if type(data) == str: f = urllib.urlopen(url,data) else: params = urllib.urlencode(data) f = urllib.urlopen(url,params) else: f = urllib.urlopen(url) data = f.read() f.close() return data except: import traceback sio = cStringIO.StringIO() traceback.print_exc(file=sio) sio.seek(0) trace = sio.read() logger.exception('error') return MapServerTemplates.ogcServiceException %("Exception occured getURLContent request, check log for details %s" %trace)
def __init__(self, id): self.id = id self.channels = PersistentMapping() self.policy = defaultPolicy
class PloneKupuLibraryTool(UniqueObject, SimpleItem, KupuLibraryTool): """Plone specific version of the kupu library tool""" id = TOOLNAME meta_type = "Kupu Library Tool" title = TOOLTITLE security = ClassSecurityInfo() # protect methods provided by super class KupuLibraryTool security.declareProtected(permissions.QueryLibraries, "getLibraries", "getPortalTypesForResourceType") security.declareProtected(permissions.ManageLibraries, "addLibrary", "deleteLibraries", "updateLibraries", "moveUp", "moveDown") security.declareProtected(permissions.ManageLibraries, "addResourceType", "updateResourceTypes", "deleteResourceTypes") def __init__(self): self._libraries = PersistentList() self._res_types = PersistentMapping() self.linkbyuid = False def manage_afterAdd(self, item, container): # We load default values here, so __init__ can still be used # in unit tests. Plus, it only makes sense to load these if # we're being added to a Plone site anyway for lib in _default_libraries: self.addLibrary(**lib) self._res_types.update(_default_resource_types) security.declareProtected('View', "getLinkbyuid") def getLinkbyuid(self): """Returns 'is linking by UID enabled'?""" try: return self.linkbyuid except AttributeError: return 1 security.declareProtected('View', "getTableClassnames") def getTableClassnames(self): """Return a list of classnames supported in tables""" try: return self.table_classnames except AttributeError: return ('plain', 'listing', 'vertical listing', 'listing nosort|unsorted listing') security.declareProtected('View', "getParagraphStyles") def getParagraphStyles(self): """Return a list of classnames supported by paragraphs""" try: return self.paragraph_styles except AttributeError: return _default_paragraph_styles security.declareProtected('View', "getHtmlExclusions") def getHtmlExclusions(self): try: return self.html_exclusions except AttributeError: self.html_exclusions = _excluded_html return self.html_exclusions security.declareProtected('View', "getStyleWhitelist") def getStyleWhitelist(self): try: return self.style_whitelist except AttributeError: self.style_whitelist = _style_whitelist return self.style_whitelist security.declareProtected('View', "getClassBlacklist") def getClassBlacklist(self): return getattr(self, 'class_blacklist', []) security.declareProtected('View', "getClassBlacklist") def installBeforeUnload(self): return getattr(self, 'install_beforeunload', True) security.declareProtected('View', 'isKupuEnabled') def isKupuEnabled(self, useragent='', allowAnonymous=False, REQUEST=None): def numerics(s): '''Convert a string into a tuple of all digit sequences ''' seq = [''] for c in s: if c.isdigit(): seq[-1] = seq[-1] + c elif seq[-1]: seq.append('') return tuple([ int(val) for val in seq if val]) # First check whether the user actually wants kupu pm = getToolByName(self, 'portal_membership') if pm.isAnonymousUser() and not allowAnonymous: return False user = pm.getAuthenticatedMember() if user.getProperty('wysiwyg_editor').lower() != 'kupu': return False # Then check whether their browser supports it. if not useragent: useragent = REQUEST['HTTP_USER_AGENT'] if 'Opera' in useragent or 'BEOS' in useragent: return False if not useragent.startswith('Mozilla/'): return False try: mozillaver = numerics(useragent[len('Mozilla/'):].split(' ')[0]) if mozillaver > (5,0): return True elif mozillaver == (5,0): rv = useragent.find(' rv:') if rv >= 0: verno = numerics(useragent[rv+4:].split(')')[0]) return verno >= (1,3,1) MSIE = useragent.find('MSIE') if MSIE >= 0: verno = numerics(useragent[MSIE+4:].split(';')[0]) return verno >= (5,5) except: # In case some weird browser makes the test code blow up. pass return False # ZMI views manage_options = (SimpleItem.manage_options[1:] + ( dict(label='Config', action='kupu_config'), dict(label='Libraries', action='zmi_libraries'), dict(label='Resource types', action='zmi_resource_types'), dict(label='Documentation', action='zmi_docs'), dict(label='Status', action='sanity_check'), )) security.declarePublic('scanIds') def scanIds(self): """Finds the relevant source files and the doller/Id/dollar strings they contain""" return scanner.scanIds() security.declarePublic('scanKWS') def scanKWS(self): """Check that kupu_wysiwyg_support is up to date""" return scanner.scanKWS() security.declarePublic('docs') def docs(self): """Returns Kupu docs formatted as HTML""" docpath = os.path.join(Globals.package_home(kupu_globals), 'doc') f = open(os.path.join(docpath, 'PLONE2.txt'), 'r') _docs = f.read() return _docs security.declareProtected(permissions.ManageLibraries, "zmi_docs") zmi_docs = PageTemplateFile("zmi_docs.pt", globals()) zmi_docs.title = 'kupu configuration documentation' security.declareProtected(permissions.ManageLibraries, "sanity_check") sanity_check = PageTemplateFile("sanity_check.pt", globals()) sanity_check.title = 'kupu status' security.declareProtected(permissions.ManageLibraries, "kupu_config") kupu_config = PageTemplateFile("kupu_config.pt", globals()) kupu_config.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_libraries") zmi_libraries = PageTemplateFile("libraries.pt", globals()) zmi_libraries.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_resource_types") zmi_resource_types = PageTemplateFile("resource_types.pt", globals()) zmi_resource_types.title = 'kupu configuration' security.declareProtected(permissions.ManageLibraries, "zmi_get_libraries") def zmi_get_libraries(self): """Return the libraries sequence for the ZMI view""" #return () def text(value): return getattr(value, 'text', value) return [dict([(key, text(value)) for key, value in lib.items()]) for lib in self._libraries] security.declareProtected(permissions.ManageLibraries, "zmi_add_library") def zmi_add_library(self, id, title, uri, src, icon, REQUEST): """Add a library through the ZMI""" self.addLibrary(id, title, uri, src, icon) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_update_libraries") def zmi_update_libraries(self, libraries, REQUEST): """Update libraries through the ZMI""" self.updateLibraries(libraries) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_delete_libraries") def zmi_delete_libraries(self, indices, REQUEST): """Delete libraries through the ZMI""" self.deleteLibraries(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_move_up") def zmi_move_up(self, indices, REQUEST): """Move libraries up through the ZMI""" self.moveUp(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declareProtected(permissions.ManageLibraries, "zmi_move_down") def zmi_move_down(self, indices, REQUEST): """Move libraries down through the ZMI""" self.moveDown(indices) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_libraries') security.declarePublic("zmi_get_default_library") def zmi_get_default_library(self): """Return the default selected library for the ZMI view""" return getattr(self, '_default_library', '') security.declareProtected(permissions.ManageLibraries, "zmi_set_default_library") def zmi_set_default_library(self, defid=''): """Return the libraries sequence for the ZMI view""" self._default_library = defid security.declareProtected(permissions.ManageLibraries, "zmi_get_type_mapping") def zmi_get_type_mapping(self): """Return the type mapping for the ZMI view""" return [(res_type, tuple(portal_type)) for res_type, portal_type in self._res_types.items()] security.declareProtected(permissions.ManageLibraries, "zmi_add_resource_type") def zmi_add_resource_type(self, resource_type, portal_types, REQUEST): """Add resource type through the ZMI""" self.addResourceType(resource_type, portal_types) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_resource_types') security.declareProtected(permissions.ManageLibraries, "zmi_update_resource_types") def zmi_update_resource_types(self, type_info, REQUEST): """Update resource types through the ZMI""" self.updateResourceTypes(type_info) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_resource_types') security.declareProtected(permissions.ManageLibraries, "zmi_delete_resource_types") def zmi_delete_resource_types(self, resource_types, REQUEST): """Delete resource types through the ZMI""" self.deleteResourceTypes(resource_types) REQUEST.RESPONSE.redirect(self.absolute_url() + '/zmi_resource_types') security.declareProtected(permissions.ManageLibraries, "configure_kupu") def configure_kupu(self, linkbyuid, table_classnames, html_exclusions, style_whitelist, class_blacklist, installBeforeUnload=None, parastyles=None, REQUEST=None): """Delete resource types through the ZMI""" self.linkbyuid = int(linkbyuid) self.table_classnames = table_classnames if installBeforeUnload is not None: self.install_beforeunload = bool(installBeforeUnload) if parastyles: self.paragraph_styles = [ line.strip() for line in parastyles if line.strip() ] newex = html_exclusions[-1] html_exclusions = [ (tuple(h.get('tags', ())), tuple(h.get('attributes', ()))) for h in html_exclusions[:-1] if h.get('keep')] tags, attr = newex.get('tags', ()), newex.get('attributes', ()) if tags or attr: tags = tuple(tags.replace(',',' ').split()) attr = tuple(attr.replace(',',' ').split()) html_exclusions.append((tags, attr)) self.html_exclusions = html_exclusions self.style_whitelist = list(style_whitelist) self.class_blacklist = list(class_blacklist) if REQUEST: REQUEST.RESPONSE.redirect(self.absolute_url() + '/kupu_config')
def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.concept_relations = PersistentMapping()
def __init__(self, url): self.recheck_interval = 300 self.lockedDownCollections = PersistentMapping()
def __init__(self): self.transforms = PersistentMapping() self.enable_anon_name = False
class SilvaFind(Query, Content, SimpleItem): __doc__ = _("""Silva Find is a powerful search feature that allows easy creation of search forms and result pages. Users can add a Find anywhere and define which fields to make searchable by site visitors and/or which fields to limit to a preset value. Users also can determine which fields should be displayed in the search results. All metadata sets/fields are supported.""") security = ClassSecurityInfo() meta_type = "Silva Find" grok.implements(IFind) silvaconf.icon('SilvaFind.png') def __init__(self, id): Content.__init__(self, id) Query.__init__(self) self.shownFields = PersistentMapping() self.shownResultsFields = PersistentMapping() # by default we only show fulltext search # and a couple of resultfields self.shownFields['fulltext'] = True self.shownResultsFields['link'] = True self.shownResultsFields['ranking'] = True self.shownResultsFields['resultcount'] = True self.shownResultsFields['icon'] = True self.shownResultsFields['date'] = True self.shownResultsFields['textsnippet'] = True self.shownResultsFields['thumbnail'] = True self.shownResultsFields['breadcrumbs'] = True # ACCESSORS security.declareProtected(SilvaPermissions.View, 'getPublicResultFields') def getPublicResultFields(self): return filter(lambda field: self.isResultShown(field.getName()), self.getResultFields()) security.declareProtected(SilvaPermissions.View, 'getPublicSearchFields') def getPublicSearchFields(self): return filter(lambda field: self.isCriterionShown(field.getName()), self.getSearchFields()) security.declareProtected(SilvaPermissions.View, 'isCriterionShown') def isCriterionShown(self, fieldName): return self.shownFields.get(fieldName, False) security.declareProtected(SilvaPermissions.View, 'isResultShown') def isResultShown(self, fieldName): return self.shownResultsFields.get(fieldName, False) security.declareProtected(SilvaPermissions.View, 'havePublicSearchFields') def havePublicSearchFields(self): # BBB map(bool) is here for previously non-boolean stored values return reduce(operator.or_, map(bool, self.shownFields.values())) security.declareProtected(SilvaPermissions.View, 'searchResults') def searchResults(self, request={}, validate=True): options = self.getSearchCriterias(request) if validate: queryEmpty = True for key, value in options.items(): if key in ['path', 'meta_type']: # these fields do not count as a real search query # they are always there to filter unwanted results continue if type(value) is unicode and value.strip(): queryEmpty = False break elif type(value) is list: queryEmpty = False break query = options.get('fulltext', '').strip() if query and query[0] in ['?', '*']: raise ValueError( _(u'Search query can not start with wildcard character.')) if queryEmpty: raise ValueError( _(u'You need to fill at least one field in the search form.' )) options['publication_status'] = ['public'] catalog = self.get_root().service_catalog try: results = catalog.searchResults(options) except ParseError: raise ValueError( _(u'Search query contains only common or reserved words.')) return results def getSearchCriterias(self, request): options = {} for field in self.getSearchFields(): name = field.getName() if (self.shownFields.get(name, False) or name == 'path'): queryPart = getMultiAdapter((field, self, request), IQueryPart) value = queryPart.getIndexValue() if value is None: value = '' options[queryPart.getIndexId()] = value return options
def __init__(self, id, title): """ constructor """ self.id = id self.title = title self.altterms = PersistentMapping()
def __init__( self, id, title ): super(AnzECASClient, self).__init__(id, title) self._ecas_id = PersistentMapping()