19.1.32. camcops_server.cc_modules.cc_pyramid


Copyright (C) 2012-2018 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.


class camcops_server.cc_modules.cc_pyramid.CamcopsMakoLookupTemplateRenderer(lookup, spec, defname, package)[source]
  1. load the Mako template
  2. shove any other keys into its dictionary
class camcops_server.cc_modules.cc_pyramid.CamcopsPage(collection: Union[Sequence[Any], sqlalchemy.orm.query.Query, sqlalchemy.sql.selectable.Select], url_maker: Callable[[int], str], page: int = 1, items_per_page: int = 20, item_count: int = None, wrapper_class: Type[Any] = None, ellipsis: str = '&hellip;', **kwargs)[source]

Fixes bugs (e.g. mutable default arguments) and nasties (e.g. enforcing “..” for the ellipsis) in the original.

pager(format: str = '(Page $page of $page_count; total $item_count records) [ $link_first $link_previous ~3~ $link_next $link_last ]', url: str = None, show_if_single_page: bool = True, separator: str = ' ', symbol_first: str = '&lt;&lt;', symbol_last: str = '&gt;&gt;', symbol_previous: str = '&lt;', symbol_next: str = '&gt;', link_attr: Dict[str, str] = None, curpage_attr: Dict[str, str] = None, dotdot_attr: Dict[str, str] = None, link_tag: Callable[[Dict[str, str]], str] = None)[source]

Return string with links to other pages (e.g. ‘1 .. 5 6 7 [8] 9 10 11 .. 50’).

format:

Format string that defines how the pager is rendered. The string can contain the following $-tokens that are substituted by the string.Template module:

  • $first_page: number of first reachable page
  • $last_page: number of last reachable page
  • $page: number of currently selected page
  • $page_count: number of reachable pages
  • $items_per_page: maximal number of items per page
  • $first_item: index of first item on the current page
  • $last_item: index of last item on the current page
  • $item_count: total number of items
  • $link_first: link to first page (unless this is first page)
  • $link_last: link to last page (unless this is last page)
  • $link_previous: link to previous page (unless this is first page)
  • $link_next: link to next page (unless this is last page)

To render a range of pages the token ‘~3~’ can be used. The number sets the radius of pages around the current page. Example for a range with radius 3:

‘1 .. 5 6 7 [8] 9 10 11 .. 50’

Default: ‘~2~’

url
The URL that page links will point to. Make sure it contains the string $page which will be replaced by the actual page number. Must be given unless a url_maker is specified to __init__, in which case this parameter is ignored.
symbol_first

String to be displayed as the text for the $link_first link above.

Default: ‘&lt;&lt;’ (<<)

symbol_last

String to be displayed as the text for the $link_last link above.

Default: ‘&gt;&gt;’ (>>)

symbol_previous

String to be displayed as the text for the $link_previous link above.

Default: ‘&lt;’ (<)

symbol_next

String to be displayed as the text for the $link_next link above.

Default: ‘&gt;’ (>)

separator:

String that is used to separate page links/numbers in the above range of pages.

Default: ‘ ‘

show_if_single_page:

if True the navigator will be shown even if there is only one page.

Default: False

link_attr (optional)

A dictionary of attributes that get added to A-HREF links pointing to other pages. Can be used to define a CSS style or class to customize the look of links.

Example: { ‘style’:’border: 1px solid green’ } Example: { ‘class’:’pager_link’ }

curpage_attr (optional)

A dictionary of attributes that get added to the current page number in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes.

Example: { ‘style’:’border: 3px solid blue’ } Example: { ‘class’:’pager_curpage’ }

dotdot_attr (optional)

A dictionary of attributes that get added to the ‘..’ string in the pager (which is obviously not a link). If this dictionary is not empty then the elements will be wrapped in a SPAN tag with the given attributes.

Example: { ‘style’:’color: #808080’ } Example: { ‘class’:’pager_dotdot’ }

link_tag (optional)
A callable that accepts single argument page (page link information) and generates string with html that represents the link for specific page. Page objects are supplied from link_map() so the keys are the same.
exception camcops_server.cc_modules.cc_pyramid.HTTPFoundDebugVersion(location: str = '', **kwargs)[source]

For debugging redirections.

class camcops_server.cc_modules.cc_pyramid.PageUrl(request: pyramid.request.Request, qualified: bool = False)[source]

A page URL generator for WebOb-compatible Request objects.

I derive new URLs based on the current URL but overriding the ‘page’ query parameter.

I’m suitable for Pyramid, Pylons, and TurboGears, as well as any other framework whose Request object has ‘application_url’, ‘path’, and ‘GET’ attributes that behave the same way as webob.Request’s.

class camcops_server.cc_modules.cc_pyramid.RouteCollection[source]

All routes, with their paths, for CamCOPS. They will be auto-read by all_routes().

To make a URL on the fly, use Request.route_url() or CamcopsRequest.route_url_params().

To associate a view with a route, use the Pyramid @view_config decorator.

classmethod all_routes() → List[camcops_server.cc_modules.cc_pyramid.RoutePath][source]

Fetch all routes.

class camcops_server.cc_modules.cc_pyramid.RoutePath(route: str, path: str, ignore_in_all_routes: bool = False)[source]

Class to hold a route/path pair.

  • Pyramid route names are just strings used internally for convenience.
  • Pyramid URL paths are URL fragments, like ‘/thing’, and can contain placeholders, like ‘/thing/{bork_id}’, which will result in the request.matchdict object containing a ‘bork_id’ key. Those can be further constrained by regular expressions, like ‘/thing/{bork_id:d+}’ to restrict to digits.
class camcops_server.cc_modules.cc_pyramid.Routes[source]

Names of Pyramid routes.

  • Used by the @view_config(route_name=…) decorator.
  • Configured via RouteCollection / RoutePath to the Pyramid route configurator.
class camcops_server.cc_modules.cc_pyramid.SqlalchemyOrmPage(query: sqlalchemy.orm.query.Query, url_maker: Callable[[int], str], page: int = 1, items_per_page: int = 25, item_count: int = None, **kwargs)[source]

A pagination page that deals with SQLAlchemy ORM objects.

class camcops_server.cc_modules.cc_pyramid.SqlalchemyOrmQueryWrapper(query: sqlalchemy.orm.query.Query)[source]

Wrapper class to access elements of an SQLAlchemy ORM query.

See: - https://docs.pylonsproject.org/projects/pylons-webframework/en/latest/helpers.html # noqa - https://docs.pylonsproject.org/projects/webhelpers/en/latest/modules/paginate.html # noqa - https://github.com/Pylons/paginate

class camcops_server.cc_modules.cc_pyramid.UrlParamType[source]

An enumeration.

class camcops_server.cc_modules.cc_pyramid.ViewArg[source]

String used as view arguments, e.g.

class camcops_server.cc_modules.cc_pyramid.ViewParam[source]

Used in the following situations:

  • as parameter names for parameterized URLs, via RoutePath to Pyramid’s route configuration, then fetched from the matchdict.
  • as form parameter names (often with some duplication as the attribute names of deform Form objects, because to avoid duplication would involve metaclass mess);
camcops_server.cc_modules.cc_pyramid.camcops_add_mako_renderer(config: pyramid.config.Configurator, extension)[source]

Replacement for add_mako_renderer from pyramid_mako, so we can use our own lookup.

camcops_server.cc_modules.cc_pyramid.get_session_factory() → <function SignedCookieSessionFactory at 0x7ff524848a60>[source]

We have to give a Pyramid request a way of making an HTTP session. We must return a session factory.

  • An example is an instance of SignedCookieSessionFactory().

  • A session factory has the signature [1]:

    sessionfactory(req: CamcopsRequest) -> session_object
    

    … where session “is a namespace” [2] … but more concretely, “implements the pyramid.interfaces.ISession interface”

  • We want to be able to make the session by reading the CamcopsConfig from the request.

[1] https://docs.pylonsproject.org/projects/pyramid/en/latest/glossary.html#term-session-factory

[2] https://docs.pylonsproject.org/projects/pyramid/en/latest/glossary.html#term-session

camcops_server.cc_modules.cc_pyramid.make_page_url(path: str, params: Dict[str, str], page: int, partial: bool = False, sort: bool = True) → str[source]

A helper function for URL generators.

I assemble a URL from its parts. I assume that a link to a certain page is done by overriding the ‘page’ query parameter.

path is the current URL path, with or without a “scheme://host” prefix.

params is the current query parameters as a dict or dict-like object.

page is the target page number.

If partial is true, set query param ‘partial=1’. This is to for AJAX calls requesting a partial page.

If sort is true (default), the parameters will be sorted. Otherwise they’ll be in whatever order the dict iterates them.