Changeset 8d6ecc3 in tailbone


Ignore:
Timestamp:
03/24/19 21:09:08 (11 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
13bba63
Parents:
3cef591
git-author:
Lance Edgar <ledgar@…> (03/24/19 17:24:43)
git-committer:
Lance Edgar <ledgar@…> (03/24/19 21:09:08)
Message:

Add basic "Buefy" support for grids (master index view)

still pretty experimental at this point, but making progress

Location:
tailbone
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • tailbone/grids/core.py

    r3cef591 r8d6ecc3  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    176176        if key in self.filters:
    177177            self.filters[key].label = label
     178
     179    def get_label(self, key):
     180        """
     181        Returns the label text for given field key.
     182        """
     183        return self.labels.get(key, prettify(key))
    178184
    179185    def set_link(self, key, link=True):
     
    901907        context = kwargs
    902908        context['grid'] = self
     909        context['request'] = self.request
    903910        context.setdefault('allow_save_defaults', True)
    904911        return render(template, context)
     912
     913    def render_buefy(self, template='/grids/buefy.mako', **kwargs):
     914        """
     915        Render the Buefy grid, including filters.
     916        """
     917        return self.render_complete(template=template, **kwargs)
    905918
    906919    def render_filters(self, template='/grids/filters.mako', **kwargs):
     
    982995        # TODO: Make configurable or something...
    983996        return [5, 10, 20, 50, 100, 200]
     997
     998    def has_static_data(self):
     999        """
     1000        Should return ``True`` if the grid data can be considered "static"
     1001        (i.e. a list of values).  Will return ``False`` otherwise, e.g. if the
     1002        data is represented as a SQLAlchemy query.
     1003        """
     1004        # TODO: should make this smarter?
     1005        if isinstance(self.data, list):
     1006            return True
     1007        return False
     1008
     1009    def get_buefy_columns(self):
     1010        """
     1011        Return a list of dicts representing all grid columns.  Meant for use
     1012        with Buefy table.
     1013        """
     1014        columns = []
     1015        for name in self.columns:
     1016            columns.append({
     1017                'field': name,
     1018                'label': self.get_label(name),
     1019                'sortable': self.sortable and name in self.sorters,
     1020            })
     1021        return columns
     1022
     1023    def get_buefy_data(self):
     1024        """
     1025        Returns a list of data rows for the grid, for use with Buefy table.
     1026        """
     1027        # filter / sort / paginate to get "visible" data
     1028        raw_data = self.make_visible_data()
     1029        data = []
     1030
     1031        # iterate over data rows
     1032        for i in range(len(raw_data)):
     1033            rowobj = raw_data[i]
     1034            row = {}
     1035
     1036            # iterate over data fields
     1037            for name in self.columns:
     1038
     1039                # leverage configured rendering logic where applicable;
     1040                # otherwise use "raw" data value as string
     1041                if self.renderers and name in self.renderers:
     1042                    row[name] = self.renderers[name](rowobj, name)
     1043                else:
     1044                    value = self.obtain_value(rowobj, name)
     1045                    if value is None:
     1046                        value = ""
     1047                    row[name] = six.text_type(value)
     1048
     1049            # set action URL(s) for row, as needed
     1050            self.set_action_urls(row, rowobj, i)
     1051
     1052            data.append(row)
     1053
     1054        results = {
     1055            'data': data,
     1056        }
     1057
     1058        if self.pageable and self.pager is not None:
     1059            results['total_items'] = self.pager.item_count
     1060            results['per_page'] = self.pager.items_per_page
     1061            results['page'] = self.pager.page
     1062            results['pages'] = self.pager.page_count
     1063            results['first_item'] = self.pager.first_item
     1064            results['last_item'] = self.pager.last_item
     1065
     1066        return results
     1067
     1068    def set_action_urls(self, row, rowobj, i):
     1069        """
     1070        Pre-generate all action URLs for the given data row.  Meant for use
     1071        with Buefy table, since we can't generate URLs from JS.
     1072        """
     1073        for action in (self.main_actions + self.more_actions):
     1074            url = action.get_url(rowobj, i)
     1075            row['_action_url_{}'.format(action.key)] = url
     1076
     1077    def is_linked(self, name):
     1078        """
     1079        Should return ``True`` if the given column name is configured to be
     1080        "linked" (i.e. table cell should contain a link to "view object"),
     1081        otherwise ``False``.
     1082        """
     1083        if self.linked_columns:
     1084            if name in self.linked_columns:
     1085                return True
     1086        return False
    9841087
    9851088
  • tailbone/templates/master/index.mako

    r3cef591 r8d6ecc3  
    1818    $(function() {
    1919
     20        % if not use_buefy:
    2021        $('.grid-wrapper').gridwrapper();
     22        % endif
    2123
    2224        % if master.mergeable and request.has_perm('{}.merge'.format(permission_prefix)):
     
    171173</%def>
    172174
    173 ${grid.render_complete(tools=capture(self.grid_tools).strip(), context_menu=capture(self.context_menu_items).strip())|n}
     175
     176% if use_buefy:
     177    ${grid.render_buefy(grid_columns=grid_columns, grid_data=grid_data, static_data=static_data)|n}
     178
     179% else:
     180    ## no buefy, so do the traditional thing
     181    ${grid.render_complete(tools=capture(self.grid_tools).strip(), context_menu=capture(self.context_menu_items).strip())|n}
     182
     183% endif
  • tailbone/templates/themes/falafel/base.mako

    r3cef591 r8d6ecc3  
    257257  ${h.javascript_link('https://unpkg.com/vue')}
    258258
     259  ## vue-resource
     260  ## (needed for e.g. this.$http.get() calls, used by grid at least)
     261  ${h.javascript_link('https://cdn.jsdelivr.net/npm/vue-resource@1.5.1')}
     262
    259263  ## Buefy 0.7.3
    260264  ${h.javascript_link('https://unpkg.com/buefy@0.7.3/dist/buefy.min.js')}
  • tailbone/views/datasync.py

    r3cef591 r8d6ecc3  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    4545    url_prefix = '/datasync/changes'
    4646    permission_prefix = 'datasync'
    47 
    4847    creatable = False
    4948    editable = False
    5049    bulk_deletable = True
     50    use_buefy = True
    5151
    5252    grid_columns = [
  • tailbone/views/email.py

    r3cef591 r8d6ecc3  
    5353    creatable = False
    5454    deletable = False
     55    use_buefy = True
    5556
    5657    grid_columns = [
  • tailbone/views/master.py

    r3cef591 r8d6ecc3  
    123123    grid_index = None
    124124    use_index_links = False
     125    # this should be turned on per-view as progress is made
     126    use_buefy = False
    125127
    126128    has_versions = False
     
    266268        self.listing = True
    267269        grid = self.make_grid()
     270        use_buefy = self.use_buefy and self.rattail_config.getbool('tailbone', 'grids.use_buefy')
    268271
    269272        # If user just refreshed the page with a reset instruction, issue a
     
    276279            self.first_visible_grid_index = grid.pager.first_item
    277280
    278         # Return grid only, if partial page was requested.
     281        # return grid only, if partial page was requested
    279282        if self.request.params.get('partial'):
    280             if six.PY3:
    281                 self.request.response.content_type = 'text/html'
    282             else:
    283                 self.request.response.content_type = b'text/html'
    284             self.request.response.text = grid.render_grid()
    285             return self.request.response
    286 
    287         return self.render_to_response('index', {'grid': grid})
     283            if use_buefy:
     284                # render grid data only, as JSON
     285                return render_to_response('json', grid.get_buefy_data(),
     286                                          request=self.request)
     287            else: # just do traditional thing, render grid HTML
     288                self.request.response.content_type = str('text/html')
     289                self.request.response.text = grid.render_grid()
     290                return self.request.response
     291
     292        context = {
     293            'grid': grid,
     294            'use_buefy': use_buefy,
     295        }
     296
     297        if use_buefy:
     298            context['grid_columns'] = grid.get_buefy_columns()
     299            context['grid_data'] = grid.get_buefy_data()
     300            context['static_data'] = grid.has_static_data()
     301
     302        return self.render_to_response('index', context)
    288303
    289304    def make_grid(self, factory=None, key=None, data=None, columns=None, **kwargs):
     
    22472262        actions = []
    22482263        prefix = self.get_permission_prefix()
     2264        use_buefy = self.use_buefy and self.rattail_config.getbool('tailbone', 'grids.use_buefy')
    22492265        if self.viewable and self.request.has_perm('{}.view'.format(prefix)):
    22502266            url = self.get_view_index_url if self.use_index_links else None
    2251             actions.append(self.make_action('view', icon='zoomin', url=url))
     2267            icon = 'eye' if use_buefy else 'zoomin'
     2268            actions.append(self.make_action('view', icon=icon, url=url))
    22522269        return actions
    22532270
     
    22622279        actions = []
    22632280        prefix = self.get_permission_prefix()
     2281        use_buefy = self.use_buefy and self.rattail_config.getbool('tailbone', 'grids.use_buefy')
    22642282        if self.editable and self.request.has_perm('{}.edit'.format(prefix)):
    2265             actions.append(self.make_action('edit', icon='pencil', url=self.default_edit_url))
     2283            icon = 'edit' if use_buefy else 'pencil'
     2284            actions.append(self.make_action('edit', icon=icon, url=self.default_edit_url))
    22662285        if self.deletable and self.request.has_perm('{}.delete'.format(prefix)):
    22672286            actions.append(self.make_action('delete', icon='trash', url=self.default_delete_url))
  • tailbone/views/tables.py

    r3cef591 r8d6ecc3  
    33#
    44#  Rattail -- Retail Software Framework
    5 #  Copyright © 2010-2018 Lance Edgar
     5#  Copyright © 2010-2019 Lance Edgar
    66#
    77#  This file is part of Rattail.
     
    4343    filterable = False
    4444    pageable = False
     45    use_buefy = True
    4546
    4647    grid_columns = [
  • tailbone/views/upgrades.py

    r3cef591 r8d6ecc3  
    6969    execute_progress_template = '/upgrade.mako'
    7070    execute_progress_initial_msg = "Upgrading"
     71    use_buefy = True
    7172
    7273    labels = {
Note: See TracChangeset for help on using the changeset viewer.