# -*- coding: utf-8 # -*- Mode: Python; py-indent-offset: 4 -*- # $Id: __init__.py 33 2006-06-02 06:49:13Z vahur $ # # Copyright (c) 2007, 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. """ Feedmoot main module """ import Globals from Globals import Acquisition, Persistent from Acquisition import aq_base, aq_inner, aq_parent, Explicit from AccessControl import ClassSecurityInfo from OFS.Folder import Folder from Products.PageTemplates.PageTemplateFile import PageTemplateFile from Products.CMFCore.Skinnable import SkinnableObjectManager from Session import Session import time import json class Feedmoot(SkinnableObjectManager, Folder, Persistent, Explicit): """ Feedmoot """ meta_type = "Feedmoot" security = ClassSecurityInfo() security.declareObjectPublic() manage_options = Folder.manage_options security.declarePrivate('manage_afterAdd') def manage_afterAdd(self, item, container): self.setupSkins() self.setupTempFolder() self.setupFeeds() 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 setupFeeds(self): """ asd """ from Feeds import Feeds feeds = Feeds() self._setObject(feeds.id, feeds) def setupTempFolder(self): """ setup temporary folder """ self.manage_addProduct['TemporaryFolder'].constructTemporaryFolder('sessions', 'Sessions') def getSkinsFolderName(self): return "portal_skins" def index_html(self, REQUEST): """ index html """ return self.feedmoot_index() # Helper methods def _getSpaces(self): return self.objectValues('Space') def _getSessions(self): return self.sessions.objectValues('Session') def _getSession(self, session_key): for s in self._getSessions(): #print "comparing", s._getSessionKey(), session_key, time.localtime(time.time()) if s._getSessionKey() == session_key: if not s.isValid(): self.sessions._delObject(s.id) return {'errno':2, 'errstr':'Session timed out'} s._setLastUsed() return s if not s.isValid(): self.sessions._delObject(s.id) continue return {'errno':1, 'errstr':'Session not found'} def _validate(self, val): return not isinstance(val, dict) def _getSpace(self, session=None, session_key=None): if session_key: session = self._getSession(session_key) if not self._validate(session): return session if session: return getattr(self, session._getSpaceId()) # API methods def makeContract(self, REQUEST): """ make a contract with user """ from utils import gen_key key = gen_key('contract') from Space import Space space = Space(gen_key('space'), key) self._setObject(space.id, space) print "made a space", space return json.write('{"errno":"0","resp":{"key":"%s"}}' % key) def initSession(self, token): """ initialize session """ from utils import gen_key for s in self._getSpaces(): if s._getAccessToken() == token: session_key = gen_key('session') sess = Session(session_key, s.getId()) self.sessions._setObject(sess.id, sess) res = {'errno':0, 'resp':{'session_key': session_key}} print "FM: initSession, space id:", s.id, " token:", token return json.write(res) return json.write({'errno':3, 'errstr':'space not found'}) def closeSession(self, session_key): """ close session """ sess = self._getSession(session_key) if not self._validate(sess): return json.write(sess) self.sessions._delObject(sess.id) print "FM: closed session" return json.write({'errno': 0}) def getCookie(self, REQUEST, session_key, came_from): """ set cookie """ REQUEST.RESPONSE.setCookie('session_key', session_key, expires="Wed, 19 Feb 2020 14:00:00 GMT") return REQUEST.RESPONSE.redirect(came_from) def listPages(self, REQUEST): """ returns a list of all pages in space """ sess = self._getSession(REQUEST.get('session_key')) if not self._validate(sess): return sess space = self._getSpace(sess) res = [] for x in space._getPages(): res.append({'view':x._getViewToken(), 'edit':x._getEditToken(), 'url':x.absolute_url(), 'title':x.getTitle()}) return json.write({'errno':0, 'resp':{'pages': res}}) def newPage(self,REQUEST, session_key='', title=''): """ create a new page """ pipeline = '' sk = None if session_key: sk = session_key else: sk = REQUEST.get('session_key') session = self._getSession(sk) if not self._validate(session): return json.write(session) space = self._getSpace(session) if not space: return json.write({'errno':3, 'errstr':'Space not found'}) res, tokens = space._newPage(title, pipeline) value = json.write({'errno': res.get('errno', 0), 'errstr': res.get('errstr', ''), 'resp':tokens}) print "newPage:", value return value def setPipeline(self, REQUEST, session_key='', edit_token='', pipeline=''): """ adds a pipeline to page requires: session_key, edit_token for page """ session = self._getSession(session_key) if not self._validate(session): return session space = self._getSpace(session) if not space: return json.write({'errno':3, 'errstr':'Space not found'}) page = space._getPage(edit_token=edit_token, json=0) if not page: return json.write({'errno':4, 'errstr':'Page not found'}) res = page._setPipeline(pipeline) return json.write({'errno':res.get('errno', 0)}) def getPipeline(self, REQUEST, session_key='', edit_token=''): """ get page's pipeline """ session = self._getSession(session_key) if not self._validate(session): return session space = self._getSpace(session) if not space: return json.write({'errno':3, 'errstr':'Space not found'}) page = space._getPage(edit_token=edit_token) print "page", edit_token, session_key if not page: return json.write({'errno':4, 'errstr':'Page not found'}) return json.write(page._getPipeline()) def deletePage(self, REQUEST, session_key='', edit_token=''): """ delete a page """ session = self._getSession(session_key) if not self._validate(session): return session space = self._getSpace(session) if not space: return json.write({'errno':3, 'errstr':'Space not found'}) res = space._deletePage(edit_token) return json.write(res) def aab(self, REQUEST): """ asdasd """ import os L = ['/export/home/vahur/zope/Z-py24-I/Products/Feedmoot/kala.py'] k = '/export/home/vahur/zope/Z-py24-I/Products/Feedmoot/kala.py' print os.spawnvpe(os.P_NOWAIT, k, L, os.environ) return "ok" def aac(self, REQUEST): """ aqsdasd """ import os try: print os.wait() except OSError: pass return "ok" def setTitle(self, REQUEST, session_key='', edit_token='', title=''): """ set page's title """ session = self._getSession(session_key) if not self._validate(session): return session space = self._getSpace(session) if not space: return json.write({'errno':3, 'errstr':'Space not found'}) page = space._getPage(edit_token=edit_token) if not page: return json.write({'errno':4, 'errstr':'Page not found'}) return json.write(page.setTitle(title)) def json(self, REQUEST, view_token='', url='', var='', elem=''): """ json """ d = url.split('/') s = getattr(self, d[0]) p = getattr(s, d[1]) js = "" if view_token != p._getViewToken(): js += 'datas['+var+'] = "invalid request";' else: data = p.xhtml(REQUEST).replace('\n', '') data = data.replace("'", "\\'"); js = "datas["+var+"] = '
"+p.getTitle()+"
';" js += "datas["+var+"] += '"+data+"';\n"; js += "display("+var+",'"+elem+"');" REQUEST.RESPONSE.setHeader('Content-Type', 'application/x-javascript') return js Globals.InitializeClass(Feedmoot) def manage_addFeedmoot(self, REQUEST, id): """ instantiate Feedmoot """ agg = Feedmoot(id) self._setObject(id, agg) if REQUEST: return self.manage_main(self, REQUEST) manage_addFeedmootForm = PageTemplateFile('skins/addFeedmootForm.pt', globals())