From fcea61b6ebce561743bfe3876dd5a705ce6a7bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 7 Mar 2013 14:25:17 +0100 Subject: [PATCH] [IMP] mail: added a cron job to delete mail attachments (lost user input or reports generated on-the-fly). They have: res_model = mail.compose.message; res_id 0; more than one day of inactivity. bzr revid: tde@openerp.com-20130307132517-4xivu90dzn3o744a --- addons/mail/data/mail_data.xml | 13 +++++++++++++ addons/mail/mail_thread.py | 20 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/addons/mail/data/mail_data.xml b/addons/mail/data/mail_data.xml index 3f517ee7858..02cfd626768 100644 --- a/addons/mail/data/mail_data.xml +++ b/addons/mail/data/mail_data.xml @@ -27,6 +27,19 @@ 1000 + + Garbage Collect Mail Attachments + + + 1 + weeks + -1 + + mail.thread + _garbage_collect_attachments + () + + Discussions diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 173ec951b32..2d43b93dd9a 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -389,6 +389,26 @@ class mail_thread(osv.AbstractModel): return [('message_unread', '=', True)] return [] + def _garbage_collect_attachments(self, cr, uid, context=None): + """ Garbage collect lost mail attachments. Those are attachments + - linked to res_model 'mail.compose.message', the composer wizard + - with res_id 0, because they were created outside of an existing + wizard (typically user input through Chatter or reports + created on-the-fly by the templates) + - unused since at least one day (create_date and write_date) + """ + limit_date = datetime.datetime.utcnow() - datetime.timedelta(days=1) + limit_date_str = datetime.datetime.strftime(limit_date, tools.DEFAULT_SERVER_DATETIME_FORMAT) + ir_attachment_obj = self.pool.get('ir.attachment') + attach_ids = ir_attachment_obj.search(cr, uid, [ + ('res_model', '=', 'mail.compose.message'), + ('res_id', '=', 0), + ('create_date', '<', limit_date_str), + ('write_date', '<', limit_date_str), + ], context=context) + ir_attachment_obj.unlink(cr, uid, attach_ids, context=context) + return True + #------------------------------------------------------ # Email specific #------------------------------------------------------