~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""A comment/message for a difference between two distribution series."""

__metaclass__ = type

__all__ = [
    'DistroSeriesDifferenceComment',
    ]

from email.Utils import make_msgid

from storm.locals import (
    Int,
    Reference,
    Storm,
    )
from zope.interface import (
    classProvides,
    implements,
    )

from lp.services.messages.model.message import Message, MessageChunk
from canonical.launchpad.interfaces.lpstorm import (
    IMasterStore,
    IStore,
    )
from lp.registry.interfaces.distroseriesdifferencecomment import (
    IDistroSeriesDifferenceComment,
    IDistroSeriesDifferenceCommentSource,
    )


class DistroSeriesDifferenceComment(Storm):
    """See `IDistroSeriesDifferenceComment`."""
    implements(IDistroSeriesDifferenceComment)
    classProvides(IDistroSeriesDifferenceCommentSource)
    __storm_table__ = 'DistroSeriesDifferenceMessage'

    id = Int(primary=True)

    distro_series_difference_id = Int(name='distro_series_difference',
                                      allow_none=False)
    distro_series_difference = Reference(
        distro_series_difference_id, 'DistroSeriesDifference.id')

    message_id = Int(name="message", allow_none=False)
    message = Reference(message_id, 'Message.id')

    @property
    def comment_author(self):
        """See `IDistroSeriesDifferenceComment`."""
        return self.message.owner

    @property
    def body_text(self):
        """See `IDistroSeriesDifferenceComment`."""
        return self.message.text_contents

    @property
    def comment_date(self):
        """See `IDistroSeriesDifferenceComment`."""
        return self.message.datecreated

    @staticmethod
    def new(distro_series_difference, owner, comment):
        """See `IDistroSeriesDifferenceCommentSource`."""
        msgid = make_msgid('distroseriesdifference')
        message = Message(
            parent=None, owner=owner, rfc822msgid=msgid,
            subject=distro_series_difference.title)
        MessageChunk(message=message, content=comment, sequence=1)

        store = IMasterStore(DistroSeriesDifferenceComment)
        dsd_comment = DistroSeriesDifferenceComment()
        dsd_comment.distro_series_difference = distro_series_difference
        dsd_comment.message = message

        comment = store.add(dsd_comment)
        store.flush()
        return comment

    @staticmethod
    def getForDifference(distro_series_difference, id):
        """See `IDistroSeriesDifferenceCommentSource`."""
        store = IStore(DistroSeriesDifferenceComment)
        DSDComment = DistroSeriesDifferenceComment
        return store.find(
            DSDComment,
            DSDComment.distro_series_difference == distro_series_difference,
            DSDComment.id == id).one()