def custom_prep(r): resource = r.resource # Call standard prep if callable(standard_prep): result = standard_prep(r) if not result: return False # Customise list_fields if r.method == "review": list_fields = ["id", "created_on", "modified_on", "name", "start_date", (T("Countries"), "location.location_id"), (T("Hazards"), "hazard.name"), (T("Lead Organization"), "organisation_id"), (T("Donors"), "donor.organisation_id"), ] elif r.representation == "xls": # All readable Fields should be exported list_fields = ["id", "name", "code", "description", "status_id", "start_date", "end_date", "drrpp.duration", (T("Countries"), "location.location_id"), "drrpp.L1", (T("Hazards"), "hazard.name"), (T("Themes"), "theme.name"), "objectives", "drrpp.activities", "output.name", "drr.hfa", "drrpp.rfa", "drrpp.pifacc", "drrpp.jnap", (T("Lead Organization"), "organisation_id"), (T("Partners"), "partner.organisation_id"), (T("Donors"), "donor.organisation_id"), "budget", "currency", "drrpp.focal_person", "drrpp.organisation_id", "drrpp.email", "url.url", "drrpp.parent_project", "comments", ] if logged_in: list_fields.extend(["created_by", "created_on", "modified_by", "modified_on", ]) else: list_fields = ["id", "name", "start_date", (T("Countries"), "location.location_id"), (T("Hazards"), "hazard.name"), (T("Lead Organization"), "organisation_id"), (T("Donors"), "donor.organisation_id"), ] resource.configure(list_fields = list_fields) # Customise report_options if r.method == "report": report_fields = ["name", (T("Countries"), "location.location_id"), (T("Hazards"), "hazard.name"), (T("Themes"), "theme.name"), (T("HFA Priorities"), "drr.hfa"), (T("RFA Priorities"), "drrpp.rfa"), (T("Lead Organization"), "organisation_id"), (T("Partner Organizations"), "partner.organisation_id"), (T("Donors"), "donor.organisation_id"), ] # Report Settings for charts if "chart" in r.get_vars and r.representation != "json": s3.crud_strings[tablename].title_report = T("Project Graph") report_fact_default = "count(id)" report_facts = [(T("Number of Projects"), "count(id)")] show_table = False else: s3.crud_strings[tablename].title_report = T("Project Matrix") report_fact_default = "count(id)" report_facts = [(T("Number of Projects"), "count(id)"), (T("Number of Countries"), "count(location.location_id)"), (T("Number of Hazards"), "count(hazard.id)"), (T("Number of Themes"), "count(theme.id)"), (T("Number of HFA Priorities"), "count(drr.hfa)"), (T("Number of RFA Priorities"), "count(drrpp.rfa)"), (T("Number of Lead Organizations"), "count(organisation_id)"), (T("Number of Partner Organizations"), "count(partner.organisation_id)"), (T("Number of Donors"), "count(donor.organisation_id)"), ] show_table = True report_options = Storage(rows = report_fields, cols = report_fields, fact = report_facts, defaults = Storage(rows = "hazard.name", cols = "location.location_id", fact = report_fact_default, totals = True, table = show_table, ) ) resource.configure(report_options = report_options) current.deployment_settings.ui.hide_report_options = True if r.interactive: # Don't show Update/Delete button on Search table if r.method is None and not r.id: resource.configure(editable = False, deletable = False ) # JS to show/hide Cook Island fields s3.scripts.append("/%s/static/themes/DRRPP/js/drrpp.js" % current.request.application) if r.method == "read": table_pl = s3db.project_location table_l = s3db.gis_location countries = [row.name for row in db((table_pl.project_id == r.record.id) & (table_pl.location_id == table_l.id) ).select(table_l.name) ] if not ("Cook Islands" in countries and len(countries) == 1): s3db.project_drrpp.L1.readable = False s3db.project_drrpp.pifacc.readable = False s3db.project_drrpp.jnap.readable = False # Filter Options project_hfa_opts = s3db.project_hfa_opts() hfa_options = dict((key, "HFA %s" % key) for key in project_hfa_opts) #hfa_options[None] = NONE # to search NO HFA project_rfa_opts = s3db.project_rfa_opts() rfa_options = dict((key, "RFA %s" % key) for key in project_rfa_opts) #rfa_options[None] = NONE # to search NO RFA project_pifacc_opts = s3db.project_pifacc_opts() pifacc_options = dict((key, "PIFACC %s" % key) for key in project_pifacc_opts) #pifacc_options[None] = NONE # to search NO PIFACC project_jnap_opts = s3db.project_jnap_opts() jnap_options = dict((key, "JNAP %s" % key) for key in project_jnap_opts) #jnap_options[None] = NONE # to search NO JNAP # Filter widgets from s3 import S3TextFilter, S3OptionsFilter, get_s3_filter_opts filter_widgets = [ S3TextFilter(["name", "code", "description", "location.location_id", "hazard.name", "theme.name", ], label = T("Search Projects"), comment = T("Search for a Project by name, code, or description."), ), S3OptionsFilter("status_id", label = T("Status"), cols = 4, ), S3OptionsFilter("location.location_id", label = T("Country"), cols = 3, hidden = True, ), #S3OptionsFilter("drrpp.L1", # label = T("Cook Islands"), # cols = 3, # hidden = True, # ), S3OptionsFilter("hazard.id", label = T("Hazard"), options = lambda: \ get_s3_filter_opts("project_hazard", translate=True), help_field = s3db.project_hazard_help_fields, cols = 4, hidden = True, ), S3OptionsFilter("theme.id", label = T("Theme"), options = lambda: \ get_s3_filter_opts("project_theme", translate=True), help_field = s3db.project_theme_help_fields, cols = 4, # Don't group size = None, hidden = True, ), S3OptionsFilter("drr.hfa", label = T("HFA"), options = hfa_options, help_field = project_hfa_opts, cols = 5, hidden = True, ), S3OptionsFilter("drrpp.rfa", label = T("RFA"), options = rfa_options, help_field = project_rfa_opts, cols = 6, hidden = True, ), S3OptionsFilter("drrpp.pifacc", label = T("PIFACC"), options = pifacc_options, help_field = project_pifacc_opts, cols = 6, hidden = True, ), S3OptionsFilter("drrpp.jnap", label = T("JNAP"), options = jnap_options, help_field = project_jnap_opts, cols = 6, hidden = True, ), S3OptionsFilter("organisation_id", label = T("Lead Organization"), cols = 3, hidden = True, ), S3OptionsFilter("partner.organisation_id", label = T("Partners"), cols = 3, hidden = True, ), S3OptionsFilter("donor.organisation_id", label = T("Donors"), cols = 3, hidden = True, ) ] resource.configure(filter_widgets=filter_widgets) return True
def custom_prep(r): # Call standard prep if callable(standard_prep): result = standard_prep(r) else: result = True if r.controller == "dvr" and not r.component: ctable = s3db.dvr_case # Mandatory Fields from s3 import IS_NOT_EMPTY from gluon import IS_EMPTY_OR # Require a case number ctable.reference.requires = IS_NOT_EMPTY() # Require organisation, site and status for fn in ("organisation_id", "site_id", "status_id"): field = ctable[fn] requires = field.requires if isinstance(requires, IS_EMPTY_OR): field.requires = requires.other root_org = current.auth.root_org() if root_org: # Set default for organisation_id and hide the field field = ctable.organisation_id field.default = root_org field.readable = field.writable = False # Hide organisation_id in list_fields, too list_fields = r.resource.get_config("list_fields") if "dvr_case.organisation_id" in list_fields: list_fields.remove("dvr_case.organisation_id") # Limit sites to root_org field = ctable.site_id requires = field.requires if requires: if isinstance(requires, IS_EMPTY_OR): requires = requires.other if hasattr(requires, "dbset"): stable = s3db.org_site query = stable.organisation_id == root_org requires.dbset = current.db(query) resource = r.resource if r.interactive: # Custom CRUD form from s3 import S3SQLCustomForm, S3SQLInlineComponent crud_form = S3SQLCustomForm( "dvr_case.reference", "dvr_case.date", "dvr_case.organisation_id", "dvr_case.site_id", "dvr_case.priority", "dvr_case.case_type_id", "dvr_case.beneficiary", "dvr_case.status_id", "first_name", "middle_name", "last_name", "date_of_birth", "gender", "person_details.marital_status", S3SQLInlineComponent( "contact", fields=[("", "value")], filterby={"field": "contact_method", "options": "EMAIL"}, label=T("Email"), multiple=False, name="email", ), S3SQLInlineComponent( "contact", fields=[("", "value")], filterby={"field": "contact_method", "options": "SMS"}, label=T("Mobile Phone"), multiple=False, name="phone", ), "person_details.nationality", "person_details.illiterate", S3SQLInlineComponent( "case_language", fields=["language", "quality", "comments"], label=T("Language / Communication Mode"), ), S3SQLInlineComponent( "contact_emergency", fields=["name", "relationship", "phone"], label=T("Emergency Contact"), multiple=False, ), S3SQLInlineComponent("identity", fields=["type", "description", "value"]), S3SQLInlineComponent( "address", label=T("Current Address"), fields=[("", "location_id")], filterby={"field": "type", "options": "1"}, link=False, multiple=False, ), "dvr_case.head_of_household", "dvr_case.hoh_name", "dvr_case.hoh_gender", "dvr_case.hoh_relationship", "dvr_case.comments", ) # Extend filter widgets filter_widgets = resource.get_config("filter_widgets") if filter_widgets is not None: from s3 import get_s3_filter_opts, S3OptionsFilter filter_widgets.extend( [ S3OptionsFilter( "dvr_case.case_type_id", options=lambda: get_s3_filter_opts("dvr_case_type") ), S3OptionsFilter( "dvr_case_activity.need_id", options=lambda: get_s3_filter_opts("dvr_need"), hidden=True, ), ] ) resource.configure(crud_form=crud_form, filter_widgets=filter_widgets) # Hide Postcode in addresses (not used) atable = s3db.pr_address from s3 import S3LocationSelector location_id = atable.location_id location_id.widget = S3LocationSelector(show_address=True, show_postcode=False) # Inject filter script for sites (filter by selected org) if not root_org: script = """$.filterOptionsS3({ 'trigger':'sub_dvr_case_organisation_id', 'target':'sub_dvr_case_site_id', 'lookupResource':'site', 'lookupPrefix':'org', 'lookupField':'site_id', 'lookupKey':'organisation_id' })""" s3.jquery_ready.append(script) # Expose additional case fields: fields = ("beneficiary", "head_of_household", "hoh_name", "hoh_gender", "hoh_relationship") for fname in fields: field = ctable[fname] field.readable = field.writable = True # Inject script to toggle Head of Household form fields path = "/%s/static/themes/STL/js/dvr.js" % current.request.application if path not in s3.scripts: s3.scripts.append(path) # Custom list fields (must be outside of r.interactive) list_fields = [ "dvr_case.reference", "dvr_case.case_type_id", "dvr_case.priority", "first_name", "middle_name", "last_name", "date_of_birth", "gender", # "dvr_case.organisation_id", "dvr_case.date", "dvr_case.status_id", ] resource.configure( list_fields=list_fields, # orderby = "dvr_case.priority desc", ) return result
def prep(r): # Filter to persons who have a case registered resource = r.resource resource.add_filter(FS("dvr_case.id") != None) if r.component and r.id: ctable = r.component.table if "case_id" in ctable.fields and \ str(ctable.case_id.type)[:18] == "reference dvr_case": # Find the Case ID dvr_case = s3db.dvr_case query = (dvr_case.person_id == r.id) & \ (dvr_case.deleted != True) cases = db(query).select(dvr_case.id, limitby=(0, 2)) case_id = ctable.case_id if cases: # Set default case_id.default = cases.first().id if len(cases) == 1: # Only one case => hide case selector case_id.readable = case_id.writable = False else: # Configure case selector case_id.requires = IS_ONE_OF( db(query), "dvr_case.id", case_id.represent, ) if r.interactive: # Adapt CRUD strings to context s3.crud_strings["pr_person"] = Storage( label_create=T("Create Case"), title_display=T("Case Details"), title_list=T("Cases"), title_update=T("Edit Case Details"), label_list_button=T("List Cases"), label_delete_button=T("Delete Case"), msg_record_created=T("Case added"), msg_record_modified=T("Case details updated"), msg_record_deleted=T("Case deleted"), msg_list_empty=T("No Cases currently registered")) if not r.component: # Module-specific CRUD form # NB: this assumes single case per person, must use # case perspective (dvr/case) for multiple cases # per person! from s3 import S3SQLCustomForm, S3SQLInlineComponent crud_form = S3SQLCustomForm( "dvr_case.reference", "dvr_case.organisation_id", "dvr_case.date", "dvr_case.status_id", "first_name", "middle_name", "last_name", "date_of_birth", "gender", S3SQLInlineComponent( "contact", fields=[ ("", "value"), ], filterby={ "field": "contact_method", "options": "EMAIL", }, label=T("Email"), multiple=False, name="email", ), S3SQLInlineComponent( "contact", fields=[ ("", "value"), ], filterby={ "field": "contact_method", "options": "SMS", }, label=T("Mobile Phone"), multiple=False, name="phone", ), "person_details.nationality", S3SQLInlineComponent( "address", label=T("Current Address"), fields=[ ("", "location_id"), ], filterby={ "field": "type", "options": "1", }, link=False, multiple=False, ), "dvr_case.comments", ) # Module-specific filter widgets from s3 import get_s3_filter_opts, S3TextFilter, S3OptionsFilter filter_widgets = [ S3TextFilter( [ "pe_label", "first_name", "middle_name", "last_name", #"email.value", #"phone.value", "dvr_case.reference", ], label=T("Search"), comment=T("You can search by name, ID or case number"), ), S3OptionsFilter( "dvr_case.status_id", cols=3, default=default_status, #label = T("Case Status"), options=s3db.dvr_case_status_filter_opts, sort=False, ), S3OptionsFilter("person_details.nationality", ), S3OptionsFilter( "case_flag_case.flag_id", label=T("Flags"), options=get_s3_filter_opts( "dvr_case_flag", translate=True, ), cols=3, hidden=True, ), ] resource.configure( crud_form=crud_form, filter_widgets=filter_widgets, ) # Module-specific list fields (must be outside of r.interactive) list_fields = [ "dvr_case.reference", "first_name", "middle_name", "last_name", "date_of_birth", "gender", "dvr_case.date", "dvr_case.status_id", ] resource.configure(list_fields=list_fields, ) return True
def custom_prep(r): # Call standard prep if callable(standard_prep): result = standard_prep(r) else: result = True if r.controller == "dvr" and not r.component: ctable = s3db.dvr_case # Mandatory Fields from s3 import IS_NOT_EMPTY from gluon import IS_EMPTY_OR # Require a case number ctable.reference.requires = IS_NOT_EMPTY() # Require organisation, site and status for fn in ( "organisation_id", "site_id", "status_id", ): field = ctable[fn] requires = field.requires if isinstance(requires, IS_EMPTY_OR): field.requires = requires.other root_org = current.auth.root_org() if root_org: # Set default for organisation_id and hide the field field = ctable.organisation_id field.default = root_org field.readable = field.writable = False # Hide organisation_id in list_fields, too list_fields = r.resource.get_config("list_fields") if "dvr_case.organisation_id" in list_fields: list_fields.remove("dvr_case.organisation_id") # Limit sites to root_org field = ctable.site_id requires = field.requires if requires: if isinstance(requires, IS_EMPTY_OR): requires = requires.other if hasattr(requires, "dbset"): stable = s3db.org_site query = (stable.organisation_id == root_org) requires.dbset = current.db(query) resource = r.resource if r.interactive: # Custom CRUD form from s3 import S3SQLCustomForm, S3SQLInlineComponent crud_form = S3SQLCustomForm( "dvr_case.reference", "dvr_case.date", "dvr_case.organisation_id", "dvr_case.site_id", "dvr_case.priority", "dvr_case.case_type_id", "dvr_case.beneficiary", "dvr_case.status_id", "first_name", "middle_name", "last_name", "date_of_birth", "gender", "person_details.marital_status", S3SQLInlineComponent( "contact", fields=[ ("", "value"), ], filterby={ "field": "contact_method", "options": "EMAIL", }, label=T("Email"), multiple=False, name="email", ), S3SQLInlineComponent( "contact", fields=[ ("", "value"), ], filterby={ "field": "contact_method", "options": "SMS", }, label=T("Mobile Phone"), multiple=False, name="phone", ), "person_details.nationality", "person_details.illiterate", S3SQLInlineComponent( "case_language", fields=[ "language", "quality", "comments", ], label=T("Language / Communication Mode"), ), S3SQLInlineComponent( "contact_emergency", fields=[ "name", "relationship", "phone", ], label=T("Emergency Contact"), multiple=False, ), S3SQLInlineComponent( "identity", fields=[ "type", "description", "value", ], ), S3SQLInlineComponent( "address", label=T("Current Address"), fields=[ ("", "location_id"), ], filterby={ "field": "type", "options": "1", }, link=False, multiple=False, ), "dvr_case.head_of_household", "dvr_case.hoh_name", "dvr_case.hoh_gender", "dvr_case.hoh_relationship", "dvr_case.comments", ) # Extend filter widgets filter_widgets = resource.get_config("filter_widgets") if filter_widgets is not None: from s3 import get_s3_filter_opts, S3OptionsFilter filter_widgets.extend([ S3OptionsFilter( "dvr_case.case_type_id", options=lambda: get_s3_filter_opts( "dvr_case_type"), ), S3OptionsFilter( "dvr_case_activity.need_id", options=lambda: get_s3_filter_opts("dvr_need"), hidden=True, ), ]) resource.configure( crud_form=crud_form, filter_widgets=filter_widgets, ) # Hide Postcode in addresses (not used) atable = s3db.pr_address from s3 import S3LocationSelector location_id = atable.location_id location_id.widget = S3LocationSelector( show_address=True, show_postcode=False, ) # Inject filter script for sites (filter by selected org) if not root_org: script = '''$.filterOptionsS3({ 'trigger':'sub_dvr_case_organisation_id', 'target':'sub_dvr_case_site_id', 'lookupResource':'site', 'lookupPrefix':'org', 'lookupField':'site_id', 'lookupKey':'organisation_id' })''' s3.jquery_ready.append(script) # Expose additional case fields: fields = ("beneficiary", "head_of_household", "hoh_name", "hoh_gender", "hoh_relationship") for fname in fields: field = ctable[fname] field.readable = field.writable = True # Inject script to toggle Head of Household form fields path = "/%s/static/themes/STL/js/dvr.js" % current.request.application if path not in s3.scripts: s3.scripts.append(path) # Custom list fields (must be outside of r.interactive) list_fields = [ "dvr_case.reference", "dvr_case.case_type_id", "dvr_case.priority", "first_name", "middle_name", "last_name", "date_of_birth", "gender", #"dvr_case.organisation_id", "dvr_case.date", "dvr_case.status_id", ] resource.configure(list_fields=list_fields, #orderby = "dvr_case.priority desc", ) return result
def prep(r): # Filter to persons who have a case registered resource = r.resource resource.add_filter(FS("dvr_case.id") != None) if r.component and r.id: ctable = r.component.table if "case_id" in ctable.fields and \ str(ctable.case_id.type)[:18] == "reference dvr_case": # Find the Case ID dvr_case = s3db.dvr_case query = (dvr_case.person_id == r.id) & \ (dvr_case.deleted != True) cases = db(query).select(dvr_case.id, limitby=(0, 2)) case_id = ctable.case_id if cases: # Set default case_id.default = cases.first().id if len(cases) == 1: # Only one case => hide case selector case_id.readable = case_id.writable = False else: # Configure case selector case_id.requires = IS_ONE_OF(db(query), "dvr_case.id", case_id.represent, ) if r.interactive: # Adapt CRUD strings to context s3.crud_strings["pr_person"] = Storage( label_create = T("Create Case"), title_display = T("Case Details"), title_list = T("Cases"), title_update = T("Edit Case Details"), label_list_button = T("List Cases"), label_delete_button = T("Delete Case"), msg_record_created = T("Case added"), msg_record_modified = T("Case details updated"), msg_record_deleted = T("Case deleted"), msg_list_empty = T("No Cases currently registered") ) if not r.component: # Module-specific CRUD form # NB: this assumes single case per person, must use # case perspective (dvr/case) for multiple cases # per person! from s3 import S3SQLCustomForm, S3SQLInlineComponent crud_form = S3SQLCustomForm( "dvr_case.reference", "dvr_case.organisation_id", "dvr_case.date", "dvr_case.status_id", "first_name", "middle_name", "last_name", "date_of_birth", "gender", S3SQLInlineComponent( "contact", fields = [("", "value"), ], filterby = {"field": "contact_method", "options": "EMAIL", }, label = T("Email"), multiple = False, name = "email", ), S3SQLInlineComponent( "contact", fields = [("", "value"), ], filterby = {"field": "contact_method", "options": "SMS", }, label = T("Mobile Phone"), multiple = False, name = "phone", ), "person_details.nationality", S3SQLInlineComponent( "address", label = T("Current Address"), fields = [("", "location_id"), ], filterby = {"field": "type", "options": "1", }, link = False, multiple = False, ), "dvr_case.comments", ) # Module-specific filter widgets from s3 import get_s3_filter_opts, S3TextFilter, S3OptionsFilter filter_widgets = [ S3TextFilter(["pe_label", "first_name", "middle_name", "last_name", #"email.value", #"phone.value", "dvr_case.reference", ], label = T("Search"), comment = T("You can search by name, ID or case number"), ), S3OptionsFilter("dvr_case.status_id", cols = 3, default = default_status, #label = T("Case Status"), options = s3db.dvr_case_status_filter_opts, sort = False, ), S3OptionsFilter("person_details.nationality", ), S3OptionsFilter("case_flag_case.flag_id", label = T("Flags"), options = get_s3_filter_opts("dvr_case_flag", translate = True, ), cols = 3, hidden = True, ), ] resource.configure(crud_form = crud_form, filter_widgets = filter_widgets, ) # Module-specific list fields (must be outside of r.interactive) list_fields = ["dvr_case.reference", "first_name", "middle_name", "last_name", "date_of_birth", "gender", "dvr_case.date", "dvr_case.status_id", ] resource.configure(list_fields = list_fields, ) return True