Example #1
0
def customize_org_office(**attr):
    """
        Customize org_office controller
    """

    s3 = current.response.s3
    s3db = current.s3db
    table = s3db.org_office

    location_id_field = table.location_id
    location_id_field.requires = IS_LOCATION_SELECTOR2(levels=levels)
    location_id_field.widget = S3LocationSelectorWidget2(levels=levels,
                                                         show_address=True,
                                                         show_map=True)
    # Don't add new Locations here
    location_id_field.comment = None

    list_fields = [
        "name",
        "organisation_id",
        "location_id",
        "phone1",
        "comments",
    ]

    crud_form = S3SQLCustomForm(*list_fields)

    # Report options
    report_fields = [
        "organisation_id",
    ]

    report_options = Storage(
        rows=report_fields,
        cols=report_fields,
        fact=["count(id)"],
        defaults=Storage(
            rows="organisation_id",
            cols="",
            fact="count(id)",
            totals=True,
            chart="barchart:rows",
            #table = "collapse",
        ))

    s3db.configure(
        "org_office",
        crud_form=crud_form,
        list_fields=list_fields,
        report_options=report_options,
    )

    # Remove rheader
    attr["rheader"] = None
    return attr
Example #2
0
def customise_pr_person_resource(r, tablename):

    s3db = current.s3db
    table = r.resource.table

    # Disallow "unknown" gender and defaults to "male"
    evr_gender_opts = dict(
        (k, v) for k, v in s3db.pr_gender_opts.items() if k in (2, 3))
    gender = table.gender
    gender.requires = IS_IN_SET(evr_gender_opts, zero=None)
    gender.default = 3

    if r.controller == "evr":
        # Hide evacuees emergency contacts
        settings.pr.show_emergency_contacts = False

        # Last name and date of birth mandatory in EVR module
        table.last_name.requires = IS_NOT_EMPTY(
            error_message=T("Please enter a last name"))

        dob_requires = s3_date("dob", future=0, past=1320,
                               empty=False).requires
        dob_requires.error_message = T("Please enter a date of birth")
        table.date_of_birth.requires = dob_requires

        # Enable Location_id
        from gluon import DIV
        from s3.s3widgets import S3LocationSelectorWidget2
        levels = (
            "L1",
            "L2",
            "L3",
        )
        location_id = table.location_id
        location_id.readable = location_id.writable = True
        location_id.label = T("Place of Birth")
        location_id.widget = S3LocationSelectorWidget2(
            levels=levels,
            lines=True,
        )
        location_id.represent = s3db.gis_LocationRepresent(sep=" | ")
        # Enable place of birth
        place_of_birth = s3db.pr_person_details.place_of_birth
        place_of_birth.label = "Specify a Different Place of Birth"
        place_of_birth.comment = DIV(
            _class="tooltip",
            _title="%s|%s" %
            (T("Different Place of Birth"),
             T("Specify a different place of birth (foreign country, village, hamlet)"
               )))
        place_of_birth.readable = place_of_birth.writable = True

        # Disable religion selection
        s3db.pr_person_details.religion.readable = False
        s3db.pr_person_details.religion.writable = False

    # Disable unneeded physical details
    pdtable = s3db.pr_physical_description
    hide_fields = [
        "race", "complexion", "height", "weight", "hair_length", "hair_style",
        "hair_baldness", "hair_comment", "facial_hair_type",
        "facial_hair_length", "facial_hair_color", "facial_hair_comment",
        "body_hair", "skin_marks", "medical_conditions"
    ]
    for fname in hide_fields:
        field = pdtable[fname]
        field.readable = field.writable = False

    # This set is suitable for Italy
    ethnicity_opts = (
        "Italian",
        "Chinese",
        "Albanese",
        "Philippine",
        "Pakistani",
        "English",
        "African",
        "Other",
        "Unknown",
    )
    ethnicity_opts = dict((v, T(v)) for v in ethnicity_opts)

    ethnicity = pdtable.ethnicity
    ethnicity.requires = IS_EMPTY_OR(IS_IN_SET(ethnicity_opts, sort=True))
    ethnicity.represent = S3Represent(options=ethnicity_opts, translate=True)
Example #3
0
    def apply_method(r, **attr):
        """
            Apply method.

            @param r: the S3Request
            @param attr: controller options for this request
        """

        if r.representation == "html":

            T = current.T
            s3db = current.s3db
            response = current.response
            table = r.table
            tracker = S3Trackable(table, record_id=r.id)

            title = T("Check-In")

            get_vars = r.get_vars

            # Are we being passed a location_id?
            location_id = get_vars.get("location_id", None)
            if not location_id:
                # Are we being passed a lat and lon?
                lat = get_vars.get("lat", None)
                if lat is not None:
                    lon = get_vars.get("lon", None)
                    if lon is not None:
                        form_vars = Storage(
                            lat=float(lat),
                            lon=float(lon),
                        )
                        form = Storage(vars=form_vars)
                        s3db.gis_location_onvalidation(form)
                        location_id = s3db.gis_location.insert(**form_vars)

            form = None
            if not location_id:
                # Give the user a form to check-in

                # Test the formstyle
                formstyle = current.deployment_settings.get_ui_formstyle()
                row = formstyle("test", "test", "test", "test")
                if isinstance(row, tuple):
                    # Formstyle with separate row for label (e.g. default Eden formstyle)
                    tuple_rows = True
                else:
                    # Formstyle with just a single row (e.g. Bootstrap, Foundation or DRRPP)
                    tuple_rows = False

                form_rows = []
                comment = ""

                _id = "location_id"
                label = LABEL("%s:" % T("Location"))

                from s3.s3widgets import S3LocationSelectorWidget2
                field = table.location_id
                #value = tracker.get_location(_fields=["id"],
                #                             as_rows=True).first().id
                value = None  # We always want to create a new Location, not update the existing one
                widget = S3LocationSelectorWidget2()(field, value)

                row = formstyle("%s__row" % _id, label, widget, comment)
                if tuple_rows:
                    form_rows.append(row[0])
                    form_rows.append(row[1])
                else:
                    form_rows.append(row)

                _id = "submit"
                label = ""
                widget = INPUT(_type="submit", _value=T("Check-In"))
                row = formstyle("%s__row" % _id, label, widget, comment)
                if tuple_rows:
                    form_rows.append(row[0])
                    form_rows.append(row[1])
                else:
                    form_rows.append(row)

                if tuple_rows:
                    # Assume TRs
                    form = FORM(TABLE(*form_rows))
                else:
                    form = FORM(*form_rows)

                if form.accepts(current.request.vars, current.session):
                    location_id = form.vars.get("location_id", None)

            if location_id:
                # We're not Checking-in in S3Track terms (that's about interlocking with another object)
                #tracker.check_in()
                #timestmp = form.vars.get("timestmp", None)
                #if timestmp:
                #    # @ToDo: Convert from string
                #    pass
                #tracker.set_location(location_id, timestmp=timestmp)
                tracker.set_location(location_id)
                response.confirmation = T("Checked-In successfully!")

            response.view = "check-in.html"
            output = dict(
                form=form,
                title=title,
            )
            return output

        # @ToDo: JSON representation for check-in from mobile devices
        else:
            raise HTTP(501, current.ERROR.BAD_METHOD)
Example #4
0
def customize_project_project(**attr):
    """
        Customize project_project controller
    """

    s3 = current.response.s3
    s3db = current.s3db
    table = s3db.project_project

    # Custom Form
    location_id_field = s3db.project_location.location_id
    location_id_field.requires = IS_LOCATION_SELECTOR2(levels=levels)
    location_id_field.widget = S3LocationSelectorWidget2(levels=levels,
                                                         show_address=True,
                                                         show_map=True)
    # Don't add new Locations here
    location_id_field.comment = None

    table.name.label = T("Description")
    table.comments.label = T("Details")
    #s3db.project_project_organisation.organisation_id.label = ""
    #s3db.project_project_project_type.project_type_id.label = ""
    crud_form = S3SQLCustomForm(
        "status_id",
        "name",
        "description",
        #"location.location_id",
        "organisation_id",
        S3SQLInlineComponent(
            "location",
            label=T("Location"),
            fields=["location_id"],
            orderby="location_id$name",
            #multiple = False,
        ),
        "start_date",
        "end_date",
    )

    filter_widgets = [
        S3TextFilter(
            [
                "name",
            ],
            label=T("Description"),
            _class="filter-search",
        ),
        S3LocationFilter(
            "location_id",
            widget="multiselect",
            levels=levels,
        ),
        S3OptionsFilter(
            "status_id",
            label=T("Status"),
            # Doesn't support translation
            #represent="%(name)s",
            # @ToDo: Introspect cols
            cols=3,
            #widget="multiselect",
        ),
        S3OptionsFilter(
            "project_project_type.project_type_id",
            # Doesn't support translation
            #represent="%(name)s",
            widget="multiselect",
        ),
        S3OptionsFilter(
            "project_organisation.organisation_id",
            # Doesn't support translation
            #represent="%(name)s",
            widget="multiselect",
        ),
        S3OptionsFilter(
            "project_organisation.organisation_id$organisation_type_id",
            # Doesn't support translation
            #represent="%(name)s",
            widget="multiselect",
        ),
    ]

    s3db.configure(
        "project_project",
        crud_form=crud_form,
        #filter_widgets = filter_widgets,
    )

    # Remove rheader
    attr["rheader"] = None
    return attr
Example #5
0
def customize_project_activity(**attr):
    """
        Customize project_activity controller
    """

    s3 = current.response.s3
    s3db = current.s3db
    table = s3db.project_activity

    #Use activities as projects (Temporary - location widget broken inline
    s3.crud_strings["project_activity"] = s3.crud_strings["project_project"]

    # Custom PreP
    standard_prep = s3.prep

    def custom_prep(r):
        if r.method in ["create", "update"]:
            # hide inline labels
            s3db.project_activity_organisation.organisation_id.label = ""
            s3db.project_activity_activity_type.activity_type_id.label = ""
        return True

    s3.prep = custom_prep

    list_fields = [
        "id",
        "status_id",
        "name",
        "location_id",
        "activity_organisation.organisation_id",
        "date",
        "end_date",
    ]

    # Custom Form
    table.location_id.requires = IS_LOCATION_SELECTOR2(levels=levels)
    table.location_id.widget = S3LocationSelectorWidget2(levels=levels,
                                                         show_address=True,
                                                         show_map=True)
    # Don't add new Locations here
    table.location_id.comment = None

    table.name.label = T("Name")
    table.comments.label = T("Description")

    crud_form = S3SQLCustomForm(
        "status_id",
        "name",
        "comments",
        "location_id",
        S3SQLInlineComponent(
            "activity_organisation",
            label=T("Organization"),
            fields=["organisation_id"],
            multiple=False,
        ),
        #S3SQLInlineComponent("activity_activity_type",
        #                     label = T("Activity Type"),
        #                     fields = ["activity_type_id"],
        #                     multiple = False,
        #                     ),
        "date",
        "end_date",
    )

    filter_widgets = [
        S3TextFilter(
            [
                "name",
            ],
            label=T("Description"),
            _class="filter-search",
        ),
        S3LocationFilter(
            "location_id",
            widget="multiselect",
            levels=levels,
        ),
        S3OptionsFilter(
            "status_id",
            label=T("Status"),
            # Doesn't support translation
            #represent="%(name)s",
            # @ToDo: Introspect cols
            cols=3,
            #widget="multiselect",
        ),
        #S3OptionsFilter("activity_activity_type.activity_type_id",
        # Doesn't support translation
        #represent="%(name)s",
        #                widget="multiselect",
        #                ),
        S3OptionsFilter(
            "activity_organisation.organisation_id",
            # Doesn't support translation
            #represent="%(name)s",
            widget="multiselect",
        ),
        S3OptionsFilter(
            "activity_organisation.organisation_id$organisation_type_id",
            # Doesn't support translation
            #represent="%(name)s",
            widget="multiselect",
        ),
    ]

    # Report options
    report_fields = [
        "activity_organisation.organisation_id",
        "status_id",
    ]

    report_options = Storage(
        rows=report_fields,
        cols=report_fields,
        fact=["count(id)"],
        defaults=Storage(
            rows="activity_organisation.organisation_id",
            cols="status_id",
            fact="count(id)",
            totals=True,
            chart="barchart:rows",
            #table = "collapse",
        ))

    s3db.configure(
        "project_activity",
        list_fields=list_fields,
        crud_form=crud_form,
        filter_widgets=filter_widgets,
        report_options=report_options,
    )

    # Remove rheader
    attr["rheader"] = None
    return attr
Example #6
0
def customize_cms_post(**attr):
    """
        Customize cms_post controller
    """

    s3 = current.response.s3
    s3db = current.s3db
    table = s3db.cms_post

    s3.dl_pagelength = 12
    s3.dl_rowsize = 2
    # CRUD Form

    table.location_id.requires = IS_LOCATION_SELECTOR2(levels=levels)
    table.location_id.widget = S3LocationSelectorWidget2(levels=levels,
                                                         show_address=True,
                                                         show_map=True)
    # Don't add new Locations here
    table.location_id.comment = None

    table.series_id.readable = table.series_id.writable = True
    table.series_id.label = T("Type")

    table.body.label = T("Description")
    table.body.widget = None
    s3db.event_event_post.event_id.label = ""
    s3db.doc_document.file.label = ""
    crud_form = S3SQLCustomForm(
        "date",
        "series_id",
        "body",
        "location_id",
        S3SQLInlineComponent(
            "event_post",
            #label = T("Disaster(s)"),
            label=T("Disaster"),
            multiple=False,
            fields=["event_id"],
            orderby="event_id$name",
        ),
        S3SQLInlineComponent(
            "document",
            name="file",
            label=T("Files"),
            fields=[
                "file",
                #"comments",
            ],
        ),
    )

    # Return to List view after create/update/delete
    # We now do all this in Popups
    #url_next = URL(c="default", f="index", args="newsfeed")

    s3db.configure(
        "cms_post",
        crud_form=crud_form,
    )

    # Remove rheader
    attr["rheader"] = None
    return attr
Example #7
0
    def custom_prep(r):
        # Call standard prep
        if callable(standard_prep):
            result = standard_prep(r)
            if not result:
                return False

        if r.interactive or r.representation == "aadata":
            s3db.org_customize_org_resource_fields(r.method)

            # Configure fields
            #table.site_id.readable = table.site_id.readable = False
            location_field = table.location_id
            #location_field.label = T("District")

            from s3.s3filter import S3TextFilter, S3OptionsFilter
            filter_widgets = [
                S3TextFilter([
                    "organisation_id$name",
                    "location_id",
                    "parameter_id$name",
                    "comments",
                ],
                             label=T("Search")),
                S3OptionsFilter(
                    "parameter_id",
                    label=T("Type"),
                    widget="multiselect",
                ),
            ]

            s3db.configure("org_resource", filter_widgets=filter_widgets)

            # Filter from a Profile page?
            # If so, then default the fields we know
            get_vars = current.request.get_vars
            location_id = get_vars.get("~.(location)", None)
            organisation_id = get_vars.get("~.(organisation)", None)
            if organisation_id:
                org_field = table.organisation_id
                org_field.default = organisation_id
                org_field.readable = org_field.writable = False
            if location_id:
                location_field.default = location_id
                # We still want to be able to specify a precise location
                #location_field.readable = location_field.writable = False
            location_field.requires = IS_LOCATION_SELECTOR2(levels=levels)
            location_field.widget = S3LocationSelectorWidget2(
                levels=levels, show_address=True, show_map=True)

            # Don't add new Locations here
            location_field.comment = None

            # Return to Sumamry view after create/update/delete (unless done via Modal)
            url_next = URL(c="org", f="resource", args="summary")

            s3db.configure(
                "org_resource",
                create_next=url_next,
                delete_next=url_next,
                update_next=url_next,
                # Don't include a Create form in 'More' popups
                listadd=False if r.method == "datalist" else True,
            )

            # This is awful in Popups & inconsistent in dataTable view (People/Documents don't have this & it breaks the styling of the main Save button)
            #s3.cancel = URL(c="org", f="resource")

        return True
Example #8
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.s3forms import S3SQLCustomForm, S3SQLInlineComponent
    from s3.s3validators import IS_LOCATION_SELECTOR2
    from s3.s3widgets import 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)

    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")

        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"))
Example #9
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.s3validators import IS_LOCATION_SELECTOR2
    from s3.s3widgets import 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.s3filter 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",
                           )
        )

    from s3.s3forms import S3SQLCustomForm, S3SQLInlineComponentMultiSelectWidget
    # Custom Crud Form
    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,
                   )