~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-02 07:46:59 UTC
  • mfrom: (13481.1.7 pillarname-vocab)
  • Revision ID: launchpad@pqm.canonical.com-20110802074659-0axtgug203q0qf1s
[r=sinzui][bug=541979] PillarVocabularyBase has been Storm'd,
 and the results are first ordered by exact match first,
 and secondly limited to 100.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1944
1944
    """Active `IPillar` objects vocabulary."""
1945
1945
    displayname = 'Needs to be overridden'
1946
1946
    _table = PillarName
1947
 
    _orderBy = 'name'
 
1947
    _limit = 100
1948
1948
 
1949
1949
    def toTerm(self, obj):
1950
1950
        """See `IVocabulary`."""
 
1951
        if type(obj) == int:
 
1952
            return self.toTerm(PillarName.get(obj))
1951
1953
        if IPillarName.providedBy(obj):
1952
1954
            assert obj.active, 'Inactive object %s %d' % (
1953
1955
                    obj.__class__.__name__, obj.id)
1968
1970
    def __contains__(self, obj):
1969
1971
        raise NotImplementedError
1970
1972
 
 
1973
    def searchForTerms(self, query=None):
 
1974
        if not query:
 
1975
            return self.emptySelectResults()
 
1976
        query = ensure_unicode(query).lower()
 
1977
        store = IStore(PillarName)
 
1978
        equal_clauses = [PillarName.name == query]
 
1979
        like_clauses = [
 
1980
            PillarName.name != query, PillarName.name.contains_string(query)]
 
1981
        if self._filter:
 
1982
            equal_clauses.extend(self._filter)
 
1983
            like_clauses.extend(self._filter)
 
1984
        ranked_results = store.execute(
 
1985
            Union(
 
1986
                Select(
 
1987
                    (PillarName.id, PillarName.name, SQL('100 AS rank')),
 
1988
                    tables=[PillarName],
 
1989
                    where=And(*equal_clauses)),
 
1990
                Select(
 
1991
                    (PillarName.id, PillarName.name, SQL('50 AS rank')),
 
1992
                    tables=[PillarName],
 
1993
                    where=And(*like_clauses)),
 
1994
                limit=self._limit, order_by=(
 
1995
                    Desc(SQL('rank')), PillarName.name), all=True))
 
1996
        results = [row[0] for row in list(ranked_results)]
 
1997
        return self.iterator(len(results), results, self.toTerm)
 
1998
 
1971
1999
 
1972
2000
class DistributionOrProductVocabulary(PillarVocabularyBase):
1973
2001
    """Active `IDistribution` or `IProduct` objects vocabulary."""
1974
2002
    displayname = 'Select a project'
1975
 
    _filter = """
1976
 
        -- An active product/distro.
1977
 
        ((active IS TRUE
1978
 
         AND (product IS NOT NULL OR distribution IS NOT NULL)
1979
 
        )
1980
 
        OR
1981
 
        -- Or an alias for an active product/distro.
1982
 
        (alias_for IN (
1983
 
            SELECT id FROM PillarName
1984
 
            WHERE active IS TRUE AND
1985
 
                (product IS NOT NULL OR distribution IS NOT NULL))
1986
 
        ))
1987
 
        """
 
2003
    _filter = [PillarName.project == None, PillarName.active == True]
1988
2004
 
1989
2005
    def __contains__(self, obj):
1990
2006
        if IProduct.providedBy(obj):
1997
2013
class DistributionOrProductOrProjectGroupVocabulary(PillarVocabularyBase):
1998
2014
    """Active `IProduct`, `IProjectGroup` or `IDistribution` vocabulary."""
1999
2015
    displayname = 'Select a project'
2000
 
    _filter = PillarName.q.active == True
 
2016
    _filter = [PillarName.active == True]
2001
2017
 
2002
2018
    def __contains__(self, obj):
2003
2019
        if IProduct.providedBy(obj) or IProjectGroup.providedBy(obj):