~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-21 21:32:29 UTC
  • mfrom: (13635.3.18 filter-target-pickers)
  • Revision ID: launchpad@pqm.canonical.com-20110821213229-mya8li508jpsj2qw
[r=sinzui][bug=820011] Wire up the vocab filtering capability to the
 pickers to allow picker search results to be filtered using named vocab
 filters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
from canonical.launchpad.webapp.vocabulary import (
125
125
    BatchedCountableIterator,
126
126
    CountableIterator,
 
127
    FilteredVocabularyBase,
127
128
    IHugeVocabulary,
128
129
    NamedSQLObjectHugeVocabulary,
129
130
    NamedSQLObjectVocabulary,
130
131
    SQLObjectVocabularyBase,
 
132
    VocabularyFilter,
131
133
    )
132
134
from lp.app.browser.tales import DateTimeFormatterAPI
133
135
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
902
904
        text = ensure_unicode(text).lower()
903
905
        return self._doSearch(text=text)
904
906
 
905
 
    def searchForTerms(self, query=None):
 
907
    def searchForTerms(self, query=None, vocab_filter=None):
906
908
        """See `IHugeVocabulary`."""
907
909
        results = self.search(query)
908
910
        return CountableIterator(results.count(), results, self.toTerm)
1141
1143
        return obj in self._get_teams()
1142
1144
 
1143
1145
 
1144
 
class ActiveMailingListVocabulary:
 
1146
class ActiveMailingListVocabulary(FilteredVocabularyBase):
1145
1147
    """The set of all active mailing lists."""
1146
1148
 
1147
1149
    implements(IHugeVocabulary)
1221
1223
            """ % sqlvalues(text, MailingListStatus.ACTIVE),
1222
1224
            clauseTables=['Person'])
1223
1225
 
1224
 
    def searchForTerms(self, query=None):
 
1226
    def searchForTerms(self, query=None, vocab_filter=None):
1225
1227
        """See `IHugeVocabulary`."""
1226
1228
        results = self.search(query)
1227
1229
        return CountableIterator(results.count(), results, self.toTerm)
1644
1646
                return self.toTerm(search_result)
1645
1647
        raise LookupError(token)
1646
1648
 
1647
 
    def searchForTerms(self, query=None):
 
1649
    def searchForTerms(self, query=None, vocab_filter=None):
1648
1650
        """See `SQLObjectVocabularyBase`."""
1649
1651
        results = self._doSearch(query)
1650
1652
        if type(results) is list:
1749
1751
        return objs
1750
1752
 
1751
1753
 
1752
 
class DistroSeriesDerivationVocabulary:
 
1754
class DistroSeriesDerivationVocabulary(FilteredVocabularyBase):
1753
1755
    """A vocabulary source for series to derive from.
1754
1756
 
1755
1757
    Once a distribution has a series that has derived from a series in another
1816
1818
        title = "%s: %s" % (series.distribution.displayname, series.title)
1817
1819
        return SimpleTerm(series, series.id, title)
1818
1820
 
1819
 
    def searchForTerms(self, query=None):
 
1821
    def searchForTerms(self, query=None, vocab_filter=None):
1820
1822
        """See `IHugeVocabulary`."""
1821
1823
        results = self.searchParents(query)
1822
1824
        return CountableIterator(len(results), results, self.toTerm)
1871
1873
            return self.find_terms(where)
1872
1874
 
1873
1875
 
1874
 
class DistroSeriesDifferencesVocabulary:
 
1876
class DistroSeriesDifferencesVocabulary(FilteredVocabularyBase):
1875
1877
    """A vocabulary source for differences relating to a series.
1876
1878
 
1877
1879
    Specifically, all `DistroSeriesDifference`s relating to a derived series.
1929
1931
        """Return the term for a `DistroSeriesDifference`."""
1930
1932
        return SimpleTerm(dsd, dsd.id)
1931
1933
 
1932
 
    def searchForTerms(self, query=None):
 
1934
    def searchForTerms(self, query=None, vocab_filter=None):
1933
1935
        """See `IHugeVocabulary`."""
1934
1936
        results = self.searchForDifferences()
1935
1937
        return CountableIterator(results.count(), results, self.toTerm)
1942
1944
        return DistroSeriesDifference.getForDistroSeries(self.distroseries)
1943
1945
 
1944
1946
 
 
1947
class VocabularyFilterProject(VocabularyFilter):
 
1948
    # A filter returning just projects.
 
1949
 
 
1950
    def __new__(cls):
 
1951
        return super(VocabularyFilter, cls).__new__(
 
1952
            cls, 'PROJECT', 'Product',
 
1953
            'Display search results associated with products')
 
1954
 
 
1955
    @property
 
1956
    def filter_terms(self):
 
1957
        return [PillarName.product != None]
 
1958
 
 
1959
 
 
1960
class VocabularyFilterProjectGroup(VocabularyFilter):
 
1961
    # A filter returning just project groups.
 
1962
 
 
1963
    def __new__(cls):
 
1964
        return super(VocabularyFilter, cls).__new__(
 
1965
            cls, 'PROJECTGROUP', 'Project',
 
1966
            'Display search results associated with projects')
 
1967
 
 
1968
    @property
 
1969
    def filter_terms(self):
 
1970
        return [PillarName.project != None]
 
1971
 
 
1972
 
 
1973
class VocabularyFilterDistribution(VocabularyFilter):
 
1974
    # A filter returning just distros.
 
1975
 
 
1976
    def __new__(cls):
 
1977
        return super(VocabularyFilter, cls).__new__(
 
1978
            cls, 'DISTRO', 'Distribution',
 
1979
            'Display search results associated with distributions')
 
1980
 
 
1981
    @property
 
1982
    def filter_terms(self):
 
1983
        return [PillarName.distribution != None]
 
1984
 
 
1985
 
1945
1986
class PillarVocabularyBase(NamedSQLObjectHugeVocabulary):
1946
1987
    """Active `IPillar` objects vocabulary."""
1947
1988
    displayname = 'Needs to be overridden'
1948
1989
    _table = PillarName
1949
1990
    _limit = 100
1950
1991
 
 
1992
    PROJECT_FILTER = VocabularyFilterProject()
 
1993
    PROJECTGROUP_FILTER = VocabularyFilterProjectGroup()
 
1994
    DISTRO_FILTER = VocabularyFilterDistribution()
 
1995
 
 
1996
    def supportedFilters(self):
 
1997
        return [self.ALL_FILTER]
 
1998
 
1951
1999
    def toTerm(self, obj):
1952
2000
        """See `IVocabulary`."""
1953
2001
        if type(obj) == int:
1972
2020
    def __contains__(self, obj):
1973
2021
        raise NotImplementedError
1974
2022
 
1975
 
    def searchForTerms(self, query=None):
 
2023
    def searchForTerms(self, query=None, vocab_filter=None):
1976
2024
        if not query:
1977
2025
            return self.emptySelectResults()
1978
2026
        query = ensure_unicode(query).lower()
1983
2031
        if self._filter:
1984
2032
            equal_clauses.extend(self._filter)
1985
2033
            like_clauses.extend(self._filter)
 
2034
        if vocab_filter:
 
2035
            equal_clauses.extend(vocab_filter.filter_terms)
 
2036
            like_clauses.extend(vocab_filter.filter_terms)
1986
2037
        ranked_results = store.execute(
1987
2038
            Union(
1988
2039
                Select(
2011
2062
        else:
2012
2063
            return IDistribution.providedBy(obj)
2013
2064
 
 
2065
    def supportedFilters(self):
 
2066
        return [
 
2067
            self.ALL_FILTER,
 
2068
            self.PROJECT_FILTER,
 
2069
            self.DISTRO_FILTER]
 
2070
 
2014
2071
 
2015
2072
class DistributionOrProductOrProjectGroupVocabulary(PillarVocabularyBase):
2016
2073
    """Active `IProduct`, `IProjectGroup` or `IDistribution` vocabulary."""
2024
2081
        else:
2025
2082
            return IDistribution.providedBy(obj)
2026
2083
 
 
2084
    def supportedFilters(self):
 
2085
        return [
 
2086
            self.ALL_FILTER,
 
2087
            self.PROJECT_FILTER,
 
2088
            self.PROJECTGROUP_FILTER,
 
2089
            self.DISTRO_FILTER]
 
2090
 
2027
2091
 
2028
2092
class FeaturedProjectVocabulary(
2029
2093
                               DistributionOrProductOrProjectGroupVocabulary):
2071
2135
            token.lower())
2072
2136
 
2073
2137
 
2074
 
class DistributionSourcePackageVocabulary:
 
2138
class DistributionSourcePackageVocabulary(FilteredVocabularyBase):
2075
2139
 
2076
2140
    implements(IHugeVocabulary)
2077
2141
    displayname = 'Select a package'
2149
2213
        distribution, package_name = self.getDistributionAndPackageName(token)
2150
2214
        return self.toTerm(package_name, distribution)
2151
2215
 
2152
 
    def searchForTerms(self, query=None):
 
2216
    def searchForTerms(self, query=None, vocab_filter=None):
2153
2217
        """See `IHugeVocabulary`."""
2154
2218
        if not query:
2155
2219
            return EmptyResultSet()