~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/launchpad/mailout/__init__.py

Merge devel.

Show diffs side-by-side

added added

removed removed

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