<commit_before># -*- coding: utf-8 -*-

from datetime import datetime
from django.utils.translation import ugettext as _
from django.db.models import Q
from modoboa.lib import tables
from modoboa.lib.webutils import static_url
from modoboa.lib.email_listing import MBconnector, EmailListing
from modoboa.lib.emailutils import *
from modoboa.admin.lib import is_domain_admin
from models import *

class Qtable(tables.Table):
    tableid = "emails"
    idkey = "mailid"

    type = tables.Column("type", align="center", width="30px",
                         label="<input type='checkbox' name='toggleselect' id='toggleselect' />")
    rstatus = tables.ImgColumn("rstatus", width='25px')
    from_ = tables.Column("from", label=_("From"), limit=30)
    subject = tables.Column("subject", label=_("Subject"), limit=40)
    time = tables.Column("date", label=_("Date"))
    to = tables.Column("to", label=_("To"), sortable=False)

    cols_order = ['type', 'rstatus', 'to', 'from_', 'subject', 'time']

    def parse_date(self, value):
        return datetime.fromtimestamp(value)

class SQLconnector(MBconnector):
    orders = {
        "from" : "mail__from_addr",
        "subject" : "mail__subject",
        "date" : "mail__time_num"
        }
    
    def __init__(self, mail_ids=None, filter=None):
        self.count = None
        self.mail_ids = mail_ids
        self.filter = filter

    def messages_count(self, **kwargs):
        if self.count is None:
            filter = Q(chunk_ind=1)
            if self.mail_ids is not None:
                filter &= Q(mail__in=self.mail_ids)
            if self.filter:
                filter &= self.filter
            self.messages = Quarantine.objects.filter(filter)
            if kwargs.has_key("order"):
                totranslate = kwargs["order"][1:]
                sign = kwargs["order"][:1]
                if sign == " ":
                    sign = ""
                order = sign + self.orders[totranslate]
                self.messages = self.messages.order_by(order)
            self.count = self.messages.count()
        return self.count

    def fetch(self, start=None, stop=None, **kwargs):
        messages = self.messages[start - 1:stop]
        emails = []
        for qm in messages:
            for rcpt in qm.mail.msgrcpt_set.all():
                m = {"from" : qm.mail.from_addr, 
                     "to" : rcpt.rid.email,
                     "subject" : qm.mail.subject,
                     "mailid" : qm.mail_id,
                     "date" : qm.mail.time_num,
                     "type" : qm.mail.content}
                if rcpt.rs == '':
                    m["class"] = "unseen"
                elif rcpt.rs == 'R':
                    m["img_rstatus"] = static_url("pics/release.png")
                emails.append(m)
        return emails

class SQLlisting(EmailListing):
    tpl = "amavis_quarantine/index.html"
    tbltype = Qtable
    deflocation = "listing/"
    defcallback = "updatelisting"
    reset_wm_url = True

    def __init__(self, user, msgs, filter, **kwargs):
        if not user.is_superuser and not is_domain_admin(user):
            try:
                Qtable.cols_order.remove('to')
            except ValueError:
                pass
        self.mbc = SQLconnector(msgs, filter)
        
        super(SQLlisting, self).__init__(**kwargs)

class SQLemail(Email):
    def __init__(self, msg, *args, **kwargs):
        super(SQLemail, self).__init__(msg, *args, **kwargs)
        fields = ["X-Amavis-Alert", "Subject", "From", "To", "Cc", "Date"]<commit_msg>Columns must be restored each time a new instance of SQLlisting is created. (static member issue)<commit_after># -*- coding: utf-8 -*-

from datetime import datetime
from django.utils.translation import ugettext as _
from django.db.models import Q
from modoboa.lib import tables
from modoboa.lib.webutils import static_url
from modoboa.lib.email_listing import MBconnector, EmailListing
from modoboa.lib.emailutils import *
from modoboa.admin.lib import is_domain_admin
from models import *

class Qtable(tables.Table):
    tableid = "emails"
    idkey = "mailid"

    type = tables.Column("type", align="center", width="30px",
                         label="<input type='checkbox' name='toggleselect' id='toggleselect' />")
    rstatus = tables.ImgColumn("rstatus", width='25px')
    from_ = tables.Column("from", label=_("From"), limit=30)
    subject = tables.Column("subject", label=_("Subject"), limit=40)
    time = tables.Column("date", label=_("Date"))
    to = tables.Column("to", label=_("To"), sortable=False)

    cols_order = []

    def parse_date(self, value):
        return datetime.fromtimestamp(value)

class SQLconnector(MBconnector):
    orders = {
        "from" : "mail__from_addr",
        "subject" : "mail__subject",
        "date" : "mail__time_num"
        }
    
    def __init__(self, mail_ids=None, filter=None):
        self.count = None
        self.mail_ids = mail_ids
        self.filter = filter

    def messages_count(self, **kwargs):
        if self.count is None:
            filter = Q(chunk_ind=1)
            if self.mail_ids is not None:
                filter &= Q(mail__in=self.mail_ids)
            if self.filter:
                filter &= self.filter
            self.messages = Quarantine.objects.filter(filter)
            if kwargs.has_key("order"):
                totranslate = kwargs["order"][1:]
                sign = kwargs["order"][:1]
                if sign == " ":
                    sign = ""
                order = sign + self.orders[totranslate]
                self.messages = self.messages.order_by(order)
            self.count = self.messages.count()
        return self.count

    def fetch(self, start=None, stop=None, **kwargs):
        messages = self.messages[start - 1:stop]
        emails = []
        for qm in messages:
            for rcpt in qm.mail.msgrcpt_set.all():
                m = {"from" : qm.mail.from_addr, 
                     "to" : rcpt.rid.email,
                     "subject" : qm.mail.subject,
                     "mailid" : qm.mail_id,
                     "date" : qm.mail.time_num,
                     "type" : qm.mail.content}
                if rcpt.rs == '':
                    m["class"] = "unseen"
                elif rcpt.rs == 'R':
                    m["img_rstatus"] = static_url("pics/release.png")
                emails.append(m)
        return emails

class SQLlisting(EmailListing):
    tpl = "amavis_quarantine/index.html"
    tbltype = Qtable
    deflocation = "listing/"
    defcallback = "updatelisting"
    reset_wm_url = True

    def __init__(self, user, msgs, filter, **kwargs):
        if not user.is_superuser and not is_domain_admin(user):
            Qtable.cols_order = ['type', 'rstatus', 'from_', 'subject', 'time']
        else:
            Qtable.cols_order = ['type', 'rstatus', 'to', 'from_', 'subject', 'time']
        self.mbc = SQLconnector(msgs, filter)
        
        super(SQLlisting, self).__init__(**kwargs)

class SQLemail(Email):
    def __init__(self, msg, *args, **kwargs):
        super(SQLemail, self).__init__(msg, *args, **kwargs)
        fields = ["X-Amavis-Alert", "Subject", "From", "To", "Cc", "Date"]
{'fraction_non_alphanumeric': 0.13134245817779622, 'fraction_numerical': 0.0035946356974975806, 'mean_word_length': 2.1630957586357673, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4544228', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2176, 'n_words': 489}