Browse Source

[11.0][MIG] mail_cleanup: Migrate to 11.0

12.0-mig-module_prototyper_last
Artem Kostyuk 6 years ago
committed by jcoux
parent
commit
873a757227
  1. 73
      mail_cleanup/README.rst
  2. 14
      mail_cleanup/__manifest__.py
  3. 16
      mail_cleanup/__openerp__.py
  4. 2
      mail_cleanup/models/__init__.py
  5. 108
      mail_cleanup/models/fetchmail_server.py
  6. 9
      mail_cleanup/readme/CONFIGURE.rst
  7. 2
      mail_cleanup/readme/CONTRIBUTORS.rst
  8. 12
      mail_cleanup/readme/DESCRIPTION.rst
  9. 2
      mail_cleanup/views/mail_view.xml

73
mail_cleanup/README.rst

@ -1,22 +1,48 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
============ ============
Mail cleanup Mail cleanup
============ ============
.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/11.0/mail_cleanup
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-11-0/server-tools-11-0-mail_cleanup
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/149/11.0
:alt: Try me on Runbot
|badge1| |badge2| |badge3| |badge4| |badge5|
This module allows to: This module allows to:
----------------------
* mark e-mails older than x days as read, * mark e-mails older than x days as read,
* move those messages in a specific folder, * move those messages in a specific folder,
* remove messages older than x days * remove messages older than x days
on IMAP servers, just before fetching them.
on IMAP servers, just before fetching them.
Since the main "mail" module does not mark unroutable e-mails as read, Since the main "mail" module does not mark unroutable e-mails as read,
this means that if junk mail arrives in the catch-all address without this means that if junk mail arrives in the catch-all address without
any default route, fetching newer e-mails will happen after re-parsing any default route, fetching newer e-mails will happen after re-parsing
those unroutable e-mails. those unroutable e-mails.
**Table of contents**
.. contents::
:local:
Configuration Configuration
============= =============
@ -25,45 +51,48 @@ per server.
Example of a configuration file (add those values to your server):: Example of a configuration file (add those values to your server)::
[incoming_mail.openerp_imap_mail1]
cleanup_days = False # default value
purge_days = False # default value
cleanup_folder = NotParsed # optional parameter
Known issues / Roadmap
======================
* None
[incoming_mail.openerp_imap_mail1]
cleanup_days = False # default value
purge_days = False # default value
cleanup_folder = NotParsed # optional parameter
Bug Tracker Bug Tracker
=========== ===========
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_. Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported. In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
`here <https://github.com/OCA/server-tools/issues/new?body=module:%20mail_cleanup%0Aversion:%209.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20mail_cleanup%0Aversion:%2011.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Do not contact contributors directly about support or help with technical issues.
Credits Credits
======= =======
Authors
~~~~~~~
* Camptocamp
Contributors Contributors
------------
~~~~~~~~~~~~
* Matthieu Dietrich <matthieu.dietrich@camptocamp.com> * Matthieu Dietrich <matthieu.dietrich@camptocamp.com>
* Artem Kostyuk <a.kostyuk@mobilunity.com>
Maintainers
~~~~~~~~~~~
Maintainer
----------
This module is maintained by the OCA.
.. image:: https://odoo-community.org/logo.png .. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association :alt: Odoo Community Association
:target: https://odoo-community.org :target: https://odoo-community.org
This module is maintained by the OCA.
OCA, or the Odoo Community Association, is a nonprofit organization whose OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and mission is to support the collaborative development of Odoo features and
promote its widespread use. promote its widespread use.
To contribute to this module, please visit https://odoo-community.org.
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/11.0/mail_cleanup>`_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

14
mail_cleanup/__manifest__.py

@ -0,0 +1,14 @@
# Copyright 2015-2016 Matthieu Dietrich (Camptocamp SA)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
'name': 'Mail cleanup',
'version': '11.0.1.0.0',
'category': 'Tools',
'summary': 'Mark as read or delete mails after a set time',
'author': 'Camptocamp, '
'Odoo Community Association (OCA)',
'license': 'AGPL-3',
'website': 'https://github.com/OCA/server-tools',
'depends': ['mail_environment'],
'data': ['views/mail_view.xml'],
}

16
mail_cleanup/__openerp__.py

@ -1,16 +0,0 @@
# -*- coding: utf-8 -*-
# © 2015-2016 Matthieu Dietrich (Camptocamp SA)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Mail cleanup',
'version': '9.0.1.0.0',
'category': 'Tools',
'summary': 'Mark as read or delete mails after a set time',
'author': "Camptocamp,Odoo Community Association (OCA)",
'license': 'AGPL-3',
'website': 'https://odoo-community.org',
'depends': ['mail_environment'],
'data': ['views/mail_view.xml'],
'installable': True,
}

2
mail_cleanup/models/__init__.py

@ -1 +1 @@
from . import mail_cleanup
from . import fetchmail_server

108
mail_cleanup/models/mail_cleanup.py → mail_cleanup/models/fetchmail_server.py

@ -1,64 +1,49 @@
# -*- coding: utf-8 -*-
# © 2015-2016 Matthieu Dietrich (Camptocamp SA)
# Copyright 2015-2016 Matthieu Dietrich (Camptocamp SA)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging import logging
import datetime import datetime
from openerp import api, fields, models
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from openerp.addons.server_environment import serv_config
from odoo import api, fields, models
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
class FetchmailServer(models.Model): class FetchmailServer(models.Model):
"""Incoming POP/IMAP mail server account"""
"""Incoming POP/IMAP mail server account."""
_inherit = 'fetchmail.server' _inherit = 'fetchmail.server'
cleanup_days = fields.Integer( cleanup_days = fields.Integer(
compute='_get_cleanup_conf',
string='Expiration days', string='Expiration days',
help="Number of days before marking an e-mail as read")
help="Number of days before marking an e-mail as read",
)
cleanup_folder = fields.Char( cleanup_folder = fields.Char(
compute='_get_cleanup_conf',
string='Expiration folder', string='Expiration folder',
help="Folder where an e-mail marked as read will be moved.")
help="Folder where an e-mail marked as read will be moved.",
)
purge_days = fields.Integer( purge_days = fields.Integer(
compute='_get_cleanup_conf',
string='Deletion days', string='Deletion days',
help="Number of days before removing an e-mail")
@api.multi
def _get_cleanup_conf(self):
"""
Return configuration
"""
for fetchmail in self:
global_section_name = 'incoming_mail'
# default vals
config_vals = {'cleanup_days': False,
'purge_days': False,
'cleanup_folder': False}
if serv_config.has_section(global_section_name):
config_vals.update(serv_config.items(global_section_name))
custom_section_name = '.'.join((global_section_name,
fetchmail.name))
if serv_config.has_section(custom_section_name):
config_vals.update(serv_config.items(custom_section_name))
# convert string values to integer
if config_vals['cleanup_days']:
config_vals['cleanup_days'] = int(config_vals['cleanup_days'])
if config_vals['purge_days']:
config_vals['purge_days'] = int(config_vals['purge_days'])
for field in ['cleanup_days', 'purge_days', 'cleanup_folder']:
fetchmail[field] = config_vals[field]
help="Number of days before removing an e-mail",
)
@property
def _server_env_fields(self):
base_fields = super()._server_env_fields
mail_cleanup_fields = {
'cleanup_days': {
'getter': 'getint',
},
'purge_days': {
'getter': 'getint',
},
'cleanup_folder': {},
}
mail_cleanup_fields.update(base_fields)
return mail_cleanup_fields
def _cleanup_fetchmail_server(self, server, imap_server): def _cleanup_fetchmail_server(self, server, imap_server):
count, failed = 0, 0 count, failed = 0, 0
@ -78,13 +63,15 @@ class FetchmailServer(models.Model):
if result[0] == 'OK': if result[0] == 'OK':
imap_server.store(num, '+FLAGS', '\\Deleted') imap_server.store(num, '+FLAGS', '\\Deleted')
except Exception: except Exception:
_logger.exception('Failed to cleanup mail from %s server %s.',
server.type, server.name)
_logger.exception(
'Failed to cleanup mail from %s server %s.',
server.type, server.name)
failed += 1 failed += 1
count += 1 count += 1
_logger.info("Marked %d email(s) as read on %s server %s; "
"%d succeeded, %d failed.", count, server.type,
server.name, (count - failed), failed)
_logger.info(
'Marked %d email(s) as read on %s server %s;'
' %d succeeded, %d failed.', count, server.type,
server.name, (count - failed), failed)
def _purge_fetchmail_server(self, server, imap_server): def _purge_fetchmail_server(self, server, imap_server):
# Purging e-mails older than the purge date, if available # Purging e-mails older than the purge date, if available
@ -99,25 +86,29 @@ class FetchmailServer(models.Model):
# Delete message # Delete message
imap_server.store(num, '+FLAGS', '\\Deleted') imap_server.store(num, '+FLAGS', '\\Deleted')
except Exception: except Exception:
_logger.exception('Failed to remove mail from %s server %s.',
server.type, server.name)
_logger.exception(
'Failed to remove mail from %s server %s.',
server.type, server.name)
failed += 1 failed += 1
count += 1 count += 1
_logger.info("Removed %d email(s) on %s server %s; "
"%d succeeded, %d failed.", count, server.type,
server.name, (count - failed), failed)
_logger.info(
'Removed %d email(s) on %s server %s;'
' %d succeeded, %d failed.', count, server.type,
server.name, (count - failed), failed)
@api.multi @api.multi
def fetch_mail(self): def fetch_mail(self):
""" Called before the fetch, in order to clean up
right before retrieving emails. """
# Called before the fetch, in order to clean up right before
# retrieving emails.
context = self.env.context.copy() context = self.env.context.copy()
context['fetchmail_cron_running'] = True context['fetchmail_cron_running'] = True
for server in self: for server in self:
_logger.info('start cleaning up emails on %s server %s', _logger.info('start cleaning up emails on %s server %s',
server.type, server.name) server.type, server.name)
context.update({'fetchmail_server_id': server.id,
'server_type': server.type})
context.update({
'fetchmail_server_id': server.id,
'server_type': server.type,
})
imap_server = False imap_server = False
if server.type == 'imap': if server.type == 'imap':
try: try:
@ -130,11 +121,12 @@ class FetchmailServer(models.Model):
# flagged as deleted # flagged as deleted
imap_server.expunge() imap_server.expunge()
except Exception: except Exception:
_logger.exception("General failure when trying to cleanup "
"mail from %s server %s.",
server.type, server.name)
_logger.exception(
'General failure when trying to cleanup'
' mail from %s server %s.',
server.type, server.name)
finally: finally:
if imap_server: if imap_server:
imap_server.close() imap_server.close()
imap_server.logout() imap_server.logout()
return super(FetchmailServer, self).fetch_mail()
return super().fetch_mail()

9
mail_cleanup/readme/CONFIGURE.rst

@ -0,0 +1,9 @@
This module depends on ``mail_environment`` in order to add "expiration dates"
per server.
Example of a configuration file (add those values to your server)::
[incoming_mail.openerp_imap_mail1]
cleanup_days = False # default value
purge_days = False # default value
cleanup_folder = NotParsed # optional parameter

2
mail_cleanup/readme/CONTRIBUTORS.rst

@ -0,0 +1,2 @@
* Matthieu Dietrich <matthieu.dietrich@camptocamp.com>
* Artem Kostyuk <a.kostyuk@mobilunity.com>

12
mail_cleanup/readme/DESCRIPTION.rst

@ -0,0 +1,12 @@
This module allows to:
----------------------
* mark e-mails older than x days as read,
* move those messages in a specific folder,
* remove messages older than x days
on IMAP servers, just before fetching them.
Since the main "mail" module does not mark unroutable e-mails as read,
this means that if junk mail arrives in the catch-all address without
any default route, fetching newer e-mails will happen after re-parsing
those unroutable e-mails.

2
mail_cleanup/views/mail_view.xml

@ -5,7 +5,7 @@
<field name="name">inherit_fetchmail_for cleanup</field> <field name="name">inherit_fetchmail_for cleanup</field>
<field name="model">fetchmail.server</field> <field name="model">fetchmail.server</field>
<!--parent python entity --> <!--parent python entity -->
<field name="inherit_id" ref="mail_environment.inherit_fetchmail"/>
<field name="inherit_id" ref="fetchmail.view_email_server_form"/>
<!-- modulename.view --> <!-- modulename.view -->
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="is_ssl" position="after"> <field name="is_ssl" position="after">

Loading…
Cancel
Save