~launchpad-pqm/launchpad/devel

14642.1.8 by Curtis Hovey
Updated copyright.
1
# Copyright 2009-2012 Canonical Ltd.  This software is licensed under the
8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
2
# GNU Affero General Public License version 3 (see the file LICENSE).
2002 by Canonical.com Patch Queue Manager
Implements the karma framework (according to the KarmaImplementation spec) and hook it into Malone events. r=spiv,stub
3
4
__metaclass__ = type
5
2736.1.23 by Mark Shuttleworth
�karma restructuring, and test fixes
6
__all__ = [
7
    'KarmaActionEditView',
8
    'KarmaActionSetNavigation',
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
9
    'KarmaContextTopContributorsView',
2736.1.23 by Mark Shuttleworth
�karma restructuring, and test fixes
10
    ]
2068 by Canonical.com Patch Queue Manager
[r=SteveA/trivial] import fascism, kill translationeffort, kill sshkey browser code, add __all__s to interfaces, other cleanings-up
11
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
12
from operator import attrgetter
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
13
7182.6.10 by Guilherme Salgado
Convert the last SQLObjectEditView into a LaunchpadEditFormView
14
from zope.component import getUtility
15
14600.1.12 by Curtis Hovey
Move i18n to lp.
16
from lp import _
14642.1.2 by Curtis Hovey
Removed action from webapp globs.
17
from lp.app.browser.launchpadform import (
18
    action,
19
    LaunchpadEditFormView,
20
    )
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
21
from lp.registry.interfaces.distribution import IDistribution
22
from lp.registry.interfaces.karma import (
23
    IKarmaAction,
24
    IKarmaActionSet,
25
    )
7675.110.3 by Curtis Hovey
Ran the migration script to move registry code to lp.registry.
26
from lp.registry.interfaces.product import IProduct
10326.1.2 by Henning Eggers
Renamed project interfaces module to projectgroup.
27
from lp.registry.interfaces.projectgroup import IProjectGroup
11382.6.34 by Gavin Panella
Reformat imports in all files touched so far.
28
from lp.services.propertycache import cachedproperty
14612.2.1 by William Grant
format-imports on lib/. So many imports.
29
from lp.services.webapp import (
30
    canonical_url,
31
    Navigation,
32
    )
14642.1.2 by Curtis Hovey
Removed action from webapp globs.
33
from lp.services.webapp.publisher import LaunchpadView
2002 by Canonical.com Patch Queue Manager
Implements the karma framework (according to the KarmaImplementation spec) and hook it into Malone events. r=spiv,stub
34
2628 by Canonical.com Patch Queue Manager
[trivial] converted a bunch of browser:traverse into navigation
35
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
36
TOP_CONTRIBUTORS_LIMIT = 20
37
38
2628 by Canonical.com Patch Queue Manager
[trivial] converted a bunch of browser:traverse into navigation
39
class KarmaActionSetNavigation(Navigation):
40
41
    usedfor = IKarmaActionSet
42
43
    def traverse(self, name):
44
        return self.context.getByName(name)
2736.1.22 by Mark Shuttleworth
karma categories in the db, and karma for specs
45
2736.1.23 by Mark Shuttleworth
�karma restructuring, and test fixes
46
9287.1.1 by Edwin Grubbs
Converted karma-action-index.pt and karmacontext-topcontributors.pt to UI 3.0.
47
class KarmaActionView(LaunchpadView):
48
    """View class for the index of karma actions."""
49
50
    page_title = 'Actions that give people karma'
51
52
7182.6.10 by Guilherme Salgado
Convert the last SQLObjectEditView into a LaunchpadEditFormView
53
class KarmaActionEditView(LaunchpadEditFormView):
54
55
    schema = IKarmaAction
56
    field_names = ["name", "category", "points", "title", "summary"]
57
9116.1.1 by Curtis Hovey
Updated karma action +index (index is a form) to UI 3.0
58
    @property
59
    def label(self):
60
        """See `LaunchpadFormView`."""
61
        return 'Edit %s karma action' % self.context.title
62
63
    @property
64
    def page_title(self):
65
        """The page title."""
66
        return self.label
67
68
    @property
69
    def cancel_url(self):
70
        """See `LaunchpadFormView`."""
71
        return canonical_url(getUtility(IKarmaActionSet))
72
7182.6.10 by Guilherme Salgado
Convert the last SQLObjectEditView into a LaunchpadEditFormView
73
    @action(_("Change"), name="change")
74
    def change_action(self, action, data):
75
        self.updateContextFromData(data)
9116.1.1 by Curtis Hovey
Updated karma action +index (index is a form) to UI 3.0
76
        self.next_url = self.cancel_url
2736.1.23 by Mark Shuttleworth
�karma restructuring, and test fixes
77
78
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
79
class KarmaContextContributor:
80
81
    def __init__(self, person, karmavalue):
82
        self.person = person
83
        self.karmavalue = karmavalue
84
85
86
class KarmaContextTopContributorsView(LaunchpadView):
87
    """List this KarmaContext's top contributors."""
88
9287.1.1 by Edwin Grubbs
Converted karma-action-index.pt and karmacontext-topcontributors.pt to UI 3.0.
89
    @property
90
    def page_title(self):
9287.1.2 by Edwin Grubbs
Fixed xx-karmacontext-topcontributors.txt test.
91
        return "Top %s Contributors" % self.context.title
9287.1.1 by Edwin Grubbs
Converted karma-action-index.pt and karmacontext-topcontributors.pt to UI 3.0.
92
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
93
    def initialize(self):
94
        context = self.context
95
        if IProduct.providedBy(context):
4152.2.2 by Francis J. Lacoste
Rename "product" to "project" in browser code.
96
            self.context_name = 'Project'
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
97
        elif IDistribution.providedBy(context):
98
            self.context_name = 'Distribution'
10326.1.1 by Henning Eggers
Mechanically renamed IProject* to IProjectGroup*.
99
        elif IProjectGroup.providedBy(context):
4152.2.6 by Francis J. Lacoste
Rename "Project" to "Project Group"
100
            self.context_name = 'Project Group'
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
101
        else:
102
            raise AssertionError(
10724.1.1 by Henning Eggers
First batch of Project -> ProjectGrpoup renamings.
103
                "Context is not a Product, Project group or Distribution: %r"
3691.336.4 by Guilherme Salgado
Add a list of top contributors for projects. Fix https://beta.launchpad.net/launchpad/+bug/67920
104
                % context)
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
105
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
106
    def _getTopContributorsWithLimit(self, limit=None):
107
        results = self.context.getTopContributors(limit=limit)
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
108
        contributors = [KarmaContextContributor(person, karmavalue)
109
                        for person, karmavalue in results]
7182.6.10 by Guilherme Salgado
Convert the last SQLObjectEditView into a LaunchpadEditFormView
110
        return sorted(
111
            contributors, key=attrgetter('karmavalue'), reverse=True)
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
112
113
    def getTopContributors(self):
114
        return self._getTopContributorsWithLimit(limit=TOP_CONTRIBUTORS_LIMIT)
115
116
    def getTopFiveContributors(self):
117
        return self._getTopContributorsWithLimit(limit=5)
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
118
119
    @cachedproperty
120
    def top_contributors_by_category(self):
121
        contributors_by_category = {}
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
122
        limit = TOP_CONTRIBUTORS_LIMIT
7182.6.10 by Guilherme Salgado
Convert the last SQLObjectEditView into a LaunchpadEditFormView
123
        results = self.context.getTopContributorsGroupedByCategory(
124
            limit=limit)
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
125
        for category, people_and_karma in results.items():
126
            contributors = []
127
            for person, karmavalue in people_and_karma:
128
                contributors.append(KarmaContextContributor(
129
                    person, karmavalue))
3691.64.2 by Guilherme Salgado
Change stuff as suggested by Bjorn on his review
130
            contributors.sort(key=attrgetter('karmavalue'), reverse=True)
3691.64.1 by Guilherme Salgado
Display top contributors for products and distributions
131
            contributors_by_category[category.title] = contributors
132
        return contributors_by_category
133
134
    @property
135
    def sorted_categories(self):
136
        return sorted(self.top_contributors_by_category.keys())