~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/browser/packagesearch.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-12-22 04:55:30 UTC
  • mfrom: (14577.1.1 testfix)
  • Revision ID: launchpad@pqm.canonical.com-20111222045530-wki9iu6c0ysqqwkx
[r=wgrant][no-qa] Fix test_publisherconfig lpstorm import. Probably a
        silent conflict between megalint and apocalypse.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
__all__ = [
 
7
    'PackageSearchViewBase'
 
8
    ]
 
9
 
 
10
from canonical.launchpad.webapp.batching import BatchNavigator
 
11
from canonical.launchpad.webapp.publisher import LaunchpadView
 
12
from lp.services.propertycache import cachedproperty
 
13
 
 
14
 
 
15
class PackageSearchViewBase(LaunchpadView):
 
16
    """A common package search interface"""
 
17
 
 
18
    def initialize(self):
 
19
        """Save the search text set by the user."""
 
20
        self.text = self.request.get("text", None)
 
21
        if self.text is not None:
 
22
            # The user may have URL hacked a query string with more than one
 
23
            # "text" parameter. We'll take the last one.
 
24
            if isinstance(self.text, list):
 
25
                self.text = self.text[-1]
 
26
            self.text = self.text.strip()
 
27
            # We need to ensure the form on the refreshed page shows the
 
28
            # correct text.
 
29
            self.request.form['text'] = self.text
 
30
 
 
31
    @property
 
32
    def search_requested(self):
 
33
        """Return whether the current view included a search request."""
 
34
        return self.text is not None
 
35
 
 
36
    @cachedproperty
 
37
    def matches(self):
 
38
        """Return the number of matched search results."""
 
39
        return self.batchnav.batch.total()
 
40
 
 
41
    @property
 
42
    def detailed(self):
 
43
        """Return whether detailed results should be provided."""
 
44
        return self.matches <= 5
 
45
 
 
46
    @cachedproperty
 
47
    def batchnav(self):
 
48
        """Return the batch navigator for the search results."""
 
49
        return BatchNavigator(self.search_results, self.request)
 
50
 
 
51
    @cachedproperty
 
52
    def search_results(self):
 
53
        """Search for packages matching the request text.
 
54
 
 
55
        Try to find the packages that match the given text, then present
 
56
        those as a list. Cache previous results so the search is only done
 
57
        once.
 
58
        """
 
59
        return self.contextSpecificSearch()
 
60
 
 
61
    def contextSpecificSearch(self):
 
62
        """Call the context specific search."""
 
63
        raise NotImplementedError(
 
64
            "do_context_specific_search needs to be implemented in sub-class"
 
65
            )