forked from osbzr/gooderp_addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_data.py
61 lines (50 loc) · 1.97 KB
/
clean_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import UserError
class BusinessDataTable(models.Model):
_name = 'business.data.table'
_description = u'业务数据表'
model = fields.Many2one('ir.model', u'需要清理的表')
name = fields.Char(u'业务数据表名', required=True)
clean_business_id = fields.Many2one(
'clean.business.data', string=u'清理数据对象')
company_id = fields.Many2one(
'res.company',
string=u'公司',
change_default=True,
default=lambda self: self.env['res.company']._company_default_get())
@api.onchange('model')
def onchange_model(self):
self.name = self.model and self.model.model
class CleanBusinessData(models.Model):
_name = 'clean.business.data'
_description = u'清理记录'
@api.model
def _get_business_table_name(self):
return self._get_business_table_name_impl()
@api.model
def _get_business_table_name_impl(self):
'''
默认取business.data.table 里的所有业务数据表清理
'''
return self.env['business.data.table'].search([])
need_clean_table = fields.One2many('business.data.table', 'clean_business_id',
default=_get_business_table_name,
string='要清理的业务数据表')
company_id = fields.Many2one(
'res.company',
string=u'公司',
change_default=True,
default=lambda self: self.env['res.company']._company_default_get())
@api.multi
def remove_data(self):
try:
for line in self.need_clean_table:
obj_name = line.name
obj = self.env[obj_name]
if obj._table_exist:
sql = "TRUNCATE TABLE %s CASCADE " % obj._table
self.env.cr.execute(sql)
except Exception, e:
raise UserError(e)
return True