Launchpad automatic translations update.
[odoo/odoo.git] / addons / anonymization / anonymization.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from lxml import etree
24 import os
25 import base64
26 try:
27     import cPickle as pickle
28 except ImportError:
29     import pickle
30 import random
31 import datetime
32 from osv import fields, osv
33 from tools.translate import _
34
35
36 FIELD_STATES = [('clear', 'Clear'), ('anonymized', 'Anonymized'), ('not_existing', 'Not Existing')]
37 ANONYMIZATION_STATES = FIELD_STATES + [('unstable', 'Unstable')]
38 ANONYMIZATION_HISTORY_STATE = [('started', 'Started'), ('done', 'Done'), ('in_exception', 'Exception occured')]
39 ANONYMIZATION_DIRECTION = [('clear -> anonymized', 'clear -> anonymized'), ('anonymized -> clear', 'anonymized -> clear')]
40
41
42 class ir_model_fields_anonymization(osv.osv):
43     _name = 'ir.model.fields.anonymization'
44     _rec_name = 'field_id'
45
46     _columns = {
47         'model_name': fields.char('Object Name', size=128, required=True),
48         'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
49         'field_name': fields.char('Field Name', size=128, required=True),
50         'field_id': fields.many2one('ir.model.fields', 'Field', ondelete='set null'),
51         'state': fields.selection(selection=FIELD_STATES, String='State', required=True, readonly=True),
52     }
53
54     _sql_constraints = [
55         ('model_id_field_id_uniq', 'unique (model_name, field_name)', _("You cannot have two records having the same model and the same field")),
56     ]
57
58     def _get_global_state(self, cr, uid, context=None):
59         ids = self.search(cr, uid, [('state', '<>', 'not_existing')], context=context)
60         fields = self.browse(cr, uid, ids, context=context)
61         if not len(fields) or len(fields) == len([f for f in fields if f.state == 'clear']):
62             state = 'clear' # all fields are clear
63         elif len(fields) == len([f for f in fields if f.state == 'anonymized']):
64             state = 'anonymized' # all fields are anonymized
65         else:
66             state = 'unstable' # fields are mixed: this should be fixed
67         return state
68
69     def _check_write(self, cr, uid, context=None):
70         # check that the field is created from the menu and not from an database update
71         # otherwise the database update can crash:
72         if context.get('manual'):
73             global_state = self._get_global_state(cr, uid, context=context)
74             if global_state == 'anonymized':
75                 raise osv.except_osv('Error !', "The database is currently anonymized, you cannot create, modify or delete fields.")
76             elif global_state == 'unstable':
77                 msg = "The database anonymization is currently in an unstable state. Some fields are anonymized," + \
78                       " while some fields are not anonymized. You should try to solve this problem before trying to create, write or delete fields."
79                 raise osv.except_osv('Error !', msg)
80
81         return True
82
83     def _get_model_and_field_ids(self, cr, uid, vals, context=None):
84         model_and_field_ids = (False, False)
85
86         if 'field_name' in vals and vals['field_name'] and 'model_name' in vals and vals['model_name']:
87             ir_model_fields_obj = self.pool.get('ir.model.fields')
88             ir_model_obj = self.pool.get('ir.model')
89
90             model_ids = ir_model_obj.search(cr, uid, [('model', '=', vals['model_name'])], context=context)
91             if model_ids:
92                 field_ids = ir_model_fields_obj.search(cr, uid, [('name', '=', vals['field_name']), ('model_id', '=', model_ids[0])], context=context)
93                 if field_ids:
94                     field_id = field_ids[0]
95                     model_and_field_ids = (model_ids[0], field_id)
96
97         return model_and_field_ids
98
99     def create(self, cr, uid, vals, context=None):
100         # check field state: all should be clear before we can add a new field to anonymize:
101         self._check_write(cr, uid, context=context)
102
103         if 'field_name' in vals and vals['field_name'] and 'model_name' in vals and vals['model_name']:
104             vals['model_id'], vals['field_id'] = self._get_model_and_field_ids(cr, uid, vals, context=context)
105
106         # check not existing fields:
107         if not vals.get('field_id'):
108             vals['state'] = 'not_existing'
109
110         res = super(ir_model_fields_anonymization, self).create(cr, uid, vals, context=context)
111
112         return res
113
114     def write(self, cr, uid, ids, vals, context=None):
115         # check field state: all should be clear before we can modify a field:
116         if not (len(vals.keys()) == 1 and vals.get('state') == 'clear'):
117             self._check_write(cr, uid, context=context)
118
119         if 'field_name' in vals and vals['field_name'] and 'model_name' in vals and vals['model_name']:
120             vals['model_id'], vals['field_id'] = self._get_model_and_field_ids(cr, uid, vals, context=context)
121
122         # check not existing fields:
123         if 'field_id' in vals:
124             if not vals.get('field_id'):
125                 vals['state'] = 'not_existing'
126             else:
127                 global_state = self._get_global_state(cr, uid, context)
128                 if global_state != 'unstable':
129                     vals['state'] = global_state
130
131         res = super(ir_model_fields_anonymization, self).write(cr, uid, ids, vals, context=context)
132
133         return res
134
135     def unlink(self, cr, uid, ids, context=None):
136         # check field state: all should be clear before we can unlink a field:
137         self._check_write(cr, uid, context=context)
138
139         res = super(ir_model_fields_anonymization, self).unlink(cr, uid, ids, context=context)
140         return res
141
142     def onchange_model_id(self, cr, uid, ids, model_id, context=None):
143         res = {'value': {
144                     'field_name': False,
145                     'field_id': False,
146                     'model_name': False,
147               }}
148
149         if model_id:
150             ir_model_obj = self.pool.get('ir.model')
151             model_ids = ir_model_obj.search(cr, uid, [('id', '=', model_id)])
152             model_id = model_ids and model_ids[0] or None
153             model_name = model_id and ir_model_obj.browse(cr, uid, model_id).model or False
154             res['value']['model_name'] = model_name
155
156         return res
157
158     def onchange_model_name(self, cr, uid, ids, model_name, context=None):
159         res = {'value': {
160                     'field_name': False,
161                     'field_id': False,
162                     'model_id': False,
163               }}
164
165         if model_name:
166             ir_model_obj = self.pool.get('ir.model')
167             model_ids = ir_model_obj.search(cr, uid, [('model', '=', model_name)])
168             model_id = model_ids and model_ids[0] or False
169             res['value']['model_id'] = model_id
170
171         return res
172
173     def onchange_field_name(self, cr, uid, ids, field_name, model_name):
174         res = {'value': {
175                 'field_id': False,
176             }}
177
178         if field_name and model_name:
179             ir_model_fields_obj = self.pool.get('ir.model.fields')
180             field_ids = ir_model_fields_obj.search(cr, uid, [('name', '=', field_name), ('model', '=', model_name)])
181             field_id = field_ids and field_ids[0] or False
182             res['value']['field_id'] = field_id
183
184         return res
185
186     def onchange_field_id(self, cr, uid, ids, field_id, model_name):
187         res = {'value': {
188                     'field_name': False,
189               }}
190
191         if field_id:
192             ir_model_fields_obj = self.pool.get('ir.model.fields')
193             field = ir_model_fields_obj.browse(cr, uid, field_id)
194             res['value']['field_name'] = field.name
195
196         return res
197
198     _defaults = {
199         'state': lambda *a: 'clear',
200     }
201
202 ir_model_fields_anonymization()
203
204
205 class ir_model_fields_anonymization_history(osv.osv):
206     _name = 'ir.model.fields.anonymization.history'
207     _order = "date desc"
208
209     _columns = {
210         'date': fields.datetime('Date', required=True, readonly=True),
211         'field_ids': fields.many2many('ir.model.fields.anonymization', 'anonymized_field_to_history_rel', 'field_id', 'history_id', 'Fields', readonly=True),
212         'state': fields.selection(selection=ANONYMIZATION_HISTORY_STATE, string='State', required=True, readonly=True),
213         'direction': fields.selection(selection=ANONYMIZATION_DIRECTION, string='Direction', required=True, readonly=True),
214         'msg': fields.text('Message', readonly=True),
215         'filepath': fields.char(string='File path', size=256, readonly=True),
216     }
217
218 ir_model_fields_anonymization_history()
219
220
221 class ir_model_fields_anonymize_wizard(osv.osv_memory):
222     _name = 'ir.model.fields.anonymize.wizard'
223
224     def _get_state(self, cr, uid, ids, name, arg, context=None):
225         res = {}
226
227         state = self._get_state_value(cr, uid, context=None)
228         for id in ids:
229             res[id] = state
230
231         return res
232
233     def _get_summary(self, cr, uid, ids, name, arg, context=None):
234         res = {}
235         summary = self._get_summary_value(cr, uid, context)
236         for id in ids:
237             res[id] = summary
238
239         return res
240
241     _columns = {
242         'name': fields.char(size='64', string='File Name'),
243         'summary': fields.function(_get_summary, method=True, type='text', string='Summary'),
244         'file_export': fields.binary(string='Export'),
245         'file_import': fields.binary(string='Import'),
246         'state': fields.function(_get_state, method=True, string='State', type='selection', selection=ANONYMIZATION_STATES, readonly=False),
247         'msg': fields.text(string='Message'),
248     }
249
250     def _get_state_value(self, cr, uid, context=None):
251         state = self.pool.get('ir.model.fields.anonymization')._get_global_state(cr, uid, context=context)
252         return state
253
254     def _get_summary_value(self, cr, uid, context=None):
255         summary = u''
256         anon_field_obj = self.pool.get('ir.model.fields.anonymization')
257         ir_model_fields_obj = self.pool.get('ir.model.fields')
258
259         anon_field_ids = anon_field_obj.search(cr, uid, [('state', '<>', 'not_existing')], context=context)
260         anon_fields = anon_field_obj.browse(cr, uid, anon_field_ids, context=context)
261
262         field_ids = [anon_field.field_id.id for anon_field in anon_fields if anon_field.field_id]
263         fields = ir_model_fields_obj.browse(cr, uid, field_ids, context=context)
264
265         fields_by_id = dict([(f.id, f) for f in fields])
266
267         for anon_field in anon_fields:
268             field = fields_by_id.get(anon_field.field_id.id)
269
270             values = {
271                 'model_name': field.model_id.name,
272                 'model_code': field.model_id.model,
273                 'field_code': field.name,
274                 'field_name': field.field_description,
275                 'state': anon_field.state,
276             }
277             summary += u" * %(model_name)s (%(model_code)s) -> %(field_name)s (%(field_code)s): state: (%(state)s)\n" % values
278
279         return summary
280
281     def default_get(self, cr, uid, fields_list, context=None):
282         res = {}
283         res['name'] = '.pickle'
284         res['summary'] = self._get_summary_value(cr, uid, context)
285         res['state'] = self._get_state_value(cr, uid, context)
286         res['msg'] = """Before executing the anonymization process, you should make a backup of your database."""
287
288         return res
289
290     def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, *args, **kwargs):
291         state = self.pool.get('ir.model.fields.anonymization')._get_global_state(cr, uid, context=context)
292         step = context.get('step', 'new_window')
293
294         res = super(ir_model_fields_anonymize_wizard, self).fields_view_get(cr, uid, view_id, view_type, context, *args, **kwargs)
295
296         eview = etree.fromstring(res['arch'])
297         placeholder = eview.xpath("group[@name='placeholder1']")
298         placeholder = len(placeholder) and placeholder[0] or None
299
300         if placeholder:
301             if step == 'new_window' and state == 'clear':
302                 # clicked in the menu and the fields are not anonymized: warn the admin that backuping the db is very important
303                 placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
304                 placeholder.addnext(etree.Element('newline'))
305                 placeholder.addnext(etree.Element('label', {'string': 'Warning'}))
306                 eview.remove(placeholder)
307             elif step == 'new_window' and state == 'anonymized':
308                 # clicked in the menu and the fields are already anonymized
309                 placeholder.addnext(etree.Element('newline'))
310                 placeholder.addnext(etree.Element('field', {'name': 'file_import', 'required': "1"}))
311                 eview.remove(placeholder)
312             elif step == 'just_anonymized':
313                 # we just ran the anonymization process, we need the file export field
314                 placeholder.addnext(etree.Element('newline'))
315                 placeholder.addnext(etree.Element('field', {'name': 'file_export'}))
316                 # we need to remove the button:
317                 buttons = eview.xpath("button")
318                 for button in buttons:
319                     eview.remove(button)
320                 # and add a message:
321                 placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
322                 placeholder.addnext(etree.Element('newline'))
323                 placeholder.addnext(etree.Element('label', {'string': 'Result'}))
324                 # remove the placeholer:
325                 eview.remove(placeholder)
326             elif step == 'just_desanonymized':
327                 # we just reversed the anonymization process, we don't need any field
328                 # we need to remove the button
329                 buttons = eview.xpath("button")
330                 for button in buttons:
331                     eview.remove(button)
332                 # and add a message
333                 # and add a message:
334                 placeholder.addnext(etree.Element('field', {'name': 'msg', 'colspan': '4', 'nolabel': '1'}))
335                 placeholder.addnext(etree.Element('newline'))
336                 placeholder.addnext(etree.Element('label', {'string': 'Result'}))
337                 # remove the placeholer:
338                 eview.remove(placeholder)
339             else:
340                 # unstable ?
341                 raise
342
343             res['arch'] = etree.tostring(eview)
344
345         return res
346
347     def _raise_after_history_update(self, cr, uid, history_id, error_type, error_msg):
348         self.pool.get('ir.model.fields.anonymization.history').write(cr, uid, history_id, {
349             'state': 'in_exception',
350             'msg': error_msg,
351         })
352         raise osv.except_osv(error_type, error_msg)
353
354     def anonymize_database(self,cr, uid, ids, context=None):
355         """Sets the 'anonymized' state to defined fields"""
356
357         # create a new history record:
358         anonymization_history_model = self.pool.get('ir.model.fields.anonymization.history')
359
360         vals = {
361             'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
362             'state': 'started',
363             'direction': 'clear -> anonymized',
364         }
365         history_id = anonymization_history_model.create(cr, uid, vals)
366
367         # check that all the defined fields are in the 'clear' state
368         state = self.pool.get('ir.model.fields.anonymization')._get_global_state(cr, uid, context=context)
369         if state == 'anonymized':
370             self._raise_after_history_update(cr, uid, history_id, 'Error !', "The database is currently anonymized, you cannot anonymize it again.")
371         elif state == 'unstable':
372             msg = "The database anonymization is currently in an unstable state. Some fields are anonymized," + \
373                   " while some fields are not anonymized. You should try to solve this problem before trying to do anything."
374             self._raise_after_history_update(cr, uid, history_id, 'Error !', msg)
375
376         # do the anonymization:
377         dirpath = os.environ.get('HOME') or os.getcwd()
378         rel_filepath = 'field_anonymization_%s_%s.pickle' % (cr.dbname, history_id)
379         abs_filepath = os.path.abspath(os.path.join(dirpath, rel_filepath))
380
381         ir_model_fields_anonymization_model = self.pool.get('ir.model.fields.anonymization')
382         field_ids = ir_model_fields_anonymization_model.search(cr, uid, [('state', '<>', 'not_existing')], context=context)
383         fields = ir_model_fields_anonymization_model.browse(cr, uid, field_ids, context=context)
384
385         if not fields:
386             msg = "No fields are going to be anonymized."
387             self._raise_after_history_update(cr, uid, history_id, 'Error !', msg)
388
389         data = []
390
391         for field in fields:
392             model_name = field.model_id.model
393             field_name = field.field_id.name
394             field_type = field.field_id.ttype
395             table_name = self.pool.get(model_name)._table
396
397             # get the current value
398             sql = "select id, %s from %s" % (field_name, table_name)
399             cr.execute(sql)
400             records = cr.dictfetchall()
401             for record in records:
402                 data.append({"model_id": model_name, "field_id": field_name, "id": record['id'], "value": record[field_name]})
403
404                 # anonymize the value:
405                 anonymized_value = None
406
407                 sid = str(record['id'])
408                 if field_type == 'char':
409                     anonymized_value = 'xxx'+sid
410                 elif field_type == 'selection':
411                     anonymized_value = 'xxx'+sid
412                 elif field_type == 'text':
413                     anonymized_value = 'xxx'+sid
414                 elif field_type == 'boolean':
415                     anonymized_value = random.choice([True, False])
416                 elif field_type == 'date':
417                     anonymized_value = '2011-11-11'
418                 elif field_type == 'datetime':
419                     anonymized_value = '2011-11-11 11:11:11'
420                 elif field_type == 'float':
421                     if record[field_name] > 0:
422                         anonymized_value = 1.0
423                     elif record[field_name] < 0:
424                         anonymized_value = -1.0
425                     else:
426                         anonymized_value = 0.0
427                 elif field_type == 'integer':
428                     anonymized_value = 1
429                 elif field_type in ['binary', 'many2many', 'many2one', 'one2many', 'reference']: # cannot anonymize these kind of fields
430                     msg = "Cannot anonymize fields of these types: binary, many2many, many2one, one2many, reference"
431                     self._raise_after_history_update(cr, uid, history_id, 'Error !', msg)
432
433                 if anonymized_value is None:
434                     self._raise_after_history_update(cr, uid, history_id, 'Error !', "Anonymized value is None. This cannot happens.")
435
436                 sql = "update %(table)s set %(field)s = %%(anonymized_value)s where id = %%(id)s" % {
437                     'table': table_name,
438                     'field': field_name,
439                 }
440                 cr.execute(sql, {
441                     'anonymized_value': anonymized_value,
442                     'id': record['id']
443                 })
444
445         # save pickle:
446         fn = open(abs_filepath, 'w')
447         pickle.dump(data, fn, pickle.HIGHEST_PROTOCOL)
448
449         # update the anonymization fields:
450         values = {
451             'state': 'anonymized',
452         }
453         ir_model_fields_anonymization_model.write(cr, uid, field_ids, values, context=context)
454
455         # add a result message in the wizard:
456         msgs = ["Anonymization successful.",
457                "",
458                "Don't forget to save the resulting file to a safe place because you will not be able to revert the anonymization without this file.",
459                "",
460                "This file is also stored in the %s directory. The absolute file path is: %s",
461               ]
462         msg = '\n'.join(msgs) % (dirpath, abs_filepath)
463
464         fn = open(abs_filepath, 'r')
465
466         self.write(cr, uid, ids, {
467             'msg': msg,
468             'file_export': base64.encodestring(fn.read()),
469         })
470         fn.close()
471
472         # update the history record:
473         anonymization_history_model.write(cr, uid, history_id, {
474             'field_ids': [[6, 0, field_ids]],
475             'msg': msg,
476             'filepath': abs_filepath,
477             'state': 'done',
478         })
479
480         # handle the view:
481         view_id = self._id_get(cr, uid, 'ir.ui.view', 'view_ir_model_fields_anonymize_wizard_form', 'anonymization')
482
483         return {
484                 'res_id': ids[0],
485                 'view_id': [view_id],
486                 'view_type': 'form',
487                 "view_mode": 'form',
488                 'res_model': 'ir.model.fields.anonymize.wizard',
489                 'type': 'ir.actions.act_window',
490                 'context': {'step': 'just_anonymized'},
491                 'target':'new',
492         }
493
494     def reverse_anonymize_database(self,cr, uid, ids, context=None):
495         """Set the 'clear' state to defined fields"""
496
497         ir_model_fields_anonymization_model = self.pool.get('ir.model.fields.anonymization')
498         anonymization_history_model = self.pool.get('ir.model.fields.anonymization.history')
499
500         # create a new history record:
501         vals = {
502             'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
503             'state': 'started',
504             'direction': 'anonymized -> clear',
505         }
506         history_id = anonymization_history_model.create(cr, uid, vals)
507
508         # check that all the defined fields are in the 'anonymized' state
509         state = ir_model_fields_anonymization_model._get_global_state(cr, uid, context=context)
510         if state == 'clear':
511             raise osv.except_osv('Error !', "The database is not currently anonymized, you cannot reverse the anonymization.")
512         elif state == 'unstable':
513             msg = "The database anonymization is currently in an unstable state. Some fields are anonymized," + \
514                   " while some fields are not anonymized. You should try to solve this problem before trying to do anything."
515             raise osv.except_osv('Error !', msg)
516
517         wizards = self.browse(cr, uid, ids, context=context)
518         for wizard in wizards:
519             if not wizard.file_import:
520                 msg = "The anonymization export file was not supplied. It is not possible to reverse the anonymization process without this file."
521                 self._raise_after_history_update(cr, uid, history_id, 'Error !', msg)
522
523             # reverse the anonymization:
524             # load the pickle file content into a data structure:
525             data = pickle.loads(base64.decodestring(wizard.file_import))
526
527             for line in data:
528                 table_name = self.pool.get(line['model_id'])._table
529                 sql = "update %(table)s set %(field)s = %%(value)s where id = %%(id)s" % {
530                     'table': table_name,
531                     'field': line['field_id'],
532                 }
533                 cr.execute(sql, {
534                     'value': line['value'],
535                     'id': line['id']
536                 })
537
538             # update the anonymization fields:
539             ir_model_fields_anonymization_model = self.pool.get('ir.model.fields.anonymization')
540             field_ids = ir_model_fields_anonymization_model.search(cr, uid, [('state', '<>', 'not_existing')], context=context)
541             values = {
542                 'state': 'clear',
543             }
544             ir_model_fields_anonymization_model.write(cr, uid, field_ids, values, context=context)
545
546             # add a result message in the wizard:
547             msg = '\n'.join(["Successfully reversed the anonymization.",
548                              "",
549                             ])
550
551             self.write(cr, uid, ids, {'msg': msg})
552
553             # update the history record:
554             anonymization_history_model.write(cr, uid, history_id, {
555                 'field_ids': [[6, 0, field_ids]],
556                 'msg': msg,
557                 'filepath': False,
558                 'state': 'done',
559             })
560
561             # handle the view:
562             view_id = self._id_get(cr, uid, 'ir.ui.view', 'view_ir_model_fields_anonymize_wizard_form', 'anonymization')
563
564             return {
565                     'res_id': ids[0],
566                     'view_id': [view_id],
567                     'view_type': 'form',
568                     "view_mode": 'form',
569                     'res_model': 'ir.model.fields.anonymize.wizard',
570                     'type': 'ir.actions.act_window',
571                     'context': {'step': 'just_desanonymized'},
572                     'target':'new',
573             }
574
575     def _id_get(self, cr, uid, model, id_str, mod):
576         if '.' in id_str:
577             mod, id_str = id_str.split('.')
578         try:
579             idn = self.pool.get('ir.model.data')._get_id(cr, uid, mod, id_str)
580             res = int(self.pool.get('ir.model.data').read(cr, uid, [idn], ['res_id'])[0]['res_id'])
581         except:
582             res = None
583         return res
584
585 ir_model_fields_anonymize_wizard()
586