def s3_role_required(): """ Role Required to access a resource - used by GIS for map layer permissions management """ T = current.T gtable = current.auth.settings.table_group f = S3ReusableField( "role_required", gtable, sortby="role", requires=IS_NULL_OR( IS_ONE_OF(db, "auth_group.id", "%(role)s", zero=T("Public"))), widget=S3AutocompleteWidget("admin", "group", fieldname="role"), represent=s3_auth_group_represent, label=T("Role Required"), comment=DIV( _class="tooltip", _title="%s|%s" % (T("Role Required"), T("If this record should be restricted then select which role is required to access the record here." ))), ondelete="RESTRICT") return f()
def super_link(self, supertable, label=None, comment=None, represent=None, orderby=None, sort=True, filterby=None, filter_opts=None, groupby=None, widget=None, empty=True, default=DEFAULT, readable=False, writable=False): """ Get a foreign key field for a super-entity @param supertable: the super-entity table @param label: label for the field @param comment: comment for the field @param readable: set the field readable @param represent: set a representation function for the field """ key = self.super_key(supertable) requires = IS_ONE_OF(current.db, "%s.%s" % (supertable._tablename, key), represent, orderby=orderby, sort=sort, groupby=groupby, filterby=filterby, filter_opts=filter_opts) if empty: requires = IS_EMPTY_OR(requires) return Field(key, supertable, default=default, requires=requires, readable=readable, writable=writable, label=label, comment=comment, represent=represent, widget=widget, ondelete="RESTRICT")
def s3_roles_permitted(name="roles_permitted", **attr): """ List of Roles Permitted to access a resource - used by CMS """ from s3validators import IS_ONE_OF T = current.T if "label" not in attr: label = T("Roles Permitted") if "sortby" not in attr: sortby = "role" if "represent" not in attr: represent = s3_auth_group_represent if "requires" not in attr: requires = IS_NULL_OR( IS_ONE_OF(current.db, "auth_group.id", "%(role)s", multiple=True)) if "comment" not in attr: comment = DIV( _class="tooltip", _title="%s|%s" % (T("Roles Permitted"), T("If this record should be restricted then select which role(s) are permitted to access the record here." ))) if "ondelete" not in attr: ondelete = "RESTRICT" f = S3ReusableField( name, "list:reference auth_group", sortby=sortby, requires=requires, represent=represent, # @ToDo #widget = S3CheckboxesWidget(lookup_table_name = "auth_group", # lookup_field_name = "role", # multiple = True), label=label, comment=comment, ondelete=ondelete) return f()
def widget(field, value, download_url=None, **attr): """ Render a widget for the Field/value @param field: the Field @param value: the value @param download_url: the download URL for upload fields @param attr: the HTML attributes for the widget @note: upload fields currently not rendered because the upload widget wouldn't render the current value, hence pointless for merge @note: custom widgets must allow override of both _id and _name attributes """ widgets = SQLFORM.widgets ftype = str(field.type) if value is not None and ftype not in ("id", "upload", "blob"): # Call field.formatter to prepare the value for the widget value = field.formatter(value) if ftype == "id": inp = None elif ftype == "upload": inp = None #if field.widget: #inp = field.widget(field, value, #download_url=download_url, **attr) #else: #inp = widgets.upload.widget(field, value, #download_url=download_url, **attr) elif field.widget: if isinstance(field.widget, S3LocationSelectorWidget): # Workaround - location selector does not support # renaming of the fields => switch to dropdown level = None if value: try: level = s3db.gis_location[value].level except: pass widget = S3LocationDropdownWidget(level, value) field.requires = IS_EMPTY_OR( IS_ONE_OF(current.db, "gis_location.id")) inp = widget(field, value, **attr) else: inp = field.widget(field, value, **attr) elif ftype == "boolean": inp = widgets.boolean.widget(field, value, **attr) elif widgets.options.has_options(field): if not field.requires.multiple: inp = widgets.options.widget(field, value, **attr) else: inp = widgets.multiple.widget(field, value, **attr) elif ftype[:5] == "list:": inp = widgets.list.widget(field, value, **attr) elif ftype == "text": inp = widgets.text.widget(field, value, **attr) elif ftype == "password": inp = widgets.password.widget(field, value, **attr) elif ftype == "blob": inp = None else: ftype = ftype in widgets and ftype or "string" inp = widgets[ftype].widget(field, value, **attr) return inp
def super_link(cls, name, supertable, label=None, comment=None, represent=None, orderby=None, sort=True, filterby=None, filter_opts=None, not_filterby=None, not_filter_opts=None, instance_types=None, realms=None, updateable=False, groupby=None, script=None, widget=None, empty=True, default=DEFAULT, ondelete="CASCADE", readable=False, writable=False): """ Get a foreign key field for a super-entity @param supertable: the super-entity table @param label: label for the field @param comment: comment for the field @param readable: set the field readable @param represent: set a representation function for the field """ if isinstance(supertable, str): supertable = cls.table(supertable) if supertable is None: if name is not None: return Field(name, "integer", readable=False, writable=False) else: raise SyntaxError("Undefined super-entity") else: key = cls.super_key(supertable) if name is not None and name != key: raise SyntaxError("Primary key %s not found in %s" % \ (name, supertable._tablename)) requires = IS_ONE_OF(current.db, "%s.%s" % (supertable._tablename, key), represent, orderby=orderby, sort=sort, groupby=groupby, filterby=filterby, filter_opts=filter_opts, instance_types=instance_types, realms=realms, updateable=updateable, not_filterby=not_filterby, not_filter_opts=not_filter_opts,) if empty: requires = IS_EMPTY_OR(requires) # Add the script into the comment if script: if comment: comment = TAG[""](comment, S3ScriptItem(script=script)) else: comment = S3ScriptItem(script=script) return Field(key, supertable, default = default, requires = requires, readable = readable, writable = writable, label = label, comment = comment, represent = represent, widget = widget, ondelete = ondelete)