19.1.35. camcops_server.cc_modules.cc_request


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_request.CamcopsDummyRequest(*args, **kwargs)[source]

Request class that allows manual manipulation of GET/POST parameters for debugging.

Notes:

  • The important base class is webob.request.BaseRequest.
  • self.params is a NestedMultiDict (see webob/multidict.py); these are intrinsically read-only.
  • self.params is also a read-only property. When read, it combines self.GET and self.POST.
  • What we do here is to manipulate the underlying GET/POST data.
class camcops_server.cc_modules.cc_request.CamcopsRequest(*args, **kwargs)[source]
config[source]

Return an instance of CamcopsConfig for the request. Access it as request.config, with no brackets.

config_filename[source]

Gets the config filename in use.

dbsession[source]

Return an SQLAlchemy session for the relevant request. The use of @reify makes this elegant. If and only if a view wants a database, it can say

dbsession = request.dbsession

and if it requests that, the cleanup callbacks get installed.

extrastring_families(sort: bool = True) → List[str][source]

Which sets of extra strings do we have?

get_all_extra_strings() → List[Tuple[str, str, str]][source]

Returns all extra strings, as a list of (task, name, value) tuples.

get_html_from_pyplot_figure(fig: matplotlib.figure.Figure) → str[source]

Make HTML (as PNG or SVG) from pyplot figure.

get_id_desc(which_idnum: int, default: str = None) → Union[str, NoneType][source]

Get server’s ID description.

get_id_shortdesc(which_idnum: int, default: str = None) → Union[str, NoneType][source]

Get server’s short ID description.

now[source]

Returns the time of the request as an Pendulum object. (Reified, so a request only ever has one time.) Exposed as a property.

now_utc[source]

Returns the time of the request as a UTC Pendulum.

remote_port[source]

The remote_port variable is an optional WSGI extra provided by some frameworks, such as mod_wsgi.

The WSGI spec: - https://www.python.org/dev/peps/pep-0333/

The CGI spec: - https://en.wikipedia.org/wiki/Common_Gateway_Interface

The Pyramid Request object: - https://docs.pylonsproject.org/projects/pyramid/en/latest/api/request.html#pyramid.request.Request # noqa - … note: that includes remote_addr, but not remote_port.

route_url_params(route_name: str, paramdict: Dict[str, Any]) → str[source]

Provides a simplified interface to Request.route_url when you have parameters to pass.

It does two things:

  1. convert all params to their str() form;
  2. allow you to pass parameters more easily using a string parameter name.

The normal Pyramid Request use is:

Request.route_url(route_name, param1=value1, param2=value2)

where “param1” is the literal name of the parameter, but here we can do

CamcopsRequest.route_url_params(route_name, {
    PARAM1_NAME: value1_not_necessarily_str,
    PARAM2_NAME: value2
})
switch_output_to_png() → None[source]

Switch server to producing figures in PNG.

switch_output_to_svg() → None[source]

Switch server to producing figures in SVG.

tabletsession[source]

Request a TabletSession, which is an information structure geared to client (tablet) database accesses. If we’re using this interface, we also want to ensure we’re using the CamcopsSession for the information provided by the tablet in the POST request, not anything already loaded/reset via cookies.

task_extrastrings_exist(taskname: str) → bool[source]

Has the server been supplied with extra strings for a specific task?

wappstring(stringname: str, default: str = None, provide_default_if_none: bool = True) → Union[str, NoneType][source]

Returns a web-safe version of an appstring (an app-wide extra string.

wxstring(taskname: str, stringname: str, default: str = None, provide_default_if_none: bool = True) → Union[str, NoneType][source]

Returns a web-safe version of an xstring (see above).

xstring(taskname: str, stringname: str, default: str = None, provide_default_if_none: bool = True) → Union[str, NoneType][source]

Looks up a string from one of the optional extra XML string files.

camcops_server.cc_modules.cc_request.command_line_request_context() → Generator[[camcops_server.cc_modules.cc_request.CamcopsRequest, NoneType], NoneType][source]

Request objects are ubiquitous, and allow code to refer to the HTTP request, config, HTTP session, database session, and so on. Here we make a special sort of request for use from the command line, and provide it as a context manager that will COMMIT the database afterwards (because the normal method, via the Pyramid router, is unavailable).

camcops_server.cc_modules.cc_request.complete_request_add_cookies(req: camcops_server.cc_modules.cc_request.CamcopsRequest, response: pyramid.response.Response)[source]

Finializes the response by adding session cookies. We do this late so that we can hot-swap the session if we’re using the database/tablet API rather than a human web browser.

Response callbacks are called in the order first-to-most-recently-added. See pyramid.request.CallbackMethodsMixin.

That looks like we can add a callback in the process of running a callback. And when we add a cookie to a Pyramid session, that sets a callback. Let’s give it a go…

camcops_server.cc_modules.cc_request.get_command_line_request() → camcops_server.cc_modules.cc_request.CamcopsRequest[source]

Creates a dummy CamcopsRequest for use on the command line. Presupposes that os.environ[ENVVAR_CONFIG_FILE] has been set, as it is in camcops.main().

camcops_server.cc_modules.cc_request.get_unittest_request(dbsession: sqlalchemy.orm.session.Session, params: Dict[str, Any] = None) → camcops_server.cc_modules.cc_request.CamcopsDummyRequest[source]

Creates a dummy CamcopsRequest for use by unit tests. Points to an existing database (e.g. SQLite in-memory database). Presupposes that os.environ[ENVVAR_CONFIG_FILE] has been set, as it is in camcops.main().