# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- # $Id$ # # Copyright (c) 2006, Vahur Rebas # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ Main module """ __version__ = "$Revision$"[11:-2] import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo, getSecurityManager from OFS.Folder import Folder from Products.PageTemplates.PageTemplateFile import PageTemplateFile from Products.CMFCore.Skinnable import SkinnableObjectManager import Package from Session import Session from utils import gen_key class Caldoz(SkinnableObjectManager, Folder, Persistent, Explicit): """ Caldoz main class """ meta_type = 'Caldoz' security = ClassSecurityInfo() security.declareObjectPublic() manage_options = Folder.manage_options # setup methods security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.setupSkins() self.setupTempFolder() security.declarePrivate('setupSkins') def setupSkins(self): """ setup skins system """ from Products.CMFCore.utils import manage_addTool add_tool = self.manage_addProduct['CMFCore'].manage_addTool add_tool('CMF Skins Tool', None) sk_tool = getattr(self, self.getSkinsFolderName()) sk_tool.manage_addFolder('default_custom', 'Customize pages here for default skin') paths = "main, default_custom" sk_tool.addSkinSelection('default', paths, make_default = 1) from Products.CMFCore.DirectoryView import addDirectoryViews addDirectoryViews(sk_tool, 'skins', globals()) def setupTempFolder(self): """ setup temporary folder for sessions """ self.manage_addProduct['TemporaryFolder'].constructTemporaryFolder('sessions', 'Sessions') def getSkinsFolderName(self): return "portal_skins" # general methods def index_html(self, REQUEST): """ index -> caldoz_index """ return REQUEST.RESPONSE.redirect(self.caldoz_root().absolute_url()+'/caldoz_index') def caldoz_root(self): """ root """ return self def getRelativeURL(self, content): """ getthe URL for an object, relative to the root folder """ return '/'.join( self.getRelativeContentPath(content) ) def getRelativeContentPath(self, content): """ Get the path for an object, relative to the iva root. """ portal_path_length = len( self.caldoz_root().getPhysicalPath() ) content_path = content.getPhysicalPath() return content_path[portal_path_length:] # ... def checkPermission(self): """ check permission """ sm = getSecurityManager() return sm.checkPermission('Manager portal', self) # methods related with package security.declareProtected('View', 'addNewPackage') def addNewPackage(self, REQUEST, file): """ add new package. file is zipfile """ import zipfile import tempfile fname = tempfile.mkstemp()[1] f = open(fname, "w+b") f.write(file.read()) f.close() if not zipfile.is_zipfile(fname): raise 'Error: file is not a zip file' file.seek(0) pac = Package.Package(file) self._setObject(pac.id, pac) import os os.unlink(fname) return REQUEST.RESPONSE.redirect(pac.id) security.declareProtected('View', 'getPackages') def getPackages(self): return self.objectValues('CaldozPackage') def addNewPackage2(self, file=None): """ add new package, return credentials file is base64 encoded string """ import zipfile import tempfile import base64 error = 0 error_explain = 'Upload was successful' fname = tempfile.mkstemp()[1] f = open(fname, "w+b") f.write(file.read()) f.close() if not zipfile.is_zipfile(fname): error = 1 file.seek(0) pac = None if not error: pac = Package.Package(file) self._setObject(pac.id, pac) import os os.unlink(fname) if error == 1: error_explain = 'Uploaded file was not a Zip file.' ntitle = pac.getMainTitles()[0][0] msg = """ %s %s %s %s %s %s """ % ( pac.getId(), str(error), error_explain, ntitle, pac._getPublicKey(), pac._getPrivateKey() ) return msg def getTOC(self, session_id): """ getTOC """ print "getTOC", session_id sess = getattr(self.sessions, session_id, None) if sess is None: return "NO TOC FOR YOU" pid = sess.package_id p = getattr(self, pid, None) if p is not None and (p._getPublicKey() == sess.access_key or p._getPrivateKey() == sess.access_key): return p.getTOC() return "NO TOC FOR YOU!" def getNavigation(self, session_id): """ get navigation """ sess = getattr(self.sessions, session_id, None) if sess is None: return "NO NAVIGATION" pid = sess.package_id p = getattr(self, pid, None) if p is None: return "Cannot find navigation" if p._getPublicKey() != sess.access_key and p._getPrivateKey() != sess.access_key: return "Invalid access key" msg = """""" msg += """""" msg += """""" msg += """""" return msg def getCookie(self, REQUEST, session_key, came_from): """ get cookie """ REQUEST.RESPONSE.setCookie('session_key', session_key, expires='Wed, 19 Feb 2020 14:00:00 GMT') return REQUEST.RESPONSE.redirect(came_from) security.declareProtected('Manager portal', 'manager_login') def manager_login(self, REQUEST): """ manager logs in """ uname = str(getSecurityManager().getUser()) sess_id = gen_key('session') s = Session(sess_id, None, None, uname) if self.checkPermission(): s._setManager(True) self.sessions._setObject(s.id, s) return REQUEST.RESPONSE.redirect('getCookie?session_key='+sess_id+'&came_from=index_html') def initialize_session(self, package_id=None, access_key=None, uname='Anonymous User'): """ initialize session. If someone knows the key, go ahead """ p = None pid = package_id if pid is not None: p = getattr(self, package_id) else: for x in self.getPackages(): if x._getPrivateKey() == access_key: p = x pid = x.getId() break session_id = None if p._getPublicKey() == access_key or p._getPrivateKey() == access_key: # TODO: create session here session_id = gen_key('session') s = Session(session_id, pid, access_key, uname) self.sessions._setObject(s.id, s) return session_id def redirectToPackage(self, REQUEST): """ redirect to package """ sess_key = REQUEST.get('session_key', None) if sess_key is None: return REQUEST.RESPONSE.redirect('index_html') sess = getattr(self.sessions, sess_key, None) if sess is None: return REQUEST.RESPONSE.redirect('index_html') return REQUEST.RESPONSE.redirect(self.caldoz_root().absolute_url()+'/'+sess.package_id) Globals.InitializeClass(Caldoz) def manage_addCaldoz(self, REQUEST, id): """ instantiate Caldoz """ caldoz = Caldoz(id) self._setObject(id, caldoz) if REQUEST: return self.manage_main(self, REQUEST) manage_addCaldozForm = PageTemplateFile('skins/addCaldozForm.pt', globals()) # EOF