changeset c4157134135a in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=c4157134135a
description:
Add __href__ and is_secure, host and http_host to url module
This ease the composition of URL pointing to Tryton route.
issue8767
review264461005
diffstat:
CHANGELOG | 2 +
trytond/res/email_reset_password.html | 4 +-
trytond/res/user.py | 5 +-
trytond/tests/test_mixins.py | 28 ++++++++++++++++-
trytond/url.py | 57 +++++++++++++++++++++++++++++++---
5 files changed, 86 insertions(+), 10 deletions(-)
diffs (181 lines):
diff -r 25b9d87265b5 -r c4157134135a CHANGELOG
--- a/CHANGELOG Mon Nov 25 21:55:22 2019 +0100
+++ b/CHANGELOG Tue Nov 26 22:16:00 2019 +0100
@@ -1,3 +1,5 @@
+* Add is_secure, host and http_host to url module
+* Add __href__ to URLMixin
* Add validate attribute to wizard's Button
* Return 400 instead of 500 for Tryton exception
* Fill the reverse field in the One2Many setter
diff -r 25b9d87265b5 -r c4157134135a trytond/res/email_reset_password.html
--- a/trytond/res/email_reset_password.html Mon Nov 25 21:55:22 2019 +0100
+++ b/trytond/res/email_reset_password.html Tue Nov 26 22:16:00 2019 +0100
@@ -10,8 +10,8 @@
<p>The password for your account,
<strong>${record.login}</strong>, has been reset.<br/>
You must set a new one from the user's preferences.<br/>
You can connect with this temporary password
<strong>${record.password_reset}</strong> to</p>
- <a
href="tryton://${hostname}/${database}">tryton://${hostname}/${database}</a><br/>
- <a
href="https://${hostname}/#${database}">https://${hostname}/#${database}</a><br/>
+ <a
href="tryton://${host}/${database}">tryton://${host}/${database}</a><br/>
+ <a
href="${http_host}/#${database}">${http_host}/#${database}</a><br/>
</div>
<hr style="margin-top: 20px; border-style: solid none none;
border-color: #EEE"></hr>
<div style="font-size: 80%; color: #777">
diff -r 25b9d87265b5 -r c4157134135a trytond/res/user.py
--- a/trytond/res/user.py Mon Nov 25 21:55:22 2019 +0100
+++ b/trytond/res/user.py Tue Nov 26 22:16:00 2019 +0100
@@ -51,7 +51,7 @@
from ..exceptions import LoginException, RateLimitException
from trytond.report import Report, get_email
from trytond.sendmail import sendmail_transactional
-from trytond.url import HOSTNAME
+from trytond.url import host, http_host
__all__ = [
'User', 'LoginAttempt', 'UserAction', 'UserGroup', 'Warning_',
@@ -973,7 +973,8 @@
Lang = pool.get('ir.lang')
context = super(EmailResetPassword, cls).get_context(records, data)
lang = Lang.get()
- context['hostname'] = HOSTNAME
+ context['host'] = host()
+ context['http_host'] = http_host()
context['database'] = Transaction().database.name
context['expire'] = lang.strftime(
records[0].password_reset_expire,
diff -r 25b9d87265b5 -r c4157134135a trytond/tests/test_mixins.py
--- a/trytond/tests/test_mixins.py Mon Nov 25 21:55:22 2019 +0100
+++ b/trytond/tests/test_mixins.py Tue Nov 26 22:16:00 2019 +0100
@@ -9,7 +9,7 @@
from trytond.transaction import Transaction
from trytond.model import ModelView
from trytond.pool import Pool
-from trytond.url import HOSTNAME
+from trytond.url import http_host, HOSTNAME
from .mixin import TestMixin, TestSecondMixin, NotMixin
@@ -37,6 +37,21 @@
HOSTNAME, urllib.parse.quote(db_name)))
@with_transaction()
+ def testModelHref(self):
+ "Test model href"
+ pool = Pool()
+ UrlObject = pool.get('test.urlobject')
+ db_name = Transaction().database.name
+
+ self.assertEqual(UrlObject.__href__,
+ '%s/#%s/model/test.urlobject' % (
+ http_host(), urllib.parse.quote(db_name)))
+
+ self.assertEqual(UrlObject(1).__href__,
+ '%s/#%s/model/test.urlobject/1' % (
+ http_host(), urllib.parse.quote(db_name)))
+
+ @with_transaction()
def testWizardURL(self):
"Test wizard URLs"
pool = Pool()
@@ -47,6 +62,17 @@
'tryton://%s/%s/wizard/test.test_wizard' % (
HOSTNAME, urllib.parse.quote(db_name)))
+ @with_transaction()
+ def testWizardHref(self):
+ "Test wizard href"
+ pool = Pool()
+ UrlWizard = pool.get('test.test_wizard', type='wizard')
+ db_name = Transaction().database.name
+
+ self.assertEqual(UrlWizard.__href__,
+ '%s/#%s/wizard/test.test_wizard' % (
+ http_host(), urllib.parse.quote(db_name)))
+
class MixinTestCase(unittest.TestCase):
"Test Mixin"
diff -r 25b9d87265b5 -r c4157134135a trytond/url.py
--- a/trytond/url.py Mon Nov 25 21:55:22 2019 +0100
+++ b/trytond/url.py Tue Nov 26 22:16:00 2019 +0100
@@ -8,7 +8,7 @@
from trytond.config import config
from trytond.transaction import Transaction
-__all__ = ['URLMixin', 'HOSTNAME']
+__all__ = ['URLMixin', 'is_secure', 'host', 'http_host']
HOSTNAME = (config.get('web', 'hostname')
or socket.getfqdn())
@@ -17,7 +17,46 @@
class URLAccessor(object):
- __slots__ = ()
+ __slots__ = ('_protocol',)
+
+ def __init__(self, protocol='tryton'):
+ self._protocol = protocol
+
+ @classmethod
+ def is_secure(cls):
+ context = Transaction().context
+ if context:
+ request = context.get('_request')
+ if request:
+ return request['is_secure']
+ return bool(config.get('ssl', 'certificate'))
+
+ @classmethod
+ def host(cls):
+ context = Transaction().context
+ if context:
+ request = context.get('_request')
+ if request:
+ return request['http_host']
+ return HOSTNAME
+
+ @classmethod
+ def http_host(cls):
+ return urllib.parse.urlunsplit((
+ 'http' + ('s' if cls.is_secure() else ''),
+ cls.host(), '', '', ''))
+
+ @property
+ def protocol(self):
+ if self._protocol == 'http':
+ return 'http' + ('s' if self.is_secure() else '')
+ return self._protocol
+
+ @property
+ def separator(self):
+ if self._protocol == 'http':
+ return '#'
+ return ''
def __get__(self, inst, cls):
from trytond.model import Model
@@ -37,12 +76,20 @@
url_part['name'] = cls.__name__
url_part['database'] = Transaction().database.name
- local_part = urllib.parse.quote('%(database)s/%(type)s/%(name)s' %
url_part)
+ local_part = urllib.parse.quote(
+ '%(database)s/%(type)s/%(name)s' % url_part)
if isinstance(inst, Model) and inst.id:
local_part += '/%d' % inst.id
- return 'tryton://%s/%s' % (HOSTNAME, local_part)
+ return '%s://%s/%s%s' % (
+ self.protocol, self.host(), self.separator, local_part)
-class URLMixin(object):
+is_secure = URLAccessor.is_secure
+host = URLAccessor.host
+http_host = URLAccessor.http_host
+
+
+class URLMixin:
__slots__ = ()
__url__ = URLAccessor()
+ __href__ = URLAccessor('http')