<commit_before># Copyright (C) 2019 Akretion
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import logging

import odoo
from odoo import api, models
from odoo.tools.safe_eval import safe_eval
import datetime

_logger = logging.getLogger(__name__)


class AutovacuumMixin(models.AbstractModel):
    _name = "autovacuum.mixin"
    _description = "Mixin used to delete messages or attachments"

    @api.multi
    def batch_unlink(self):
        with api.Environment.manage():
            with odoo.registry(
                    self.env.cr.dbname).cursor() as new_cr:
                new_env = api.Environment(new_cr, self.env.uid,
                                          self.env.context)
                try:
                    while self:
                        batch_delete = self[0:1000]
                        self -= batch_delete
                        # do not attach new env to self because it may be
                        # huge, and the cache is cleaned after each unlink
                        # so we do not want to much record is the env in
                        # which we call unlink because odoo would prefetch
                        # fields, cleared right after.
                        batch_delete.with_env(new_env).unlink()
                        new_env.cr.commit()
                except Exception as e:
                    _logger.exception(
                        "Failed to delete Ms : %s" % (self._name, str(e)))

    # Call by cron
    @api.model
    def autovacuum(self, ttype='message'):
        rules = self.env['vacuum.rule'].search([('ttype', '=', ttype)])
        for rule in rules:
            records = rule._search_autovacuum_records()
            records.batch_unlink()

    def _get_autovacuum_domain(self, rule):
        return []

    def _get_autovacuum_records(self, rule):<commit_msg>[FIX] autovacuum_message_attachment : wrong message formating<commit_after># Copyright (C) 2019 Akretion
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

import logging

import odoo
from odoo import api, models
from odoo.tools.safe_eval import safe_eval
import datetime

_logger = logging.getLogger(__name__)


class AutovacuumMixin(models.AbstractModel):
    _name = "autovacuum.mixin"
    _description = "Mixin used to delete messages or attachments"

    @api.multi
    def batch_unlink(self):
        with api.Environment.manage():
            with odoo.registry(
                    self.env.cr.dbname).cursor() as new_cr:
                new_env = api.Environment(new_cr, self.env.uid,
                                          self.env.context)
                try:
                    while self:
                        batch_delete = self[0:1000]
                        self -= batch_delete
                        # do not attach new env to self because it may be
                        # huge, and the cache is cleaned after each unlink
                        # so we do not want to much record is the env in
                        # which we call unlink because odoo would prefetch
                        # fields, cleared right after.
                        batch_delete.with_env(new_env).unlink()
                        new_env.cr.commit()
                except Exception as e:
                    _logger.exception(
                        "Failed to delete Ms : %s - %s" % (self._name, str(e)))

    # Call by cron
    @api.model
    def autovacuum(self, ttype='message'):
        rules = self.env['vacuum.rule'].search([('ttype', '=', ttype)])
        for rule in rules:
            records = rule._search_autovacuum_records()
            records.batch_unlink()

    def _get_autovacuum_domain(self, rule):
        return []

    def _get_autovacuum_records(self, rule):
{'fraction_non_alphanumeric': 0.0848981750859561, 'fraction_numerical': 0.0058185665167944985, 'mean_word_length': 1.6652572233967582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '933101', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1007, 'n_words': 288}