Massimo, You go above and beyond! Thanks you so much for the through explanation. This second version is much much cleaner then the version before it - thanks for hacking trunk to add the change! Now I have to integrate it and make sure I understand how everything works!
-Rob On Aug 8, 4:32 pm, mdipierro <[email protected]> wrote: > Actually your questions gave me some ideas on how to simplify the > web2py syntax in using components. With the code in trunk you can now > rewrite the component > > {{=LOAD('rsvp','rsvp',ajax=True)}} > > as follows: > > def rsvp(): > > """ > self contained component to handle RSVP requests and > notificaitons > """ > rsvp = db.rsvp(event=request.args(0),attendee=auth.user_id) > if not rsvp and not auth.user: > return A('login to > rsvp',_href=URL('default','user',args='login')) > elif request.args(1)=='yes' and not rsvp: > db.rsvp.insert(event=request.args(0),attendee=auth.user_id) > response.js='jQuery("#%s").hide().fadeIn("slow")' % > request.cid > return 'Thanks for RSVPing for this event' > elif rsvp: > return 'You are registered for this event' > else: > return A('You are not registered for this event, click to > RSVP', > > _href=URL(args=(request.args(0),'yes')),cid=request.cid) > > Notice how: > - request.cid is the ID of the <div>...</div> that contains the > component and it is assigned automatically > - If A(...,cid=request.cid) is set the target of the link stays in the > component > - response.js sets the JS to be executed after component response > > All component logic stays in one function. This function could have > its own view but in this case it is not needed. > > REQUIRES TRUNK! > > On Aug 8, 1:20 pm, mdipierro <[email protected]> wrote: > > > > > > > I would do this (on top of the scaffolding app) > > > 1) model/db_rsvp.py > > > db.define_table('event', > > Field('name',requires=IS_NOT_EMPTY()), > > Field('start_time','datetime'), > > Field('location'), > > > Field('posted_by',db.auth_user,default=auth.user_id,writable=False)) > > db.define_table('rsvp', > > Field('attendee',db.auth_user), > > Field('event',db.event)) > > > 2) controllers/srvp.py > > > def events(): > > > """ > > List future events and allow to create new events if logged- > > in > > """ > > form = auth.user and crud.create(db.event) > > events = db(db.event.start_time>request.now).select() > > return dict(events=events,form=form) > > > @auth.requires_login() > > def show(): > > > """ > > Show info avout event id==arg(0), list attendees and allow rsvp > > via load > > Also allow event owner to edit > > event > > """ > > event = db.event(request.args(0),posted_by=auth.user_id) > > form = event and crud.update(db.event,event) > > rsvps = db(db.rsvp.event==event.id).select() > > attendees = db(db.auth_user.id.belongs(r.attendee for r in > > rsvps)).select() > > return dict(event=event,attendees=attendees,form=form) > > > 3) views/rsvp/events.html > > > {{extend 'layout.html'}} > > {{for event in events:}} > > {{=event.start_time}} > > {{=A(event.name,_href=URL('show',args=event.id))}}<br/> > > {{pass}} > > {{=form or A('login to post a new > > event',_href=URL('download','user',args='login'))}} > > > 4) views/rsvp/show.html > > > {{extend 'layout.html'}} > > <h1>{{=event.name}}</h1> > > <h2>{{=event.location}} @ {{=prettydate(event.start_time)}}</h2> > > {{=LOAD('rsvp','rsvp',args=event.id,ajax=True)}} > > <h3>Registered Attendees</h3> > > {{for attendee in attendees:}} > > {{='%(first_name)s %(last_name)s' % attendee}}<br/> > > {{pass}} > > > 5) ajax callback that handles RSVP component: > > > def rsvp(): > > > """ > > self contained component to handle RSVP requests and > > notificaitons > > """ > > component_url = URL(r=request,args=(request.args(0),'yes')) > > component_id = request.env.http_web2py_component_element > > rsvp = db.rsvp(event=request.args(0),attendee=auth.user_id) > > if not rsvp and not auth.user: > > return A('login to > > rsvp',_href=URL('default','user',args='login')) > > elif request.args(1)=='yes' and not rsvp: > > db.rsvp.insert(event=request.args(0),attendee=auth.user_id) > > response.headers['web2py-component- > > command']='alert("Thanks!");' > > return SPAN('Thanks for RSVPing for this event') > > elif rsvp: > > return SPAN('You are registered for this event') > > else: > > return SPAN('You are not registered for this event ', > > A('click here to RSVP', > > > _onclick='web2py_ajax_page("GET","%s",null,"%s");return false' % \ > > (component_url,component_id))) > > > perhaps can simplify the API a bit.

