Refactor tempmon views to leverage rattail-tempmon database

This commit is contained in:
Lance Edgar 2016-12-05 19:14:20 -06:00
parent 083f8ecde0
commit 8674cf1650
7 changed files with 80 additions and 42 deletions

View file

@ -87,6 +87,8 @@ def make_rattail_config(settings):
# however, this is *not* desirable. # however, this is *not* desirable.
rattail.db.Session.configure(bind=rattail_engines['default']) rattail.db.Session.configure(bind=rattail_engines['default'])
tailbone.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 # Make sure rattail config object uses our scoped session, to avoid
# unnecessary connections (and pooling limits). # unnecessary connections (and pooling limits).

View file

@ -37,6 +37,9 @@ from rattail.db.continuum import versioning_manager
Session = scoped_session(sessionmaker(class_=SessionBase, rattail_config=None, rattail_record_changes=False)) 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): class TailboneSessionDataManager(datamanager.SessionDataManager):
"""Integrate a top level sqlalchemy session transaction into a zope transaction """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. # 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): if tuple(int(x) for x in sa.__version__.split('.')) >= (0, 7):
register(Session) register(Session)
register(TempmonSession)
else: else:
Session.configure(extension=ZopeTransactionExtension()) Session.configure(extension=ZopeTransactionExtension())
TempmonSession.configure(extension=ZopeTransactionExtension())

View file

@ -26,6 +26,8 @@ Views for tempmon
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from .core import MasterView, ClientFieldRenderer, ProbeFieldRenderer
def includeme(config): def includeme(config):
config.include('tailbone.views.tempmon.clients') config.include('tailbone.views.tempmon.clients')

View file

@ -28,13 +28,13 @@ from __future__ import unicode_literals, absolute_import
import subprocess import subprocess
from rattail.db import model from rattail_tempmon.db import model as tempmon
import formalchemy as fa import formalchemy as fa
from webhelpers.html import HTML, tags from webhelpers.html import HTML, tags
from tailbone.db import Session from tailbone.db import TempmonSession
from tailbone.views import MasterView from tailbone.views.tempmon import MasterView
class ProbesFieldRenderer(fa.FieldRenderer): class ProbesFieldRenderer(fa.FieldRenderer):
@ -51,10 +51,10 @@ class ProbesFieldRenderer(fa.FieldRenderer):
def unique_config_key(value, field): def unique_config_key(value, field):
client = field.parent.model client = field.parent.model
query = Session.query(model.TempmonClient)\ query = TempmonSession.query(tempmon.Client)\
.filter(model.TempmonClient.config_key == value) .filter(tempmon.Client.config_key == value)
if client.uuid: if client.uuid:
query = query.filter(model.TempmonClient.uuid != client.uuid) query = query.filter(tempmon.Client.uuid != client.uuid)
if query.count(): if query.count():
raise fa.ValidationError("Config key must be unique") raise fa.ValidationError("Config key must be unique")
@ -63,7 +63,7 @@ class TempmonClientView(MasterView):
""" """
Master view for tempmon clients. Master view for tempmon clients.
""" """
model_class = model.TempmonClient model_class = tempmon.Client
route_prefix = 'tempmon.clients' route_prefix = 'tempmon.clients'
url_prefix = '/tempmon/clients' url_prefix = '/tempmon/clients'

View file

@ -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 <http://www.gnu.org/licenses/>.
#
################################################################################
"""
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))

View file

@ -26,35 +26,21 @@ Views for tempmon probes
from __future__ import unicode_literals, absolute_import 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 import forms
from tailbone.views import MasterView from tailbone.views.tempmon import MasterView, ClientFieldRenderer
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 TempmonProbeView(MasterView): class TempmonProbeView(MasterView):
""" """
Master view for tempmon probes. Master view for tempmon probes.
""" """
model_class = model.TempmonProbe model_class = tempmon.Probe
route_prefix = 'tempmon.probes' route_prefix = 'tempmon.probes'
url_prefix = '/tempmon/probes' url_prefix = '/tempmon/probes'
def _preconfigure_grid(self, g): def _preconfigure_grid(self, g):
g.joiners['client'] = lambda q: q.join(model.TempmonClient) g.joiners['client'] = lambda q: q.join(tempmon.Client)
g.sorters['client'] = g.make_sorter(model.TempmonClient.config_key) g.sorters['client'] = g.make_sorter(tempmon.Client.config_key)
g.default_sortkey = 'client' g.default_sortkey = 'client'
g.config_key.set(label="Key") g.config_key.set(label="Key")
g.appliance_type.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.TEMPMON_APPLIANCE_TYPE)) g.appliance_type.set(renderer=forms.renderers.EnumFieldRenderer(self.enum.TEMPMON_APPLIANCE_TYPE))

View file

@ -26,29 +26,14 @@ Views for tempmon readings
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from rattail.db import model from tailbone.views.tempmon import MasterView, ClientFieldRenderer, ProbeFieldRenderer
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))
class TempmonReadingView(MasterView): class TempmonReadingView(MasterView):
""" """
Master view for tempmon readings. Master view for tempmon readings.
""" """
model_class = model.TempmonReading model_class = tempmon.Reading
route_prefix = 'tempmon.readings' route_prefix = 'tempmon.readings'
url_prefix = '/tempmon/readings' url_prefix = '/tempmon/readings'
creatable = False creatable = False