Changeset - 4c78223eb95e
[Not reviewed]
0 2 0
Lance Edgar (lance) - 5 years ago 2020-02-07 16:21:14
lance@edbob.org
Add new `ProblemReportEmail` base class, for simpler email previews

just so some of the template context can be provided automatically. although,
this *is* a bit heavy currently - should let caller pass in email handler etc.
2 files changed with 48 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rattail/emails.py
Show inline comments
 
# -*- coding: utf-8; -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2019 Lance Edgar
 
#  Copyright © 2010-2020 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU General Public License as published by the Free Software
 
#  Foundation, either version 3 of the License, or (at your option) any later
 
#  version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
#  details.
 
@@ -27,24 +27,55 @@ Common email config objects
 
from __future__ import unicode_literals, absolute_import
 

	
 
import sys
 
import socket
 
from traceback import format_exception
 

	
 
import six
 

	
 
from rattail.mail import Email
 
from rattail.util import load_object
 
from rattail.core import Object
 
from rattail.time import make_utc, localtime
 
from rattail.problems import ProblemReport, get_problem_report_handler
 

	
 

	
 
class ProblemReportEmail(Email):
 
    """
 
    Base class for all "problem report" emails
 
    """
 
    abstract = True
 

	
 
    def obtain_sample_data(self, request):
 
        data = self.sample_data(request)
 
        handler = get_problem_report_handler(self.config)
 

	
 
        if 'report' not in data:
 
            reports = handler.get_all_problem_reports()
 
            email_key = self.__class__.__name__
 
            for report in reports:
 
                if report.email_key == email_key:
 
                    data['report'] = report(self.config)
 
                    break
 

	
 
            if 'report' not in data:
 
                report = ProblemReport(self.config)
 
                report.problem_title = "Generic Title (problem report not found)"
 
                data['report'] = report
 

	
 
        if 'system_title' not in data:
 
            system_key = data['report'].system_key or 'rattail'
 
            data['system_title'] = handler.get_system_title(system_key)
 

	
 
        return data
 

	
 

	
 
class datasync_error_watcher_get_changes(Email):
 
    """
 
    When any datasync watcher thread encounters an error trying to get changes,
 
    this email is sent out.
 
    """
 
    default_subject = "Watcher failed to get changes"
 

	
 
    def sample_data(self, request):
 
        from rattail.datasync import DataSyncWatcher
 
        try:
rattail/mail.py
Show inline comments
 
# -*- coding: utf-8; -*-
 
################################################################################
 
#
 
#  Rattail -- Retail Software Framework
 
#  Copyright © 2010-2018 Lance Edgar
 
#  Copyright © 2010-2020 Lance Edgar
 
#
 
#  This file is part of Rattail.
 
#
 
#  Rattail is free software: you can redistribute it and/or modify it under the
 
#  terms of the GNU General Public License as published by the Free Software
 
#  Foundation, either version 3 of the License, or (at your option) any later
 
#  version.
 
#
 
#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
 
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
#  details.
 
@@ -300,25 +300,40 @@ class Email(object):
 
                raise exceptions.ConfigurationError("Email instance has no key: {0}".format(repr(self)))
 

	
 
        if fallback_key:
 
            self.fallback_key = fallback_key
 
        if default_subject:
 
            self.default_subject = default_subject
 

	
 
        templates = config.getlist('rattail.mail', 'templates')
 
        if templates:
 
            templates = [resource_path(p) for p in templates]
 
        self.templates = TemplateLookup(directories=templates)
 

	
 
    def obtain_sample_data(self, request):
 
        """
 
        This method is responsible for obtaining the full set of sample data,
 
        to be used as context when generating a preview for the email.
 

	
 
        Note, you normally should not override this method!  Please see also
 
        the :meth:`sample_data()` method.
 
        """
 
        return self.sample_data(request)
 

	
 
    def sample_data(self, request):
 
        """
 
        This method can return a dict of sample data, to be used as context
 
        when generating a preview for the email.  Subclasses are welcome to
 
        override this method.
 
        """
 
        return {}
 

	
 
    def get_enabled(self):
 
        """
 
        Get the enabled flag for the email's message type.
 
        """
 
        enabled = self.config.getbool('rattail.mail', '{0}.enabled'.format(self.key))
 
        if enabled is not None:
 
            return enabled
 
        enabled = self.config.getbool('rattail.mail', 'default.enabled')
 
        if enabled is not None:
 
            return enabled
0 comments (0 inline, 0 general)