magpie.ui.management.views¶
Attributes¶
Classes¶
Supported statuses of user-group relationships. |
|
Values applicable to User statues. |
|
Applicable Permission values (names) under certain Service and Resource. |
|
Explicit definition of a |
|
Regroups multiple administration-level operations to be dispatched to the API requests. |
|
Base methods for Magpie UI pages. |
|
Regroups multiple administration-level operations to be dispatched to the API requests. |
Functions¶
|
Get remote resources for a single service. |
|
Obtain the date-time of the last known sync event for a service. |
Main function to sync resources with remote server. |
|
|
Search in order for matched value of |
|
|
|
Decorator that encapsulates the operation in a try/except block, and redirects the response to the UI error page |
|
Use a pyramid sub-request to request Magpie API routes via the UI. This avoids max retries and closed connections |
|
Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation. |
|
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when |
|
Module Contents¶
- magpie.ui.management.views.fetch_single_service(service: magpie.models.Service | int, session: sqlalchemy.orm.session.Session) None[source]¶
Get remote resources for a single service.
- Parameters:
service – Specific service for which to synchronize remote resources.
session – Database connexion to apply synchronization changes.
- magpie.ui.management.views.get_last_sync(service_id: int, session: sqlalchemy.orm.session.Session) datetime.datetime | None[source]¶
Obtain the date-time of the last known sync event for a service.
- magpie.ui.management.views.merge_local_and_remote_resources(resources_local: magpie.typedefs.RemoteResourceTree, service_sync_type: magpie.typedefs.Str, service_id: int, session: sqlalchemy.orm.session.Session) magpie.typedefs.RemoteResourceTree[source]¶
Main function to sync resources with remote server.
- magpie.ui.management.views.SYNC_SERVICES_TYPES: Dict[magpie.typedefs.Str, Type[SyncServiceInterface]][source]¶
- magpie.ui.management.views.get_constant(constant_name: magpie.typedefs.Str, settings_container: magpie.typedefs.AnySettingsContainer | None = None, settings_name: magpie.typedefs.Str | None = None, default_value: magpie.typedefs.SettingValue | None = None, raise_not_set: bool = True, raise_missing: bool = True, print_missing: bool = False, empty_missing: bool = False) magpie.typedefs.SettingValue[source]¶
- Search in order for matched value of
constant_name: search in
MAGPIE_CONSTANTSsearch in settings if specified
search alternative setting names (see below)
search in
magpie.constantsdefinitionssearch in environment variables
Parameter
constant_nameis expected to have the formatMAGPIE_[VARIABLE_NAME]although any value can be passed to retrieve generic settings from all above-mentioned search locations.If
settings_nameis provided as alternative name, it is used as is to search for results ifconstant_namewas not found. Otherwise,magpie.[variable_name]is used for additional search when the formatMAGPIE_[VARIABLE_NAME]was used forconstant_name(i.e.:MAGPIE_ADMIN_USERwill also search formagpie.admin_userand so on for corresponding constants).- Parameters:
constant_name – key to search for a value
settings_container – WSGI application settings container (if not provided, uses found one in current thread)
settings_name – alternative name for settings if specified
default_value – default value to be returned if not found anywhere, and exception raises are disabled.
raise_not_set – raise an exception if the found key is
None, search until last case if others areNoneraise_missing – raise exception if key is not found anywhere
print_missing – print message if key is not found anywhere, return
Noneempty_missing – consider an empty value for an existing key as if it was missing (i.e.: as if not set).
- Returns:
found value or default_value
- Raises:
ValueError – if resulting value is invalid based on options (by default raise missing/empty/
Nonevalue)LookupError – if no appropriate value could be found from all search locations (according to options)
- Search in order for matched value of
- class magpie.ui.management.views.UserGroupStatus[source]¶
Bases:
magpie.utils.FlexibleNameEnumSupported statuses of user-group relationships.
- ALL = 'all'¶
- ACTIVE = 'active'¶
- PENDING = 'pending'¶
- class magpie.ui.management.views.UserStatuses[source]¶
Bases:
enum.IntFlag,magpie.utils.FlexibleNameEnumValues applicable to User statues.
Provides allowed values for the
statussearch query ofUserandUserPendingentries. Also, defines the possible values ofUser.statusfield, omittingUserStatuses.Pendingreserved for objects defined byUserPending.Initialize self. See help(type(self)) for accurate signature.
- OK = 1¶
- WebhookError = 2¶
- Pending = 4¶
- classmethod _get_one(status: AnyUserStatus) UserStatuses | None[source]¶
- classmethod get(status: None | int | magpie.typedefs.Str | UserStatuses | Iterable[None, int, magpie.typedefs.Str, UserStatuses], default: UserStatuses | None = None) UserStatuses | None[source]¶
Obtains the combined flag
UserStatuses
- classmethod allowed() List[None | int | magpie.typedefs.Str][source]¶
Returns all supported representation values that can be mapped to a valid status for
UserSearchService.
- classmethod all() UserStatuses[source]¶
Representation of all flags combined.
- __or__(other: UserStatuses | int) UserStatuses[source]¶
Return self|value.
- __and__(other: UserStatuses | int) UserStatuses[source]¶
Return self&value.
- __xor__(other: UserStatuses | int) UserStatuses[source]¶
Return self^value.
- __iter__() Iterable[UserStatuses][source]¶
- class magpie.ui.management.views.Permission[source]¶
Bases:
magpie.utils.ExtendedEnumApplicable Permission values (names) under certain Service and Resource.
- READ = 'read'¶
- WRITE = 'write'¶
- ACCESS = 'access'¶
- BROWSE = 'browse'¶
- GET_CAPABILITIES = 'getcapabilities'¶
- GET_MAP = 'getmap'¶
- GET_FEATURE_INFO = 'getfeatureinfo'¶
- GET_LEGEND_GRAPHIC = 'getlegendgraphic'¶
- GET_METADATA = 'getmetadata'¶
- GET_PROPERTY_VALUE = 'getpropertyvalue'¶
- GET_FEATURE = 'getfeature'¶
- GET_FEATURE_WITH_LOCK = 'getfeaturewithlock'¶
- GET_GML_OBJECT = 'getgmlobject'¶
- DESCRIBE_FEATURE_TYPE = 'describefeaturetype'¶
- DESCRIBE_LAYER = 'describelayer'¶
- DESCRIBE_PROCESS = 'describeprocess'¶
- EXECUTE = 'execute'¶
- LOCK_FEATURE = 'lockfeature'¶
- TRANSACTION = 'transaction'¶
- CREATE_STORED_QUERY = 'createstoredquery'¶
- DROP_STORED_QUERY = 'dropstoredquery'¶
- LIST_STORED_QUERIES = 'liststoredqueries'¶
- DESCRIBE_STORED_QUERIES = 'describestoredqueries'¶
- class magpie.ui.management.views.PermissionSet(permission: magpie.typedefs.AnyPermissionType, access: Access | magpie.typedefs.Str | None = None, scope: Scope | magpie.typedefs.Str | None = None, typ: PermissionType | None = None, reason: magpie.typedefs.Str | None = None)[source]¶
Bases:
objectExplicit definition of a
Permissionwith applicableAccessandScopeto resolve it.The
Permissionis the name of the applicable permission on themagpie.models.Resource. TheScopedefines how thePermissionshould impact the resolution of the perceived Effective Permissions over amagpie.models.Resourcetree hierarchy. TheAccessdefines how thePermissionaccess should be interpreted (granted or denied).Optionally, a
PermissionTypecan be provided to specifically indicate which kind of permission this set represents. This type is only for informative purposes, and is not saved to database nor displayed by the explicit string representation. It is returned within JSON representation and can be employed by Effective Permissions resolution to be more verbose about returned results.On missing
AccessorScopespecifications, they default toAccess.ALLOWandScope.RECURSIVEto handle backward compatible naming convention of plainpermission_name.Initializes the permission definition, possibly using required conversion from other implementations.
- Parameters:
permission – Name of the permission, or any other implementation from which the name can be inferred.
access – Effective behaviour of the permissions. Generally, grant or deny the specified permission.
scope – Scope for which the permission affects hierarchical resources. Important for effective resolution.
typ – Type of permission being represented. Informative only, does not impact behavior if omitted.
reason – Slightly more indicative information on why the current permission-type has this value. Value should be either explicitly provided or will be inferred if converted from input PermissionTuple.
See also
- __slots__ = ['_name', '_access', '_scope', '_tuple', '_type', '_reason']¶
- __lt__(other: Any) bool[source]¶
Ascending sort of permission according to their name, access and scope modifiers.
First sort by permission name alphabetically, followed by increasing restrictive access and increasing range of scoped resources.
Using this sorting methodology, similar permissions by name are grouped together first, and permissions of same name with modifiers are then ordered, the first having less priority when selecting a single item to display with conflicting possibilities. Respecting
Access.DENYis more important thanAccess.ALLOW(to protect the Resource), andScope.MATCHis closer to the actual Resource thanScope.RECURSIVEpermission received from a farther parent in the hierarchy.Sorted explicit string representation becomes:
[name1]-[allow]-[match] [name1]-[allow]-[recursive] [name1]-[deny]-[match] [name1]-[deny]-[recursive] [name2]-[allow]-[match] [name2]-[allow]-[recursive] [name2]-[deny]-[match] [name2]-[deny]-[recursive] ...
- We then obtain two crucial ordering results:
We can easily pick the last sorted item with highest resolution priority to find the final result of corresponding permissions. (note: final result for same user or group, their direct/inherited resolution is not considered here).
Picking the first element with lowest priority also displays the permission that impacts the widest range of resources. For instance in Magpie UI, indicating that a permission as
Scope.RECURSIVEis more verbose as it tell other resources under it are also receive the specifiedAccessmodifier rather than only the punctual resource.
Warning
Alphabetically sorting permissions by string representation (implicit/explicit) is not equivalent to sorting them according to Permission priority according to how modifiers are resolved. To obtain the prioritized sorting as strings, a list of
PermissionSet(with the strings as input) should be used to convert and correctly interpreted the raw strings, and then be converted back after sorting.# valid priority-sorted strings [str(perm) for perm in sorted(PermissionSet(p) for p in permission_strings)] # not equivalent to raw sorting list(sorted(permission_strings))
- __str__() magpie.typedefs.Str[source]¶
Obtains the compound literal representation of the
PermissionSet.Employed for database storage supporting
zigguratformat.
- __repr__() magpie.typedefs.Str[source]¶
Obtains the visual representation of the
PermissionSet.
- like(other: Any) bool[source]¶
Evaluates if one permission is similar to another permission definition regardless of modifiers.
This is different than
==operator which will evaluate exactly equal permission definitions.
- json() magpie.typedefs.PermissionDict[source]¶
Obtains the JSON representation of this
PermissionSet.
- webhook_params() magpie.typedefs.JSON[source]¶
Obtain JSON representation employed for Webhook reference.
- ace(user_or_group: magpie.models.User | magpie.models.Group | None) magpie.typedefs.AccessControlEntryType[source]¶
Converts the
PermissionSetinto an ACE thatpyramidcan understand.
- property reason: magpie.typedefs.Str | None¶
Indicative reason of the returned value defined by
type()or inferred by thePermissionTuple.See also
combine()- Returns:
Single string that describes the reason (source) of the permission, or multiple strings if updated by combination of multiple permissions.
- classmethod resolve(permission1: magpie.typedefs.ResolvablePermissionType, permission2: magpie.typedefs.ResolvablePermissionType, context: PermissionType = PermissionType.INHERITED, multiple_choice: magpie.typedefs.Str | None = None) magpie.typedefs.ResolvablePermissionType[source]¶
Resolves provided permissions into a single one considering various modifiers and groups for a resource.
Permissions MUST have the same Permission name. The associated Resource on which the two compared permissions are applied on should also be the same This method SHOULD NOT be used by itself to obtain for Effective Permission since it does not handle multi-level Resource resolution. Resolution is accomplished in this case only for a given level in the tree hierarchy.
The comparison considers both the
AccessandScopeof every Inherited Permission of the User, as well as its Group memberships sorted by their priority.See also
magpie.api.management.user.user_utils.combine_user_group_permissions()
- Parameters:
permission1 – Permission to compare.
permission2 – Permission to compare.
context – Control the resolution context (local/effective) of the permissions (safeguard against invalid definitions).
multiple_choice – Alternate explanation to default
PERMISSION_REASON_MULTIPLEapplied if multiple Permission refer to distinct Group of equal priority and equivalent access definitions, meaning they are interchangeable without impacting resolution to access the same target Resource.
- Returns:
Permission with highest priority to resolve access a resource without considering scope.
- property group_priority: magpie.typedefs.GroupPriority | None¶
Priority accessor in case of group inherited permission resolved by
PermissionTuple.
- property perm_tuple: ziggurat_foundations.permissions.PermissionTuple | None¶
Get the original
PermissionTupleif available (PermissionSetmust have been created by one).
- property implicit_permission: magpie.typedefs.Str | None¶
Obtain the implicit string representation of the
PermissionSetas plainPermissionname.This representation is backward compatible with prior versions of Magpie where explicit representation of permission names in the database did not exist.
If the contained modifiers of the
PermissionSet(notably theAccess.DENY) result in a string representation that is not possible according to non existing permissions for older Magpie instances, the returned value will beNone.See also
explicit_permission()for the new representation.
- property explicit_permission: magpie.typedefs.Str¶
Obtain the explicit string representation of the
PermissionSet.This format is always guaranteed to be completely defined contrary to
implicit_permission().See also
__str__()(default string value).implicit_permission()for the old representation.
- property name: Permission¶
- permission¶
- property type: PermissionType | None¶
- classmethod _convert(permission: magpie.typedefs.AnyPermissionType) PermissionSet | None[source]¶
Converts any permission representation to the
PermissionSetwith applicable enum members.Supports older
Permissionrepresentation such that implicit conversion of permission name withoutaccessandscopevalues are padded with defaults. Also, pre-defined partial or full definition from literal string representation are parsed to generate thePermissionSetinstance.- Parameters:
permission – implicit or explicit permission name string, or any other known permission implementation
- Raises:
ValueError – when the permission name cannot be identified or parsed
- class magpie.ui.management.views.AdminRequests(request)[source]¶
Bases:
BaseViewsRegroups multiple administration-level operations to be dispatched to the API requests.
- create_user_default_template_data(data)[source]¶
Generates all the default values for the various fields employed for display purposes of the user creation form.
- Parameters:
data – any template data that should override the defaults.
- Returns:
updated template data with defaults and overridden values.
- get_admin_session() magpie.typedefs.CookiesType[source]¶
Temporarily login as default administrator to execute an elevated operation that the current user cannot make.
Warning
Cookies MUST NOT be preserved or memorized, to avoid user gaining restricted access. This is intended only for basic operations such as validating information. Care must be taken such information retrieved this way do not provide a way of non-administrator to indirectly infer some otherwise protected information. User sparingly.
- Returns:
Cookies of the administrator login.
- get_group_users(group_name: magpie.typedefs.Str, user_group_status: magpie.models.UserGroupStatus = UserGroupStatus.ACTIVE) List[magpie.typedefs.Str][source]¶
- update_group_info(group_name: magpie.typedefs.Str, group_info: magpie.typedefs.JSON) magpie.typedefs.JSON[source]¶
- get_user_groups(user_name: magpie.typedefs.Str, user_group_status: magpie.models.UserGroupStatus = UserGroupStatus.ACTIVE) List[magpie.typedefs.Str][source]¶
- get_user_statuses(status: magpie.typedefs.Str | int = 0) List[magpie.typedefs.Str][source]¶
Obtains all user names that have the corresponding status value.
- get_user_details(status: str | int | None = None, cookies: magpie.typedefs.CookiesType | None = None) List[magpie.typedefs.JSON][source]¶
Obtains all user details, optionally filtered to by corresponding status value.
Employ this method to avoid multiple requests fetching individual information.
- get_resource_types()[source]¶
- Returns:
dictionary of all resources as {id: ‘resource_type’}
- Return type:
dict
- static flatten_tree_resource(resource_node, resource_dict)[source]¶
- Parameters:
resource_node – any-level dictionary composing the resources tree
resource_dict – reference of flattened dictionary across levels
- Returns:
flattened dictionary resource_dict of all {id: ‘resource_type’}
- Return type:
dict
- create_user(data)[source]¶
Processes the user creation form with fields input data.
All the fields are pre- and post-validated according to expected behaviour by the API. Pre-validations attempt soft checks to detect as many potential errors such that they can all be simultaneously displayed on the form, to avoid back-and-forth erroneous submissions by the user. Post-validations are the hard checks imposed by the API, which include some of the pre-checks.
Whenever some failure occurs, returned data will contain
is_errorwithTrueorFalseaccordingly. Following successful request without error, the User will be created. It is up to the calling function to redirect the response and further process the returned data as needed.- Parameters:
data – initial templated data overrides according to who is initiation the user creation.
- Returns:
updated template data with any relevant error messages and statuses if applicable.
- Raises:
HTTPException – any unhandled or unknown HTTP error received from the API.
- class magpie.ui.management.views.BaseViews(request)[source]¶
Bases:
objectBase methods for Magpie UI pages.
- MAGPIE_FIXED_USERS_REFS = []¶
Special User that cannot have any relationship edited.
This includes both Group memberships and Permission references.
- MAGPIE_USER_PWD_LOCKED = []¶
Special User that could self-edit themselves, but is disabled since conflicting with other policies.
- MAGPIE_USER_PWD_DISABLED = []¶
Special User where password cannot be edited (managed by Magpie configuration settings).
- MAGPIE_ANONYMOUS_GROUP = None¶
Reference to
magpie.constants.MAGPIE_ANONYMOUS_GROUPfor convenience in UI pages.
- add_template_data(data: Dict[magpie.typedefs.Str, Any] | None = None) Dict[magpie.typedefs.Str, Any][source]¶
Adds required template data for the ‘heading’ mako template applied to every UI page.
- render(template: magpie.typedefs.Str, data: Dict[magpie.typedefs.Str, Any] | None = None) pyramid.response.Response[source]¶
Render the response with an explicit Mako template reference.
Views that are decorated by
pyramid.view.view_config()or registered bypyramid.config.Configurator.add_view()with arendererparameter do not require to call this function as it is auto-resolved with the submitteddata.
- magpie.ui.management.views.check_response(response: magpie.typedefs.AnyResponseType) magpie.typedefs.AnyResponseType[source]¶
- Returns:
response if the HTTP status code is successful.
- Raises:
HTTPError – (of appropriate type) if the response corresponds to an HTTP error code
- magpie.ui.management.views.handle_errors(func: Callable) Callable[source]¶
Decorator that encapsulates the operation in a try/except block, and redirects the response to the UI error page with API error contents.
In worst case scenario where the operation cannot figure out what to do with the exception response, raise the most basic
HTTPInternalServerErrorthat can be formulated from available details.See also
redirect_error()
- magpie.ui.management.views.request_api(request: pyramid.request.Request, path: magpie.typedefs.Str, method: magpie.typedefs.Str = 'GET', data: magpie.typedefs.JSON | magpie.typedefs.Str | None = None, headers: magpie.typedefs.HeadersType | None = None, cookies: magpie.typedefs.CookiesType | None = None) magpie.typedefs.AnyResponseType[source]¶
Use a pyramid sub-request to request Magpie API routes via the UI. This avoids max retries and closed connections when using 1 worker (eg: during tests).
Some information is retrieved from
requestto pass down to the sub-request (eg: cookies). If they are passed as argument, corresponding values will override the ones found inrequest.All sub-requests to the API are assumed to be
magpie.common.CONTENT_TYPE_JSONunless explicitly overridden withheaders. Headers are also looked for additionalSet-Cookieheader in case they need to be passed down tocookies.- Parameters:
request – incoming Magpie UI request that requires sub-request to Magpie API, to retrieve required details.
path – local Magpie API path (relative to root without URL).
method – HTTP method to send the API sub-request.
data – JSON dictionary or literal string content of the request body.
headers – override headers to employ for the API sub-request. Defaults to JSON Accept & Content-Type headers.
cookies – Override cookies to employ for the API sub-request. Defaults to current logged user. For empty cookies (no user), explicitly provide an empty dictionary.
- magpie.ui.management.views.get_json(request_or_response: magpie.typedefs.AnyRequestType | magpie.typedefs.AnyResponseType) magpie.typedefs.JSON[source]¶
Retrieves the ‘JSON’ body of a response using the property/callable according to the response’s implementation.
- magpie.ui.management.views.get_logger(name: magpie.typedefs.Str, level: int | None = None, force_stdout: bool = None, message_format: magpie.typedefs.Str | None = None, datetime_format: magpie.typedefs.Str | None = None) logging.Logger[source]¶
Immediately sets the logger level to avoid duplicate log outputs from the root logger and this logger when level is
logging.NOTSET.
- class magpie.ui.management.views.ManagementViews(request)[source]¶
Bases:
magpie.ui.utils.AdminRequests,magpie.ui.utils.BaseViewsRegroups multiple administration-level operations to be dispatched to the API requests.
- add_user()[source]¶
User creation by a logged administrator.
Note
The template employed for this form is reused for user self-registration as the fields and validation of inputs are essentially the same. Their actual processing is different though, as the administrator user is already logged in this case, and nobody is logged in the other.
- edit_user()[source]¶
Edit the fields of any referenced user profile by an administrator.
See also
magpie.ui.user.views.UserViews.edit_current_user()for corresponding operation by user self-update
- view_pending_user()[source]¶
Displays a pending user registration profile details.
Note
View configuration is added dynamically because this page it should be available only when the corresponding feature is activated with configuration settings.
- get_user_or_group_resources_permissions_dict(user_or_group_name, services, service_type, is_user=False, is_inherit_groups_permissions=False)[source]¶
Get the user or group applied permissions as well as applicable permissions for corresponding services.
- Result is a
tupleof: combined Allowed Permissions (names only) for services and their children Resources.
dictionary of key-service-name, each with recursive map value of children resource details including the Applied Permissions or Inherited Resources for the corresponding User or Group accordingly to specified arguments.
- Result is a
- sync_services(services: Dict[magpie.typedefs.Str, magpie.typedefs.JSON]) Tuple[List[magpie.typedefs.Str], magpie.typedefs.Str | None][source]¶
Syncs specified services.
- Returns:
names of services that produced a sync error and corresponding sync message (if any).