first commit

This commit is contained in:
Suherdy Yacob 2025-12-29 13:49:37 +07:00
commit 4d98f20cc2
9 changed files with 122 additions and 0 deletions

1
__init__.py Normal file
View File

@ -0,0 +1 @@
from . import wizard

16
__manifest__.py Normal file
View File

@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
{
'name': 'Survey Attachment Cleanup',
'version': '18.0.1.0.0',
'summary': 'Batch delete survey image attachments',
'category': 'Tools',
'author': 'Suherdy Yacob',
'depends': ['base', 'survey', 'website'],
'data': [
'security/ir.model.access.csv',
'wizard/sh_survey_attachment_cleanup_wizard_views.xml',
],
'installable': True,
'application': False,
'license': 'LGPL-3',
}

Binary file not shown.

View File

@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_sh_survey_attachment_cleanup_wizard,sh.survey.attachment.cleanup.wizard,model_sh_survey_attachment_cleanup_wizard,base.group_user,1,1,1,1
1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_sh_survey_attachment_cleanup_wizard sh.survey.attachment.cleanup.wizard model_sh_survey_attachment_cleanup_wizard base.group_user 1 1 1 1

1
wizard/__init__.py Normal file
View File

@ -0,0 +1 @@
from . import sh_survey_attachment_cleanup_wizard

Binary file not shown.

View File

@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
from odoo import api, fields, models
class SurveyAttachmentCleanupWizard(models.TransientModel):
_name = 'sh.survey.attachment.cleanup.wizard'
_description = 'Survey Attachment Cleanup Wizard'
website_id = fields.Many2one('website', string="Website")
limit = fields.Integer(string="Batch Limit", default=0, help="Limit number of deletions per batch (0 for no limit)")
attachment_count = fields.Integer(string="Attachments Found", compute='_compute_attachment_count')
@api.depends('website_id')
def _compute_attachment_count(self):
for record in self:
domain = [
('answer_type', '=', 'upload_file'),
]
# Find all relevant lines first
lines = self.env['survey.user_input.line'].search(domain)
total_count = 0
# Iterate to count attachments
# NOTE: this might be slow for huge datasets, but accurate given the m2m structure
# To optimize, we could do SQL, but ORM is safer for now given < 15k records
# We can't easily search ir.attachment directly because the relationship is via m2m table
# `survey_user_input_line_rel` (or similar, depending on define).
# Actually, `value_file_data_ids` is Many2many('ir.attachment')
# Let's collect IDs first (limit to reasonable number if performance issue?)
# But count needs to be accurate-ish.
# Filter attachments
# We search ir.attachment directly to find ALL matching files, including orphans.
# Criteria: Mimetype JPEG, Website Matches, No Res Model (typical for M2M/Orphans)
domain = [
('mimetype', '=', 'image/jpeg'),
('res_model', '=', False),
('res_field', '=', False),
]
if record.website_id:
domain.append(('website_id', '=', record.website_id.id))
# Count directly
count = self.env['ir.attachment'].search_count(domain)
record.attachment_count = count
def action_delete_attachments(self):
self.ensure_one()
domain = [
('mimetype', '=', 'image/jpeg'),
('res_model', '=', False),
('res_field', '=', False),
]
if self.website_id:
domain.append(('website_id', '=', self.website_id.id))
# Search with limit if needed
limit = self.limit if self.limit > 0 else None
attachments_to_delete = self.env['ir.attachment'].search(domain, limit=limit)
return attachments_to_delete.unlink()

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_sh_survey_attachment_cleanup_wizard_form" model="ir.ui.view">
<field name="name">sh.survey.attachment.cleanup.wizard.form</field>
<field name="model">sh.survey.attachment.cleanup.wizard</field>
<field name="arch" type="xml">
<form string="Survey Attachment Cleanup">
<group>
<field name="website_id" placeholder="Leave empty for all websites"/>
<field name="limit"/>
<field name="attachment_count" readonly="1"/>
</group>
<footer>
<button string="Delete Attachments" name="action_delete_attachments" type="object" class="btn-danger" confirm="Are you sure you want to delete these attachments?"/>
<button string="Cancel" class="btn-secondary" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_sh_survey_attachment_cleanup_wizard" model="ir.actions.act_window">
<field name="name">Survey Attachment Cleanup</field>
<field name="res_model">sh.survey.attachment.cleanup.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem id="menu_sh_survey_attachment_cleanup"
name="Attachment Cleanup"
parent="survey.menu_surveys"
action="action_sh_survey_attachment_cleanup_wizard"
sequence="100"/>
</odoo>