~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/mail/__init__.py

Move the functions around again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Classes and utilities for Launchpad to send and receive email."""
5
 
 
6
 
from zope.security.proxy import isinstance as zope_isinstance
7
 
from lazr.enum import BaseItem
8
 
 
9
 
 
10
 
def value_string(item):
11
 
    """Return a unicode string representing an SQLObject value."""
12
 
    if item is None:
13
 
        return '(not set)'
14
 
    elif zope_isinstance(item, BaseItem):
15
 
        return item.title
16
 
    else:
17
 
        return unicode(item)
18
 
 
19
 
 
20
 
def text_delta(instance_delta, delta_names, state_names, interface):
21
 
    """Return a textual delta for a Delta object.
22
 
 
23
 
    A list of strings is returned.
24
 
 
25
 
    Only modified members of the delta will be shown.
26
 
 
27
 
    :param instance_delta: The delta to generate a textual representation of.
28
 
    :param delta_names: The names of all members to show changes to.
29
 
    :param state_names: The names of all members to show only the new state
30
 
        of.
31
 
    :param interface: The Zope interface that the input delta compared.
32
 
    """
33
 
    output = []
34
 
    indent = ' ' * 4
35
 
 
36
 
    # Fields for which we have old and new values.
37
 
    for field_name in delta_names:
38
 
        delta = getattr(instance_delta, field_name, None)
39
 
        if delta is None:
40
 
            continue
41
 
        title = interface[field_name].title
42
 
        old_item = value_string(delta['old'])
43
 
        new_item = value_string(delta['new'])
44
 
        output.append("%s%s: %s => %s" % (indent, title, old_item, new_item))
45
 
    for field_name in state_names:
46
 
        delta = getattr(instance_delta, field_name, None)
47
 
        if delta is None:
48
 
            continue
49
 
        title = interface[field_name].title
50
 
        if output:
51
 
            output.append('')
52
 
        output.append('%s changed to:\n\n%s' % (title, delta))
53
 
    return '\n'.join(output)
54
 
 
55
 
 
56
 
def append_footer(main, footer):
57
 
    """Append a footer to an email, following signature conventions.
58
 
 
59
 
    If there is no footer, do nothing.
60
 
    If there is already a signature, append an additional footer.
61
 
    If there is no existing signature, append '-- \n' and a footer.
62
 
 
63
 
    :param main: The main content, which may have a signature.
64
 
    :param footer: An additional footer to append.
65
 
    :return: a new version of main that includes the footer.
66
 
    """
67
 
    if footer == '':
68
 
        footer_separator = ''
69
 
    elif '\n-- \n' in main:
70
 
        footer_separator = '\n'
71
 
    else:
72
 
        footer_separator = '\n-- \n'
73
 
    return ''.join((main, footer_separator, footer))