new puppy example please?

16 views
Skip to first unread message

murray3

unread,
Feb 7, 2009, 7:58:24 PM2/7/09
to web2py Web Framework
Could any of you guy's run through the T2 "puppy" example using the
new gluon.tools syntax.
I am missing something (porting my T2 based code) and I'm sure this
would get me restarted.

thanks
chris

mdipierro

unread,
Feb 7, 2009, 8:31:57 PM2/7/09
to web2py Web Framework
asap. We need to rewrite T2 to use tools first. if anybody can help
please raise your hand.

Massimo

murray3

unread,
Feb 8, 2009, 12:04:52 PM2/8/09
to web2py Web Framework
Massimo,
having trouble getting email registration to work on localhost with
the T2 Puppies app. What needs to be set up?

Also how do you get the puppies app to run on GAE SDK, I keep getting
internal error, is it supposed to work?

regards
chris

Peter

unread,
Feb 8, 2009, 6:32:21 PM2/8/09
to web2py Web Framework
Chris, this is something I was working on, based on the t2.pdf
example... See if it is of any use to you. The indentation will be
screwed up pretty badly, I'm afraid...

These instructions replicate the puppy example in the
T2.PDF docs, pages 7-11, for the new tools.py
environment in web2py 1.56

0. From the admin interface, add new app 'puppy'

1. Define table 'puppy' and create a crud instance

# db.py:

db.define_table('puppy',
SQLField('name'),
SQLField('image','upload') )
db.puppy.name.requires=IS_NOT_EMPTY()

from gluon.tools import Mail, Auth, Crud
crud=Crud(globals(),db)


2. Enable a RESTful interface through crud

# controllers/default.py:

def data():
return dict(form=crud())

def download():
return response.download(request,db)


3. Go to http://localhost:8000/puppy/default/data/create/puppy
to see a 'create puppy' form and play around with the default CRUD
interface


4. Create a template for the CRUD pages

# views/default/data.html

{{extend 'layout.html'}}
{{=form}}
<p>You are now using the data.html template.</p>

...and retry the CRUD URLs; the nice admin/session/etc buttons showed
earlier should be missing now...


5. Define a combo page that shows items + a create form

# views/default/customcreate.html
{{extend 'layout.html'}}

<div class="frame">
<h2>Post the image of a Puppy</h2>
{{=form}}
</div>
<div class="frame">
<h2>Puppies</h2>
{{=items}}
</div>
<p>You are now using the customcreate.html template.</p>

# controllers/default.py
def customcreate():
form = crud.create('puppy')
items = crud.select('puppy', linkto=URL(r=request,f='data',args=
['read']))
return dict(form=form, items=items)


6. Optionally include the "search" widget from t2.py?
(see t2.pdf, page 9)
(the high-level stuff such as search, comments, etc has gracefully
been split out of tools.py)


7. Add authentication

# in models/db.py:
# define your own auth_user table so you can extend it later...
# you can also set auth.settings.table_user later, but you may lose
users already registered (?)
db.define_table('auth_user',
SQLField('first_name',length=32,default=''),
SQLField('last_name',length=50,default=''),
SQLField('email',requires=IS_EMAIL()),
SQLField('password','password',requires=CRYPT()),
SQLField('registration_key',length=64),
SQLField('pic','upload',default='')) # and add any field you
want...

auth=Auth(globals(),db)
auth.settings.table_user=db.auth_user
auth.define_tables()

# in controllers/default.py:
def user(): return dict(form=auth())

# and add a menu so we can see state
if auth.user: response.menu=[
['logout',False,URL(r=request,f='user',args=['logout'])]]
else: response.menu=[
['login',False,URL(r=request,f='user',args=['login'])],
['register',False,URL(r=request,f='user',args=['register'])]]

Go to http://localhost:8000/puppy/default/user/register to register as
a user.
Go to http://localhost:8000/puppy/default/user/login to log in.


8. Now restrict access to these puppies to logged in users

# in controllers/default.py:

@auth.requires_login()
def data():
return dict(form=crud())

@auth.requires_login()
def download():
return response.download(request,db)

@auth.requires_login()
def customcreate():
form = crud.create('puppy')
items = crud.select('puppy', linkto=URL(r=request,f='data',args=
['read']))
return dict(form=form, items=items)

(Note: the T2 requires_login had the option of specifying a 'next'
function which
was passed in the vars._destination... This is missing in tools for
now, AFAICT)

---

I don't use the authorization stuff yet, powerful as it is, so I'll
leave this to someone else.
For myself, the next stop is subclassing Auth... one reason being to
adapt email registration.

Cheers,
-Peter

mdipierro

unread,
Feb 8, 2009, 6:47:29 PM2/8/09
to web2py Web Framework
Peter,

if you and some others (Fran? Ceej?) want to take over T2 and move it
on top of tools.py please go ahead.

the source of t2.pdf is on launchpad together with the rest of the
code.

Massimo


On Feb 8, 5:32 pm, Peter <peter.kleyn...@gmail.com> wrote:
> Chris, this is something I was working on, based on the t2.pdf
> example... See if it is of any use to you. The indentation will be
> screwed up pretty badly, I'm afraid...
>
> These instructions replicate the puppy example in the
> T2.PDF docs, pages 7-11, for the new tools.py
> environment in web2py 1.56
>
> 0. From the admin interface, add new app 'puppy'
>
> 1. Define table 'puppy' and create a crud instance
>
> # db.py:
>
> db.define_table('puppy',
>     SQLField('name'),
>     SQLField('image','upload') )
> db.puppy.name.requires=IS_NOT_EMPTY()
>
> from gluon.tools import Mail, Auth, Crud
> crud=Crud(globals(),db)
>
> 2. Enable a RESTful interface through crud
>
> # controllers/default.py:
>
> def data():
>     return dict(form=crud())
>
> def download():
>     return response.download(request,db)
>
> 3. Go tohttp://localhost:8000/puppy/default/data/create/puppy
> Go tohttp://localhost:8000/puppy/default/user/registerto register as
> a user.
> Go tohttp://localhost:8000/puppy/default/user/loginto log in.

Peter

unread,
Feb 9, 2009, 2:33:24 AM2/9/09
to web2py Web Framework
Massimo,

The old T2/T3 was "too narrow and too tall" for me: I have too many
use cases that just plainly don't fit. That's why I was glad that you
isolated crud and auth, both of them "broad and flat" utitility
layers.

So while I understand that the "rest of T2" needs to be redone, I have
more interest in documenting and expanding what is now in tools.py
than what was left out of it. ;-)

Cheers,
-Peter
> > Go tohttp://localhost:8000/puppy/default/user/registertoregister as
> > a user.
> > Go tohttp://localhost:8000/puppy/default/user/logintolog in.

murray3

unread,
Feb 9, 2009, 8:59:36 AM2/9/09
to web2py Web Framework
Peter,
thanks for this I will have a go later (I'm in Dublin GMT), just one
thing
what needs to be set up to get the code to send the registration email
other wise when you enable authentication you are locking yourself out
as you can't register or login?
regards
chris

On Feb 8, 11:32 pm, Peter <peter.kleyn...@gmail.com> wrote:
> Chris, this is something I was working on, based on the t2.pdf
> example... See if it is of any use to you. The indentation will be
> screwed up pretty badly, I'm afraid...
>
> These instructions replicate the puppy example in the
> T2.PDF docs, pages 7-11, for the new tools.py
> environment in web2py 1.56
>
> 0. From the admin interface, add new app 'puppy'
>
> 1. Define table 'puppy' and create a crud instance
>
> # db.py:
>
> db.define_table('puppy',
>     SQLField('name'),
>     SQLField('image','upload') )
> db.puppy.name.requires=IS_NOT_EMPTY()
>
> from gluon.tools import Mail, Auth, Crud
> crud=Crud(globals(),db)
>
> 2. Enable a RESTful interface through crud
>
> # controllers/default.py:
>
> def data():
>     return dict(form=crud())
>
> def download():
>     return response.download(request,db)
>
> 3. Go tohttp://localhost:8000/puppy/default/data/create/puppy
> Go tohttp://localhost:8000/puppy/default/user/registerto register as
> a user.
> Go tohttp://localhost:8000/puppy/default/user/loginto log in.
> > > > chris- Hide quoted text -
>
> - Show quoted text -

Peter

unread,
Feb 9, 2009, 9:56:59 AM2/9/09
to web2py Web Framework
Hi Chris,

AFAIK you only need to configure the mailer as documented in the
tools.py source. There is an error in the docstring in the Mail class,
use the example code in the Auth class.
Check if you can reach the mail server that you configure by running
"telnet <server> 25" and check that you get a connection on that port.
Ehmm, I have ZERO experience on GAE, can't help you with mail settings
there...

# in db.py:

from gluon.tools import Mail, Auth, Crud
mail=Mail()
mail.settings.server='smtp.yourprovider.tld:25'
mail.settings.sender='y...@somewhere.com'
# only configure login if you need smtp authentication
# check your regular mail setup
mail.settings.login='username:password'

# plus, after the line... auth=Auth(globals(),db)
auth.settings.mailer=mail

def fullurl(a=None,c=None,f=None,r=None,args=[],vars={}):
return '%s://%s%s' % (r.env.wsgi_url_scheme,r.env.http_host,URL
(a=a,c=c,f=f,r=r,args=args,vars=vars))

auth.messages.verify_email = \
'Click on the link ' + fullurl(r=request,f='user',args=
['verify_email']) + '/%(key)s to verify your email'

These two last lines are necessary because Massimo maybe wasn't really
really /really/ ready with the Auth verify code yet... ;-)

That should do the trick... If you want to bypass user validation,
ie, grant access without entering the validation string, you can
simply use the web db admin to clear the validation_key field for that
user.

If you run into any problems, please post. I'm in Amsterdam GMT+1 so
timewise that's closer than you may think. ;-)

Cheers,
-Peter
> > Go tohttp://localhost:8000/puppy/default/user/registertoregister as
> > a user.
> > Go tohttp://localhost:8000/puppy/default/user/logintolog in.

Peter

unread,
Feb 9, 2009, 11:04:54 AM2/9/09
to web2py Web Framework
Massimo,

You are understandably cautious about using absolute urls, but sending
verification mails is one place where we really need them.

Currently, in tools.py line 646 you insert only the key into the
verification message; how about inserting the validation_url as well?

Line 360 could then be something like
auth.messages.verify_email = \
'Click on the link: %(validate_url)s/%(key)s to verify your
email'

I would be happy to suggest a patch, but I don't want to sneak in a
fullurl() helper... ;-)

Cheers,
-Peter

On Feb 9, 3:56 pm, Peter <peter.kleyn...@gmail.com> wrote:
> Hi Chris,
>
> AFAIK you only need to configure the mailer as documented in the
> tools.py source. There is an error in the docstring in the Mail class,
> use the example code in the Auth class.
> Check if you can reach the mail server that you configure by running
> "telnet <server> 25" and check that you get a connection on that port.
> Ehmm, I have ZERO experience on GAE, can't help you with mail settings
> there...
>
> # in db.py:
>
> from gluon.tools import Mail, Auth, Crud
> mail=Mail()
> mail.settings.server='smtp.yourprovider.tld:25'
> mail.settings.sender='...@somewhere.com'

Wes James

unread,
Feb 9, 2009, 11:07:27 AM2/9/09
to web...@googlegroups.com
As mentioned before it is so the developer can put in their own messge.

-wj

On Mon, Feb 9, 2009 at 9:04 AM, Peter <peter.k...@gmail.com> wrote:
>
> Massimo,
>
> You are understandably cautious about using absolute urls, but sending
> verification mails is one place where we really need them.
>
> Currently, in tools.py line 646 you insert only the key into the
> verification message; how about inserting the validation_url as well?
>
> Line 360 could then be something like
> auth.messages.verify_email = \
> 'Click on the link: %(validate_url)s/%(key)s to verify your
> email'
>

<snip>

Peter

unread,
Feb 9, 2009, 11:44:54 AM2/9/09
to web2py Web Framework
> As mentioned before

I am aware that I can set message strings if I don't like them.
However, from my POV, email verification is *broken* out of the box.
Sure, a mail gets sent, but the link in that mail doesn't work. Do we
want to make it easy to use this stuff, or what? If this was discussed
before, my apologies.

Now we can do two things. 1. Document this peculiar web2py behavior,
ie, big letters in the Auth docstring, "don't forget to set
message.verify_email, and you may need a fullurl function to go with
it, which we won't provide for security reasons, nah nah nah nah nah"
or 2. Fix it, since it is trivial enough. ;-)

Maybe this is my chance to contribute: please put my suggestion to
Chris in the "official web2py documentation", by way of implementing
option 1. ;-))

Cheers,
-Peter

On Feb 9, 5:07 pm, Wes James <compte...@gmail.com> wrote:
> As mentioned before it is so the developer can put in their own messge.
>
> -wj
>

Peter

unread,
Feb 9, 2009, 12:48:14 PM2/9/09
to web2py Web Framework
Hi Wes, on rereading --
maybe my earlier msg gave you the impression that I suggested fixing
the message string itself?? That wasn't what I meant, of course. I
referred to line 360 only to illustrate how a proposed fix would be
used.

Every verification mail will need a functioning (=absolute) link to
the verify_email function, currently not easily provided by web2py --
that is what this is about.

Cheers,
-Peter

On Feb 9, 5:07 pm, Wes James <compte...@gmail.com> wrote:
> As mentioned before it is so the developer can put in their own messge.
>
> -wj
>
Reply all
Reply to author
Forward
0 new messages