# -*- coding: utf-8 # Copyright 2004-2006 by Vahur Rebas """ WWW data handler """ import Globals from Globals import Persistent from zope.interface import implements from zope.publisher.interfaces import IPublishTraverse from zope.publisher.interfaces import NotFound from ZPublisher.BaseRequest import DefaultPublishTraverse from zope.publisher.http import IHTTPRequest from zope.component import adapts from OFS.SimpleItem import SimpleItem from AccessControl import ClassSecurityInfo from OFS.PropertyManager import PropertyManager from permissions import * import time, copy from interfaces import IWWW class WWW(PropertyManager, SimpleItem): """ WWW handler """ meta_type = 'WWW' security = ClassSecurityInfo() security.declareObjectPublic() implements(IWWW) id = 'wwwdata' manage_options = PropertyManager.manage_options+SimpleItem.manage_options def __init__(self): self.history = {} self.data = {} from zExceptions import BadRequest try: self.manage_addProperty('registered_pages', (), 'lines') except BadRequest: pass try: self.manage_addProperty('site_titles', (), 'lines') except BadRequest: pass def getTitle(self): """ return site's title for top """ t = '' lang = self.getLanguage(self.REQUEST) for tline in self.getProperty('site_titles'): if tline.startswith(lang): return tline[tline.index(':')+1:] return t def getPage(self, pagename): """ return page' content """ lang = self.getLanguage(self.REQUEST) other = pagename + '_' + lang if self.data.has_key(other): return self.data.get(other).get('data') if self.data.has_key(pagename): return self.data.get(pagename).get('data') return "" def isPage(self, pagename): """ is page """ p = False for pline in self.getProperty('registered_pages'): if pline.split('|')[0] == pagename: return True return p security.declareProtected(perm_edit_page, 'getPageEdit') def getPageEdit(self, REQUEST, pagename): """ return page' content """ lang = self.getLanguage(self.REQUEST) REQUEST.RESPONSE.setHeader('Content-Type', 'text/html;charset=utf-8') if self.data.has_key(pagename+'_'+lang): return "%s" % self.data.get(pagename+'_'+lang).get('data') if self.data.has_key(pagename): return "%s" % self.data.get(pagename).get('data') return "" security.declareProtected(perm_edit_page, '_setPage') def _setPage(self, pagename, value, username): """ set page value """ lang = self.getLanguage(self.REQUEST) pagename = pagename + '_' + lang if not self.history.has_key(pagename): self.history[pagename] = {} if self.data.has_key(pagename): self.history[pagename][self.data[pagename]['timestamp']] = copy.copy(self.data[pagename]) self.data[pagename] = { 'data' : value, 'timestamp' : time.time(), 'who' : username, } self._p_changed = 1 security.declareProtected(perm_edit_page, 'kupu_handler') def kupu_handler(self, REQUEST): """ get new page content from kupu """ from xml.dom.minidom import parseString import re doc = parseString(REQUEST.get('kupu')) bodytag = doc.getElementsByTagName('body')[0] stri = bodytag.toxml() b = re.compile('()|()', re.I) stri = stri.encode('utf-8') res = b.sub('', stri) res = unicode(res, 'utf-8').encode('utf-8') self._setPage(REQUEST.get('pagename'), res, str(REQUEST.AUTHENTICATED_USER)) return REQUEST.RESPONSE.redirect(self.absolute_url()+'/'+REQUEST.get('pagename', 'korpus_index')) security.declareProtected(perm_edit_page, 'editpage') def editpage(self, REQUEST, pagename): """ return to edit form """ #return self.korpus_root().kupudata(pagename=pagename) #return REQUEST.RESPONSE.redirect(self.korpus_root().absolute_url()+'/kupudata.html?pagename='+pagename) return self.korpus_root().restrictedTraverse("kupudata.html")(pagename=pagename) def getPagesList(self, lang): """ return a list of pages for lang """ rp = self.getProperty('registered_pages') res = [] for r in rp: opt = r.split('|') if opt[-1] == '1': for o in opt: if o.startswith(lang+':'): res.append([opt[0], o[3:]]) continue return res Globals.InitializeClass(WWW) class WWWDataProvider(object): """ WWW Data Provider """ adapts(IWWW, IHTTPRequest) implements(IPublishTraverse) def __init__(self, context, request): self.context = context self.default = DefaultPublishTraverse(context,request) def publishTraverse(self, request, name): try: obj = self.default.publishTraverse(request, name) except (NotFound, AttributeError), e: obj = self.context.restrictedTraverse('pagedrawer.html') obj.setPage(name) if self.context.isPage(name): obj.setPage(name) return obj