[MERGE] orm: use directly lxml.etree to generate default views.
authorVo Minh Thu <vmt@openerp.com>
Tue, 4 Oct 2011 09:28:28 +0000 (11:28 +0200)
committerVo Minh Thu <vmt@openerp.com>
Tue, 4 Oct 2011 09:28:28 +0000 (11:28 +0200)
bzr revid: vmt@openerp.com-20111004092828-79hhaiwgw4dltimd

1  2 
openerp/osv/orm.py

@@@ -1727,13 -1731,69 +1728,69 @@@ class BaseModel(object)
                  raise except_orm('View error', msg)
          return arch, fields
  
-     def __get_default_calendar_view(self):
-         """Generate a default calendar view (For internal use only).
+     def _get_default_form_view(self, cr, user, context=None):
+         """ Generates a default single-line form view using all fields
+         of the current model except the m2m and o2m ones.
+         :param cr: database cursor
+         :param int user: user id
+         :param dict context: connection context
+         :returns: a form view as an lxml document
+         :rtype: etree._Element
          """
-         # TODO could return an etree instead of a string
+         view = etree.Element('form', string=self._description)
+         # TODO it seems fields_get can be replaced by _all_columns (no need for translation)
+         for field, descriptor in self.fields_get(cr, user, context=context).iteritems():
+             if descriptor['type'] in ('one2many', 'many2many'):
+                 continue
+             etree.SubElement(view, 'field', name=field)
+             if descriptor['type'] == 'text':
+                 etree.SubElement(view, 'newline')
+         return view
  
-         arch = ('<?xml version="1.0" encoding="utf-8"?>\n'
-                 '<calendar string="%s"') % (self._description)
+     def _get_default_tree_view(self, cr, user, context=None):
+         """ Generates a single-field tree view, using _rec_name if
+         it's one of the columns or the first column it finds otherwise
+         :param cr: database cursor
+         :param int user: user id
+         :param dict context: connection context
+         :returns: a tree view as an lxml document
+         :rtype: etree._Element
+         """
+         _rec_name = self._rec_name
+         if _rec_name not in self._columns:
+             _rec_name = self._columns.keys()[0]
+         view = etree.Element('tree', string=self._description)
+         etree.SubElement(view, 'field', name=_rec_name)
+         return view
+     def _get_default_calendar_view(self, cr, user, context=None):
+         """ Generates a default calendar view by trying to infer
+         calendar fields from a number of pre-set attribute names
+         
+         :param cr: database cursor
+         :param int user: user id
+         :param dict context: connection context
+         :returns: a calendar view
+         :rtype: etree._Element
+         """
+         def set_first_of(seq, in_, to):
 -            """Sets the first value of ``set`` also found in ``in`` to
++            """Sets the first value of ``seq`` also found in ``in_`` to
+             the ``to`` attribute of the view being closed over.
+             Returns whether it's found a suitable value (and set it on
+             the attribute) or not
+             """
+             for item in seq:
+                 if item in in_:
 -                    view.set(key, item)
++                    view.set(to, item)
+                     return True
+             return False
+         view = etree.Element('calendar', string=self._description)
+         etree.SubElement(view, 'field', name=self._rec_name)
  
          if (self._date_name not in self._columns):
              date_found = False