2017-07-04 08:34:26 +02:00
|
|
|
# w.c.s. - web application for online forms
|
|
|
|
# Copyright (C) 2005-2017 Entr'ouvert
|
|
|
|
#
|
|
|
|
# This program 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 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import types
|
2021-02-28 16:19:33 +01:00
|
|
|
import urllib.parse
|
2019-02-28 15:17:35 +01:00
|
|
|
|
2021-01-07 23:18:47 +01:00
|
|
|
import django.template.base
|
|
|
|
import django.template.defaulttags
|
2017-07-04 08:34:26 +02:00
|
|
|
import quixote
|
|
|
|
import quixote.publish
|
|
|
|
|
|
|
|
_thread_local = threading.local()
|
|
|
|
|
|
|
|
cleanup_orig = quixote.publish.cleanup
|
|
|
|
PublisherOrig = quixote.publish.Publisher
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
class Publisher(quixote.publish.Publisher):
|
|
|
|
def __init__(self, root_directory, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
PublisherOrig.__init__(self, root_directory, *args, **kwargs)
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
2014-01-05 12:15:19 +01:00
|
|
|
self.set_in_thread()
|
2017-07-04 08:34:26 +02:00
|
|
|
self.root_directory = root_directory
|
|
|
|
self._request = None
|
|
|
|
|
2014-01-05 12:15:19 +01:00
|
|
|
def set_in_thread(self):
|
|
|
|
_thread_local.publisher = self
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_publisher():
|
|
|
|
return getattr(_thread_local, 'publisher', None)
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_request():
|
|
|
|
return _thread_local.publisher.get_request()
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_response():
|
2018-05-18 10:34:18 +02:00
|
|
|
return get_request() and _thread_local.publisher.get_request().response
|
2017-07-04 08:34:26 +02:00
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_field(name, default=None):
|
|
|
|
return _thread_local.publisher.get_request().get_field(name, default)
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_cookie(name, default=None):
|
|
|
|
return _thread_local.publisher.get_request().get_cookie(name, default)
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_path(n=0):
|
|
|
|
return _thread_local.publisher.get_request().get_path(n)
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def redirect(location, permanent=False):
|
|
|
|
"""(location : string, permanent : boolean = false) -> string
|
|
|
|
|
|
|
|
Create a redirection response. If the location is relative, then it
|
|
|
|
will automatically be made absolute. The return value is an HTML
|
|
|
|
document indicating the new URL (useful if the client browser does
|
|
|
|
not honor the redirect).
|
|
|
|
"""
|
|
|
|
request = _thread_local.publisher.get_request()
|
2021-02-28 16:19:33 +01:00
|
|
|
location = urllib.parse.urljoin(request.get_url(), str(location))
|
2017-07-04 08:34:26 +02:00
|
|
|
return request.response.redirect(location, permanent)
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_session():
|
2020-02-19 13:44:04 +01:00
|
|
|
request = _thread_local.publisher.get_request()
|
|
|
|
return request.session if request else None
|
2017-07-04 08:34:26 +02:00
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_session_manager():
|
|
|
|
return _thread_local.publisher.session_manager
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def get_user():
|
|
|
|
session = _thread_local.publisher.get_request().session
|
|
|
|
if session is None:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return session.user
|
|
|
|
|
2020-01-18 20:33:44 +01:00
|
|
|
|
2017-07-04 08:34:26 +02:00
|
|
|
def cleanup():
|
|
|
|
cleanup_orig()
|
|
|
|
_thread_local.publisher = None
|
|
|
|
|
|
|
|
|
2019-09-29 20:52:08 +02:00
|
|
|
for key, value in list(locals().items()):
|
2021-02-28 16:19:33 +01:00
|
|
|
if type(value) in (types.FunctionType, type):
|
2017-07-04 08:34:26 +02:00
|
|
|
setattr(quixote, key, value)
|
|
|
|
setattr(quixote.publish, key, value)
|
2021-01-07 23:18:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
if not hasattr(django.template.base, '_monkeypatched'):
|
|
|
|
# patch render_value_in_context function to add a complex data mark when
|
|
|
|
# "printing" variables.
|
|
|
|
django.template.base._monkeypatched = True
|
|
|
|
orig_render_value_in_context = django.template.base.render_value_in_context
|
|
|
|
|
|
|
|
def new_render_value_in_context(value, context):
|
2021-01-17 11:56:16 +01:00
|
|
|
rendered_value = orig_render_value_in_context(value, context)
|
2021-01-15 15:44:15 +01:00
|
|
|
if context.get('allow_complex') and not isinstance(value, str):
|
2021-01-17 11:56:16 +01:00
|
|
|
return get_publisher().cache_complex_data(value, rendered_value)
|
|
|
|
return rendered_value
|
2021-01-07 23:18:47 +01:00
|
|
|
|
|
|
|
django.template.base.render_value_in_context = new_render_value_in_context
|
|
|
|
django.template.defaulttags.render_value_in_context = new_render_value_in_context
|