Commits

xiaq committed 67d4007

Metadata display helpers

A new MoinMoin.schema package is created, in the hope that Flatland widgets
and Whoosh schema helpers can be integrated later.

  • Participants
  • Parent commits 78ed0ce

Comments (0)

Files changed (4)

File MoinMoin/constants/display_widgets.py

+# Copyright: 2013 MoinMoin:CheerXiao
+# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
+
+"""
+MoinMoin - display widget names
+
+:const:`DISPLAY_*` here refer to widgets defined in templates/display_widgets.html.
+"""
+
+
+from __future__ import absolute_import, division
+
+
+DISPLAY_PLAIN = 'plain'
+DISPLAY_TAGS = 'tags'
+DISPLAY_REFERENCE = 'reference'

File MoinMoin/schema/__init__.py

+# Copyright: 2013 MoinMoin:CheerXiao
+# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
+
+"""
+MoinMoin - schema helpers
+
+This package contains helpers for dealing with sets of metadata fields
+associated with certain itemtypes, or borrowing relation database terms, the
+"schema" of that itemtype.
+
+Although the metadata of items is stored in JSON and thus schema-free, various
+information about metadata fields is often useful, e.g. how a certain field
+should be displayed in the index, or edited in the metadata editor. The Whoosh
+indexer also needs a schema.
+
+Currently this package only has helpers for displaying metadata, but eventually
+MoinMoin/forms.py should belong here too. There might be support for a more
+dynamic Whoosh schema too (which is now hardcoded in
+MoinMoin/storage/middleware/indexing.py).
+"""

File MoinMoin/schema/display.py

+# Copyright: 2013 MoinMoin:CheerXiao
+# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
+
+"""
+MoinMoin - helpers for displaying metadata fields
+"""
+
+
+from __future__ import absolute_import, division
+
+from collections import namedtuple
+
+
+class DisplayField(namedtuple('DisplayField', 'name label widget')):
+    """
+    Information about displaying a metadata field. Useful on the item page or
+    index pages.
+
+    The `widget` attribute should be one of the constants defined in
+    :mod:`MoinMoin.constants.display_widgets`, referring to one of the widget
+    in templates/display_widgets.html.
+    """

File MoinMoin/templates/display_widgets.html

+{#
+Implementation of metadata field displaying widgets.
+#}
+
+{% macro plain(value) %}
+  {{ value }}
+{% endmacro %}
+
+{% macro tags(tags) %}
+  <ul class="moin-tags">
+    {% for t in tags %}
+      <li>
+        {{ t }}
+      </li>
+    {% endfor %}
+  </ul>
+{% endmacro %}
+
+{% macro reference(itemid) %}
+  {% if itemid %}
+    {% set name = storage.get_item(itemid=itemid).name %}
+    <a href="{{ url_for_item(name) }}"> {{ name }} </a>
+  {% else %}
+    {{ _('(None)') }}
+  {% endif %}
+{% endmacro %}
+
+{% macro render(value, widget) %}
+  {%- set macro = {
+    'plain': plain,
+    'tags': tags,
+    'reference': reference,
+  }[widget] or undefined -%}
+  {% if macro is defined %}
+    {{ macro(value, *varargs, **kwargs) }}
+  {% else %}
+    STUB: widget {{ '%r'|format(widget) }} is not implemented. The value is {{ '%r'|format(value) }}.
+  {% endif %}
+{% endmacro %}