# -*- 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 zope.interface import implements import Package from Session import Session from utils import gen_key from interfaces import ICaldoz import logging LOG = logging.getLogger('Caldoz') class Caldoz(Folder, Persistent, Explicit): """ Caldoz main class """ meta_type = 'Caldoz' security = ClassSecurityInfo() security.declareObjectPublic() implements(ICaldoz) manage_options = Folder.manage_options # setup methods security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.setupTempFolder() def setupTempFolder(self): """ setup temporary folder for sessions """ self.manage_addProduct['TemporaryFolder'].constructTemporaryFolder('sessions', 'Sessions') def getSkinsFolderName(self): return "portal_skins" # general methods 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) def managerHandler(self, REQUEST=None, package_id=None, reloadButton=None): """ manager handler """ if reloadButton and package_id: pac = getattr(self, package_id) pac.manage_afterAdd(pac, self) if REQUEST is not None: return REQUEST.RESPONSE.redirect('caldoz_manager.html') return "reloaded" # methods related with package security.declareProtected('View', 'addNewPackage') def addNewPackage(self, REQUEST, file): """ add new package. file is zipfile used by caldoz directly """ 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, clientID="browser") self._setObject(pac.id, pac) import os os.unlink(fname) return REQUEST.RESPONSE.redirect(pac.id) def addNewPackage2(self, file=None, clientid=''): """ add new package, return credentials file is base64 encoded string can be called from remote clients """ import zipfile import tempfile import base64 error = 0 error_explain = 'Upload was successful' fname = tempfile.mkstemp()[1] LOG.info('fname:'+fname) 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: LOG.info('will create package') pac = Package.Package(file, clientID=clientid) self._setObject(pac.id, pac) else: LOG.info('not creating package'+str(error)) import os os.unlink(fname) if error == 1: error_explain = 'Uploaded file was not a Zip file.' LOG.info(error) 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 security.declareProtected('View', 'getPackages') def getPackages(self): return self.objectValues('CaldozPackage') 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') print "================================== WE SET A COOKIE!!! ==========" 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='+self.absolute_url()+'/caldoz_manager.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(self.absolute_url()) sess = getattr(self.sessions, sess_key, None) if sess is None: return REQUEST.RESPONSE.redirect(self.absolute_url()) 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