示例#1
0
    def vehicle_report(r, **attr):
        """
            Custom method to provide a report on Vehicle Deployment Times
            - this is one of the main tools currently used to manage an Incident
        """

        rheader = attr.get("rheader", None)
        if rheader:
            rheader = rheader(r)

        station_id = r.id
        if station_id:

            T = current.T
            s3db = current.s3db
            ftable = s3db.fire_station
            atable = s3db.asset_asset
            eatable = s3db.event_asset
            itable = s3db.event_incident

            query = (ftable.id == station_id) & \
                    (ftable.site_id == atable.site_id) & \
                    (atable.type == 1) & \
                    (atable.id == eatable.asset_id) & \
                    (eatable.start_date != None) & \
                    (eatable.end_date == None)

            current.response.s3.crud_strings["event_asset"] = Storage(
                title_report=T("Vehicle Deployment Times"), )

            eatable.asset_id.label = T("Vehicle")

            # Add field method for minutes
            def minutes(row):
                if hasattr(row, "event_asset"):
                    row = row.event_asset
                if hasattr(row, "start_date") and row.start_date:
                    return int(
                        (r.utcnow - row.start_date).total_seconds() / 60)
                else:
                    return 0

            from gluon import Field
            eatable.minutes = Field.Method(
                "minutes",
                minutes,
            )
            s3db.configure(
                "event_asset",
                extra_fields=["start_date"],
            )

            from s3 import S3Report
            req = r.factory(
                prefix="event",
                name="asset",
                args=["report"],
                vars=Storage(
                    rows="asset_id",
                    cols="incident_id",
                    fact="sum(minutes)",
                ),
            )
            req.set_handler("report", S3Report())
            req.resource.add_filter(query)
            return req(rheader=rheader)
示例#2
0
    def customise_req_organisation_needs_resource(r, tablename):

        s3db = current.s3db
        table = current.s3db.req_organisation_needs

        CASH = T("Cash Donations needed")

        if r.tablename == "req_organisation_needs":

            from s3 import IS_ONE_OF, S3DateTime

            # Allow only organisations which do not have a needs record
            # yet (single component):
            field = table.organisation_id
            dbset = current.db(table.id == None)
            left = table.on(
                table.organisation_id == current.s3db.org_organisation.id)
            field.requires = IS_ONE_OF(
                dbset,
                "org_organisation.id",
                field.represent,
                left=left,
                orderby="org_organisation.name",
                sort=True,
            )

            # Format modified_on as date
            field = table.modified_on
            field.represent = lambda d: S3DateTime.date_represent(d, utc=True)

        if r.representation in ("html", "aadata", "iframe"):

            # Structured lists for interactive views
            from gluon import Field
            table.needs_skills = Field.Method(lambda row: \
                                    organisation_needs(row, need_type="skills"))
            table.needs_items = Field.Method(lambda row: \
                                    organisation_needs(row, need_type="items"))
            current.response.s3.stylesheets.append("../themes/RW/needs.css")

            needs_skills = (T("Volunteers needed"), "needs_skills")
            needs_items = (T("Supplies needed"), "needs_items")

            # Filter widgets
            from s3 import S3LocationFilter, S3OptionsFilter, S3TextFilter
            filter_widgets = [  #S3TextFilter(["organisation_id$name",
                #              ],
                #              label = T("Search"),
                #             ),
                S3OptionsFilter("organisation_id"),
                S3OptionsFilter(
                    "organisation_needs_skill.skill_id",
                    label=T("Skills sought"),
                ),
                S3OptionsFilter(
                    "organisation_needs_item.item_id",
                    label=T("Supplies sought"),
                ),
                S3LocationFilter(
                    "organisation_id$active_service_location.site_id$location_id",
                ),
            ]

            # CRUD form
            from s3 import S3SQLCustomForm, S3SQLInlineComponent
            crud_form = S3SQLCustomForm(
                "organisation_id",
                S3SQLInlineComponent(
                    "organisation_needs_skill",
                    label=T("Volunteers needed"),
                    fields=[
                        "skill_id",
                        "demand",
                        "comments",
                    ],
                ),
                S3SQLInlineComponent(
                    "organisation_needs_item",
                    label=T("Supplies needed"),
                    fields=[
                        "item_id",
                        "demand",
                        "comments",
                    ],
                ),
                (CASH, "money"),
                "money_details",
                #"vol",
                #"vol_details",
            )

            next_page = r.url(method="") \
                        if r.tablename == "req_organisation_needs" else None

            s3db.configure(
                "req_organisation_needs",
                crud_form=crud_form,
                filter_widgets=filter_widgets,
                create_next=next_page,
                update_next=next_page,
            )
        else:
            # Simple fields for exports
            needs_skills = (T("Volunteers needed"),
                            "organisation_needs_skill.skill_id")
            needs_items = (T("Supplies needed"),
                           "organisation_needs_item.item_id")

        # List fields (all formats)
        list_fields = [
            "organisation_id",
            needs_skills,
            needs_items,
            (CASH, "money"),
            (T("Cash Donation Details"), "money_details"),
            (T("Last Update"), "modified_on"),
        ]

        s3db.configure(
            "req_organisation_needs",
            list_fields=list_fields,
        )
示例#3
0
    def __call__(self):
        """ The userstats controller """

        # Require ORG_GROUP_ADMIN
        auth = current.auth
        if not auth.s3_has_role("ORG_GROUP_ADMIN"):
            auth.permission.fail()

        from s3 import S3CRUD, s3_get_extension, s3_request

        request = current.request
        args = request.args

        # Create an S3Request
        r = s3_request(
            "org",
            "organisation",
            c="default",
            f="index/%s" % args[0],
            args=args[1:],
            extension=s3_get_extension(request),
        )

        # Filter to root organisations
        resource = r.resource
        resource.add_filter(FS("id").belongs(self.root_orgs))

        # Configure field methods
        from gluon import Field
        table = resource.table
        table.total_accounts = Field.Method("total_accounts",
                                            self.total_accounts)
        table.active_accounts = Field.Method("active_accounts",
                                             self.active_accounts)
        table.disabled_accounts = Field.Method("disabled_accounts",
                                               self.disabled_accounts)
        table.active30 = Field.Method("active30", self.active30)

        # Labels for field methods
        T = current.T
        TOTAL = T("Total User Accounts")
        ACTIVE = T("Active")
        DISABLED = T("Inactive")
        ACTIVE30 = T("Logged-in Last 30 Days")

        # Configure list_fields
        list_fields = (
            "id",
            "name",
            (TOTAL, "total_accounts"),
            (ACTIVE, "active_accounts"),
            (DISABLED, "disabled_accounts"),
            (ACTIVE30, "active30"),
        )

        # Configure form
        from s3 import S3SQLCustomForm, S3SQLVirtualField
        crud_form = S3SQLCustomForm(
            "name",
            S3SQLVirtualField(
                "total_accounts",
                label=TOTAL,
            ),
            S3SQLVirtualField(
                "active_accounts",
                label=ACTIVE,
            ),
            S3SQLVirtualField(
                "disabled_accounts",
                label=DISABLED,
            ),
            S3SQLVirtualField(
                "active30",
                label=ACTIVE30,
            ),
        )

        # Configure read-only
        resource.configure(
            insertable=False,
            editable=False,
            deletable=False,
            crud_form=crud_form,
            filter_widgets=None,
            list_fields=list_fields,
        )

        output = r(rheader=self.rheader)

        if isinstance(output, dict):

            output["title"] = T("User Statistics")

            # URL to open the resource
            open_url = resource.crud._linkto(r, update=False)("[id]")

            # Add action button for open
            action_buttons = S3CRUD.action_buttons
            action_buttons(
                r,
                deletable=False,
                copyable=False,
                editable=False,
                read_url=open_url,
            )

        return output