15.3.31. crate_anon.crateweb.core.utils¶
Copyright (C) 2015-2018 Rudolf Cardinal (rudolf@pobox.com).
This file is part of CRATE.
CRATE 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.
CRATE 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 CRATE. If not, see <http://www.gnu.org/licenses/>.
-
crate_anon.crateweb.core.utils.
is_superuser
(user: auth.User) → bool[source]¶ - Function for user with decorator, e.g.
- @user_passes_test(is_superuser)
Superuser equates to Research Database Manager.
-
crate_anon.crateweb.core.utils.
site_absolute_url
(path: str) → str[source]¶ Returns an absolute URL for the site, given a relative part. Use like:
url = site_absolute_url(static('red.png')) # ... determined in part by STATIC_URL. url = site_absolute_url(reverse('clinician_response', args=[self.id])) # ... determined by SCRIPT_NAME or FORCE_SCRIPT_NAME # ... which is context-dependent: see below
We need to generate links to our site outside the request environment, e.g. for inclusion in e-mails, even when we’re generating the e-mails offline via Celery. There’s no easy way to do this automatically (site path information comes in only via requests), so we put it in the settings.
See also:
- http://stackoverflow.com/questions/4150258/django-obtaining-the-absolute-url-without-access-to-a-request-object # noqa
- https://fragmentsofcode.wordpress.com/2009/02/24/django-fully-qualified-url/ # noqa
IMPORTANT
BEWARE: reverse() will produce something different inside a request and outside it.
So the only moderately clean way of doing this is to do this in the Celery backend jobs, for anything that uses Django URLs (e.g. reverse) – NOT necessary for anything using only static URLs (e.g. pictures in PDFs).
from django.conf import settings from django.urls import set_script_prefix set_script_prefix(settings.FORCE_SCRIPT_NAME)
But that does at least mean we can use the same method for static and Django URLs.