pyiem.webutil module#

Utility functions for iemwebfarm applications.

class pyiem.webutil.TELEMETRY(timing, status_code, client_addr, app, request_uri, vhost)#

Bases: tuple

app#

Alias for field number 3

client_addr#

Alias for field number 2

request_uri#

Alias for field number 4

status_code#

Alias for field number 1

timing#

Alias for field number 0

vhost#

Alias for field number 5

class pyiem.webutil.CGIModel[source]#

Bases: BaseModel

A Pydantic model that parses CGI arguments.

model_config = {'arbitrary_types_allowed': True}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

classmethod xss_protect(v)[source]#

Protect against XSS attacks.

pyiem.webutil.model_to_rst(model)[source]#

Convert a Pydantic model to a reStructuredText table.

Parameters:

model (BaseModel) – The Pydantic model to convert

Return type:

str

Returns: A reStructuredText table

pyiem.webutil.write_telemetry(data)[source]#

Write telemetry to the database.

Parameters:

data (TELEMETRY)

Return type:

bool

pyiem.webutil.ensure_list(environ, key, parse_commas=True)[source]#

Ensure that we get something that is at least an empty list.

Parameters:
  • environ – the WSGI environ

  • key – the key to look for

  • parse_commas – split each found value based on commas, default True.

Return type:

list

pyiem.webutil.clean_form(form)[source]#

Opinionated cleaning of form data.

pyiem.webutil.log_request(environ, multiplier=1)[source]#

Log the request to database for future processing.

Parameters:
pyiem.webutil.compute_ts_from_string(form, key)[source]#

Convert a string to a timestamp.

pyiem.webutil.compute_ts(form, suffix)[source]#

Figure out the timestamp.

pyiem.webutil.add_to_environ(environ, form, **kwargs)[source]#

Build out some things auto-parsed from the request.

pyiem.webutil.iemapp(**kwargs)[source]#

Attempt to do all kinds of nice things for the user and the developer.

kwargs:
  • default_tz: The default timezone to use for timestamps, the default is America/Chicago.

  • enable_telemetry: Enable telemetry logging, default True.

  • help: Default help text, default Help not available.

  • parse_times: Parse the form for timestamps, default True.

  • schema (BaseModel): A Pydantic model to parse the form with.

  • memcachekey (str or callable): A memcache key to use for caching the response. If the callable returns None, no caching is done.

  • memcacheexpire (int or callable): The number of seconds to cache the response, defaults to 3600.

  • content_type (str or callable): The content type to use for the response.

  • allowed_as_list (list): CGI parameters that are permitted to be lists.

What all this does:
  1. Attempts to catch database connection errors and handle nicely

  2. Updates environ with some auto-parsed values + form content.

  3. If the wrapped function returns a str or bytes, it will be encoded and made into a list for the WSGI response.

Notes

  • raise NoDataFound to have a nice error message generated