From f5d24133f75b4baa0655b029a1fb510386ef62db Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Fri, 4 Mar 2022 17:44:34 -0600 Subject: [PATCH] Make common web view a bit more common i.e. avoid the need to subclass it in derived projects --- tailbone/views/common.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tailbone/views/common.py b/tailbone/views/common.py index b4a947fa..3f846f75 100644 --- a/tailbone/views/common.py +++ b/tailbone/views/common.py @@ -28,17 +28,15 @@ from __future__ import unicode_literals, absolute_import import six -import rattail from rattail.db import model from rattail.batch import consume_batch_id from rattail.mail import send_email -from rattail.util import OrderedDict, simple_error +from rattail.util import OrderedDict, simple_error, import_module_path from rattail.files import resource_path from pyramid import httpexceptions from pyramid.response import Response -import tailbone from tailbone import forms from tailbone.forms.common import Feedback from tailbone.db import Session @@ -51,8 +49,6 @@ class CommonView(View): """ Base class for common views; override as needed. """ - project_title = "Tailbone" - project_version = tailbone.__version__ robots_txt_path = resource_path('tailbone.static:robots.txt') def home(self, **kwargs): @@ -96,11 +92,24 @@ class CommonView(View): response.content_type = b'text/plain' return response + def get_project_title(self): + return self.rattail_config.app_title() + + def get_project_version(self): + + # TODO: deprecate this + if hasattr(self, 'project_version'): + return self.project_version + + pkg = self.rattail_config.app_package() + mod = import_module_path(pkg) + return mod.__version__ + def exception(self): """ Generic exception view """ - return {'project_title': self.project_title} + return {'project_title': self.get_project_title()} def about(self): """ @@ -108,8 +117,8 @@ class CommonView(View): """ use_buefy = self.get_use_buefy() context = { - 'project_title': self.project_title, - 'project_version': self.project_version, + 'project_title': self.get_project_title(), + 'project_version': self.get_project_version(), 'packages': self.get_packages(), 'use_buefy': use_buefy, } @@ -122,6 +131,7 @@ class CommonView(View): Should return the full set of packages which should be displayed on the 'about' page. """ + import rattail, tailbone return OrderedDict([ ('rattail', rattail.__version__), ('Tailbone', tailbone.__version__),