magpie.ui.user.views ==================== .. py:module:: magpie.ui.user.views Attributes ---------- .. autoapisummary:: magpie.ui.user.views.LOGGER Classes ------- .. autoapisummary:: magpie.ui.user.views.UserGroupStatus magpie.ui.user.views.BaseViews magpie.ui.user.views.UserViews Functions --------- .. autoapisummary:: magpie.ui.user.views.get_constant magpie.ui.user.views.check_response magpie.ui.user.views.handle_errors magpie.ui.user.views.request_api magpie.ui.user.views.get_json magpie.ui.user.views.get_logger Module Contents --------------- .. py:function:: get_constant(constant_name: magpie.typedefs.Str, settings_container: Optional[magpie.typedefs.AnySettingsContainer] = None, settings_name: Optional[magpie.typedefs.Str] = None, default_value: Optional[magpie.typedefs.SettingValue] = None, raise_not_set: bool = True, raise_missing: bool = True, print_missing: bool = False, empty_missing: bool = False) -> magpie.typedefs.SettingValue Search in order for matched value of :paramref:`constant_name`: 1. search in :py:data:`MAGPIE_CONSTANTS` 2. search in settings if specified 3. search alternative setting names (see below) 4. search in :mod:`magpie.constants` definitions 5. search in environment variables Parameter :paramref:`constant_name` is expected to have the format ``MAGPIE_[VARIABLE_NAME]`` although any value can be passed to retrieve generic settings from all above-mentioned search locations. If :paramref:`settings_name` is provided as alternative name, it is used as is to search for results if :paramref:`constant_name` was not found. Otherwise, ``magpie.[variable_name]`` is used for additional search when the format ``MAGPIE_[VARIABLE_NAME]`` was used for :paramref:`constant_name` (i.e.: ``MAGPIE_ADMIN_USER`` will also search for ``magpie.admin_user`` and so on for corresponding constants). :param constant_name: key to search for a value :param settings_container: WSGI application settings container (if not provided, uses found one in current thread) :param settings_name: alternative name for `settings` if specified :param default_value: default value to be returned if not found anywhere, and exception raises are disabled. :param raise_not_set: raise an exception if the found key is ``None``, search until last case if others are ``None`` :param raise_missing: raise exception if key is not found anywhere :param print_missing: print message if key is not found anywhere, return ``None`` :param empty_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/``None`` value) :raises LookupError: if no appropriate value could be found from all search locations (according to options) .. py:class:: UserGroupStatus Bases: :py:obj:`magpie.utils.FlexibleNameEnum` Supported statuses of user-group relationships. .. py:attribute:: ALL :value: 'all' .. py:attribute:: ACTIVE :value: 'active' .. py:attribute:: PENDING :value: 'pending' .. py:method:: allowed() -> List[magpie.typedefs.Str] :classmethod: Returns all supported representation values that can be mapped to a valid status. .. py:class:: BaseViews(request) Bases: :py:obj:`object` Base methods for Magpie UI pages. .. py:attribute:: MAGPIE_FIXED_GROUP_MEMBERSHIPS :value: [] Special :term:`Group` memberships that cannot be edited. .. py:attribute:: MAGPIE_FIXED_GROUP_EDITS :value: [] Special :term:`Group` details that cannot be edited. .. py:attribute:: MAGPIE_FIXED_USERS :value: [] Special :term:`User` details that cannot be edited. .. py:attribute:: MAGPIE_FIXED_USERS_REFS :value: [] Special :term:`User` that cannot have any relationship edited. This includes both :term:`Group` memberships and :term:`Permission` references. .. py:attribute:: MAGPIE_USER_PWD_LOCKED :value: [] Special :term:`User` that *could* self-edit themselves, but is disabled since conflicting with other policies. .. py:attribute:: MAGPIE_USER_PWD_DISABLED :value: [] Special :term:`User` where password cannot be edited (managed by `Magpie` configuration settings). .. py:attribute:: MAGPIE_ANONYMOUS_GROUP :value: None Reference to :py:data:`magpie.constants.MAGPIE_ANONYMOUS_GROUP` for convenience in UI pages. .. py:method:: add_template_data(data: Optional[Dict[magpie.typedefs.Str, Any]] = None) -> Dict[magpie.typedefs.Str, Any] Adds required template data for the 'heading' mako template applied to every UI page. .. py:method:: render(template: magpie.typedefs.Str, data: Optional[Dict[magpie.typedefs.Str, Any]] = None) -> pyramid.response.Response Render the response with an explicit Mako template reference. Views that are decorated by :func:`pyramid.view.view_config` or registered by :meth:`pyramid.config.Configurator.add_view` with a ``renderer`` parameter do not require to call this function as it is auto-resolved with the submitted :paramref:`data`. .. py:function:: check_response(response: magpie.typedefs.AnyResponseType) -> magpie.typedefs.AnyResponseType :returns: response if the HTTP status code is successful. :raises HTTPError: (of appropriate type) if the response corresponds to an HTTP error code .. py:function:: handle_errors(func: Callable) -> Callable 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 :class:`HTTPInternalServerError` that can be formulated from available details. .. seealso:: :func:`redirect_error` .. py:function:: request_api(request: pyramid.request.Request, path: magpie.typedefs.Str, method: magpie.typedefs.Str = 'GET', data: Optional[Union[magpie.typedefs.JSON, magpie.typedefs.Str]] = None, headers: Optional[magpie.typedefs.HeadersType] = None, cookies: Optional[magpie.typedefs.CookiesType] = None) -> magpie.typedefs.AnyResponseType 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 :paramref:`request` to pass down to the sub-request (eg: cookies). If they are passed as argument, corresponding values will override the ones found in :paramref:`request`. All sub-requests to the API are assumed to be :py:data:`magpie.common.CONTENT_TYPE_JSON` unless explicitly overridden with :paramref:`headers`. Headers are also looked for additional ``Set-Cookie`` header in case they need to be passed down to :paramref:`cookies`. :param request: incoming Magpie UI request that requires sub-request to Magpie API, to retrieve required details. :param path: local Magpie API path (relative to root without URL). :param method: HTTP method to send the API sub-request. :param data: JSON dictionary or literal string content of the request body. :param headers: override headers to employ for the API sub-request. Defaults to JSON Accept & Content-Type headers. :param 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. .. py:function:: get_json(request_or_response: Union[magpie.typedefs.AnyRequestType, magpie.typedefs.AnyResponseType]) -> magpie.typedefs.JSON Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation. .. py:function:: get_logger(name: magpie.typedefs.Str, level: Optional[int] = None, force_stdout: bool = None, message_format: Optional[magpie.typedefs.Str] = None, datetime_format: Optional[magpie.typedefs.Str] = None) -> logging.Logger Immediately sets the logger level to avoid duplicate log outputs from the `root logger` and `this logger` when `level` is ``logging.NOTSET``. .. py:data:: LOGGER .. py:class:: UserViews(request) Bases: :py:obj:`magpie.ui.utils.BaseViews` Base methods for Magpie UI pages. .. py:method:: add_template_data(data=None) Adds required template data for the 'heading' mako template applied to every UI page. .. py:method:: get_current_user_groups(user_group_status: magpie.models.UserGroupStatus = UserGroupStatus.ACTIVE) -> magpie.typedefs.List[str] .. py:method:: get_current_user_info() -> magpie.typedefs.JSON .. py:method:: get_discoverable_groups() -> magpie.typedefs.List[str] .. py:method:: join_discoverable_group(group_name) Registers the current user to the discoverable group. :raises HTTPBadRequest: if the operation is not valid. .. py:method:: leave_discoverable_group(group_name: magpie.typedefs.Str) -> None Unregisters the current user from the discoverable group. :raises HTTPBadRequest: if the operation is not valid. .. py:method:: edit_current_user() Edit the own fields of the current user profile (self-update information). .. seealso:: - :meth:`magpie.ui.management.views.ManagementViews.edit_user` for corresponding operation by administrator