Module: check_mk
Branch: master
Commit: 9d0b58289df1a7ee91fe57e7d174e55b64b27716
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=9d0b58289df1a7…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue May 23 16:23:57 2017 +0200
Cleaned up odd pagetype inheritance in pagetype constructors
Change-Id: I1cf4389cffffede20b0f9e667a7337a4d3aca469
---
web/htdocs/pagetypes.py | 25 +++++++++++++------------
1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/web/htdocs/pagetypes.py b/web/htdocs/pagetypes.py
index 5be5c53..247c3cd 100644
--- a/web/htdocs/pagetypes.py
+++ b/web/htdocs/pagetypes.py
@@ -60,11 +60,6 @@ class Base(object):
# and saved to files using repr().
self._ = d
- # Now give all subclasses that chance to add mandatory keys
- # if they are missing
- for clazz in inspect.getmro(self.__class__)[::-1]:
- if "sanitize" in clazz.__dict__:
- clazz.sanitize(d)
def internal_representation(self):
return self._
@@ -324,7 +319,7 @@ class PageRenderer(Base):
# Parameters special for page renderers. These can be added to the sidebar,
# so we need a topic and a checkbox for the visibility
@classmethod
- def parameters(self, clazz):
+ def parameters(self, cls):
return [(_("General Properties"), [
( 1.4, 'topic', TextUnicode(
title = _('Topic') + '<sup>*</sup>',
@@ -408,9 +403,9 @@ class PageRenderer(Base):
# '----------------------------------------------------------------------'
class Overridable(Base):
- @classmethod
- def sanitize(self, d):
- d.setdefault("public", False)
+ def __init__(self, d):
+ super(Overridable, self).__init__(d)
+ self._.setdefault("public", False)
@classmethod
@@ -1080,9 +1075,10 @@ class Overridable(Base):
# '----------------------------------------------------------------------'
class Container(Base):
- @classmethod
- def sanitize(self, d):
- d.setdefault("elements", [])
+ def __init__(self, d):
+ super(Container, self).__init__(d)
+ self._.setdefault("elements", [])
+
# Which kind of elements are allowed to be added to this container?
# Defaulting to all possible elements.
@@ -1090,21 +1086,26 @@ class Container(Base):
def may_contain(self, element_type_name):
return True
+
def elements(self):
return self._["elements"]
+
def add_element(self, element):
self._["elements"].append(element)
+
def move_element(self, nr, whither):
el = self._["elements"][nr]
del self._["elements"][nr]
self._["elements"][whither:whither] = [ el ]
+
def is_empty(self):
return not self.elements()
+
class OverridableContainer(Overridable, Container):
# The popup for "Add to ...", e.g. for adding a graph to a report
# or dashboard. This is needed for page types with the aspect "ElementContainer".
and page_handlers()
Message-ID: <59254009.PZgylAlO+zpNi+Ph%lm(a)mathias-kettner.de>
User-Agent: Heirloom mailx 12.5 6/20/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Module: check_mk
Branch: master
Commit: 1a0c6a46291988f77b5115360f8ee2a77889466f
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1a0c6a46291988…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed May 24 09:00:06 2017 +0200
Cleaned up odd pagetype inheritance in pagetype parameters() and page_handlers()
Change-Id: I82a9c559c9428f104d27c2f423ade8d45224633e
---
web/htdocs/pagetypes.py | 99 ++++++++++++++++++++++--------------------
web/plugins/sidebar/shipped.py | 2 +-
2 files changed, 53 insertions(+), 48 deletions(-)
diff --git a/web/htdocs/pagetypes.py b/web/htdocs/pagetypes.py
index ee38f3f..c360cfa 100644
--- a/web/htdocs/pagetypes.py
+++ b/web/htdocs/pagetypes.py
@@ -32,16 +32,17 @@
# comment "don't override this". It would be much clearer to split
# this into separate classes.
-import os, inspect
-import config, table, forms, userdb
-from lib import *
-from valuespec import *
+import os
+import json
+
import cmk.store as store
-try:
- import simplejson as json
-except ImportError:
- import json
+import config
+import table
+import forms
+import userdb
+from lib import *
+from valuespec import *
# .--Base----------------------------------------------------------------.
# | ____ |
@@ -83,22 +84,18 @@ class Base(object):
# Implement this function in a subclass in order to add parameters
# to be editable by the user when editing the details of such page
- # type. Note: This method does *not* use overriding, but all methods
- # of this name will be called in all inherited classes and concatenated.
- # Note:
- # - self is the original class, e.g. PageRenderer
- # - cls is the derived class, e.g. GraphCollection
+ # type.
# Returns a list of entries.
# Each entry is a pair of a topic and a list of elements.
# Each element is a triple of order, key and valuespec
# TODO: Add topic here
@classmethod
- def parameters(self, cls):
+ def parameters(cls):
return [ ( _("General Properties"), [
( 1.1, 'name', ID(
title = _('Unique ID'),
help = _("The ID will be used do identify this page in URLs. If this page has the "
- "same ID as a builtin page of the type <i>%s</i> then it will shadow the builtin one.") % self.phrase("title"),
+ "same ID as a builtin page of the type <i>%s</i> then it will shadow the builtin one.") % cls.phrase("title"),
)),
( 1.2, 'title', TextUnicode(
title = _('Title') + '<sup>*</sup>',
@@ -118,28 +115,19 @@ class Base(object):
# in index.py. That way we do not need to hard code page handlers for all types of
# PageTypes in plugins/pages. It is simply sufficient to register a PageType and
# all page handlers will exist :-)
- # Do *not* override this. It collects all page handlers of our
- # page type by calling _page_handlers() for each class
@classmethod
- def page_handlers(self):
- # Collect all page handlers from subclasses
- handlers = {}
- for cls in inspect.getmro(self)[::-1]:
- if "_page_handlers" in cls.__dict__:
- handlers.update(cls._page_handlers(self))
- return handlers
+ def page_handlers(cls):
+ return {}
# Do *not* override this. It collects all editable parameters of our
# page type by calling parameters() for each class
@classmethod
- def collect_parameters(self):
+ def collect_parameters(cls):
topics = {}
- for cls in inspect.getmro(self)[::-1]:
- if "parameters" in cls.__dict__:
- for topic, elements in cls.parameters(self):
- el = topics.setdefault(topic, [])
- el += elements
+ for topic, elements in cls.parameters():
+ el = topics.setdefault(topic, [])
+ el += elements
# Sort topics and elements in the topics
for topic in topics.values():
@@ -323,8 +311,10 @@ class PageRenderer(Base):
# Parameters special for page renderers. These can be added to the sidebar,
# so we need a topic and a checkbox for the visibility
@classmethod
- def parameters(self, cls):
- return [(_("General Properties"), [
+ def parameters(cls):
+ parameters = super(PageRenderer, cls).parameters()
+
+ parameters += [(_("General Properties"), [
( 1.4, 'topic', TextUnicode(
title = _('Topic') + '<sup>*</sup>',
size = 50,
@@ -336,12 +326,16 @@ class PageRenderer(Base):
)),
])]
+ return parameters
+
@classmethod
- def _page_handlers(self, cls):
- return {
+ def page_handlers(cls):
+ handlers = super(PageRenderer, cls).page_handlers()
+ handlers.update({
cls.type_name(): lambda: cls.page_show(),
- }
+ })
+ return handlers
# Most important: page for showing the page ;-)
@@ -413,24 +407,28 @@ class Overridable(Base):
@classmethod
- def parameters(self, cls):
+ def parameters(cls):
+ parameters = super(Overridable, cls).parameters()
+
if cls.has_overriding_permission("publish"):
- return [( _("General Properties"), [
+ parameters += [( _("General Properties"), [
( 2.2, 'public', Checkbox(
title = _("Visibility"),
label = _('Make available for all users')
)),
])]
- else:
- return []
+
+ return parameters
@classmethod
- def _page_handlers(self, cls):
- return {
+ def page_handlers(cls):
+ handlers = super(Overridable, cls).page_handlers()
+ handlers.update({
"%ss" % cls.type_name() : lambda: cls.page_list(),
"edit_%s" % cls.type_name() : lambda: cls.page_edit(),
- }
+ })
+ return handlers
def page_header(self):
@@ -1148,6 +1146,16 @@ class OverridableContainer(Overridable, Container):
html.close_li()
+ @classmethod
+ def page_handlers(cls):
+ handlers = super(OverridableContainer, cls).page_handlers()
+ handlers.update({
+ # Ajax handler for adding elements to a container
+ "ajax_pagetype_add_element": lambda: cls.ajax_add_element()
+ })
+ return handlers
+
+
# Callback for the Javascript function pagetype_add_to_container(). The
# create_info will contain a dictionary that is known to the underlying
# element. Note: this is being called with the base class object Container,
@@ -1229,12 +1237,9 @@ def all_page_types():
# page handler table
def page_handlers():
page_handlers = {}
- for page_type in page_types.values():
- page_handlers.update(page_type.page_handlers())
+ for page_type_class in page_types.values():
+ page_handlers.update(page_type_class.page_handlers())
- # Ajax handler for adding elements to a container
- # TODO: Shouldn't we move that declaration into the class?
- page_handlers["ajax_pagetype_add_element"] = lambda: OverridableContainer.ajax_add_element()
return page_handlers
diff --git a/web/plugins/sidebar/shipped.py b/web/plugins/sidebar/shipped.py
index c1e3ebd..352cb50 100644
--- a/web/plugins/sidebar/shipped.py
+++ b/web/plugins/sidebar/shipped.py
@@ -1307,7 +1307,7 @@ class BookmarkList(pagetypes.Overridable):
@classmethod
- def parameters(self, clazz):
+ def parameters(cls):
vs_topic = TextUnicode(
title = _("Topic") + "<sup>*</sup>",
size = 50,
Module: check_mk
Branch: master
Commit: bf9298248262087424a53b84238b128c78dd36a1
URL: http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=bf929824826208…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed May 24 09:02:49 2017 +0200
Updated notes
Change-Id: Ifd9cac1cc160157ccf183eb7ad6525aed8075190
---
web/htdocs/pagetypes.py | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/web/htdocs/pagetypes.py b/web/htdocs/pagetypes.py
index c360cfa..18a71da 100644
--- a/web/htdocs/pagetypes.py
+++ b/web/htdocs/pagetypes.py
@@ -25,12 +25,18 @@
# Boston, MA 02110-1301 USA.
# TODO:
-# - The classes here mix two things:
+# - The classes here mix two things:
# a) Manager/Container classes
# b) The object classes
# This is done by a lot of classmethods where some have even have a
# comment "don't override this". It would be much clearer to split
# this into separate classes.
+# - The classes are more used as namespaces (lot of classmethods).
+# It would be easier to understand what's happening here when we
+# used real instances. We could - for example - add a single instance
+# per type to the page_types dictionary. Or add some management object
+# for this
+
import os
import json