~launchpad-pqm/launchpad/devel

8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
13651.1.15 by j.c.sackett
Better check for dealing with distributionsourcepackageindatabase within toTerm.
4
"""Adapters for registry objects."""
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
5
6
__metaclass__ = type
7
8
__all__ = [
12494.2.14 by Gavin Panella
De-register the IDistroSeries->IDistribution and IProductSeries->IProduct adapters.
9
    'distroseries_to_distribution',
12156.3.1 by Curtis Hovey
Restored polls so that they can be used by a handful of important teams once a year.
10
    'PollSubset',
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
11
    'productseries_to_product',
13483.1.14 by Curtis Hovey
Added tests to verify IDistribution adapters work with casting.
12
    'sourcepackage_to_distribution',
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
13
    ]
14
15
12156.3.1 by Curtis Hovey
Restored polls so that they can be used by a handful of important teams once a year.
16
from zope.component import getUtility
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
17
from zope.component.interfaces import ComponentLookupError
12156.3.1 by Curtis Hovey
Restored polls so that they can be used by a handful of important teams once a year.
18
from zope.interface import implements
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
19
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
20
from lp.archivepublisher.interfaces.publisherconfig import IPublisherConfigSet
12156.3.1 by Curtis Hovey
Restored polls so that they can be used by a handful of important teams once a year.
21
from lp.registry.interfaces.poll import (
22
    IPollSet,
23
    IPollSubset,
24
    PollAlgorithm,
25
    PollStatus,
26
    )
14612.2.1 by William Grant
format-imports on lib/. So many imports.
27
from lp.services.webapp.interfaces import ILaunchpadPrincipal
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
28
29
13483.1.13 by Curtis Hovey
Use common implementation to adapt source packages to distros.
30
def sourcepackage_to_distribution(source_package):
31
    """Adapts `ISourcePackage` object to `IDistribution`.
32
33
    This also supports `IDistributionSourcePackage`
34
    """
35
    return source_package.distribution
36
37
11582.1.7 by j.c.sackett
Updated adapter configuration.
38
def distroseries_to_distribution(distroseries):
39
    """Adapts `IDistroSeries` object to `IDistribution`.
11582.1.11 by j.c.sackett
lint fixes.
40
11582.1.7 by j.c.sackett
Updated adapter configuration.
41
    This is useful for adapting to `IServiceUsage`
42
    or `ILaunchpadUsage`."""
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
43
    return distroseries.distribution
44
11411.7.33 by j.c.sackett
Lint fixes.
45
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
46
def person_from_principal(principal):
47
    """Adapt `ILaunchpadPrincipal` to `IPerson`."""
48
    if ILaunchpadPrincipal.providedBy(principal):
49
        if principal.person is None:
50
            raise ComponentLookupError
51
        return principal.person
52
    else:
53
        # This is not actually necessary when this is used as an adapter
54
        # from ILaunchpadPrincipal, as we know we always have an
55
        # ILaunchpadPrincipal.
56
        #
57
        # When Zope3 interfaces allow returning None for "cannot adapt"
58
        # we can return None here.
59
        ##return None
60
        raise ComponentLookupError
61
62
12156.3.1 by Curtis Hovey
Restored polls so that they can be used by a handful of important teams once a year.
63
class PollSubset:
64
    """Adapt an `IPoll` to an `IPollSubset`."""
65
    implements(IPollSubset)
66
67
    title = 'Team polls'
68
69
    def __init__(self, team=None):
70
        self.team = team
71
72
    def new(self, name, title, proposition, dateopens, datecloses,
73
            secrecy, allowspoilt, poll_type=PollAlgorithm.SIMPLE):
74
        """See IPollSubset."""
75
        assert self.team is not None, (
76
            'team cannot be None to call this method.')
77
        return getUtility(IPollSet).new(
78
            self.team, name, title, proposition, dateopens,
79
            datecloses, secrecy, allowspoilt, poll_type)
80
81
    def getByName(self, name, default=None):
82
        """See IPollSubset."""
83
        assert self.team is not None, (
84
            'team cannot be None to call this method.')
85
        pollset = getUtility(IPollSet)
86
        return pollset.getByTeamAndName(self.team, name, default)
87
88
    def getAll(self):
89
        """See IPollSubset."""
90
        assert self.team is not None, (
91
            'team cannot be None to call this method.')
92
        return getUtility(IPollSet).selectByTeam(self.team)
93
94
    def getOpenPolls(self, when=None):
95
        """See IPollSubset."""
96
        assert self.team is not None, (
97
            'team cannot be None to call this method.')
98
        return getUtility(IPollSet).selectByTeam(
99
            self.team, [PollStatus.OPEN], orderBy='datecloses', when=when)
100
101
    def getClosedPolls(self, when=None):
102
        """See IPollSubset."""
103
        assert self.team is not None, (
104
            'team cannot be None to call this method.')
105
        return getUtility(IPollSet).selectByTeam(
106
            self.team, [PollStatus.CLOSED], orderBy='datecloses', when=when)
107
108
    def getNotYetOpenedPolls(self, when=None):
109
        """See IPollSubset."""
110
        assert self.team is not None, (
111
            'team cannot be None to call this method.')
112
        return getUtility(IPollSet).selectByTeam(
113
            self.team, [PollStatus.NOT_YET_OPENED],
114
            orderBy='dateopens', when=when)
115
116
8203.2.1 by Curtis Hovey
Consolidated regisrty tests under lp.registry.tests. Consolidated adapters/* to
117
def productseries_to_product(productseries):
118
    """Adapts `IProductSeries` object to `IProduct`.
119
120
    This is useful for adapting to `IHasExternalBugTracker`
121
    or `ILaunchpadUsage`.
122
    """
123
    return productseries.product
7675.1063.1 by Julian Edwards
basic implementation of +pubconf page, sans tests. So sue me for not doing TDD, I don't care.
124
125
126
def distribution_to_publisherconfig(distro):
127
    """Adapts `IDistribution` to `IPublisherConfig`."""
128
    # Used for traversal from distro to +pubconf.
129
    config = getUtility(IPublisherConfigSet).getByDistribution(distro)
130
    return config
13939.3.15 by Curtis Hovey
Created an ISourcePackageName adapter for SP and DSP.
131
132
133
def package_to_sourcepackagename(package):
134
    """Adapts a package to its `ISourcePackageName`."""
135
    return package.sourcepackagename