dispatcher.py :  » Database » PyDO » skunkweb-3.4.4 » pylibs » formlib » 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 » Database » PyDO 
PyDO » skunkweb 3.4.4 » pylibs » formlib » dispatcher.py
# Time-stamp: <03/01/21 13:49:37 smulloni>

######################################################################## 
#  Copyright (C) 2002 Jacob Smullyan <smulloni@smullyan.org>,
#                     Drew Csillag <drew_csillag@yahoo.com>
#  
#      You may distribute under the terms of either the GNU General
#      Public License or the SkunkWeb License, as specified in the
#      README file.
########################################################################

from containers.fieldcontainer import FieldContainer
from form import _getname

class Goto(object):
    def __init__(self, formname, args=None):
        self.formname=formname
        self.args=args
        
    def dispatch(self,
                 form,
                 dispatcher,
                 argdict,
                 ns=None):
        if ns is None:
            ns={}
        form.submit(argdict)
        if form.errors:
            return form
        dispatcher.statemgr.store_form(form)
        form.process(argdict, dispatcher, ns)
        if self.formname:
            return dispatcher.createForm(self.formname, args=self.args)

class Pop(object):
    def __init__(self, args=None):
        self.args=args
        
    def dispatch(self,
                 form,
                 dispatcher,
                 argdict,
                 ns=None):
        if ns is None:
            ns={}
        form.submit(argdict)
        if form.errors:
            return form
        dispatcher.statemgr.store_form(form)        
        form.process(argdict, dispatcher, ns)
        nextformname=dispatcher.statemgr.pop_formname()
        if nextformname:
            f=dispatcher.createForm(nextformname, args=self.args)
            f.setData(dispatcher.statemgr.state.get(nextformname, {}))
            return f

class Push(object):
    def __init__(self, formname, valid=0, args=None):
        self.formname=formname
        self.valid=valid
        self.args=args
        
    def dispatch(self,
                 form,
                 dispatcher, 
                 argdict,
                 ns=None):
        if ns is None:
            ns={}
        if self.valid:
            form.submit(argdict)
            if form.errors:
                return form
        dispatcher.statemgr.store_form(form)                    
        dispatcher.statemgr.push_formname(form.name)
        f=dispatcher.createForm(self.formname, args=self.args)
        f.setData(dispatcher.statemgr.state.get(self.formname, {}))
        return f
    
class AbstractFormDispatcher(object):
    def __init__(self,
                 statemgr,
                 stateVariable='_state'):
        self.statemgr=statemgr
        self.stateVariable=stateVariable

    def createForm(self, formname, argdict=None, args=None):
        raise NotImplementedError

    def getStartForm(self, argdict):
        raise NotImplementedError

    def next(self, form, argdict, ns):
        raise NotImplementedError

    def dispatch(self, argdict, ns):
        # extract the state
        statestr=argdict.get(self.stateVariable)
        if statestr:
            self.statemgr.read(statestr)
        # identify the form being submitted.
        if self.statemgr.stack:
            form=self.createForm(self.statemgr.pop_formname(), argdict)
            # get the next action.
            action=self.next(form,
                             argdict,
                             ns)
            # get the next form, if any
            form=action.dispatch(form,
                                 self,
                                 argdict,
                                 ns)
            
        # if there isn't one, return the start form.
        else:
            form=self.getStartForm(argdict)
            if form:
                form.reset()
        if form:
            # update the state with the new form name
            self.statemgr.push_formname(form.name)
            # set the new state in the form's stateVariable 
            form.fields[self.stateVariable].value=self.statemgr.write()
        return form

class LinearFormDispatcher(AbstractFormDispatcher):
    def __init__(self,
                 statemgr,
                 forms,
                 stateVariable='_state'):
        self.forms=FieldContainer(forms,
                                  fieldmapper=_getname,
                                  storelists=0)        
        AbstractFormDispatcher.__init__(self, statemgr, stateVariable)
        
    def createForm(self, formname, argdict=None):
        return self.forms.get(formname)

    def getStartForm(self, argdict):
        return self.forms[0]

    def next(self, form, argdict, ns):
        ind=self.forms.index(form)+1
        if ind < len(self.forms):
            return Goto(self.forms[ind].name)
        return Goto(None)

__all__=['Goto',
         'Pop',
         'Push',
         'AbstractFormDispatcher',
         'LinearFormDispatcher']
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.