# Path: post_office/utils.py
# def cleanup_expired_mails(cutoff_date, delete_attachments=True, batch_size=1000):
#     """
#     Delete all emails before the given cutoff date.
#     Optionally also delete pending attachments.
#     Return the number of deleted emails and attachments.
#     """
#     expired_emails_ids = Email.objects.filter(created__lt=cutoff_date).values_list('id', flat=True)
#     email_id_batches = split_emails(expired_emails_ids, batch_size)
#     total_deleted_emails = 0
#     
#     for email_ids in email_id_batches:
#         # Delete email and incr total_deleted_emails counter
#         _, deleted_data = Email.objects.filter(id__in=email_ids).delete()
#         if deleted_data:
#             total_deleted_emails += deleted_data['post_office.Email']
# 
#     if delete_attachments:
#         attachments = Attachment.objects.filter(emails=None)
#         for attachment in attachments:
#             # Delete the actual file
#             attachment.file.delete()
#         attachments_count, _ = attachments.delete()
#     else:
#         attachments_count = 0
# 
#     return total_deleted_emails, attachments_count


import datetime
from django.core.management.base import BaseCommand
from django.utils.timezone import now
from ...utils import cleanup_expired_mails




class Command(BaseCommand):
    help = 'Place deferred messages back in the queue.'

    def add_arguments(self, parser):
        parser.add_argument('-d', '--days',
                            type=int, default=90,
                            help="Cleanup mails older than this many days, defaults to 90.")

        parser.add_argument('-da', '--delete-attachments', action='store_true',
                            help="Delete orphaned attachments.")

        parser.add_argument('-b', '--batch-size', type=int, default=1000, help="Batch size for cleanup.")

    def handle(self, verbosity, days, delete_attachments, batch_size, **options):
        # Delete mails and their related logs and queued created before X days
        cutoff_date = now() - datetime.timedelta(days)
        num_emails, num_attachments = cleanup_expired_mails(cutoff_date, delete_attachments, batch_size)

# Path: post_office/utils.py
# def cleanup_expired_mails(cutoff_date, delete_attachments=True, batch_size=1000):
#     """
#     Delete all emails before the given cutoff date.
#     Optionally also delete pending attachments.
#     Return the number of deleted emails and attachments.
#     """
#     expired_emails_ids = Email.objects.filter(created__lt=cutoff_date).values_list('id', flat=True)
#     email_id_batches = split_emails(expired_emails_ids, batch_size)
#     total_deleted_emails = 0
#     
#     for email_ids in email_id_batches:
#         # Delete email and incr total_deleted_emails counter
#         _, deleted_data = Email.objects.filter(id__in=email_ids).delete()
#         if deleted_data:
#             total_deleted_emails += deleted_data['post_office.Email']
# 
#     if delete_attachments:
#         attachments = Attachment.objects.filter(emails=None)
#         for attachment in attachments:
#             # Delete the actual file
#             attachment.file.delete()
#         attachments_count, _ = attachments.delete()
#     else:
#         attachments_count = 0
# 
#     return total_deleted_emails, attachments_count


# Path: post_office/management/commands/cleanup_mail.py
import datetime
from django.core.management.base import BaseCommand
from django.utils.timezone import now
from ...utils import cleanup_expired_mails





class Command(BaseCommand):
    help = 'Place deferred messages back in the queue.'

    def add_arguments(self, parser):
        parser.add_argument('-d', '--days',
                            type=int, default=90,
                            help="Cleanup mails older than this many days, defaults to 90.")

        parser.add_argument('-da', '--delete-attachments', action='store_true',
                            help="Delete orphaned attachments.")

        parser.add_argument('-b', '--batch-size', type=int, default=1000, help="Batch size for cleanup.")

    def handle(self, verbosity, days, delete_attachments, batch_size, **options):
        # Delete mails and their related logs and queued created before X days
        cutoff_date = now() - datetime.timedelta(days)
        num_emails, num_attachments = cleanup_expired_mails(cutoff_date, delete_attachments, batch_size)

        msg = "Deleted {0} mails created before {1} and {2} attachments."