[zopeorg-checkins] CVS: Products/Redirector - README.txt:1.1 Redirector.py:1.1 VERSION.txt:1.1 __init__.py:1.1 addRedirector_form.dtml:1.1 manageRedirector.dtml:1.1

Sidnei da Silva sidnei at x3ng.com.br
Fri May 30 11:17:53 EDT 2003


Update of /cvs-zopeorg/Products/Redirector
In directory cvs.zope.org:/tmp/cvs-serv19195/Redirector

Added Files:
	README.txt Redirector.py VERSION.txt __init__.py 
	addRedirector_form.dtml manageRedirector.dtml 
Log Message:
Adding products needed for migration of NZO

=== Added File Products/Redirector/README.txt ===
Redirector

  Copyright © 1999 "Alexander Staubo":mailto:alex at mop.no.
   Extended 1999 "Dylan Jay":mailto:djay at slarken.org.au.


  <pre>
  ANY USE BY YOU OF THE SOFTWARE IS AT YOUR OWN RISK. THE SOFTWARE IS PROVIDED
  FOR USE "AS IS" WITHOUT WARRANTY OF ANY KIND. TO THE MAXIMUM EXTENT
  PERMITTED BY LAW, THE AUTHOR (ALEXANDER STAUBO) DISCLAIMS ALL WARRANTIES OF
  ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, IMPLIED
  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. THE AUTHOR (ALEXANDER STAUBO) IS NOT OBLIGATED TO PROVIDE
  ANY UPDATES TO THE SOFTWARE. SO THERE.
  </pre>

  What it Does

    Redirector is an experimental folder product for redirecting
    object requests. Any request to a subobject (whether it
    exists or not) in the folder is redirected to a designated
    target URL. A flexible mechanism for mapping specific URLs is
    also provided.

    For example, you might have an URL such as *http:
    //oldserver/foo* (running Zope, of course) which you've moved
    to your brand new server *http://newserver/bar* (Zope not
    required). Once you have deleted or moved the foo folder or
    object out of the way, create a Redirector object called
    *foo*, set its Target property to *http://newserver/bar*,
    following which requests for *http://oldserver/foo* will then
    be redirected to *http://newserver/bar*. Given this, if a
    user then requests *http://oldserver/foo/fnargh*, the request
    will simply be redirected to *http://newserver/bar/fnargh*. In 
    fact a call such as *http://oldserver/foo/fnargh/something/doSomething?param=blah*
    will also correctly redirect to 
    *http://newserver/bar/fnargh/something/doSomething?param=blah*

    The Redirector object also supports exceptions to this rule:
    For example, if the old server had a subobject called *milk*,
    which now exists on the new server under the name of *honey*,
    then you can create a mapping which simply maps the old
    object to the new object. Mappings support (or rather,
    require) regular expressions to be used, so you could also
    map any object starting with the letter *M* to a specific
    object. Regular expression groups are supported. A \n
    where n is an integer in the replacement field will be replaced
    by the corresponding group in the FROM: field. See the python
    regular expression syntax for clarification on this.

    Added bonus feature: If you set the Redirector to "hard
    mapping" mode, it will fall back to the default target URL if
    none of the mappings match the requested URL. That means --
    if you're like me, converting a dull old Netscape server to a
    new Zope site with lots of magic fairy dust added -- that you
    can create mappings for all the old documents that should be
    redirected to their Zopefied counterpart URLs, but for all
    the documents that are deprecated on the new server, the
    redirection will be set to the server's root folder.

  Documentation

    File under "self-explanatory". In short, what you're seeing
    here is what you get.

  Bugs and Such

    This being an experimental Zope Product, I can't promise that
    it will work as promised. Indeed, I won't promise anything. I
    can only give the typical excuse that "it works here".

    Please report problems to "me":mailto:alex at mop.no. If you
    include the line "This is important and you're just one dandy
    programmer, Alex!" I'll promise to give you lots of priority.

  Future Improvements


=== Added File Products/Redirector/Redirector.py ===
# Redirector 1.1 Copyright (c) 1999 Alexander Staubo <alex at mop.no>
#     Extended 9/1999 Dylan Jay <djay at slarken.org.au>           
# See README.txt for more information about this Zope product.

# $Header: /var/cvs-zopeorg/Products/Redirector/Redirector.py,v 1.1 2003/05/30 15:17:52 sidnei Exp $

__version__ = '$Revision: 1.1 $'[11:-2]

from Globals import Persistent, HTMLFile, HTML, MessageDialog
from AccessControl.Role import RoleManager
import Acquisition
import sys
import string
import OFS
import Globals
import time
import copy
import re

manage_addRedirectorForm = HTMLFile('addRedirector_form', globals())

def manage_addRedirector(self, id, title = '', Target = '',
  Mappings = {}, HardMap = 0, REQUEST = None):
  '''Add a Redirector into the system'''

  Obj = Redirector()
  Obj.id = id
  Obj.title = title
  Obj._init(Target, Mappings, HardMap)
  self._setObject(id, Obj)
  if REQUEST:
    return self.manage_main(self, REQUEST)

class RedirectorBase(Acquisition.Implicit, OFS.Folder.Folder,
  RoleManager):
  '''Redirector base class'''

  meta_type = 'Redirector'
  icon = 'misc_/Redirector/icon'
  manage = manage_main = HTMLFile('manageRedirector', globals())

  manage_options = (
    {'icon': '', 'label': 'Edit', 'action': 'manage_main',
    'target': 'manage_main'},
    {'icon': '', 'label': 'Security', 'action': 'manage_access',
    'target': 'manage_main'},
  )

  __ac_permissions__ = OFS.Folder.Folder.__ac_permissions__ + (
    ('View management screens', ('manage',)),
    ('Change configuration', ('manage_makeChanges',)),
    ('Use Redirector services',('',)),
  )

  def __init__(self):
    '''Initialize'''

    self.Target = ''
    self.Mappings = {}
    self.HardMap = 0

  def _init(self, Target, Mappings = {}, HardMap = 0):
    '''Initialize with configuration'''

    self.Mappings = copy.copy(Mappings)
    self.Target = Target
    self.HardMap = HardMap

  def manage_makeChanges(self, title = '', Target = '',
    HardMap = 0, MapFrom = [], MapTo = [], REQUEST = None):
    '''Perform changes'''

    self.title = title

    NewMap = {}
    i = 0
    while i < len(MapFrom):
      NewMap[MapFrom[i]] = MapTo[i]
      i = i + 1
    for k in NewMap.keys():
      if not (k and NewMap[k]):
        del NewMap[k]

    self._init(Target, NewMap, HardMap)

    if REQUEST:
      return MessageDialog(
        title = 'Changed %s' % self.id,
        message = '%s has been updated' % self.id,
        action = REQUEST['URL1'] + '/manage_main',
        target = 'manage_main')

  def __bobo_traverse__(self, REQUEST, Name = ''):
  
    if Name[:6] != 'manage':
      import ExtensionClass
      class doTraverse(ExtensionClass.Base):
         def __init__(self, path, Mappings, Target, HardMap):
            self.path = path
            self.Mappings = Mappings
            self.Target = Target
            self.HardMap = HardMap
         
         def __bobo_traverse__(self, REQUEST, Name = ''):
            self.path = self.path + '/' + Name
            return self.__class__(self.path, self.Mappings, self.Target, self.HardMap)
            
         def __call__(self, REQUEST):
            path = self.path
            if REQUEST['QUERY_STRING']:
              path = path + "?" + REQUEST['QUERY_STRING']
            for k in self.Mappings.keys():
              if re.match(k, path):
                 path = re.sub(k, self.Mappings[k], path)
                 break
            else:
              if self.HardMap:
                 raise 'Redirect', self.Target
            def Merge(Target, Url):
              if Url[:5] == 'http:' or Url[:4] == 'ftp:':
                 return Url
              else:
                 return Target + Url
            raise 'Redirect', Merge(self.Target , path)
            
      return doTraverse(Name, self.Mappings, self.Target, self.HardMap)
     

    if hasattr(self, 'aq_base'):
      b = self.aq_base
      if hasattr(b, Name):
        return getattr(self, Name)
    try:
      return self[Name]
    except:
      return getattr(self, Name)

  def index_html(self):
    raise 'Redirect', self.Target

class Redirector(RedirectorBase, Persistent):
  '''Redirector product'''


=== Added File Products/Redirector/VERSION.txt ===
1.1


=== Added File Products/Redirector/__init__.py ===
# Redirector 1.0 Copyright (c) 1999 Alexander Staubo <alex at mop.no>
# See README.txt for more information about this Zope product.

# $Header: /var/cvs-zopeorg/Products/Redirector/__init__.py,v 1.1 2003/05/30 15:17:52 sidnei Exp $

__version__ = '$Revision: 1.1 $'[11:-2]

__doc__='''Redirector Product Initialization'''

import Redirector

def initialize(context):
  context.registerClass(
    Redirector.Redirector,
    permission = 'Add Redirector objects',
    constructors = (
      Redirector.manage_addRedirectorForm,
      Redirector.manage_addRedirector),
    icon = 'www/RedirectorIcon.gif',
    legacy = (
      ('manage_addRedirector', Redirector.manage_addRedirector),
      ('manage_addRedirector_form',
      Redirector.manage_addRedirectorForm),
    )
  )


=== Added File Products/Redirector/addRedirector_form.dtml ===
<HTML>
<HEAD>
<TITLE>Add Redirector</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
<H2>Add Redirector</H2>

<P>
A redirector object transparently redirects all sub-requests to another URL.
<p>
For example, if the redirector object is <tt>/Zope</tt> and the target is set to
http://www.zope.org, then any request for <tt>/Zope/foo.html</tt> will be redirected
to <tt>http://www.zope.org/foo.html</tt>.
<p>
<i>Target</i> must be an absolute or relative URL. If it denotes a folder, it
<b>must</b> end with a slash, thus: <tt>http://www.zorg.org/Projects/</tt>.
<p>

<FORM ACTION="manage_addRedirector" METHOD="POST">
<TABLE CELLSPACING="2">
<TR VALIGN="TOP">
  <TH ALIGN="LEFT">Id</TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="TEXT" NAME="id" SIZE="40" value="Redirector">
  </TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT"><EM>Title</EM></TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="TEXT" NAME="title" SIZE="40">
  </TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT">Target</TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="TEXT" NAME="Target" SIZE="40" VALUE="">
  </TD>
</TR>
<TR VALIGN="TOP">
<TD>&nbsp;</TD>
<TD><BR><INPUT TYPE="SUBMIT" VALUE=" Add "></TD>
</TR>
</TABLE>
</FORM>

</BODY>
</HTML>


=== Added File Products/Redirector/manageRedirector.dtml ===
<HTML>
<HEAD>
<TITLE>Edit Redirector</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" LINK="#000099" VLINK="#555555">
<!--#var manage_tabs-->
<p>
<i>Target</i> must be an absolute or relative URL. If it denotes a folder, it
<b>must</b> end with a slash, thus: <tt>http://www.zope.org/Projects/</tt>.
<p>
<i>Mappings</i> is a list of URL mappings. The "From" part is compared against
the requested sub-URL (including query string if any) and, if matching, translated into the "To" part. If the
"To" part starts with <tt>http:</tt> or <tt>ftp:</tt>, the URL is preserved;
otherwise, it is appended to the URL in <i>Target</i>. Groups matched in the "From" part
will replace "\n" type strings in the "To" part, where "n" is an integer.

<p>

<FORM ACTION="manage_makeChanges" METHOD="POST">
<TABLE CELLSPACING="2">
<TR VALIGN="TOP">
  <TH ALIGN="LEFT">Id</TH>
  <TD ALIGN="LEFT"><!--#var id--></TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT"><EM>Title</EM></TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="TEXT" NAME="title" SIZE="40" VALUE="<!--#var title-->">
  </TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT">Target (absolute or relative URL)</TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="TEXT" NAME="Target" SIZE="40" value="<dtml-var Target html_quote>">
  </TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT"><i>Hard mapping mode</i></TH>
  <TD ALIGN="LEFT">
    <INPUT TYPE="checkbox" NAME="HardMap" <dtml-if HardMap>checked</dtml-if> value="1">
    If checked, mappings are required to match. If a match fails, <i>Target</i> is used
     by default, ignoring the requested URL.
  </TD>
</TR>
<TR VALIGN="TOP">
  <TH ALIGN="LEFT"><i>Mappings</i></TH>
  <TD ALIGN="LEFT">
    <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <th>From (regular expression)</th>
      <th>&nbsp;</th>
      <th>To</th>
    </tr>
    <tr>
      <td><INPUT TYPE="TEXT" NAME="MapFrom:list" SIZE="30">
      </td>
      <td>&nbsp;</td>
      <td><INPUT TYPE="TEXT" NAME="MapTo:list" SIZE="30">
      </td>
    </tr>
    <dtml-in "Mappings.keys()">
    <tr>
      <td><INPUT TYPE="TEXT" NAME="MapFrom:list" SIZE="30"
        value="<dtml-var sequence-item html_quote>">
      </td>
      <td>&nbsp;</td>
      <td><INPUT TYPE="TEXT" NAME="MapTo:list" SIZE="30"
        value="<dtml-var "Mappings[_['sequence-item']]" html_quote>">
      </td>
    </tr>
    </dtml-in>
    </table>
  </TD>
</TR>

<TR VALIGN="TOP">
<TD></TD>
<TD><BR><INPUT TYPE="SUBMIT" VALUE="Change"></TD>
</TR>
</TABLE>
</FORM>

</BODY>
</HTML>





More information about the zopeorg-checkins mailing list