Skip to content

Commit

Permalink
S3XMLContents helper to replace {{}} expressions with local URLs, to
Browse files Browse the repository at this point in the history
allow easy inter-linking of CMS pages (applied in cms/page), RMS: enable
Rich-Text for CMS
  • Loading branch information
nursix committed May 7, 2015
1 parent 10b5e1e commit 0319f54
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nursix-1.1.0-devel-2302-g40c70a1 (2015-05-07 11:05:12)
nursix-1.1.0-devel-2305-g10b5e1e (2015-05-07 14:05:42)
2 changes: 1 addition & 1 deletion controllers/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def prep(r):
# Post-process
def postp(r, output):
if r.record and not r.transformable():
output = {"item": r.record.body}
output = {"item": s3base.S3XMLContents(r.record.body)()}
current.menu.options = None
response.view = s3base.S3CRUD._view(r, "cms/page.html")
if r.record.replies:
Expand Down
71 changes: 71 additions & 0 deletions modules/s3/s3widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"s3_comments_widget",
"s3_richtext_widget",
"search_ac",
"S3XMLContents",
"ICON",
)

Expand Down Expand Up @@ -7710,6 +7711,76 @@ def search_ac(r, **attr):
current.response.headers["Content-Type"] = "application/json"
return json.dumps(output, separators=SEPARATORS)

# =============================================================================
class S3XMLContents(object):
"""
Renderer for db-stored XML contents (e.g. CMS)
Replaces {{page}} expressions inside the contents with local URLs.
{{page}} - gives the URL of the current page
{{name:example}} - gives the URL of the current page with
a query ?name=example (can add any number
of query variables)
{{c:org,f:organisation}} - c and f tokens override controller and
function of the current page, in this
example like /org/organisation
@note: does not check permissions for the result URLs
"""

def __init__(self, contents):
"""
Constructor
@param contents: the contents (string)
"""

self.contents = contents

# -------------------------------------------------------------------------
def link(self, match):
"""
Replace {{}} expressions with local URLs, with the ability to
override controller, function and URL query variables.Called
from re.sub.
@param match: the re match object
"""

# Parse the expression
parameters = {}
tokens = match.group(1).split(",")
for token in tokens:
if not token:
continue
elif ":" in token:
key, value = token.split(":")
else:
key, value = token, None
if not value:
continue
parameters[key.strip()] = value.strip()

# Construct the URL
request = current.request
c = parameters.pop("c", request.controller)
f = parameters.pop("f", request.function)

return URL(c=c, f=f, vars=parameters, host=True)

# -------------------------------------------------------------------------
def __call__(self):
""" Render the output """

return re.sub(r"\{\{(.+?)\}\}", self.link, self.contents)

# -------------------------------------------------------------------------
def xml(self):
""" Render the output as XML object (to suppress XML escaping) """

return XML(self())

# =============================================================================
class ICON(I):
"""
Expand Down
6 changes: 4 additions & 2 deletions modules/s3db/cms.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
OTHER DEALINGS IN THE SOFTWARE.
"""

__all__ = ["S3ContentModel",
__all__ = ("S3ContentModel",
"S3ContentMapModel",
"S3ContentOrgModel",
"S3ContentOrgGroupModel",
Expand All @@ -37,7 +37,9 @@
"cms_customise_post_fields",
"cms_post_list_layout",
"S3CMS",
]
)

import re

try:
import json # try stdlib (Python 2.6)
Expand Down
1 change: 1 addition & 0 deletions modules/templates/IFRC/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def currency_default(default):
#
# Uncomment this to hide CMS from module index pages
settings.cms.hide_index = True
settings.cms.richtext = True

# -----------------------------------------------------------------------------
# Messaging
Expand Down

0 comments on commit 0319f54

Please sign in to comment.