-
Notifications
You must be signed in to change notification settings - Fork 7
/
partner_rm_image.py
51 lines (47 loc) · 1.59 KB
/
partner_rm_image.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
# If you want avoid creating new images you should avoid the following method:
# https://github.com/odoo/odoo/blob/ba23c9f354/odoo/addons/base/res/res_partner.py#L284-L310
# If you want to know how much bytes will be free after run the script
"""
SELECT count(*), pg_size_pretty(SUM(file_size))
FROM (
SELECT checksum, MAX(file_size) AS size
FROM ir_attachment
WHERE res_model = 'res.partner'
AND res_field IS NOT NULL
GROUP BY checksum
HAVING count(1) > (
SELECT count(1)
FROM res_partner
WHERE parent_id IS NOT NULL
GROUP BY parent_id
ORDER BY count(1) DESC
LIMIT 1)
) vw
INNER JOIN ir_attachment a
ON a.checksum = vw.checksum
"""
# Odoo uses same image for children like parent
# So, it is checking what is the max image repeated
env.cr.execute("""
SELECT count(1)
FROM res_partner
WHERE parent_id IS NOT NULL
GROUP BY parent_id
ORDER BY count(1) DESC
LIMIT 1""")
max_img_repeated = env.cr.fetchone()[0] + 1
# Get all partner using duplicated images checksum from partner.image* fields
env.cr.execute("""
SELECT DISTINCT ON (res_id) res_id AS partner_id
FROM ir_attachment
WHERE checksum IN (
SELECT checksum
FROM ir_attachment
WHERE res_model = 'res.partner'
AND res_field IS NOT NULL
GROUP BY checksum, res_field
HAVING count(1) > %s
)
""", (max_img_repeated,))
partner_ids = [partner_id[0] for partner_id in env.cr.fetchall()]
env['res.partner'].browse(partner_ids).write({'image': False})