~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
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Base class view for merge queue listings."""

__metaclass__ = type

__all__ = [
    'MergeQueueListingView',
    'HasMergeQueuesMenuMixin',
    'PersonMergeQueueListingView',
    ]

from zope.component import getUtility

from lp.code.interfaces.branchmergequeuecollection import (
    IAllBranchMergeQueues,
    )
from lp.services.browser_helpers import get_plural_text
from lp.services.feeds.browser import FeedsMixin
from lp.services.propertycache import cachedproperty
from lp.services.webapp import (
    LaunchpadView,
    Link,
    )


class HasMergeQueuesMenuMixin:
    """A context menus mixin for objects that can own merge queues."""

    def _getCollection(self):
        return getUtility(IAllBranchMergeQueues).visibleByUser(self.user)

    @property
    def person(self):
        """The `IPerson` for the context of the view.

        In simple cases this is the context itself, but in others, like the
        PersonProduct, it is an attribute of the context.
        """
        return self.context

    def mergequeues(self):
        return Link(
            '+merge-queues',
            get_plural_text(
                self.mergequeue_count,
                'merge queue', 'merge queues'), site='code')

    @cachedproperty
    def mergequeue_count(self):
        return self._getCollection().ownedBy(self.person).count()


class MergeQueueListingView(LaunchpadView, FeedsMixin):

    # No feeds initially
    feed_types = ()

    branch_enabled = True
    owner_enabled = True

    label_template = 'Merge Queues for %(displayname)s'

    @property
    def label(self):
        return self.label_template % {
            'displayname': self.context.displayname,
            'title': getattr(self.context, 'title', 'no-title')}

    # Provide a default page_title for distros and other things without
    # breadcrumbs..
    page_title = label

    def _getCollection(self):
        """Override this to say what queues will be in the listing."""
        raise NotImplementedError(self._getCollection)

    def getVisibleQueuesForUser(self):
        """Branch merge queues that are visible by the logged in user."""
        collection = self._getCollection().visibleByUser(self.user)
        return collection.getMergeQueues()

    @cachedproperty
    def mergequeues(self):
        return self.getVisibleQueuesForUser()

    @cachedproperty
    def mergequeue_count(self):
        """Return the number of merge queues that will be returned."""
        return self._getCollection().visibleByUser(self.user).count()

    @property
    def no_merge_queue_message(self):
        """Shown when there is no table to show."""
        return "%s has no merge queues." % self.context.displayname


class PersonMergeQueueListingView(MergeQueueListingView):

    label_template = 'Merge Queues owned by %(displayname)s'
    owner_enabled = False

    def _getCollection(self):
        return getUtility(IAllBranchMergeQueues).ownedBy(self.context)