# -*- coding: utf-8 # Copyright 2006 by Vahur Rebas """ Marks that appear in text. """ import Globals from Globals import Persistent from OFS.Folder import Folder from AccessControl import ClassSecurityInfo from OFS.PropertyManager import PropertyManager from xml.dom.minidom import Document from zope.interface import implements from interfaces import IMarksContainer from permissions import * class MarksContainer(PropertyManager, Folder): """ Marks that appear in text. """ meta_type = 'MarksContainer' security = ClassSecurityInfo() security.declareObjectPublic() implements(IMarksContainer) id = 'Marks' manage_options = Folder.manage_options+PropertyManager.manage_options security.declareProtected(perm_manage, 'manage_afterAdd') def manage_afterAdd(self, item, container): """ add global marks """ from Marks import Marks from zExceptions import BadRequest try: self._setObject('global_marks', Marks('global_marks')) except BadRequest: pass security.declareProtected(perm_mark_document, 'pydataset') def pydataset(self, REQUEST, context=None, doc=None, root=''): """ return marks. """ doc = Document() root = doc.createElement('div') root.setAttribute('id', 'data_island') root.setAttribute('class', 'data_island') doc.appendChild(root) personal = self.getPersonalMarks(REQUEST, obj=1) if personal: personal.build_pydataset(None, root, doc) self.global_marks.build_pydataset(None, root, doc) #REQUEST.RESPONSE.setHeader('Content-type','text/xml') return str(root.toxml()) security.declareProtected(perm_view, 'get_marks') def get_marks(self, REQUEST): """ return marks object. maybe we want to merge user's and global marks """ if self.getPersonalMarks(REQUEST): return self.getPersonalMarks(REQUEST, obj=1) return self.global_marks security.declareProtected(perm_view, 'prettyCodeTitle') def prettyCodeTitle(self, code): """ returns code's title. First it must find which marks tree to use """ mark = self.getCode(code) if not mark or mark == 1: return "gone" return mark.getTitle() security.declarePrivate('getMarkContainer') def getMarkContainer(self, code): """ depending on code's prefix, return right marks tree """ pref = code.split('_')[0] marks = getattr(self, pref+'_marks', None) return marks security.declarePrivate('getCode') def getCode(self, code): marks = self.getMarkContainer(code) if not marks: return 1 mark = marks.getCodeById(marks, code) return mark security.declareProtected(perm_view, 'getPersonalMarks') def getPersonalMarks(self, REQUEST, obj=0): """ get user's personal marks data """ uname = str(REQUEST.AUTHENTICATED_USER) personal = getattr(self, uname+'_marks', None) if personal is None: # create personal marks container from Marks import Marks m = Marks(uname+'_marks') self._setObject(m.id, m) obj = getattr(self, m.id) if obj: return getattr(self, uname+'_marks') return personal.absolute_url() security.declareProtected(perm_view, 'getCodesTree') def getCodesTree(self, REQUEST): """ return codes tree for select menu """ result = '' personal = self.getPersonalMarks(REQUEST, obj=1) if personal: result += personal.treePrinter('') result += self.global_marks.treePrinter('') return result def getChildMarks(self, code): """ get child marks/codes for code """ code_obj = self.getCode(code) if code_obj == 1 or code_obj is None: return [] res = [] for c in code_obj.getCodes(): res.append(c.getCodeId()) self._get_child_marks(c, res) return res def _get_child_marks(self, context, res): """ see above. helper method """ for c in context.getCodes(): res.append(c.getCodeId()) self._get_child_marks(c, res) return res def isPublished(self, code): # isn't published. cont = self.getMarkContainer(code) if hasattr(cont, 'is_public'): return cont.is_public return 0 Globals.InitializeClass(MarksContainer)