Re: Anybody have interesting uses for interfaces.py?

Thomas Arendsen Hein <[email protected]>
Newsgroups gmane.comp.bug-tracking.roundup.user
Message-ID <[email protected]>
* John P. Rouillard <[email protected]> [20191123 01:02]:
> new examples.

Here is what we use at Intevation for our support trackers, it
allows anonymous posts to the tracker, the account "support" will be
used instead of creating new accounts.

Since the user "support" gets added to the nosy lists, mails to this
account should be discarded. (I should adjust nosyreaction to not
send mails, but ... the workaround was so easy :))

Depending on the target email address, subject prefixes are inserted
and/or keywords set.

I have removed configs for or production instances as well as
something that needs our customizations, but it should work in the
way posted below.

It relies on the Delivered-To: headers inserted by the postfix
instance on our roundup tracker VM, but adjusting it to other
situations should work.


=== postfix/virtual on the main MTA ===

[email protected]    [email protected]
[email protected]   [email protected]
[email protected]   [email protected]


=== interfaces.py ===

import roundup.mailgw
import email.utils

class SupportTracker(object):
    def __init__(self, prefix=None, keyword=None):
        self.prefix = prefix
        self.keyword = keyword

support_trackers = {
    ### production instances ###


    ### test instances ###
    'roundup-test+support-a':
        SupportTracker(prefix='Support 1', keyword='support1'),
    'roundup-test+support-b':
        SupportTracker(prefix='Support 2', keyword='support2'),
    'roundup-test2+support-a':
        SupportTracker(prefix='Support 1', keyword='support1'),
    'roundup-test2+support-b':
        SupportTracker(prefix='Support 2', keyword='support2'),
}


class parsedMessage(roundup.mailgw.parsedMessage):
    def __init__(self, mailgw, message, support_tracker):
        roundup.mailgw.parsedMessage.__init__(self, mailgw, message)
        if support_tracker.prefix:
            self.prefix = '%s: ' % support_tracker.prefix
        else:
            self.prefix = ''
        self.keywords = []
        if support_tracker.keyword:
            try:
                self.keywords = [
                    self.db.keyword.lookup(support_tracker.keyword)]
            except KeyError:
                pass
        self.config.ADD_AUTHOR_TO_NOSY = 'no'
        self.config.ADD_RECIPIENTS_TO_NOSY = 'no'
        self.config.MAILGW_KEEP_QUOTED_TEXT = 'yes'
        self.config.MAILGW_LEAVE_BODY_UNCHANGED = 'yes'
        self.classname = 'issue'
        self.pfxmode = 'loose'
        self.sfxmode = 'none'
        self.fixed_author = self.db.user.lookup('support')
        self.fixed_props = {
            'nosy': [self.fixed_author],
            'keyword': self.keywords,
        }

    def handle_help(self):
        pass

    def check_subject(self):
        if not self.subject:
            self.subject = 'no subject'

    def rego_confirm(self):
        pass

    def get_author_id(self):
        self.author = self.fixed_author

    def get_props(self):
        self.props = {}
        if not self.nodeid:
            self.props.update(self.fixed_props)
            self.props['title'] = ("%s%s" % (
                self.prefix, self.subject.replace('[', '(').replace(']', ')')))

    def get_content_and_attachments(self):
        roundup.mailgw.parsedMessage.get_content_and_attachments(self)
        if not self.content:
            self.content = 'no text'
        intro = []
        for header in ['From', 'To', 'Cc']:
            for addr in self.message.getaddrlist(header):
                intro.append('%s: %s' % (header, email.utils.formataddr(addr)))
        intro.append('Subject: %s' % self.subject)
        intro.append('\n')
        self.content = '\n'.join(intro) + self.content


class MailGW(roundup.mailgw.MailGW):
    def parsed_message_class(self, mailgw, message):
        support_tracker = None
        recipients = message.getaddrlist('delivered-to')
        if recipients:
            localpart = recipients[0][1].rpartition('@')[0]
            support_tracker = support_trackers.get(localpart)
        if support_tracker:
            return parsedMessage(mailgw, message, support_tracker)
        else:
            return roundup.mailgw.parsedMessage(mailgw, message)

-- 
Thomas Arendsen Hein <[email protected]>
OpenPGP key: https://intevation.de/~thomas/thomas_pgp.asc (0xD45DE28FF3A2250C)
Intevation GmbH, Neuer Graben 17, 49074 Osnabrueck - AG Osnabrueck, HR B 18998
Geschaeftsfuehrer: Frank Koormann, Bernhard Reiter, Dr. Jan-Oliver Wagner
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.