Changeset 839f6af in tailbone


Ignore:
Timestamp:
07/09/19 22:14:12 (5 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
fa825da
Parents:
0d7492f
Message:

Add basic "DB picker" support, for views which allow multiple engines

i.e. whichever engine is "current" will determine where data comes from

Location:
tailbone
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tailbone/templates/themes/falafel/base.mako

    r0d7492f r839f6af  
    133133          </div>
    134134
     135          % if expose_db_picker is not Undefined and expose_db_picker:
     136              <div class="level-item">
     137                <p>DB:</p>
     138              </div>
     139              <div class="level-item">
     140                ${h.form(url('change_db_engine'))}
     141                ${h.csrf_token(request)}
     142                ${h.hidden('engine_type', value=master.engine_type_key)}
     143                <div class="select">
     144                  ${h.select('dbkey', db_picker_selected, db_picker_options, id='db-picker')}
     145                </div>
     146                ${h.end_form()}
     147              </div>
     148          % endif
     149
    135150        </div><!-- level-left -->
    136151        <div class="level-right">
     
    304319    var logout_url = '${request.route_url('logout')}';
    305320    var noop_url = '${request.route_url('noop')}';
     321    % if expose_db_picker is not Undefined and expose_db_picker:
     322        $(function() {
     323            $('#db-picker').change(function() {
     324                $(this).parents('form:first').submit();
     325            });
     326        });
     327    % endif
    306328    % if expose_theme_picker and request.has_perm('common.change_app_theme'):
    307329        $(function() {
  • tailbone/views/common.py

    r0d7492f r839f6af  
    144144        return self.redirect(self.request.get_referrer())
    145145
     146    def change_db_engine(self):
     147        """
     148        Simple view which can change user's "current" database engine, of a
     149        given type, then redirect back to referring page.
     150        """
     151        engine_type = self.request.POST.get('engine_type')
     152        if engine_type:
     153            dbkey = self.request.POST.get('dbkey')
     154            if dbkey:
     155                self.request.session['tailbone.engines.{}.current'.format(engine_type)] = dbkey
     156                if self.rattail_config.getbool('tailbone', 'engines.flash_after_change', default=True):
     157                    self.request.session.flash("Switched '{}' database to: {}".format(engine_type, dbkey))
     158        return self.redirect(self.request.get_referrer())
     159
    146160    def feedback(self):
    147161        """
     
    213227        config.add_view(cls, attr='about', route_name='mobile.about', renderer='/mobile/about.mako')
    214228
     229        # change db engine
     230        config.add_tailbone_permission('common', 'common.change_db_engine',
     231                                       "Change which Database Engine is active (for user)")
     232        config.add_route('change_db_engine', '/change-db-engine', request_method='POST')
     233        config.add_view(cls, attr='change_db_engine', route_name='change_db_engine')
     234
    215235        # change theme
    216236        config.add_tailbone_permission('common', 'common.change_app_theme',
  • tailbone/views/master.py

    r0d7492f r839f6af  
    3737
    3838import sqlalchemy_continuum as continuum
     39from sqlalchemy_utils.functions import get_primary_keys
    3940
    4041from rattail.db import model, Session as RattailSession
     
    4849from rattail.excel import ExcelWriter
    4950from rattail.gpc import GPC
     51from rattail.util import OrderedDict
    5052
    5153import colander
     
    125127    has_thumbnail = False
    126128
     129    # can set this to true, and set type key as needed, and implement some
     130    # other things also, to get a DB picker in the header for all views
     131    supports_multiple_engines = False
     132    engine_type_key = 'rattail'
     133
    127134    row_attrs = {}
    128135    cell_attrs = {}
     
    20972104        if hasattr(cls, 'model_key'):
    20982105            return cls.model_key
    2099         mapper = orm.class_mapper(cls.get_model_class())
    2100         return ','.join([k.key for k in mapper.primary_key])
     2106
     2107        pkeys = get_primary_keys(cls.get_model_class())
     2108        return ','.join(pkeys)
    21012109
    21022110    @classmethod
     
    23242332        return ['/master/{}.mako'.format(template)]
    23252333
     2334    def get_current_engine_dbkey(self):
     2335        """
     2336        Returns the "current" engine's dbkey, for the current user.
     2337        """
     2338        return self.request.session.get('tailbone.engines.{}.current'.format(self.engine_type_key),
     2339                                        'default')
     2340
    23262341    def template_kwargs(self, **kwargs):
    23272342        """
    23282343        Supplement the template context, for all views.
    23292344        """
     2345        # whether or not to show the DB picker?
     2346        kwargs['expose_db_picker'] = False
     2347        if self.supports_multiple_engines:
     2348
     2349            # view declares support for multiple engines, but we only want to
     2350            # show the picker if we have more than one engine configured
     2351            engines = self.get_db_engines()
     2352            if len(engines) > 1:
     2353
     2354                # user session determines "current" db engine *of this type*
     2355                # (note that many master views may declare the same type, and
     2356                # would therefore share the "current" engine)
     2357                selected = self.get_current_engine_dbkey()
     2358                kwargs['expose_db_picker'] = True
     2359                kwargs['db_picker_options'] = [tags.Option(k) for k in engines]
     2360                kwargs['db_picker_selected'] = selected
     2361
    23302362        return kwargs
     2363
     2364    def get_db_engines(self):
     2365        """
     2366        Must return a dict (or even better, OrderedDict) which contains all
     2367        supported database engines for the master view.  Used with the DB
     2368        picker feature.
     2369        """
     2370        engines = OrderedDict()
     2371        if self.rattail_config.rattail_engine:
     2372            engines['default'] = self.rattail_config.rattail_engine
     2373        for dbkey in sorted(self.rattail_config.rattail_engines):
     2374            if dbkey != 'default':
     2375                engines[dbkey] = self.rattail_config.rattail_engines[dbkey]
     2376        return engines
    23312377
    23322378    ##############################
     
    24472493            return {self.model_key: row[self.model_key]}
    24482494        else:
    2449             keys = [k.key for k in mapper.primary_key]
     2495            pkeys = get_primary_keys(row)
     2496            keys = list(pkeys)
    24502497            values = [getattr(row, k) for k in keys]
    24512498            return dict(zip(keys, values))
Note: See TracChangeset for help on using the changeset viewer.