~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/registry/vocabularies.py

[r=benji][bug=817408] Use a new DistroSeriesDifferences vocabulary in
        place of the hand-crafted one in IDifferencesFormSchema.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    'DistributionSourcePackageVocabulary',
34
34
    'DistributionVocabulary',
35
35
    'DistroSeriesDerivationVocabulary',
 
36
    'DistroSeriesDifferencesVocabulary',
36
37
    'DistroSeriesVocabulary',
37
38
    'FeaturedProjectVocabulary',
38
39
    'FilteredDistroSeriesVocabulary',
138
139
    IDistributionSourcePackage,
139
140
    )
140
141
from lp.registry.interfaces.distroseries import IDistroSeries
 
142
from lp.registry.interfaces.distroseriesdifference import (
 
143
    IDistroSeriesDifference,
 
144
    )
141
145
from lp.registry.interfaces.mailinglist import (
142
146
    IMailingListSet,
143
147
    MailingListStatus,
167
171
from lp.registry.interfaces.sourcepackage import ISourcePackage
168
172
from lp.registry.model.distribution import Distribution
169
173
from lp.registry.model.distroseries import DistroSeries
 
174
from lp.registry.model.distroseriesdifference import DistroSeriesDifference
170
175
from lp.registry.model.distroseriesparent import DistroSeriesParent
171
176
from lp.registry.model.featuredproject import FeaturedProject
172
177
from lp.registry.model.karma import KarmaCategory
194
199
from lp.soyuz.model.binarypackagename import BinaryPackageName
195
200
from lp.soyuz.model.binarypackagerelease import BinaryPackageRelease
196
201
from lp.soyuz.model.distroarchseries import DistroArchSeries
197
 
from lp.soyuz.model.publishing import (
198
 
    SourcePackagePublishingHistory,
199
 
    )
 
202
from lp.soyuz.model.publishing import SourcePackagePublishingHistory
200
203
from lp.soyuz.model.sourcepackagerelease import SourcePackageRelease
201
204
 
202
205
 
1866
1869
            return self.find_terms(where)
1867
1870
 
1868
1871
 
 
1872
class DistroSeriesDifferencesVocabulary:
 
1873
    """A vocabulary source for differences relating to a series.
 
1874
 
 
1875
    Specifically, all `DistroSeriesDifference`s relating to a derived series.
 
1876
    """
 
1877
 
 
1878
    implements(IHugeVocabulary)
 
1879
 
 
1880
    displayname = "Choose a difference"
 
1881
    step_title = 'Search'
 
1882
 
 
1883
    def __init__(self, context):
 
1884
        """Create a new vocabulary for the context.
 
1885
 
 
1886
        :type context: `IDistroSeries`.
 
1887
        """
 
1888
        assert IDistroSeries.providedBy(context)
 
1889
        self.distroseries = context
 
1890
 
 
1891
    def __len__(self):
 
1892
        """See `IIterableVocabulary`."""
 
1893
        return self.searchForDifferences().count()
 
1894
 
 
1895
    def __iter__(self):
 
1896
        """See `IIterableVocabulary`."""
 
1897
        for difference in self.searchForDifferences():
 
1898
            yield self.toTerm(difference)
 
1899
 
 
1900
    def __contains__(self, value):
 
1901
        """See `IVocabulary`."""
 
1902
        return (
 
1903
            IDistroSeriesDifference.providedBy(value) and
 
1904
            value.derived_series == self.distroseries)
 
1905
 
 
1906
    def getTerm(self, value):
 
1907
        """See `IVocabulary`."""
 
1908
        if value not in self:
 
1909
            raise LookupError(value)
 
1910
        return self.toTerm(value)
 
1911
 
 
1912
    def getTermByToken(self, token):
 
1913
        """See `IVocabularyTokenized`."""
 
1914
        if not token.isdigit():
 
1915
            raise LookupError(token)
 
1916
        difference = IStore(DistroSeriesDifference).get(
 
1917
            DistroSeriesDifference, int(token))
 
1918
        if difference is None:
 
1919
            raise LookupError(token)
 
1920
        elif difference.derived_series != self.distroseries:
 
1921
            raise LookupError(token)
 
1922
        else:
 
1923
            return self.toTerm(difference)
 
1924
 
 
1925
    @staticmethod
 
1926
    def toTerm(dsd):
 
1927
        """Return the term for a `DistroSeriesDifference`."""
 
1928
        return SimpleTerm(dsd, dsd.id)
 
1929
 
 
1930
    def searchForTerms(self, query=None):
 
1931
        """See `IHugeVocabulary`."""
 
1932
        results = self.searchForDifferences()
 
1933
        return CountableIterator(results.count(), results, self.toTerm)
 
1934
 
 
1935
    def searchForDifferences(self):
 
1936
        """The set of `DistroSeriesDifference`s related to the context.
 
1937
 
 
1938
        :return: `IResultSet` yielding `IDistroSeriesDifference`.
 
1939
        """
 
1940
        return DistroSeriesDifference.getForDistroSeries(self.distroseries)
 
1941
 
 
1942
 
1869
1943
class PillarVocabularyBase(NamedSQLObjectHugeVocabulary):
1870
1944
    """Active `IPillar` objects vocabulary."""
1871
1945
    displayname = 'Needs to be overridden'