# Path: modoboa_amavis/models.py
# class Maddr(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     id = models.BigIntegerField(primary_key=True)  # NOQA:A003
#     email = models.CharField(max_length=255)
#     domain = models.CharField(max_length=255)
# 
#     class Meta:
#         db_table = "maddr"
#         unique_together = [("partition_tag", "email")]
#         managed = False
# 
# class Msgrcpt(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     mail = models.ForeignKey(Msgs, primary_key=True, on_delete=models.CASCADE)
#     rid = models.ForeignKey(Maddr, db_column="rid", on_delete=models.CASCADE)
#     rseqnum = models.IntegerField(default=0)
#     is_local = models.CharField(max_length=3)
#     content = models.CharField(max_length=3)
#     ds = models.CharField(max_length=3)
#     rs = models.CharField(max_length=3)
#     bl = models.CharField(max_length=3, blank=True)
#     wl = models.CharField(max_length=3, blank=True)
#     bspam_level = models.FloatField(null=True, blank=True)
#     smtp_resp = models.CharField(max_length=765, blank=True)
# 
#     class Meta:
#         db_table = "msgrcpt"
#         managed = False
#         unique_together = ("partition_tag", "mail", "rseqnum")
# 
# class Msgs(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     mail_id = models.CharField(max_length=12, primary_key=True)
#     secret_id = models.BinaryField()
#     am_id = models.CharField(max_length=60)
#     time_num = models.IntegerField()
#     time_iso = models.CharField(max_length=48)
#     sid = models.ForeignKey(Maddr, db_column="sid", on_delete=models.CASCADE)
#     policy = models.CharField(max_length=765, blank=True)
#     client_addr = models.CharField(max_length=765, blank=True)
#     size = models.IntegerField()
#     originating = models.CharField(max_length=3)
#     content = models.CharField(max_length=1, blank=True)
#     quar_type = models.CharField(max_length=1, blank=True)
#     quar_loc = models.CharField(max_length=255, blank=True)
#     dsn_sent = models.CharField(max_length=3, blank=True)
#     spam_level = models.FloatField(null=True, blank=True)
#     message_id = models.CharField(max_length=765, blank=True)
#     from_addr = models.CharField(max_length=765, blank=True)
#     subject = models.CharField(max_length=765, blank=True)
#     host = models.CharField(max_length=765)
# 
#     class Meta:
#         db_table = "msgs"
#         managed = False
#         unique_together = ("partition_tag", "mail_id")
# 
# Path: modoboa_amavis/modo_extension.py
# class Amavis(ModoExtension):
#     """The Amavis extension."""
# 
#     name = "modoboa_amavis"
#     label = ugettext_lazy("Amavis frontend")
#     version = __version__
#     description = ugettext_lazy("Simple amavis management frontend")
#     url = "quarantine"
#     available_for_topredirection = True
# 
#     def load(self):
#         param_tools.registry.add("global", forms.ParametersForm, "Amavis")
#         param_tools.registry.add(
#             "user", forms.UserSettings, ugettext_lazy("Quarantine"))
# 
#     def load_initial_data(self):
#         """Create records for existing domains and co."""
#         for dom in Domain.objects.all():
#             policy = create_user_and_policy("@{0}".format(dom.name))
#             for domalias in dom.domainalias_set.all():
#                 domalias_pattern = "@{0}".format(domalias.name)
#                 create_user_and_use_policy(domalias_pattern, policy)


import time
            import logging
from django.core.management.base import BaseCommand
from django.db.models import Count
from modoboa.parameters import tools as param_tools
from ...models import Maddr, Msgrcpt, Msgs
from ...modo_extension import Amavis

    def __vprint(self, msg):
        if not self.verbose:
            return
        print(msg)

    def handle(self, *args, **options):
        Amavis().load()
        if options["debug"]:
            log = logging.getLogger("django.db.backends")
            log.setLevel(logging.DEBUG)
            log.addHandler(logging.StreamHandler())
        self.verbose = options["verbose"]

        conf = dict(param_tools.get_global_parameters("modoboa_amavis"))

        flags = ["D"]
        if conf["released_msgs_cleanup"]:
            flags += ["R"]

        self.__vprint("Deleting marked messages...")
        ids = Msgrcpt.objects.filter(rs__in=flags).values("mail_id").distinct()
        for msg in Msgs.objects.filter(mail_id__in=ids):
            if not msg.msgrcpt_set.exclude(rs__in=flags).count():
                msg.delete()

        self.__vprint(
            "Deleting messages older than {} days...".format(
                conf["max_messages_age"]))
        limit = int(time.time()) - (conf["max_messages_age"] * 24 * 3600)

# Path: modoboa_amavis/models.py
# class Maddr(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     id = models.BigIntegerField(primary_key=True)  # NOQA:A003
#     email = models.CharField(max_length=255)
#     domain = models.CharField(max_length=255)
# 
#     class Meta:
#         db_table = "maddr"
#         unique_together = [("partition_tag", "email")]
#         managed = False
# 
# class Msgrcpt(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     mail = models.ForeignKey(Msgs, primary_key=True, on_delete=models.CASCADE)
#     rid = models.ForeignKey(Maddr, db_column="rid", on_delete=models.CASCADE)
#     rseqnum = models.IntegerField(default=0)
#     is_local = models.CharField(max_length=3)
#     content = models.CharField(max_length=3)
#     ds = models.CharField(max_length=3)
#     rs = models.CharField(max_length=3)
#     bl = models.CharField(max_length=3, blank=True)
#     wl = models.CharField(max_length=3, blank=True)
#     bspam_level = models.FloatField(null=True, blank=True)
#     smtp_resp = models.CharField(max_length=765, blank=True)
# 
#     class Meta:
#         db_table = "msgrcpt"
#         managed = False
#         unique_together = ("partition_tag", "mail", "rseqnum")
# 
# class Msgs(models.Model):
#     partition_tag = models.IntegerField(default=0)
#     mail_id = models.CharField(max_length=12, primary_key=True)
#     secret_id = models.BinaryField()
#     am_id = models.CharField(max_length=60)
#     time_num = models.IntegerField()
#     time_iso = models.CharField(max_length=48)
#     sid = models.ForeignKey(Maddr, db_column="sid", on_delete=models.CASCADE)
#     policy = models.CharField(max_length=765, blank=True)
#     client_addr = models.CharField(max_length=765, blank=True)
#     size = models.IntegerField()
#     originating = models.CharField(max_length=3)
#     content = models.CharField(max_length=1, blank=True)
#     quar_type = models.CharField(max_length=1, blank=True)
#     quar_loc = models.CharField(max_length=255, blank=True)
#     dsn_sent = models.CharField(max_length=3, blank=True)
#     spam_level = models.FloatField(null=True, blank=True)
#     message_id = models.CharField(max_length=765, blank=True)
#     from_addr = models.CharField(max_length=765, blank=True)
#     subject = models.CharField(max_length=765, blank=True)
#     host = models.CharField(max_length=765)
# 
#     class Meta:
#         db_table = "msgs"
#         managed = False
#         unique_together = ("partition_tag", "mail_id")
# 
# Path: modoboa_amavis/modo_extension.py
# class Amavis(ModoExtension):
#     """The Amavis extension."""
# 
#     name = "modoboa_amavis"
#     label = ugettext_lazy("Amavis frontend")
#     version = __version__
#     description = ugettext_lazy("Simple amavis management frontend")
#     url = "quarantine"
#     available_for_topredirection = True
# 
#     def load(self):
#         param_tools.registry.add("global", forms.ParametersForm, "Amavis")
#         param_tools.registry.add(
#             "user", forms.UserSettings, ugettext_lazy("Quarantine"))
# 
#     def load_initial_data(self):
#         """Create records for existing domains and co."""
#         for dom in Domain.objects.all():
#             policy = create_user_and_policy("@{0}".format(dom.name))
#             for domalias in dom.domainalias_set.all():
#                 domalias_pattern = "@{0}".format(domalias.name)
#                 create_user_and_use_policy(domalias_pattern, policy)


# Path: modoboa_amavis/management/commands/qcleanup.py
import time
            import logging
from django.core.management.base import BaseCommand
from django.db.models import Count
from modoboa.parameters import tools as param_tools
from ...models import Maddr, Msgrcpt, Msgs
from ...modo_extension import Amavis


    def __vprint(self, msg):
        if not self.verbose:
            return
        print(msg)

    def handle(self, *args, **options):
        Amavis().load()
        if options["debug"]:
            log = logging.getLogger("django.db.backends")
            log.setLevel(logging.DEBUG)
            log.addHandler(logging.StreamHandler())
        self.verbose = options["verbose"]

        conf = dict(param_tools.get_global_parameters("modoboa_amavis"))

        flags = ["D"]
        if conf["released_msgs_cleanup"]:
            flags += ["R"]

        self.__vprint("Deleting marked messages...")
        ids = Msgrcpt.objects.filter(rs__in=flags).values("mail_id").distinct()
        for msg in Msgs.objects.filter(mail_id__in=ids):
            if not msg.msgrcpt_set.exclude(rs__in=flags).count():
                msg.delete()

        self.__vprint(
            "Deleting messages older than {} days...".format(
                conf["max_messages_age"]))
        limit = int(time.time()) - (conf["max_messages_age"] * 24 * 3600)

        Msgs.objects.filter(time_num__lt=limit).delete()