redirect.py :  » Web-Frameworks » Spyce » spyce-2.1 » modules » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Spyce 
Spyce » spyce 2.1 » modules » redirect.py
##################################################
# SPYCE - Python-based HTML Scripting
# Copyright (c) 2002 Rimon Barr.
#
# Refer to spyce.py
# CVS: $Id: redirect.py 640 2005-04-25 04:30:37Z jbe $
##################################################

from spyceModule import spyceModule
import spyceException, spyceUtil, spyce

__doc__ = '''Redirect module provides support for different kinds of request
redirection, currently: internal, external and externalRefresh.
- internal: flush the current output bufffer (assuming it has not been sent)
  and raise an appropriate exception that will start the processing of the
  new file, if left to percolate all the way to the Spyce engine. The
  browser url does not change.
- external: send an HTTP return code that signals a permanent or temporary
  page move, depending on the boolean parameter. Spyce file execution will
  continue to termination, but the output buffer is flushed at the end and a
  special redirect document is generated. The browser is expected, as per the
  standard, to immediately redirect and perform a new request, thus the url
  will change.
- externalRefresh: send an HTTP Refresh header that requests a page refresh to
  a (possibly) new location within some number of seconds. The current Spyce
  page will be displayed until that time. This is often used to display a page
  before redirecting the browser to a file download.
'''

class redirect(spyceModule):
  def start(self):
    self.clear = 0
  def finish(self, theError=None):
    if not theError:
      if self.clear:
        self._api.getModule('response').clear()
  def internal(self, url=None, filename=None):
    "Perform an internal redirect."
    if (url and filename) or not (url or filename):
      raise 'Specify exactly one of {url, filename}'
    spyce.DEBUG('internal redirect to %s at the behest of %s' % (url, self._api.getModule('request').stack()[-1]))
    self._api.getModule('response').clearHeaders()
    self._api.getModule('response').clear()
    if url:
      filename = spyceUtil.url2file(url, self._api.getFilename())
    raise spyceException.spyceRedirect(filename)
  def external(self, url, permanent=0):
    "Perform an external redirect."
    spyce.DEBUG('external redirect to %s at the behest of %s' % (url, self._api.getModule('request').stack()[-1]))
    self._api.getModule('response').addHeader('Location', url)
    if permanent:
      self._api.getModule('response').setReturnCode(self._api.getResponse().RETURN_MOVED_PERMANENTLY)
    else:
      self._api.getModule('response').setReturnCode(self._api.getResponse().RETURN_MOVED_TEMPORARILY)
    self.clear = 1
  def externalRefresh(self, url, sec=0):
    "Perform an external redirect, via refresh."
    self._api.getModule('response').addHeader('Refresh', '%d; URL=%s' % (sec, url))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.