Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass through the calculated dimensions to the template #34

Merged
merged 8 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
1.0.3 (unreleased)
------------------

- #34: Pass through the calculated dimensions to the template
- #33: Include D3JS and support for Range Graphs
- #32: Added language selector
- #31: Fix sort order of uniquified items
Expand Down
13 changes: 11 additions & 2 deletions src/senaite/impress/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ def ajax_render_reports(self, *args):
# update the request form with the parsed json data
data = self.get_json()

paperformat = data.get("format", "A4")
orientation = data.get("orientation", "portrait")

# Create a collection of the requested UIDs
collection = self.get_collection(data.get("items"))

Expand All @@ -162,12 +165,18 @@ def ajax_render_reports(self, *args):
for client_uid, collection in grouped_by_client.items():
# render multi report
if is_multi_template:
html = self.render_multi_report(collection, template)
html = self.render_multi_report(collection,
template,
paperformat=paperformat,
orientation=orientation)
htmls.append(html)
else:
# render single report
for model in collection:
html = self.render_report(model, template)
html = self.render_report(model,
template,
paperformat=paperformat,
orientation=orientation)
htmls.append(html)

return "\n".join(htmls)
Expand Down
20 changes: 12 additions & 8 deletions src/senaite/impress/analysisrequest/reportview.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,18 @@ def __init__(self, model, request):
self.model = model
self.request = request

def render(self, template):
context = self.get_template_context(self.model)
def render(self, template, **kw):
context = self.get_template_context(self.model, **kw)
template = Template(template).safe_substitute(context)
return SINGLE_TEMPLATE.safe_substitute(context, template=template)

def get_template_context(self, model):
return {
def get_template_context(self, model, **kw):
context = {
"uids": model.UID(),
"client_uid": model.getClientUID(),
}
context.update(kw)
return context


class MultiReportView(ReportView):
Expand All @@ -275,19 +277,21 @@ def __init__(self, collection, request):
self.collection = collection
self.request = request

def render(self, template):
def render(self, template, **kw):
"""Wrap the template and render
"""
context = self.get_template_context(self.collection)
context = self.get_template_context(self.collection, **kw)
template = Template(template).safe_substitute(context)
return MULTI_TEMPLATE.safe_substitute(context, template=template)

def get_template_context(self, collection):
def get_template_context(self, collection, **kw):
if not collection:
return {}
uids = map(lambda m: m.uid, collection)
client_uid = collection[0].getClientUID()
return {
context = {
"uids": ",".join(uids),
"client_uid": client_uid,
}
context.update(kw)
return context
36 changes: 25 additions & 11 deletions src/senaite/impress/publishview.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,27 +160,35 @@ def get_report_type(self):
# any other content type as well.
return self.request.form.get("type", "AnalysisRequest")

def render_report(self, model, template, **kw):
def render_report(self, model, template, paperformat, orientation, **kw):
"""Render a SuperModel to HTML
"""
_type = self.get_report_type()
# Query the controller view as a multi-adapter to allow 3rd party
# overriding with a browser layer
view = getMultiAdapter((model, self.request), IReportView, name=_type)
return view.render(self.read_template(template, view, **kw))
# pass through the calculated dimensions to the template
dimensions = self.calculate_dimensions(paperformat, orientation)
template = self.read_template(template, view, **dimensions)
return view.render(
template, paperformat=paperformat, orientation=orientation, **kw)

def render_multi_report(self, collection, template, **kw):
def render_multi_report(self, collection, template, paperformat, orientation, **kw): # noqa
"""Render multiple SuperModels to HTML
"""
_type = self.get_report_type()
# Query the controller view as a multi-adapter to allow 3rd party
# overriding with a browser layer
view = getMultiAdapter(
(collection, self.request), IMultiReportView, name=_type)
return view.render(self.read_template(template, view, **kw))

def get_print_css(self, paperformat="A4", orientation="portrait"):
"""Returns the generated print CSS for the given format/orientation
# pass through the calculated dimensions to the template
dimensions = self.calculate_dimensions(paperformat, orientation)
template = self.read_template(template, view, **dimensions)
return view.render(
template, paperformat=paperformat, orientation=orientation, **kw)

def calculate_dimensions(self, paperformat="A4", orientation="portrait"):
"""Calculate the page and content dimensions
"""
pf = self.get_paperformat(paperformat)

Expand All @@ -201,15 +209,21 @@ def get_print_css(self, paperformat="A4", orientation="portrait"):
content_height = page_height - margin_top - margin_bottom

# prepare the substitution context
context = pf.copy()
context.update({
dimensions = pf.copy()
dimensions.update({
"page_width": page_width,
"page_height": page_height,
"content_width": content_width,
"content_height": content_height,
})
return dimensions

return CSS.safe_substitute(context)
def get_print_css(self, paperformat="A4", orientation="portrait"):
"""Returns the generated print CSS for the given format/orientation
"""
dimensions = self.calculate_dimensions(
paperformat=paperformat, orientation=orientation)
return CSS.safe_substitute(dimensions)

def get_paperformat(self, paperformat):
"""Return the paperformat dictionary
Expand Down Expand Up @@ -286,7 +300,7 @@ def get_report_template(self, template=None):

def read_template(self, template, instance, **kw):
if self.is_page_template(template):
template = ViewPageTemplateFile(template, **kw)(instance)
template = ViewPageTemplateFile(template)(instance, **kw)
else:
with open(template, "r") as template:
template = template.read()
Expand Down
2 changes: 1 addition & 1 deletion src/senaite/impress/reportview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def __init__(self, *args, **kwargs):
# needed for template rendering
self.context = api.get_portal()

def render(self, template):
def render(self, template, **kw):
raise NotImplemented("Must be implemented by subclass")

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/senaite/impress/static/js/src/publish.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ class PublishController extends React.Component
if name == "template"
# reload HTML and Preview if the template changed
return @loadReports()
# reload only the Preview
return @loadPreview()
# N.B. we always render now the full reports, so that the template can
# handle the changed dimensions of the paperformat and orientation
return @loadReports()


isMultiReport: ->
Expand Down
Loading