← Back to team overview

openerp-expert-framework team mailing list archive

Re: a plan for dealing with the many (Magento) custom EAV product attributes

 

On Tuesday 15 March 2011, Olivier Dony wrote:

> > 2) I don't think there is a real need for a full blown EAV pattern  ...
> >Instead of all
> >  having their custom columns, they could rather pull/push their value
> > form/into a a JSON ( http://www.json.org/ )  text element inside a
> > single field of "product.extended"  ...  
> > it's just
> > a custom data bag where we when to store custom attributes in a
> > formatted manner. 
> I'm not 100% comfortable with this approach, but I guess it makes sense.
> 

Which reminds me of the "struct" field, which I wrote, and then stashed away 
because thought "who needs it, anyway?"




-- 
Say NO to spam and viruses. Stop using Microsoft Windows!
diff --git a/bin/osv/fields.py b/bin/osv/fields.py
index 27d9d65..b76e8a6 100644
--- a/bin/osv/fields.py
+++ b/bin/osv/fields.py
@@ -1082,6 +1082,7 @@ class dummy(function):
 # Serialized fields
 # ---------------------------------------------------------
 class serialized(_column):
+    # Does anybody use it still?
     def __init__(self, string='unknown', serialize_func=repr, deserialize_func=eval, type='text', **args):
         self._serialize_func = serialize_func
         self._deserialize_func = deserialize_func
@@ -1091,6 +1092,34 @@ class serialized(_column):
         super(serialized, self).__init__(string=string, **args)
 
 
+try:
+    import json
+    def _symbol_set_struct(val):
+        return json.dumps(val)
+
+    def _symbol_get_struct(val):
+        return json.loads(val)
+except ImportError:
+    def _symbol_set_struct(val):
+        raise NotImplementedError
+        return json.dumps(val)
+
+    def _symbol_get_struct(val):
+        raise NotImplementedError
+        return json.loads(val)
+
+class struct(_column):
+    """ A field able to store an arbitrary python data structure.
+    
+        Note: only plain components allowed.
+    """
+    _type = 'struct'
+
+    _symbol_c = '%s'
+    _symbol_f = _symbol_set
+    _symbol_set = (_symbol_c, _symbol_f)
+    _symbol_get = _symbol_get_struct
+
 # TODO: review completly this class for speed improvement
 class property(function):
 

References