magpie.ui.login.views

Classes

AdminRequests

Regroups multiple administration-level operations to be dispatched to the API requests.

BaseViews

Base methods for Magpie UI pages.

LoginViews

Handles UI operations related to login to, logout from, or registration of user accounts.

Functions

check_response(→ magpie.typedefs.AnyResponseType)

returns:

response if the HTTP status code is successful.

handle_errors(→ Callable)

Decorator that encapsulates the operation in a try/except block, and redirects the response to the UI error page

request_api(→ magpie.typedefs.AnyResponseType)

Use a pyramid sub-request to request Magpie API routes via the UI. This avoids max retries and closed connections

get_json(→ magpie.typedefs.JSON)

Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation.

Module Contents

class magpie.ui.login.views.AdminRequests(request)[source]

Bases: BaseViews

Regroups 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_all_groups(first_default_group=None)[source]
get_group_info(group_name: magpie.typedefs.Str) magpie.typedefs.JSON[source]
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]
delete_group(group_name: magpie.typedefs.Str) 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_names()[source]

Obtains all user names.

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_emails()[source]
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

get_services(cur_svc_type)[source]
get_service_data(service_name)[source]
get_service_types()[source]
update_service_name(old_service_name, new_service_name, service_push)[source]
update_service_url(service_name, new_service_url, service_push)[source]
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_error with True or False accordingly. 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.login.views.BaseViews(request)[source]

Bases: object

Base methods for Magpie UI pages.

MAGPIE_FIXED_GROUP_MEMBERSHIPS = []

Special Group memberships that cannot be edited.

MAGPIE_FIXED_GROUP_EDITS = []

Special Group details that cannot be edited.

MAGPIE_FIXED_USERS = []

Special User details that cannot be edited.

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_GROUP for 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 by pyramid.config.Configurator.add_view() with a renderer parameter do not require to call this function as it is auto-resolved with the submitted data.

magpie.ui.login.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.login.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 HTTPInternalServerError that can be formulated from available details.

See also

redirect_error()

magpie.ui.login.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 request to pass down to the sub-request (eg: cookies). If they are passed as argument, corresponding values will override the ones found in request.

All sub-requests to the API are assumed to be magpie.common.CONTENT_TYPE_JSON unless explicitly overridden with headers. Headers are also looked for additional Set-Cookie header in case they need to be passed down to cookies.

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.login.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.

class magpie.ui.login.views.LoginViews(request)[source]

Bases: magpie.ui.utils.AdminRequests, magpie.ui.utils.BaseViews

Handles UI operations related to login to, logout from, or registration of user accounts.

Warning

Admin requests are applicable only when using the temporary login. The temporary session is handled by dispatching operations to AdminRequest. Only those methods should work with elevated session to ensure that returning those views in this class are back to unauthenticated level access.

request_providers_json()[source]
login()[source]
logout()[source]
get_group_info(group_name)[source]
register_user()[source]

User self-registration form results.

Note

The template employed for this form is reused for user creation by an administrator as the fields and validation of inputs are essentially the same. Their actual processing is different though, as in this case, the user attempting registration is not yet logged nor has any administrative access.