Fix package diff table for upgrade view template, per Buefy

This commit is contained in:
Lance Edgar 2019-06-15 18:06:54 -05:00
parent 63807e71fd
commit 558e127caa
4 changed files with 83 additions and 12 deletions

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2017 Lance Edgar # Copyright © 2010-2019 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -35,7 +35,8 @@ class Diff(object):
Core diff class. In sore need of documentation. Core diff class. In sore need of documentation.
""" """
def __init__(self, old_data, new_data, columns=None, fields=None, render_field=None, render_value=None, monospace=False): def __init__(self, old_data, new_data, columns=None, fields=None, render_field=None, render_value=None, monospace=False,
extra_row_attrs=None):
self.old_data = old_data self.old_data = old_data
self.new_data = new_data self.new_data = new_data
self.columns = columns or ["field name", "old value", "new value"] self.columns = columns or ["field name", "old value", "new value"]
@ -43,6 +44,7 @@ class Diff(object):
self._render_field = render_field or self.render_field_default self._render_field = render_field or self.render_field_default
self.render_value = render_value or self.render_value_default self.render_value = render_value or self.render_value_default
self.monospace = monospace self.monospace = monospace
self.extra_row_attrs = extra_row_attrs
def make_fields(self): def make_fields(self):
return sorted(set(self.old_data) | set(self.new_data), key=lambda x: x.lower()) return sorted(set(self.old_data) | set(self.new_data), key=lambda x: x.lower())
@ -61,6 +63,32 @@ class Diff(object):
context['diff'] = self context['diff'] = self
return HTML.literal(render(template, context)) return HTML.literal(render(template, context))
def get_row_attrs(self, field):
"""
Returns a *rendered* set of extra attributes for the ``<tr>`` element
for the given field. May be an empty string, or a snippet of HTML
attribute syntax, e.g.:
.. code-highlight:: none
class="diff" foo="bar"
If you wish to supply additional attributes, please define
:attr:`extra_row_attrs`, which can be either a static dict, or a
callable returning a dict.
"""
attrs = {}
if self.values_differ(field):
attrs['class'] = 'diff'
if self.extra_row_attrs:
if callable(self.extra_row_attrs):
attrs.update(self.extra_row_attrs(field, attrs))
else:
attrs.update(self.extra_row_attrs)
return HTML.render_attrs(attrs)
def render_field(self, field): def render_field(self, field):
return self._render_field(field, self) return self._render_field(field, self)

View file

@ -9,7 +9,7 @@
</thead> </thead>
<tbody> <tbody>
% for field in diff.fields: % for field in diff.fields:
<tr${' class="diff"' if diff.values_differ(field) else ''|n}> <tr ${diff.get_row_attrs(field)|n}>
<td class="field">${diff.render_field(field)}</td> <td class="field">${diff.render_field(field)}</td>
<td class="old-value">${diff.render_old_value(field)}</td> <td class="old-value">${diff.render_old_value(field)}</td>
<td class="new-value">${diff.render_new_value(field)}</td> <td class="new-value">${diff.render_new_value(field)}</td>

View file

@ -3,6 +3,7 @@
<%def name="extra_javascript()"> <%def name="extra_javascript()">
${parent.extra_javascript()} ${parent.extra_javascript()}
% if not use_buefy:
<script type="text/javascript"> <script type="text/javascript">
function show_packages(type) { function show_packages(type) {
@ -34,6 +35,7 @@
}); });
</script> </script>
% endif
</%def> </%def>
<%def name="render_form_buttons()"> <%def name="render_form_buttons()">
@ -60,5 +62,13 @@
% endif % endif
</%def> </%def>
<%def name="modify_tailbone_form()">
<script type="text/javascript">
TailboneFormData.showingPackages = 'diffs'
</script>
</%def>
${parent.body()} ${parent.body()}

View file

@ -240,24 +240,57 @@ class UpgradeView(MasterView):
return filename return filename
def render_package_diff(self, upgrade, fieldname): def render_package_diff(self, upgrade, fieldname):
use_buefy = self.get_use_buefy()
try: try:
before = self.parse_requirements(upgrade, 'before') before = self.parse_requirements(upgrade, 'before')
after = self.parse_requirements(upgrade, 'after') after = self.parse_requirements(upgrade, 'after')
kwargs = {}
if use_buefy:
kwargs['extra_row_attrs'] = self.get_extra_diff_row_attrs
diff = self.make_diff(before, after, diff = self.make_diff(before, after,
columns=["package", "old version", "new version"], columns=["package", "old version", "new version"],
render_field=self.render_diff_field, render_field=self.render_diff_field,
render_value=self.render_diff_value, render_value=self.render_diff_value,
) **kwargs)
showing = HTML.tag('div',
"showing: " kwargs = {}
+ tags.link_to("all", '#', class_='all') if use_buefy:
kwargs['@click'] = "showingPackages = 'all'"
kwargs[':style'] = "{'font-weight': showingPackages == 'all' ? 'bold' : null}"
else:
kwargs['class_'] = 'all'
all_link = tags.link_to("all", '#', **kwargs)
kwargs = {}
if use_buefy:
kwargs['@click'] = "showingPackages = 'diffs'"
kwargs[':style'] = "{'font-weight': showingPackages == 'diffs' ? 'bold' : null}"
else:
kwargs['class_'] = 'diffs'
diffs_link = tags.link_to("diffs only", '#', **kwargs)
kwargs = {}
if not use_buefy:
kwargs['class_'] = 'showing'
showing = HTML.tag('div', c=["showing: "
+ all_link
+ " / " + " / "
+ tags.link_to("diffs only", '#', class_='diffs'), + diffs_link],
class_='showing') **kwargs)
return showing + diff.render_html()
return HTML.tag('div', c=[showing + diff.render_html()])
except: except:
log.debug("failed to render package diff for upgrade: {}".format(upgrade), exc_info=True) log.debug("failed to render package diff for upgrade: {}".format(upgrade), exc_info=True)
return "(not available for this upgrade)" return HTML.tag('div', c="(not available for this upgrade)")
def get_extra_diff_row_attrs(self, field, attrs):
# note, this is only needed/used with Buefy
extra = {}
if attrs.get('class') != 'diff':
extra['v-show'] = "showingPackages == 'all'"
return extra
def changelog_link(self, project, url): def changelog_link(self, project, url):
return tags.link_to(project, url, target='_blank') return tags.link_to(project, url, target='_blank')