# -*- coding: utf-8 # Copyright 2006 by Vahur Rebas """ Marks container that appear in text. """ import time import re import Globals from Globals import Persistent from OFS.Folder import Folder from AccessControl import ClassSecurityInfo from OFS.PropertyManager import PropertyManager from zope.interface import implements from permissions import * from interfaces import IMarks, Imark class mark(Folder): security = ClassSecurityInfo() security.declareObjectPublic() implements(Imark) meta_type = 'mark' def __init__(self, _id, code_id, title, desc): self.id = _id self._updateProperty('title', title) self.manage_addProperty('code_id', code_id, 'string') self.manage_addProperty('desc', desc, 'lines') self.manage_addProperty('documents_using_this', [], 'lines') self.manage_addProperty('deleted', 0, 'boolean') self.manage_addProperty('numeration', '0.0.0', 'string') def getCodeId(self): return self.code_id def getDocuments(self): return self.documents_using_this def isDeleted(self): return self.deleted def getDesc(self): return self.desc def getTitle(self): return self.title_or_id() def getNumeration(self): "nummerdus" if hasattr(self, 'numeration'): return self.numeration else: return "numbritu" def getCodes(self): """ return codes under this code """ return self.objectValues('mark') def getCodesFromTree(self): """all subcodes""" puhver=[self,] vastus=[] while puhver: uuritav=puhver.pop(0) puhver=puhver+uuritav.getCodes() vastus.append(uuritav) return vastus security.declareProtected(perm_mark_document, 'addDocument') def addDocument(self, id): self.documents_using_this += (id,) self._p_changed = 1 security.declareProtected(perm_mark_document, 'delDocument') def delDocument(self, id): res = () for x in self.documents_using_this: if x == id: continue res += (id,) self._updateProperty('documents_using_this', res) def getMes(self): q = {'code': self.id, 'is_deleted': 0} res = [] qres = self.Errors.errors_catalog(q) for r in qres: tmp = [] tmp.append(r.document) tmp.append(r.content) tmp.append(r.pointer) res.append(tmp) res.sort() return res class Marks(PropertyManager, Folder): """ Marks that appear in text. """ meta_type = 'Marks' security = ClassSecurityInfo() security.declareObjectPublic() implements(IMarks) manage_options = Folder.manage_options+PropertyManager.manage_options def __init__(self, _id): """ init """ self.id = _id self.title = '' self.codes = {} self.map = [] def manage_afterAdd(self, item, container): """ add global marks """ pass security.declareProtected(perm_edit_tree, 'setTitle') def setTitle(self, title): self.title = title security.declareProtected(perm_view, 'treePrinter') def treePrinter(self, res = '', context=None, level=0): """ nice treeprinter """ if context is None: context = self for x in context.objectValues('mark'): if x.isDeleted(): continue res += '\n' % (x.getId(), '-'*level, x.getTitle()) res += self.treePrinter('', x, level+1) return res def replacePrefix(self, context=None, new_prefix=''): """ replace global with username in mark.id """ if context is None: context = self for x in context.objectValues('mark'): old_id = x.getId() new_id = re.sub('global_', new_prefix+'_', old_id) x._setId(new_id) self.replacePrefix(x, new_prefix) return 0 security.declarePrivate('build_pydataset') def build_pydataset(self, context=None, root='', doc=None, level=0): """ return marks. fills dataset.xml """ if not context: context = self for x in context.objectValues('mark'): if x.isDeleted(): continue y = doc.createElement('div') y.setAttribute('code', x.getCodeId()) y.setAttribute('id', x.getCodeId()) y.setAttribute('name', x.getTitle()) y.setAttribute('level', str(level)) y.setAttribute('class', 'marks_tree_element') margins = 'margin-left:'+str(10*level)+'px;'; if level>0: y.setAttribute('style', margins+';display:none') else: y.setAttribute('style', margins) cp = doc.createElement('input') cp.setAttribute('type', 'checkbox') cp.setAttribute('name', 'chosen_xcodes') cp.setAttribute('value', x.getCodeId()) y.appendChild(cp) sp = doc.createElement('div') sp.setAttribute('class', 'mark_element_title') txt = doc.createTextNode(x.getTitle()) sp.appendChild(txt) y.appendChild(sp) tul = x.build_pydataset(x, y, doc, level+1) root.appendChild(y) return security.declareProtected(perm_view, 'getCodes') def getCodes(self): """ return marks/codes """ return self.objectValues('mark') def getCodeById(self, context, id): """ return code object with give id """ res = None for x in context.objectValues('mark'): if x.getCodeId() != id: res = x.getCodeById(x, id) else: return x if res: return res return res security.declareProtected(perm_edit_tree, 'setCodes') def setCodes(self, REQUEST=None, add_button='', remove_button='', modify_button='', title='', desc='', parent='' ): """ add new or remove existing code """ if add_button: cont = self if parent: cont = self.getCodeById(self, parent) ran = re.sub('\.', '', str(time.time())) own = self.getPrefix() nodeid = own+'_'+ran a = mark(nodeid, nodeid, title, desc) cont._setObject(a.id, a) elif modify_button: if not parent and not title: return mod = self.getCodeById(self, parent) mod._updateProperty('title', title) mod._updateProperty('desc', desc) elif remove_button: mod = self.getCodeById(self, parent) mod._updateProperty('deleted', 1) if REQUEST: return REQUEST.RESPONSE.redirect(self.absolute_url()) else: return 0 return security.declareProtected(perm_view, 'getPrefix') def getPrefix(self): id = self.getId() return id.split('_')[0] Globals.InitializeClass(Marks)