As I learning exercise I have set up a little volunteer database.
You can recreate by doing the following:
1. Create a new simple application called smartgridex
2. At the bottom of db.py paste the following:
db.define_table('volunteer',
Field('firstname', requires=[IS_NOT_EMPTY()]),
Field('lastname', requires=[IS_NOT_EMPTY()]),
Field('street'),
Field('city'),
Field('state'),
Field('zip'),
Field('phone'),
format='%(firstname)s'
)
db.define_table('availability',
Field('avail_name', requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()]),
Field('volid', 'reference volunteer', requires=IS_IN_DB(db,
'volunteer.id', '%(firstname)s')),
Field('day', 'date', requires=[IS_NOT_EMPTY()]),
Field('start_time', 'time'),
Field('end_time', 'time')
)
db.availability.volid.represent = lambda id, r: str(id) + ' | ' +
db.volunteer[id].firstname
db.define_table('skill',
Field('skill_description', requires=[IS_NOT_EMPTY()])
)
db.define_table('skills_known',
Field('volid', 'reference volunteer', requires=IS_IN_DB(db,
'volunteer.id', '%(firstname)s')),
Field('skillid', 'reference skill', requires=IS_IN_DB(db, 'skill.id',
'%(skill_description)s')),
Field('notes', 'text')
)
db.skills_known.volid.represent = lambda id, r: str(id) + ' | ' +
db.volunteer[id].firstname
db.skills_known.skillid.represent = lambda id, r: str(id) + ' | ' +
db.skill[id].skill_description
db.define_table('job',
Field('volid', 'reference volunteer', requires=IS_IN_DB(db,
'volunteer.id', '%(firstname)s')),
Field('job_date', 'date', requires=[IS_NOT_EMPTY()]),
Field('start_time', 'time'),
Field('estimated_duration', requires=[IS_NOT_EMPTY()]),
Field('supervisor', requires=[IS_NOT_EMPTY()]),
Field('job_description', requires=[IS_NOT_EMPTY()]),
Field('end_time', 'time'),
Field('worked_flag', 'boolean')
)
3. at the bottom of default.py paste the following:
def all_volunteers():
grid = SQLFORM.smartgrid(db.volunteer,linked_tables=['availability',
'skills_known', 'job'])
return locals()
def all_availabilities():
grid = SQLFORM.grid(db.availability)
return locals()
def all_skills():
grid = SQLFORM.smartgrid(db.skill,linked_tables=['skills_known'])
return locals()
def all_skills_known():
grid = SQLFORM.grid(db.skills_known)
return locals()
def all_jobs():
grid = SQLFORM.grid(db.job)
return locals()
4. Replace default/index.html with the following:
{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}
<h4/>{{=T('What Now?')}}</h4>
<ol>
<li>{{=A('Show All Volunteers', _href="all_volunteers")}}</li>
<li>{{=A('Show All Availabilities', _href="all_availabilities")}}</li>
<li>{{=A('Show All Skills', _href="all_skills")}}</li>
<li>{{=A('Show All Skills Known', _href="all_skills_known")}}</li>
<li>{{=A('Show All Jobs', _href="all_jobs")}}</li>
</ol>
It all kind of works but it has issues and I feel certain that there must
be a better way.
1. The query option does not show the linked to columns, only the native
columns. See the Show All Skills Known option number 4.
2. I am having to play these sorts of games:
db.skills_known.volid.represent = lambda id, r: str(id) + ' | ' +
db.volunteer[id].firstname
because I cannot do a query and search on the displayed info but only on
the underlying "id"
3. Is there a better way to do the table defines for this sort of thing?
all the best,
drew
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.