librelist archives

« back to archive

[PATCH 0 of 3] Bunch of patches

[PATCH 0 of 3] Bunch of patches

From:
Mikhail Krivushin
Date:
2010-08-17 @ 22:07
Patches to admin site, description and initialization.

[PATCH 1 of 3] return setting value

From:
Mikhail Krivushin
Date:
2010-08-17 @ 22:07
# HG changeset patch
# User Mikhail Krivushin <krivushinme@gmail.com>
# Date 1282082521 -14400
# Node ID 2a7529a84af77cb9ce2274d559b17e882f0d31f5
# Parent  3cc736fce1029efb7a05ef5a4034106be9ca77db
return setting value

diff -r 3cc736fce102 -r 2a7529a84af7 svarga/apps/__init__.py
--- a/svarga/apps/__init__.py	Fri Aug 13 10:35:12 2010 +0300
+++ b/svarga/apps/__init__.py	Wed Aug 18 02:02:01 2010 +0400
@@ -85,6 +85,9 @@
             raise ValueError('Use only uppercase names as settings keys')
         if not hasattr(self.env_class.settings, key):
             setattr(self.env_class.settings, key, value)
+            return value
+        else:
+            return getattr(self.env_class.settings, key)
 
     def add_apps_init(self, handler):
         self.apps_init.append(handler)

[PATCH 2 of 3] Put current node as link

From:
Mikhail Krivushin
Date:
2010-08-17 @ 22:07
# HG changeset patch
# User Mikhail Krivushin <krivushinme@gmail.com>
# Date 1282082560 -14400
# Node ID 7766825dec00429e374ede30de28cc3c32c171e9
# Parent  2a7529a84af77cb9ce2274d559b17e882f0d31f5
Put current node as link

diff -r 2a7529a84af7 -r 7766825dec00 svarga/contrib/admin/templates/lib.html
--- a/svarga/contrib/admin/templates/lib.html	Wed Aug 18 02:02:01 2010 +0400
+++ b/svarga/contrib/admin/templates/lib.html	Wed Aug 18 02:02:40 2010 +0400
@@ -3,7 +3,7 @@
      {% for n in node.childs %}
      {% set is_current = child and (n.name == env.admin_view.Meta.menu[depth]) %}
      <div class={% if is_current %}"selected item"{% else %}"item"{% endif %}>
-       {% if is_current or not n.view %}
+       {% if not n.view %}
          {{ n.name }}
        {% else %}
          <a href="{{ n.view.reverse() }}">{{ n.name }}</a>

[PATCH 3 of 3] Add descriptions and modify modelform

From:
Mikhail Krivushin
Date:
2010-08-17 @ 22:07
# HG changeset patch
# User Mikhail Krivushin <krivushinme@gmail.com>
# Date 1282082667 -14400
# Node ID 595cd631b2db51a244051d9ca0e5d0807364af83
# Parent  7766825dec00429e374ede30de28cc3c32c171e9
Add descriptions and modify modelform

diff -r 7766825dec00 -r 595cd631b2db svarga/core/description.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/svarga/core/description.py	Wed Aug 18 02:04:27 2010 +0400
@@ -0,0 +1,55 @@
+from copy import deepcopy
+
+class MetaDescription(type):
+    def __new__(cls, classname, bases, fields):
+        fs = {}
+        meta = None
+        for k, v in fields.iteritems():
+            if isinstance(v, dict)  or k == 'get_desc':
+                fs[k] = v
+            elif k.startswith('__'):
+                continue
+            elif k == 'Meta':
+                meta = v
+            else:
+                fs[k] = dict((k, v) for k, v in v.__dict__.items()
+                        if not k.startswith('_'))
+              
+        desc_type = type.__new__(cls, classname, bases, fs)
+
+        target = getattr(meta, 'target', None)
+        if target:
+            if not hasattr(target, 'Meta'):
+                target.Meta = type('Meta', (object,), {})
+            target.Meta._description = desc_type
+        # else: type is not descript any object, but can be an ancestor
+
+        return desc_type
+
+
+class Description(object):
+    __metaclass__ = MetaDescription
+
+    @classmethod
+    def get_desc(cls, attr_name, desc_name, default=None):
+        if hasattr(cls, attr_name):
+            return getattr(cls, attr_name, {}).get(desc_name, default)
+        else:
+            return default
+
+
+def get_description(target):
+    if not hasattr(target, 'Meta'):
+        target.Meta = type('Meta', (object,), {})
+
+    if not hasattr(target.Meta, '_description'):
+        desc = Description()
+        for base in target.mro():
+            d = getattr( getattr(base, 'Meta', None), '_description', None)
+            if d:
+                desc = d
+                break
+        target.Meta._description = deepcopy(desc)
+    return target.Meta._description
+
+
diff -r 7766825dec00 -r 595cd631b2db svarga/forms/model.py
--- a/svarga/forms/model.py	Wed Aug 18 02:02:40 2010 +0400
+++ b/svarga/forms/model.py	Wed Aug 18 02:04:27 2010 +0400
@@ -4,6 +4,7 @@
 from wtforms.widgets import html_params
 
 from svarga.core.metadata import create_metadata
+from svarga.core.description import get_description
 from svarga.utils.imports import import_attribute
 
 __all__ = ['ModelForm', 'ForeignKeyField', 'MultiForeignKeyField']
@@ -240,8 +241,8 @@
 
                 if not f.foreign_key and not f.primary_key and n not in fields:
                     # Try locating field label in model's Meta
-                    label = getattr(model.Meta, 'description', {}
-                                    ).get(n, f.name)
+                    label = get_description(model).get_desc(f.name,
+                                    'verbose_name', f.name)
 
                     # Create new field
                     new_field = converter.convert(f, label)