Hi,

I'm trying to create a view that allows a user to create a new project and 
add (existing) users to that project. In order to represent the two tables 
on a single page, I've been following a recipe from pyslices:

http://www.web2pyslices.com/article/show/1542/manage-users-and-memebership-in-the-same-form

So I have a project table and a project_members table and the db and 
controller functions are below.

The structure is subtly different from the slice: that is editing an 
existing member, so the auth_user id is known and is used in the forms to 
select relevant records. In my case, I'm trying to create a new project, so 
neither controller 'knows' the next project table id, and my view shows the 
SQLFORM to create a new project but then a SQLFORM.grid of *all* members of 
*all* projects.

If it was just the project SQLFORM, it wouldn't be a problem, because 
project would get the ID automatically on create, but I want to be able to 
pass the project row ID to the  new_project_membership() controller, so 
that I can get the SQLFORM.grid to show only member of this project (as 
they are added) and insert it for newly added entries to project_members. 
So my plan was to get the row ID from the db somehow and pass it on to the 
new_project_membership() controller.

Of course, the deeper issue is that I really want an atomic commit - the 
project row and project_membership row(s) only go in when a single final 
submit is pressed, so the answer may be "don't do it like that"! In which 
case, pointers to the right approach would be really welcome!

Thanks,
David


*Table definitions:*

db.define_table('project',
    Field('title','string', notnull=True),
    Field('start_date','date', notnull=True),
    Field('end_date','date', notnull=True))

db.define_table('project_members',
    Field('project_id', 'reference project', notnull=True),
    Field('user_id', 'reference auth_user', notnull=True),
    Field('project_role', notnull=True))

*Controllers:*

@auth.requires_login()
def new_project():

    # set up the project form
    form = SQLFORM(db.project, fields = ['title', 'start_date', 'end_date'
]).process()
    
    # set up a membership panel
    membership_panel = LOAD(request.controller,
                            'new_project_membership.html',
                             #args=[project_id],
                             ajax=True)
    
    # pass both of those to the view
    return dict(form=form, membership_panel=membership_panel)


@auth.requires_login()
def new_project_membership():

    # function to return a grid containing a membership panel
    form = SQLFORM.grid(db.project_members,
                       #args=[project_id],
                       searchable=False,
                       deletable=False,
                       details=False,
                       selectable=False,
                       csv=False) 
    return form

*View:*

{{extend 'layout.html'}}
{{=H2('New project and members')}}
<p> Please use the form below to create a new project and add members and 
roles/</p>
{{=H4('Project details')}}
<br>
{{=form}}
<br>
{{=H4('Project members')}}
<br>
{{=membership_panel}}
<br>




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to