magpie.ui.utils

Module Contents

Classes

BaseViews

Base methods for Magpie UI pages.

Functions

check_response(response: AnyResponseType) → AnyResponseType

returns

response if the HTTP status code is successful.

request_api(request: Request, path: Str, method: Str = ‘GET’, data: Optional[Union[JSON, Str]] = None, headers: Optional[HeadersType] = None, cookies: Optional[CookiesType] = None) → AnyResponseType

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

redirect_error(request: Request, code: int = None, content: Optional[JSON] = None) → AnyResponseType

Redirects the contents to be rendered by the UI ‘error’ page.

handle_errors(func)

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

magpie.ui.utils.LOGGER[source]
magpie.ui.utils.check_response(response: AnyResponseType) → 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.utils.request_api(request: Request, path: Str, method: Str = 'GET', data: Optional[Union[JSON, Str]] = None, headers: Optional[HeadersType] = None, cookies: Optional[CookiesType] = None) → 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 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.

magpie.ui.utils.redirect_error(request: Request, code: int = None, content: Optional[JSON] = None) → AnyResponseType[source]

Redirects the contents to be rendered by the UI ‘error’ page.

Parameters
  • request – incoming request that resulted into some kind of error.

  • code – explicit HTTP status code for the error response, extracted from contents if otherwise available.

  • content – any body content provided as error response from the API.

magpie.ui.utils.handle_errors(func)[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()

class magpie.ui.utils.BaseViews(request)[source]

Bases: object

Base methods for Magpie UI pages.

Initialize self. See help(type(self)) for accurate signature.

MAGPIE_FIXED_GROUP_MEMBERSHIPS = [][source]
MAGPIE_FIXED_GROUP_EDITS = [][source]
add_template_data(self: Optional[Dict[Str, Any]], data=None) → Dict[Str, Any][source]

Adds required template data for the ‘heading’ mako template applied to every UI page.