Re: forwarding req.values ?

John Dickinson <[email protected]>
Newsgroups gmane.comp.python.webware
Message-ID <[email protected]>
I hope the following helps explain the separation for an MVC design I 
described earlier. It's really just off the top of my head, so please 
forgive any minor errors/typos. The general structure is the idea I'm 
trying to get across.

I have used a structure very similar to the code below, with good 
results. I know it's not the only way to do things, but it has served me 
well.

--John

my_servlet.py
--------------
# my_servlet.py could also be a psp page, I'm sure, but since I don't 
really have any experience with psp...
from WebKit.Page import Page
from some.other.place import MyGenericAPI # a facade class that offers a 
static API for business logic

class my_servlet(Page):
    # preAction and postAction are changed to ensure that actions don't 
write anything out to the screen (they just set state for writeHTML())
    def preAction(self,name):
       pass
    def postAction(self):
       self.writeHTML()
    def actions(self):
       return Page.actions() + ['login']
    def login(self):
       login_id = self.request().fields().get('login_id','').strip()
       passwd = self.request().fields().get('login_pw','').strip()
       api = MyGenericAPI() # this could be implemented in some site 
page as an attribute of the class
       good_login,err_msg = api.VerifyLogin(login_id,passwd)
       if good_login is True:
          # use sendRedirectAndEnd so that we won't call writeHTML again
          self.response().sendRedirectAndEnd('the_next_page_after_login.py')
       else:
          self.displayMessage = err_msg # this could be more elegant, 
but this is an example, right?
          # this action will now fall out to writeHTML

    def writeContent(self):
       # best way:
       
#render_template_for_this_page(self.displayMessage,blah,blah,whatever)
       # slightly more straightforward but harder to maintain way:
       # (I've left out the css niceties, etc)
       wl = self.writeln
       if self.displayMessage != "":
            wl('Error: %s' % self.displayMessage)
       wl('<form action="my_servlet.py" method="post">')
       wl('Login ID:<input type="text" name="login_id" />')
       wl('Password:<input type="password" name="login_pw" />')
       wl('<input type="submit" name="_action_login" value="Log Me In!" />')
       wl('</form>')

some.other.place.py
--------------------
import login_mod
class MyGenericAPI(object):
    # the implementation of these functions may change, but the 
functions themselves should not change much (if ever)
    def VerifyLogin(self,login_id,pw):
       return login_mod.verify(login_id,pw)

login_mod.py
-------------
import storage_mod
class login_class(object):
    user_id = None
    passwd = None
def load(which):
    return storage_mod.load(which)
def save(which,where):
    storage_mod.save(which,where)
def verify(some_id,some_pw):
    login_class_instance = storage_mod.load(some_id)
    if login_class_instance.passwd == some_hash(some_pw):
       return true
    else:
       return false

storage_mod.py
---------------
# whatever storage method you are using, but basically, this is a common 
gateway for all storage stuff (like MyGenericAPI above)
import magic
def load(request):
    return magic.sprinkles(request)
def save(which,where):
    magic.pixie_dust(which,where)

maker joe wrote:
> hi john,
> can i have a simple CRUD code example to see how it looks like ?
> kind regards
> makerjoe

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.