|
1 # |
|
2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 |
|
18 # import interfaces |
|
19 from pyams_content.interfaces.container import IOrderedContainer |
|
20 from pyams_sequence.interfaces import IInternalReference |
|
21 |
|
22 # import packages |
|
23 from pyams_sequence.schema import InternalReferenceField |
|
24 from zope.container.constraints import contains, containers |
|
25 from zope.interface import Interface, Attribute, invariant, Invalid |
|
26 from zope.schema import Bool, TextLine |
|
27 |
|
28 from pyams_content import _ |
|
29 |
|
30 |
|
31 REDIRECT_MANAGER_KEY = 'pyams_content.redirect' |
|
32 |
|
33 |
|
34 class IRedirectionRule(IInternalReference): |
|
35 """Redirection rule interface""" |
|
36 |
|
37 containers('.IRedirectManager') |
|
38 |
|
39 active = Bool(title=_("Active rule?"), |
|
40 description=_("If 'no', selected rule is inactive"), |
|
41 required=True, |
|
42 default=False) |
|
43 |
|
44 chained = Bool(title=_("Chained rule?"), |
|
45 description=_("If 'no', and if this rule is matching received request URL, the rule " |
|
46 "returns a redirection response; otherwise, the rule just rewrites the " |
|
47 "input URL which is forwarded to the next rule"), |
|
48 required=True, |
|
49 default=False) |
|
50 |
|
51 permanent = Bool(title=_("Permanent redirect?"), |
|
52 description=_("Define if this redirection should be permanent or temporary"), |
|
53 required=True, |
|
54 default=True) |
|
55 |
|
56 url_pattern = TextLine(title=_("URL pattern"), |
|
57 description=_("Regexp pattern of matching URLs for this redirection rule"), |
|
58 required=True) |
|
59 |
|
60 pattern = Attribute("Compiled URL pattern") |
|
61 |
|
62 reference = InternalReferenceField(title=_("Internal redirection target"), |
|
63 description=_("Internal redirection reference. You can search a reference using " |
|
64 "'+' followed by internal number, of by entering text matching " |
|
65 "content title."), |
|
66 required=False) |
|
67 |
|
68 target_url = TextLine(title=_("Target URL"), |
|
69 description=_("URL to which source URL should be redirected"), |
|
70 required=False) |
|
71 |
|
72 @invariant |
|
73 def check_reference_and_target(self): |
|
74 if self.reference and self.target_url: |
|
75 raise Invalid(_("You can only provide an internal reference OR a target URL")) |
|
76 elif not (self.reference or self.target_url): |
|
77 raise Invalid(_("You must provide an internal reference OR a target URL")) |
|
78 |
|
79 def match(self, source_url): |
|
80 """Return regexp URL match on given URL""" |
|
81 |
|
82 def rewrite(self, source_url, request): |
|
83 """Rewrite given source URL""" |
|
84 |
|
85 |
|
86 class IRedirectionManager(IOrderedContainer): |
|
87 """Redirection manager""" |
|
88 |
|
89 contains(IRedirectionRule) |
|
90 |
|
91 def get_active_items(self): |
|
92 """Get iterator over active items""" |
|
93 |
|
94 def get_response(self, request): |
|
95 """Get new response for given request""" |
|
96 |
|
97 def test_rules(self, source_url, request, check_inactive_rules=False): |
|
98 """Test rules against given URL""" |
|
99 |
|
100 |
|
101 class IRedirectionManagerTarget(Interface): |
|
102 """Redirection manager target marker interface""" |