Skip to content

Commit 9d3fe03

Browse files
committed
feat(edi_core_oca): add backend-controlled auto-cleanup for exchange records
Add configurable auto-archiving and auto-deletion of EDI exchange records per backend. This backend-specific settings that can be managed individually. Changes: - Add "Auto Cleanup" tab in backend form view for easy configuration - Add archive cron job that respect backend-specific retention policies - Add delete cron job that respect backend-specific retention policies - Add "Archived" filter to exchange record search view Benefits: - Each backend can have different retention policies - Setting fields to 0 (default value) disables the respective cleanup behavior - More flexible data retention management - Better control over storage and performance optimization The cron jobs iterate through backends with configured retention settings, applying appropriate cutoff values.
1 parent 62eba93 commit 9d3fe03

File tree

7 files changed

+83
-1
lines changed

7 files changed

+83
-1
lines changed

edi_core_oca/__manifest__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"data/ir_actions_server.xml",
3030
"data/sequence.xml",
3131
"data/edi_configuration.xml",
32+
"data/ir_cron_archive_old_edi_records.xml",
33+
"data/ir_cron_delete_old_archived_edi_records.xml",
3234
"security/res_groups.xml",
3335
"security/ir_model_access.xml",
3436
"views/edi_backend_views.xml",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record id="ir_cron_archive_old_edi_records" model="ir.cron">
4+
<field name="name">Archive Old EDI Exchange Records</field>
5+
<field name="model_id" ref="edi_core_oca.model_edi_exchange_record" />
6+
<field name="state">code</field>
7+
<field name="code">
8+
# Archive old EDI exchange records based on backend configuration
9+
backends = env['edi.backend'].search([
10+
('auto_archive_records_after_days', '&gt;', 0)
11+
])
12+
for backend in backends:
13+
cutoff_date = datetime.datetime.now() - datetime.timedelta(days=backend.auto_archive_records_after_days)
14+
records = model.search([
15+
('backend_id', '=', backend.id),
16+
('create_date', '&lt;', cutoff_date),
17+
('active', '=', True)
18+
], limit=10000, order="create_date asc")
19+
if records:
20+
records.action_archive()
21+
</field>
22+
<field name="interval_number">1</field>
23+
<field name="interval_type">days</field>
24+
<field name="active" eval="True" />
25+
<field name="priority" eval="10" />
26+
</record>
27+
</odoo>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo noupdate="1">
3+
<record id="ir_cron_delete_old_archived_edi_records" model="ir.cron">
4+
<field name="name">Delete Old Archived EDI Exchange Records</field>
5+
<field name="model_id" ref="edi_core_oca.model_edi_exchange_record" />
6+
<field name="state">code</field>
7+
<field name="code">
8+
# Delete old archived EDI exchange records based on backend configuration
9+
backends = env['edi.backend'].search([
10+
('auto_delete_records_after_days', '&gt;', 0)
11+
])
12+
for backend in backends:
13+
cutoff_date = datetime.datetime.now() - datetime.timedelta(days=backend.auto_delete_records_after_days)
14+
records = model.search([
15+
('backend_id', '=', backend.id),
16+
('create_date', '&lt;', cutoff_date),
17+
('active', '=', False)
18+
], limit=100, order="create_date asc")
19+
if records:
20+
records.unlink()
21+
</field>
22+
<field name="interval_number">3</field>
23+
<field name="interval_type">hours</field>
24+
<field name="active" eval="True" />
25+
<field name="priority" eval="10" />
26+
</record>
27+
</odoo>

edi_core_oca/models/edi_backend.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ class EDIBackend(models.Model):
6262
)
6363
active = fields.Boolean(default=True)
6464
company_id = fields.Many2one("res.company", string="Company")
65+
auto_archive_records_after_days = fields.Integer(
66+
string="Auto-archive records after (days)",
67+
default=0,
68+
help="Automatically archive EDI exchange records after X days. "
69+
"Set to 0 to disable auto-archiving.",
70+
)
71+
auto_delete_records_after_days = fields.Integer(
72+
string="Auto-delete archived records after (days)",
73+
default=0,
74+
help="Automatically delete archived EDI exchange records after X days. "
75+
"Set to 0 to disable auto-deletion.",
76+
)
6577

6678
@property
6779
def exchange_record_model(self):

edi_core_oca/models/edi_exchange_record.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class EDIExchangeRecord(models.Model):
142142
help="The record state can be rolled back manually in case of failure.",
143143
)
144144
company_id = fields.Many2one("res.company", string="Company")
145+
active = fields.Boolean(default=True)
145146

146147
_sql_constraints = [
147148
("identifier_uniq", "unique(identifier)", "The identifier must be unique."),

edi_core_oca/views/edi_backend_views.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@
5757
<field name="company_id" groups="base.group_multi_company" />
5858
</group>
5959
<!-- Hook to add more config -->
60-
<notebook />
60+
<notebook>
61+
<page name="auto_cleanup" string="Auto Cleanup">
62+
<group>
63+
<field name="auto_archive_records_after_days" />
64+
<field name="auto_delete_records_after_days" />
65+
</group>
66+
</page>
67+
</notebook>
6168
</sheet>
6269
</form>
6370
</field>

edi_core_oca/views/edi_exchange_record_views.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@
265265
help="Show all records created in the last 7 days"
266266
/>
267267
<separator />
268+
<filter
269+
string="Archived"
270+
name="filter_archived"
271+
domain="[('active', '=', False)]"
272+
/>
273+
<separator />
268274
<field name="backend_id" />
269275
<field name="type_id" />
270276
<field name="res_id" />

0 commit comments

Comments
 (0)