~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/blueprints/vocabularies/specificationdependency.py

[r=thumper][ui=none][no-qa] Improve test coverage and implementation
        of SpecificationDepCandidatesVocabulary

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
                    spec.target == self.context.target
56
56
                    and spec not in self.context.all_blocked)]
57
57
 
58
 
    def _doSearch(self, query):
59
 
        """Return terms where query is in the text of name
60
 
        or title, or matches the full text index.
61
 
        """
62
 
 
 
58
    def toTerm(self, obj):
 
59
        return SimpleTerm(obj, obj.name, obj.title)
 
60
 
 
61
    def getTermByToken(self, token):
 
62
        """See `zope.schema.interfaces.IVocabularyTokenized`.
 
63
 
 
64
        The tokens for specifications are just the name of the spec.
 
65
        """
 
66
        spec = self.context.target.getSpecification(token)
 
67
        if spec is not None:
 
68
            filtered = self._filter_specs([spec])
 
69
            if len(filtered) > 0:
 
70
                return self.toTerm(filtered[0])
 
71
        raise LookupError(token)
 
72
 
 
73
    def search(self, query):
 
74
        """See `SQLObjectVocabularyBase.search`.
 
75
 
 
76
        We find specs where query is in the text of name or title, or matches
 
77
        the full text index and then filter out ineligible specs using
 
78
        `_filter_specs`.
 
79
        """
63
80
        if not query:
64
 
            return []
65
 
 
 
81
            return CountableIterator(0, [])
66
82
        quoted_query = quote_like(query)
67
83
        sql_query = ("""
68
84
            (Specification.name LIKE %s OR
71
87
            """
72
88
            % (quoted_query, quoted_query, quoted_query))
73
89
        all_specs = Specification.select(sql_query, orderBy=self._orderBy)
74
 
 
75
 
        return self._filter_specs(all_specs)
76
 
 
77
 
    def toTerm(self, obj):
78
 
        return SimpleTerm(obj, obj.name, obj.title)
79
 
 
80
 
    def getTermByToken(self, token):
81
 
        search_results = self._doSearch(token)
82
 
        for search_result in search_results:
83
 
            if search_result.name == token:
84
 
                return self.toTerm(search_result)
85
 
        raise LookupError(token)
86
 
 
87
 
    def search(self, query):
88
 
        candidate_specs = self._doSearch(query)
89
 
        return CountableIterator(len(candidate_specs),
90
 
                                 candidate_specs)
91
 
 
 
90
        candidate_specs = self._filter_specs(all_specs)
 
91
        return CountableIterator(len(candidate_specs), candidate_specs)
 
92
 
 
93
    @property
92
94
    def _all_specs(self):
93
 
        all_specs = self.context.target.specifications(
 
95
        return self.context.target.specifications(
94
96
            filter=[SpecificationFilter.ALL],
95
97
            prejoin_people=False)
96
 
        return self._filter_specs(all_specs)
97
98
 
98
99
    def __iter__(self):
99
 
        return (self.toTerm(spec) for spec in self._all_specs())
 
100
        return (self.toTerm(spec)
 
101
                for spec in self._filter_specs(self._all_specs))
100
102
 
101
103
    def __contains__(self, obj):
102
 
        # We don't use self._all_specs here, since it will call
103
 
        # self._filter_specs(all_specs) which will cause all the specs
104
 
        # to be loaded, whereas obj in all_specs will query a single object.
105
 
        all_specs = self.context.target.specifications(
106
 
            filter=[SpecificationFilter.ALL],
107
 
            prejoin_people=False)
108
 
        return obj in all_specs and len(self._filter_specs([obj])) > 0
 
104
        return obj in self._all_specs and len(self._filter_specs([obj])) > 0