From 8674cf16506fe8a4b12fb95d5aecbc74a36f0040 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 5 Dec 2016 19:14:20 -0600 Subject: [PATCH] Refactor tempmon views to leverage rattail-tempmon database --- tailbone/app.py | 2 ++ tailbone/db.py | 5 +++ tailbone/views/tempmon/__init__.py | 2 ++ tailbone/views/tempmon/clients.py | 14 ++++---- tailbone/views/tempmon/core.py | 58 ++++++++++++++++++++++++++++++ tailbone/views/tempmon/probes.py | 22 +++--------- tailbone/views/tempmon/readings.py | 19 ++-------- 7 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 tailbone/views/tempmon/core.py diff --git a/tailbone/app.py b/tailbone/app.py index ec58faf6..508d2b5d 100644 --- a/tailbone/app.py +++ b/tailbone/app.py @@ -87,6 +87,8 @@ def make_rattail_config(settings): # however, this is *not* desirable. rattail.db.Session.configure(bind=rattail_engines['default']) tailbone.db.Session.configure(bind=rattail_engines['default']) + if hasattr(rattail_config, 'tempmon_engine'): + tailbone.db.TempmonSession.configure(bind=rattail_config.tempmon_engine) # Make sure rattail config object uses our scoped session, to avoid # unnecessary connections (and pooling limits). diff --git a/tailbone/db.py b/tailbone/db.py index bc754e2b..27579da3 100644 --- a/tailbone/db.py +++ b/tailbone/db.py @@ -37,6 +37,9 @@ from rattail.db.continuum import versioning_manager Session = scoped_session(sessionmaker(class_=SessionBase, rattail_config=None, rattail_record_changes=False)) +# not necessarily used, but here if you need it +TempmonSession = scoped_session(sessionmaker()) + class TailboneSessionDataManager(datamanager.SessionDataManager): """Integrate a top level sqlalchemy session transaction into a zope transaction @@ -144,5 +147,7 @@ def register(session, initial_state=datamanager.STATUS_ACTIVE, # TODO: We can probably assume a new SA version since we use Continuum now. if tuple(int(x) for x in sa.__version__.split('.')) >= (0, 7): register(Session) + register(TempmonSession) else: Session.configure(extension=ZopeTransactionExtension()) + TempmonSession.configure(extension=ZopeTransactionExtension()) diff --git a/tailbone/views/tempmon/__init__.py b/tailbone/views/tempmon/__init__.py index b9b8d1af..b67c66b0 100644 --- a/tailbone/views/tempmon/__init__.py +++ b/tailbone/views/tempmon/__init__.py @@ -26,6 +26,8 @@ Views for tempmon from __future__ import unicode_literals, absolute_import +from .core import MasterView, ClientFieldRenderer, ProbeFieldRenderer + def includeme(config): config.include('tailbone.views.tempmon.clients') diff --git a/tailbone/views/tempmon/clients.py b/tailbone/views/tempmon/clients.py index 125de836..22db63df 100644 --- a/tailbone/views/tempmon/clients.py +++ b/tailbone/views/tempmon/clients.py @@ -28,13 +28,13 @@ from __future__ import unicode_literals, absolute_import import subprocess -from rattail.db import model +from rattail_tempmon.db import model as tempmon import formalchemy as fa from webhelpers.html import HTML, tags -from tailbone.db import Session -from tailbone.views import MasterView +from tailbone.db import TempmonSession +from tailbone.views.tempmon import MasterView class ProbesFieldRenderer(fa.FieldRenderer): @@ -51,10 +51,10 @@ class ProbesFieldRenderer(fa.FieldRenderer): def unique_config_key(value, field): client = field.parent.model - query = Session.query(model.TempmonClient)\ - .filter(model.TempmonClient.config_key == value) + query = TempmonSession.query(tempmon.Client)\ + .filter(tempmon.Client.config_key == value) if client.uuid: - query = query.filter(model.TempmonClient.uuid != client.uuid) + query = query.filter(tempmon.Client.uuid != client.uuid) if query.count(): raise fa.ValidationError("Config key must be unique") @@ -63,7 +63,7 @@ class TempmonClientView(MasterView): """ Master view for tempmon clients. """ - model_class = model.TempmonClient + model_class = tempmon.Client route_prefix = 'tempmon.clients' url_prefix = '/tempmon/clients' diff --git a/tailbone/views/tempmon/core.py b/tailbone/views/tempmon/core.py new file mode 100644 index 00000000..5802aa59 --- /dev/null +++ b/tailbone/views/tempmon/core.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2016 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU Affero General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for +# more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Rattail. If not, see . +# +################################################################################ +""" +Common stuff for tempmon views +""" + +from __future__ import unicode_literals, absolute_import + +from formalchemy.fields import SelectFieldRenderer +from webhelpers.html import tags + +from tailbone import views +from tailbone.db import TempmonSession + + +class MasterView(views.MasterView): + """ + Base class for tempmon views. + """ + Session = TempmonSession + + +class ClientFieldRenderer(SelectFieldRenderer): + + def render_readonly(self, **kwargs): + client = self.raw_value + if not client: + return '' + return tags.link_to(client, self.request.route_url('tempmon.clients.view', uuid=client.uuid)) + + +class ProbeFieldRenderer(SelectFieldRenderer): + + def render_readonly(self, **kwargs): + probe = self.raw_value + if not probe: + return '' + return tags.link_to(probe, self.request.route_url('tempmon.probes.view', uuid=probe.uuid)) diff --git a/tailbone/views/tempmon/probes.py b/tailbone/views/tempmon/probes.py index a70eec19..fed80604 100644 --- a/tailbone/views/tempmon/probes.py +++ b/tailbone/views/tempmon/probes.py @@ -26,35 +26,21 @@ Views for tempmon probes from __future__ import unicode_literals, absolute_import -from rattail.db import model - -from formalchemy.fields import SelectFieldRenderer -from webhelpers.html import tags - from tailbone import forms -from tailbone.views import MasterView - - -class ClientFieldRenderer(SelectFieldRenderer): - - def render_readonly(self, **kwargs): - client = self.raw_value - if not client: - return '' - return tags.link_to(client, self.request.route_url('tempmon.clients.view', uuid=client.uuid)) +from tailbone.views.tempmon import MasterView, ClientFieldRenderer class TempmonProbeView(MasterView): """ Master view for tempmon probes. """ - model_class = model.TempmonProbe + model_class = tempmon.Probe route_prefix = 'tempmon.probes' url_prefix = '/tempmon/probes' def _preconfigure_grid(self, g): - g.joiners['client'] = lambda q: q.join(model.TempmonClient) - g.sorters['client'] = g.make_sorter(model.TempmonClient.config_key) + g.joiners['client'] = lambda q: q.join(tempmon.Client) + g.sorters['client'] = g.make_sorter(tempmon.Client.config_key) g.default_sortkey = 'client' g.config_key.set(label="Key") g.appliance_type.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.TEMPMON_APPLIANCE_TYPE)) diff --git a/tailbone/views/tempmon/readings.py b/tailbone/views/tempmon/readings.py index e2f35fc3..894c2674 100644 --- a/tailbone/views/tempmon/readings.py +++ b/tailbone/views/tempmon/readings.py @@ -26,29 +26,14 @@ Views for tempmon readings from __future__ import unicode_literals, absolute_import -from rattail.db import model - -from formalchemy.fields import SelectFieldRenderer -from webhelpers.html import tags - -from tailbone.views import MasterView -from tailbone.views.tempmon.probes import ClientFieldRenderer - - -class ProbeFieldRenderer(SelectFieldRenderer): - - def render_readonly(self, **kwargs): - probe = self.raw_value - if not probe: - return '' - return tags.link_to(probe, self.request.route_url('tempmon.probes.view', uuid=probe.uuid)) +from tailbone.views.tempmon import MasterView, ClientFieldRenderer, ProbeFieldRenderer class TempmonReadingView(MasterView): """ Master view for tempmon readings. """ - model_class = model.TempmonReading + model_class = tempmon.Reading route_prefix = 'tempmon.readings' url_prefix = '/tempmon/readings' creatable = False