Sort of best-pratices question

6 views
Skip to first unread message

Guyon Morée

unread,
May 2, 2005, 8:15:51 AM5/2/05
to cherryp...@googlegroups.com
Hi everyone, I have a question regarding best-practices.

Say you have an input form. I can imagine having X states with this
form:


- Show the empty form to be filled out
- validate form input and either:
- save the data and show OK-screen
- discard the data, show the errors and give the user
the possibility to re-enter

Ok, this is quite some functionality for only 1 form, what do yo think
is a best practice to keep all of this compact in a CherryPy class?


cheers,

Guyon

midtoad

unread,
May 4, 2005, 11:17:23 AM5/4/05
to cherryp...@googlegroups.com
no one answered your question, so I'll take a shot in spite of being
new to CherryPy. I can see the possibility to have all of your
functionality in a single class, with several methods. where you would
do better with several classes is where you begin to do a variety of
things with the data.

In the app I am building, I tried to do a lot of stuff using a single
default method, and it ended up getting very messy, with a lot of 'if'
statements.

So, try it with one class, and if you don't like the results, re-build
! CherryPy is straightforward enough that you can restructure quickly.


cheers
S

Sylvain Hellegouarch

unread,
May 4, 2005, 11:26:39 AM5/4/05
to cherryp...@googlegroups.com
Hi Guyon,

It's a good question as I've found that when the further I develop my
applications with CP, the more I need to change my site structure. The all
thing comes down to the fact that each URL is handled via a python callable
(usually a function).

So I ended having :

add and doAdd because I didn't want to handle in one function (add) two
different behaviors (the add function shows he empty form and the doAdd is
called chen clicking submit).

But in a way, I've found that to be a bit ugly and not good practice.

I'm still playing around that with no good and definite answer for my needs.

- Sylvain

Selon Guyon Morée <guyon...@gmail.com>:
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

Sylvain Hellegouarch

unread,
May 4, 2005, 11:28:13 AM5/4/05
to cherryp...@googlegroups.com

Selon midtoad <stewart....@gmail.com>:

> no one answered your question

it's a bit harsh to say that :)

midtoad

unread,
May 6, 2005, 12:17:25 AM5/6/05
to cherryp...@googlegroups.com
well, I see Sylvain provided an answer a mere 9 minutes after I did;
probably he was already typing up his answer when I wrote that no one
had answered yet. So now you have two replies!

Guyon Morée

unread,
May 7, 2005, 6:13:30 AM5/7/05
to cherryp...@googlegroups.com
Thank you all!

I've taken no offence, it's a difficult question I suppose ;)

I started building my app, with the following structure. It still has
to show if it is a good way of doing it, but I like it so far.

---------
#--- addurl functions
def addurl(self, url=None, title=None, description=None, tags=None,
action=None):
""" show entry page """
if not isLoggedIn(): httptools.redirect('/auth/login')
if not action:
return self.show_addurl(None, url, title, description, tags)
else:
errors = self.validate_addurl(url, title, description, tags)
if errors:
return self.show_addurl(errors, url, title, description,
tags)
else:
return self.process_addurl(url, title, description, tags)
addurl.exposed = True
---------------------------


It has 1 exposed method: addurl. This one is also called from the form
with an action parameter. This way I know if the request comes from the
form.

So, if no action is present: show the form (show_addurl).
If action is present, check input for errors (validate_addurl)
If errors, show_addurl+errors
if no errors, process_addurl.

Remco Boerma

unread,
May 7, 2005, 11:25:21 AM5/7/05
to cherryp...@googlegroups.com
Based on another thread -
Guyon Morée wrote:
Thank you all!

I've taken no offence, it's a difficult question I suppose ;)

I started building my app, with the following structure. It still has
to show if it is a good way of doing it, but I like it so far.
  
I now i'm kinda late with my reaction, but concerning your question:

Say you have an input form. I can imagine having X states with this
form:


 - Show the empty form to be filled out
 - validate form input and either:
    - save the data and show OK-screen
    - discard the data, show the errors and give the user
      the possibility to re-enter
Using the same idea, with a bit different logic bound to it (and sticking it all in 1 form, using 1 url :) )

Check CherryForms @ http://www.cherrypy.org/wiki/CherryForms
This also is my announcement of CherryForms birth.

Cheers!
Remco Boerma



Guyon Morée

unread,
May 8, 2005, 6:57:25 PM5/8/05
to cherryp...@googlegroups.com
Hoi Remco, landgenoot ;)


This looks pretty cool, though I haven't been trying it out yet. I am
working on a del.icio.us - clone to get to know CherryPy. The features
are pretty much there, but it really needs a refactoring job. This
might clean up the form handling code a bit. I'll try it out and let
you know!

thanx,

Guyon
http://gumuz.looze.net/

Reply all
Reply to author
Forward
0 new messages