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 #------------------------------------------------------