Example #1
0
def customise_org_facility_resource(r, tablename):
    """
        Customise event_event resource
        - List Fields
        - Form
        - Filter
        - Report 
        Runs after controller customisation
        But runs before prep
    """

    s3db = current.s3db

    from s3 import IS_LOCATION_SELECTOR2, S3LocationSelectorWidget2
    levels = ("L0", "L1", "L2")
    loc_field = r.table.location_id
    loc_field.requires = IS_LOCATION_SELECTOR2(levels=levels)
    loc_field.widget = S3LocationSelectorWidget2(levels=levels,
                                                 show_address=True)

    list_fields = [
        "name",
        (T("Type"), "facility_type.name"),
        #"organisation_id",
        "location_id",
        "contact",
        "phone1",
        "email",
        "comments",
    ]

    from s3 import S3OptionsFilter, S3TextFilter
    filter_widgets = [
        S3TextFilter(
            [
                "name",
                "site_facility_type.facility_type_id",
                #"organisation_id",
                "location_id",
                "contact",
                "phone1",
                "email",
                "comments"
            ],
            label=T("Search"),
        ),
        S3OptionsFilter(
            "site_facility_type.facility_type_id",
            header=True,
            label=T("Type of Place"),
        ),
        #S3OptionsFilter("organisation_id",
        #                header = True,
        #                represent = "%(name)s",
        #                ),
    ]

    report_fields = [  #"name",
        "site_facility_type.facility_type_id",
        "site_org_group.group_id",
        "location_id$L3",
        "organisation_id",
    ]

    report_options = Storage(
        rows=report_fields,
        cols=[],
        fact=[(T("Number of Facilities"), "count(name)")],
        defaults=Storage(
            rows="site_facility_type.facility_type_id",
            #cols = "site_org_group.group_id",
            fact="count(name)",
            totals=True,
            chart="barchart:rows",
            table="collapse",
        ))

    # Custom Crud Form
    from s3 import S3SQLCustomForm, S3SQLInlineComponentMultiSelectWidget
    crud_form = S3SQLCustomForm(
        "name",
        S3SQLInlineComponentMultiSelectWidget(
            "facility_type",
            #label = T("Type of Place"),
            field="facility_type_id",
        ),
        #"organisation_id",
        "location_id",
        "contact",
        "phone1",
        "email",
        "comments",
    )

    s3db.configure(
        tablename,
        crud_form=crud_form,
        filter_widgets=filter_widgets,
        list_fields=list_fields,
        report_options=report_options,
    )
Example #2
0
def customise_event_event_resource(r, tablename):
    """
        Customise event_event resource
        - List Fields
        - CRUD Strings
        - Form
        - Filter
        - Report 
        Runs after controller customisation
        But runs before prep
    """

    from s3 import S3SQLCustomForm, S3SQLInlineComponent, IS_LOCATION_SELECTOR2, S3LocationSelectorWidget2

    db = current.db
    s3db = current.s3db
    table = r.table
    table.name.label = T("Disaster Number")

    location_field = s3db.event_event_location.location_id
    location_field.requires = IS_LOCATION_SELECTOR2(levels=gis_levels)
    location_field.widget = S3LocationSelectorWidget2(levels=gis_levels)

    impact_fields = OrderedDict(
        killed="Killed",
        total_affected="Total Affected",
        est_damage="Estimated Damage (US$ Million)",
    )

    ptable = s3db.stats_impact_type
    rows = db(ptable.name.belongs(impact_fields.values())).select(
        ptable.id,
        ptable.name,
    )
    parameters = rows.as_dict(key="name")

    impact_components = []
    impact_crud_form_fields = []
    impact_list_fields = []
    impact_report_fields = []
    for tag, label in impact_fields.items():
        parameter = parameters[label]["id"]
        impact_components.append({
            "name": tag,
            "link": "event_event_impact",
            "joinby": "event_id",
            "key": "impact_id",
            "filterby": "parameter_id",
            "filterfor": (parameter, ),
        })
        label = T(label)
        impact_crud_form_fields.append(
            S3SQLInlineComponent(tag,
                                 label=label,
                                 link=False,
                                 multiple=False,
                                 fields=[("", "value")],
                                 filterby=dict(field="parameter_id",
                                               options=parameter)))
        impact_list_fields.append((label, "%s.value" % tag))
        impact_report_fields.append(
            (T("Total %(param)s") % dict(param=label), "sum(%s.value)" % tag))

    s3db.add_components(
        "event_event",
        stats_impact=impact_components,
    )

    crud_form = S3SQLCustomForm(
        "name",
        "event_type_id",
        "start_date",
        "end_date",
        # @ToDo: Inline location_id field
        #S3SQLInlineComponent("event_location",
        #                     label = T("Location"),
        #                     multiple = False,
        #                     fields = [("", "location_id")],
        #                     ),
        "comments",
        *impact_crud_form_fields)

    list_fields = [  #"name",
        "event_type_id",
    ]
    lappend = list_fields.append

    for level in gis_levels:
        location_level = "event_location.location_id$%s" % level
        lappend(location_level)

    s3db.add_components(
        "gis_location",
        gis_location_tag={
            "name": "pcode",
            "joinby": "location_id",
            "filterby": "tag",
            "filterfor": ("PCode", ),
        },
    )
    lappend(("PCode", "event_location.location_id$pcode.value"))

    list_fields.extend((
        "start_date",
        "end_date",
    ))
    list_fields.extend(impact_list_fields)

    report_facts = [(T("Number of Disasters"), "count(id)")]
    report_facts.extend(impact_report_fields)

    report_options = s3db.get_config("event_event", "report_options")
    report_options.fact = report_facts

    s3db.configure(
        "event_event",
        crud_form=crud_form,
        list_fields=list_fields,
    )

    if r.interactive:
        # Labels
        table.comments.label = T("Description")

        current.response.s3.crud_strings["event_event"] = Storage(
            label_create=T("Record Disaster"),
            title_display=T("Disaster Details"),
            title_list=T("Disasters"),
            title_update=T("Edit Disaster"),
            label_list_button=T("List Disasters"),
            label_delete_button=T("Delete Disaster"),
            msg_record_created=T("Disaster added"),
            msg_record_modified=T("Disaster updated"),
            msg_record_deleted=T("Disaster deleted"),
            msg_list_empty=T("No Disasters currently registered"))