Commits

Albert Cervera i Areny (NaN·tic) committed 89c1a00

Convert create, write, read, delete, copy, on_change, getters and setters into classmethods.

Comments (0)

Files changed (2)

2.6/01-active-record.py

 
 def ids_to_records(line):
     line = re.sub(r'self.browse\(ids\)', r'records', line)
+    line = re.sub(r'cls.browse\(ids\)', r'records', line)
     line = re.sub(r'(\W)ids(\W)', r'\1records\2', line)
     return line
 
     text = text.split('\n')
     return ['%s\n' % x for x in text] 
 
+def values_to_self(line):
+    line = re.sub(r"vals.get\('(\w+)'\)", r'self.\1', line)
+    line = re.sub(r"values.get\('(\w+)'\)", r'self.\1', line)
+    line = re.sub(r"vals\['(\w+)'\]", r'self.\1', line)
+    line = re.sub(r"values\['(\w+)'\]", r'self.\1', line)
+    line = re.sub(r"vals.get\('(\w+)',(.*?)\)", r'self.\1 or \2', line)
+    line = re.sub(r"values.get\('(\w+)',(.*?)\)", r'self.\1 or \2', line)
+    line = line.replace(', values', '')
+    line = line.replace(', vals', '')
+    line = re.sub(r'\w+\.browse\((self\..*?)\)', r'\1', line)
+    return line
+
 def add_before_after_imports(original_content, before=None, after=None):
     if before is None:
         before = []
                     }
             classes.append(current_class_info)
         f = get_function_name(line)
-        if f:
+        # If ident != 4 it means it's an embedded function
+        if f and get_ident_count(line) == 4:
             is_function_definition = True
             current_function = f
 
             g = m.group(1)
             line = line.replace('%s_obj' % g, camelcase(g))
 
+        # Classmethod for create, write, read, delete
+        if is_function_definition and current_function in ('create', 'write',
+                'read', 'delete', 'copy'):
+            add_classmethod(result, line)
+        if current_function in ('create', 'write', 'read', 'delete', 'copy'):
+            line = ids_to_records(line)
+            line = self_to_cls(line)
+
         # Convert search_rec_name to classmethod
         if is_function_definition and current_function == 'search_rec_name':
             add_classmethod(result, line)
             add_classmethod(result, line)
         if current_function and current_function.startswith('check_'):
             line = ids_to_records(line)
+            line = self_to_cls(line)
+
+        if current_function and current_function.startswith('on_change_'):
+            line = values_to_self(line)
+
+        if current_function and current_function.startswith('autocomplete'):
+            line = values_to_self(line)
+
+        # Getter and Setter into classmethods
+        if current_function and (current_function.startswith('get_') 
+                or current_function.startswith('set_')):
+            if is_function_definition:
+                add_classmethod(result, line)
+            line = ids_to_records(line)
+            line = self_to_cls(line)
+
+        # Module-specific changes
+        if current_function and current_function in ('explode_bom'):
+            line = values_to_self(line)
 
         result.append(line)
 
     classes = module['classes']
     model_classes = [x['name'] for x in classes if 'Wizard' not in x['parents']]
     wizard_classes = [x['name'] for x in classes if 'Wizard' in x['parents']]
+    report_classes = [x['name'] for x in classes if 'Report' in x['parents']]
     after = ''
     if model_classes:
         after += (
             ',\n'
             "        module='%s', type_='wizard')\n" % module['name']
             )
+    if report_classes:
+        after += (
+            '    Pool.register(\n'
+            '        ' + 
+            ',\n        '.join(report_classes) +
+            ',\n'
+            "        module='%s', type_='report')\n" % module['name']
+            )
     if after:
         after = '\n\ndef register():\n' + after
     after = after.splitlines(True)

2.6/01-active-record.rst

 
 - ** Replace init by __register__
 - ** Replace default_xxx method by classmethod or staticmethod (depending if
-  it uses self/cls)
-- Change constraint methods into classmethod with records as input or
+  it uses self/cls) => SHOULD USE @classmethod if function uses 'self' anywhere
+- ** Change constraint methods into classmethod with records as input or
   instance method
 - Change on_change, on_change_with, autocomplete into instance method
   without values
-- Change getter into:
+- ** Change getter into: => CONVERTS ALL FUNCTIONS STARTING WITH 'get_' into classmethod
     - instance method that just return value
     - classmethod with records that return a dict with values
-- Change setter into classmethod with records
+- ** Change setter into classmethod with records => CONVERTS ALL FUNCTIONS STARTING WITH 'set_' into classmethod
 
 - ** Rename xxx_obj into Xxx
 - browse takes only list of ids, for int just instanciate the Model
 - create return a instance
-- write, delete are classmethod with list of instance and return nothing
+- ** write, delete are classmethod with list of instance and return nothing 
+    => DOES NOT REMOVE 'return'
+    => DOES NOT CONVERT INTO LIST-ONLY
 - read takes only list of ids, for int use this: value, = cls.read([id])
-- copy is a classmethod that takes only a list of instance and return a
-  list of instance
+- ** copy is a classmethod that takes only a list of instance and return a
+  list of instance 
+    => DOES NOT CONVERT INTO LIST-ONLY
 - ** Replace session from wizard method by self
 - Use PoolMeta for extending Model
 - Cache method is replaced by a Cache instance on the class