# $Id: Cruft.py,v 1.24 2002/07/04 15:22:45 tarmo Exp $ # # Copyright 2001, 2002 by Fle3 Team and contributors # # This file is part of Fle3. # # Fle3 is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Fle3 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Fle3; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """Contains class Cruft, which is used as a placeholder for miscellaneous utility functions that many objects need.""" __version__ = "$Revision: 1.24 $"[11:-2] from AccessControl import ClassSecurityInfo from urllib import quote import common class Cruft: """Class containing miscellaneous methods that are historical baggage, but possible be gotten rid of, or at least implemented differently or somewhere else.""" security = ClassSecurityInfo() security.declarePublic('find_URL_of_fle_root') def x(self): """...""" for i in self.aq_chain: if hasattr(i,'id'): print i.id else: print i def find_URL_of_fle_root(self, REQUEST): """Return URL of our FLE installation.""" import FLE from string import rfind url = REQUEST.URL1 obj = self while not isinstance(obj, FLE.FLE): obj = obj.parent() url = url[:rfind(url,'/')] return url def find_URL_of_user_info(self, REQUEST): """Return URL of UserInfo.""" import UserInfo from string import rfind url = REQUEST.URL1 obj = self while not isinstance(obj, UserInfo.UserInfo): obj = obj.parent() url = url[:rfind(url,'/')] return url security.declarePublic('find_URL_of_webtop') def find_URL_of_webtop(self, REQUEST): """Return URL of Webtop.""" import Webtop from string import rfind url = REQUEST.URL1 obj = self while not isinstance(obj, Webtop.Webtop): obj = obj.parent() url = url[:rfind(url,'/')] return url security.declarePublic('find_URL_of_group_folder') def find_URL_of_group_folder(self, REQUEST): """Return URL of toplevel group folder or group folder proxy.""" import GroupFolder import GroupFolderProxy from string import rfind url = REQUEST.URL1 obj = self while 1: if isinstance(obj, GroupFolderProxy.GroupFolderProxy): return url if isinstance(obj, GroupFolder.GroupFolder): return url obj = obj.parent() url = url[:rfind(url,'/')] return url security.declarePublic('find_URL_of_thread_start_node') def find_URL_of_thread_start_node(self, REQUEST): """Return URL of starting note.""" import CourseContext from string import rfind url = REQUEST.URL1 obj = self while not isinstance(obj.parent(), CourseContext.CourseContext): obj = obj.parent() url = url[:rfind(url,'/')] return url security.declarePublic('find_URL_of_course') def find_URL_of_course(self, REQUEST): """Return URL of starting note.""" obj = self while not obj.meta_type == 'Course': obj = obj.parent() return obj.absolute_url() security.declarePublic('find_URL_of_course_context') def find_URL_of_course_context(self, REQUEST): """Return URL of starting note.""" import CourseContext from string import rfind url = REQUEST.URL1 obj = self while not isinstance(obj, CourseContext.CourseContext): obj = obj.parent() url = url[:rfind(url,'/')] return url security.declarePublic('get_current_user') def get_current_user(self, REQUEST): """Extract current user's name from the REQUEST object.""" return str(REQUEST.AUTHENTICATED_USER) security.declarePublic('urlquote') def urlquote(self, text): """urlquote given text.""" return quote(text) def get_url_to_object(self,obj): # Make a list of parents without the Application object # (from obj to FLE) objs = obj.aq_inner.aq_chain[:-1] objs.reverse() path='/'.join([x.getId() for x in objs])+'/' return path def get_object_of_url(self,url,base): path=url.split('/') path.pop() # remove last entry path.reverse() if path[-1]=='http:': # remove protocol path.pop() path.pop() path.pop() # remove first (root) entry obj = base.getPhysicalRoot() try: while path: oname=path.pop() obj=getattr(obj,oname).__of__(obj) return obj except: #raise 'Internal link error, object not found',str(obj)+' - '+oname return None def set_undefined_page(self, message, REQUEST): """Set REQUEST.RESPONSE.redirect, so that we go to undefined page and give an explanation.""" message = common.quote_html_hack(message) REQUEST.RESPONSE.redirect(self.state_href(REQUEST, "undefined_page?explanation=%s" % message)) security.declarePublic('to_str_list') # Using eval would be simpler and more general, but # we want to be paranoid. def to_str_list(self, s): """Reverse str representation of list of strings back to list.""" return [x.strip() for x in s[1:-1].replace("'", "").split(',')] # EOF