~launchpad-pqm/launchpad/devel

8728.4.1 by Tim Penhey
Add the view that is used by the branch-count-summary page template and register the view against many objects.
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
"""View classes for branch summaries."""
5
6
__metaclass__ = type
7
__all__ = [
8
    'BranchCountSummaryView',
9
    ]
10
11
12
from canonical.launchpad import _
13
from canonical.launchpad.webapp.publisher import LaunchpadView
8728.4.4 by Tim Penhey
Make the product summary use the new active counts.
14
from lp.code.interfaces.branch import DEFAULT_BRANCH_STATUS_IN_LISTING
8728.4.1 by Tim Penhey
Add the view that is used by the branch-count-summary page template and register the view against many objects.
15
from lp.code.interfaces.branchcollection import IBranchCollection
16
from lp.code.interfaces.revisioncache import IRevisionCache
10154.1.14 by Paul Hummer
Cleaned up some import violations
17
from lp.services.browser_helpers import get_plural_text
11382.6.34 by Gavin Panella
Reformat imports in all files touched so far.
18
from lp.services.propertycache import cachedproperty
8728.4.1 by Tim Penhey
Add the view that is used by the branch-count-summary page template and register the view against many objects.
19
20
21
class BranchCountSummaryView(LaunchpadView):
22
    """A view to give a summary of interesting counts."""
23
24
    @cachedproperty
25
    def _collection(self):
26
        """Return the branch collection for this context."""
8728.4.4 by Tim Penhey
Make the product summary use the new active counts.
27
        collection = IBranchCollection(self.context).visibleByUser(self.user)
28
        collection = collection.withLifecycleStatus(
29
            *DEFAULT_BRANCH_STATUS_IN_LISTING)
30
        return collection
8728.4.1 by Tim Penhey
Add the view that is used by the branch-count-summary page template and register the view against many objects.
31
32
    @cachedproperty
33
    def _revision_cache(self):
34
        """Return the revision cache for this context."""
35
        return IRevisionCache(self.context)
36
37
    @cachedproperty
38
    def branch_count(self):
39
        """The number of total branches the user can see."""
40
        return self._collection.count()
41
42
    @cachedproperty
43
    def branch_owners(self):
44
        """The number of individuals and teams that own branches."""
45
        return self._collection.ownerCounts()
46
47
    @property
48
    def person_owner_count(self):
49
        return self.branch_owners[0]
50
51
    @property
52
    def team_owner_count(self):
53
        return self.branch_owners[1]
54
55
    @cachedproperty
56
    def commit_count(self):
57
        return self._revision_cache.count()
58
59
    @cachedproperty
60
    def committer_count(self):
61
        return self._revision_cache.authorCount()
62
63
    @property
64
    def branch_text(self):
8728.4.4 by Tim Penhey
Make the product summary use the new active counts.
65
        return get_plural_text(
66
            self.branch_count, _('active branch'), _('active branches'))
8728.4.1 by Tim Penhey
Add the view that is used by the branch-count-summary page template and register the view against many objects.
67
68
    @property
69
    def person_text(self):
70
        return get_plural_text(
71
            self.person_owner_count, _('person'), _('people'))
72
73
    @property
74
    def team_text(self):
75
        return get_plural_text(self.team_owner_count, _('team'), _('teams'))
76
77
    @property
78
    def commit_text(self):
79
        return get_plural_text(self.commit_count, _('commit'), _('commits'))
80
81
    @property
82
    def committer_text(self):
83
        return get_plural_text(self.committer_count, _('person'), _('people'))