~camptocamp/server-env-tools/pending-merge-7.0

« back to all changes in this revision

Viewing changes to mass_editing/wizard/mass_editing_wizard.py

  • Committer: Nhomar Hernandez
  • Date: 2013-06-09 17:39:04 UTC
  • mfrom: (32.2.9 mass_editing_7.0)
  • Revision ID: nhomar@gmail.com-20130609173904-iwssbsimwh4avjty
[MERGE] Added mass_editing module, thanks to Serpentcs Extraordinary Job

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    This module uses OpenERP, Open Source Management Solution Framework.
 
5
#    Copyright (C) 2012-Today Serpent Consulting Services (<http://www.serpentcs.com>)
 
6
#
 
7
#    This program is free software: you can redistribute it and/or modify
 
8
#    it under the terms of the GNU General Public License as published by
 
9
#    the Free Software Foundation, either version 3 of the License, or
 
10
#    (at your option) any later version.
 
11
#
 
12
#    This program is distributed in the hope that it will be useful,
 
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#    GNU General Public License for more details.
 
16
#
 
17
#    You should have received a copy of the GNU General Public License
 
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>
 
19
#
 
20
##############################################################################
 
21
 
 
22
from openerp.osv import orm, fields
 
23
import openerp.tools as tools
 
24
from lxml import etree
 
25
 
 
26
class mass_editing_wizard(orm.TransientModel):
 
27
    _name = 'mass.editing.wizard'
 
28
 
 
29
    _columns = {
 
30
    }
 
31
 
 
32
    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
 
33
        result = super(mass_editing_wizard, self).fields_view_get(cr, uid, view_id, view_type, context, toolbar, submenu)
 
34
        if context.get('mass_editing_object'):
 
35
            mass_object = self.pool.get('mass.object') 
 
36
            editing_data = mass_object.browse(cr, uid, context.get('mass_editing_object'), context)
 
37
            all_fields = {}
 
38
            xml_form = etree.Element('form', {'string': tools.ustr(editing_data.name), 'version':'7.0'})
 
39
            xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
 
40
            etree.SubElement(xml_group, 'label', {'string': '', 'colspan': '2'})
 
41
            xml_group = etree.SubElement(xml_form, 'group', {'colspan': '4'})
 
42
            model_obj = self.pool.get(context.get('active_model'))
 
43
            field_info = model_obj.fields_get(cr, uid, [], context)
 
44
            for field in editing_data.field_ids:
 
45
                if field.ttype == "many2many":
 
46
                    all_fields[field.name] = field_info[field.name] 
 
47
                    all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove_m2m', 'Remove'), ('add', 'Add')]}
 
48
                    xml_group = etree.SubElement(xml_group, 'group', {'colspan': '4'})
 
49
                    etree.SubElement(xml_group, 'separator', {'string': field_info[field.name]['string'], 'colspan': '2'})
 
50
                    etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
 
51
                    etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove_m2m')]}"})
 
52
                elif field.ttype == "many2one":
 
53
                    all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
 
54
                    all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'relation': field.relation}
 
55
                    etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
 
56
                    etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
 
57
                elif field.ttype == "char":
 
58
                    all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
 
59
                    all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'size': field.size or 256}
 
60
                    etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2', 'colspan':'2'})
 
61
                    etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan':'2'})
 
62
                elif field.ttype == 'selection':
 
63
                    all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
 
64
                    etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan':'2'})
 
65
                    etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'colspan':'2', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
 
66
                    all_fields[field.name] = {'type':field.ttype, 'string': field.field_description, 'selection': field_info[field.name]['selection']}
 
67
                else:
 
68
                    all_fields[field.name] = {'type':field.ttype, 'string': field.field_description}
 
69
                    all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
 
70
                    if field.ttype == 'text':
 
71
                        xml_group = etree.SubElement(xml_group, 'group', {'colspan': '6'})
 
72
                        etree.SubElement(xml_group, 'separator', {'string': all_fields[field.name]['string'], 'colspan': '2'})
 
73
                        etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', 'nolabel':'1'})
 
74
                        etree.SubElement(xml_group, 'field', {'name': field.name, 'colspan':'4', 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}"})
 
75
                    else:
 
76
                        all_fields["selection__" + field.name] = {'type':'selection', 'string': field_info[field.name]['string'], 'selection':[('set', 'Set'), ('remove', 'Remove')]}
 
77
                        etree.SubElement(xml_group, 'field', {'name': "selection__" + field.name, 'colspan': '2', })
 
78
                        etree.SubElement(xml_group, 'field', {'name': field.name, 'nolabel':'1', 'attrs':"{'invisible':[('selection__" + field.name + "','=','remove')]}", 'colspan': '2', })
 
79
            etree.SubElement(xml_form, 'separator', {'string' : '', 'colspan': '4'})
 
80
            xml_group3 = etree.SubElement(xml_form, 'footer', {})
 
81
            etree.SubElement(xml_group3, 'button', {'string' :'Close', 'icon': "gtk-close", 'special' :'cancel'})
 
82
            etree.SubElement(xml_group3, 'button', {'string' :'Apply', 'icon': "gtk-execute", 'type' :'object', 'name':"action_apply"})
 
83
            root = xml_form.getroottree()
 
84
            result['arch'] = etree.tostring(root)
 
85
            result['fields'] = all_fields
 
86
        return result
 
87
 
 
88
    def create(self, cr, uid, vals, context=None):
 
89
        if context.get('active_model') and context.get('active_ids'):
 
90
            model_obj = self.pool.get(context.get('active_model'))
 
91
            dict = {}
 
92
            for key , val in vals.items():
 
93
                if key.startswith('selection__'):
 
94
                    split_key = key.split('__', 1)[1]
 
95
                    if val == 'set':
 
96
                        dict.update({split_key: vals.get(split_key, False)})
 
97
                    elif val == 'remove':
 
98
                        dict.update({split_key: False})
 
99
                    elif val == 'remove_m2m':
 
100
                        dict.update({split_key: [(3, id) for id in vals.get(split_key, False)[0][2]]})
 
101
                    elif val == 'add':
 
102
                        m2m_list = []
 
103
                        for m2m_id in vals.get(split_key, False)[0][2]:
 
104
                            m2m_list.append((4, m2m_id))
 
105
                        dict.update({split_key: m2m_list})
 
106
            if dict:
 
107
                model_obj.write(cr, uid, context.get('active_ids'), dict, context)
 
108
        result = super(mass_editing_wizard, self).create(cr, uid, {}, context)
 
109
        return result
 
110
 
 
111
    def action_apply(self, cr, uid, ids, context=None):
 
112
        return  {'type': 'ir.actions.act_window_close'}
 
113
 
 
114
mass_editing_wizard()
 
115
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: