Пример #1
0
    def prep(r):
        if r.component_name == "group_membership":

            from s3 import S3AddPersonWidget, S3SQLCustomForm

            table = s3db.pr_group_membership
            f = table.person_id
            f.widget = S3AddPersonWidget(controller="stdm")

            if auth.s3_has_role("INFORMAL_SETTLMENT"):
                f = table.role_id
                f.readable = f.writable = True
                f.label = T("Household Relation")
                f.comment = S3PopupLink(
                    c="stdm",
                    f="group_member_role",
                    label=T("Create Household Relation"),
                    vars={"child": "role_id"},
                )

                list_fields = [
                    "person_id",
                    "role_id",
                ]

                crud_form = S3SQLCustomForm(
                    "person_id",
                    "role_id",
                )
            else:
                list_fields = [
                    "person_id",
                ]

                crud_form = S3SQLCustomForm("person_id", )

            s3db.configure(
                "pr_group_membership",
                crud_form=crud_form,
                list_fields=list_fields,
            )
        return True
Пример #2
0
    def prep(r):

        resource = r.resource
        table = resource.table

        get_vars = r.get_vars
        if "viewing" in get_vars:

            try:
                vtablename, record_id = get_vars["viewing"].split(".")
            except ValueError:
                return False

            if vtablename == "pr_person":

                # Set contacts-method to retain the tab
                s3db.set_method(
                    "pr",
                    "person",
                    method="contacts",
                    action=s3db.pr_Contacts,
                )

                # Get all group_ids with this person_id
                gtable = s3db.pr_group
                join = gtable.on(gtable.id == table.group_id)
                query = (table.person_id == record_id) & \
                        (gtable.group_type == 7) & \
                        (table.deleted != True)
                rows = db(query).select(
                    table.group_id,
                    join=join,
                )
                group_ids = set(row.group_id for row in rows)
                # Hide the link for this person (to prevent changes/deletion)
                if group_ids:
                    # Single group ID?
                    group_id = tuple(group_ids)[0] if len(
                        group_ids) == 1 else None
                elif r.http == "POST":
                    name = s3_fullname(record_id)
                    group_id = gtable.insert(
                        name=name,
                        group_type=7,
                    )
                    s3db.update_super(gtable, {"id": group_id})
                    table.insert(
                        group_id=group_id,
                        person_id=record_id,
                        group_head=True,
                    )
                    group_ids = {group_id}
                resource.add_filter(FS("person_id") != record_id)
            else:
                group_ids = set()

            # Add-Person widget to use BR controller and expose pe_label
            from s3 import S3AddPersonWidget
            field = table.person_id
            field.represent = s3db.pr_PersonRepresent(show_link=True)
            field.widget = S3AddPersonWidget(
                controller="br",
                pe_label=True,
            )

            # Expose Family Member Roles
            ROLE = T("Role")
            field = table.role_id
            field.readable = field.writable = True
            field.label = ROLE
            field.comment = DIV(
                _class="tooltip",
                _title="%s|%s" % (
                    T("Role"),
                    T("The role of the person within the family"),
                ))
            field.requires = IS_EMPTY_OR(
                IS_ONE_OF(
                    db,
                    "pr_group_member_role.id",
                    field.represent,
                    filterby="group_type",
                    filter_opts=(7, ),
                ))

            # Adjust label for group_head
            field = table.group_head
            field.label = T("Head of Family")

            # Show only links for relevant cases
            # NB Filter also prevents showing all links if case_ids is empty
            if not r.id:
                if len(group_ids) == 1:
                    resource.add_filter(FS("group_id") == group_id)
                else:
                    resource.add_filter(FS("group_id").belongs(group_ids))

            # Adjust list-fields for this perspective
            s3db.pr_person.pe_label.label = T("ID")
            list_fields = [
                "person_id$pe_label",
                "person_id",
                "person_id$gender",
                "person_id$date_of_birth",
                "group_head",
                (ROLE, "role_id"),
                (T("Case Status"), "person_id$case.status_id"),
                "comments",
            ]

            if len(group_ids) == 0:
                # No case group exists, will be auto-generated on POST,
                # hide the field in the form:
                field = table.group_id
                field.readable = field.writable = False
            elif len(group_ids) == 1:
                field = table.group_id
                field.default = group_id
                # If we have only one relevant case, then hide the group ID:
                field.readable = field.writable = False
            elif len(group_ids) > 1:
                # Show the case ID in list fields if there is more than one
                # relevant case
                list_fields.insert(0, "group_id")
            resource.configure(
                filter_widgets=None,
                list_fields=list_fields,
            )

            # Adjust CRUD strings for this perspective
            s3.crud_strings["pr_group_membership"] = Storage(
                label_create=T("Add Family Member"),
                title_display=T("Family Member Details"),
                title_list=T("Family Members"),
                title_update=T("Edit Family Member"),
                label_list_button=T("List Family Members"),
                label_delete_button=T("Remove Family Member"),
                msg_record_created=T("Family Member added"),
                msg_record_modified=T("Family Member updated"),
                msg_record_deleted=T("Family Member removed"),
                msg_list_empty=T("No Family Members currently registered"))

        # Do not allow update of person_id
        if r.id:
            field = table.person_id
            field.writable = False
            field.comment = None

        return True
Пример #3
0
    def prep(r):

        tablename = "pr_group"
        s3db.configure(tablename,
                       deletable = False,
                       )

        if r.component:

            if r.component_name == "group_membership":

                ctable = r.component.table

                # Hide group_head field
                field = ctable.group_head
                field.readable = field.writable = False

                # Configure person_id widget
                settings.pr.request_dob = False
                settings.pr.request_gender = False

                from s3 import S3AddPersonWidget
                ctable.person_id.widget = S3AddPersonWidget(controller = "deploy")

                # Configure list_fields for this context
                list_fields = ["person_id",
                               "comments",
                               ]
                s3db.configure("pr_group_membership",
                               list_fields = list_fields,
                               )
        elif not r.id:

            table = r.table

            # Have we got a group defined?
            ltable = s3db.org_organisation_team
            query = (table.deleted == False) & \
                    (table.system == False) & \
                    (table.group_type == 5)
            organisation_id = auth.user.organisation_id
            if organisation_id:
                left = ltable.on((ltable.group_id == table.id) & \
                                 ((ltable.organisation_id == organisation_id) | \
                                  (ltable.organisation_id == None)))
            else:
                left = None
            groups = db(query).select(table.id,
                                      ltable.organisation_id,
                                      left = left,
                                      )
            if organisation_id and len(groups) > 1:
                _channels = groups.find(lambda row: row["org_organisation_team.organisation_id"] == organisation_id)
                if not _channels:
                    _channels = groups.find(lambda row: row["org_organisation_team.organisation_id"] == None)
                record = _channels.first()
            else:
                record = groups.first()

            if record:
                record_id = record.pr_group.id
                r.id = record_id
                r.resource.add_filter(table.id == record_id)
                r.method = "update"
            else:
                r.method = "create"

        return True