~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/linkchecker.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-23 06:02:34 UTC
  • mfrom: (13570.1.16 4595-upgrade-bug-linking)
  • Revision ID: launchpad@pqm.canonical.com-20110823060234-3bjpplcbvfqbzjd6
[r=benji,
        stub] [r=stub][bug=4595] Distinguish between valid and invalid bugs
        when autolinkifying bugs

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
import simplejson
12
12
from zope.component import getUtility
13
13
 
 
14
from canonical.launchpad.searchbuilder import any
 
15
from canonical.launchpad.webapp import LaunchpadView
 
16
 
14
17
from lp.app.errors import NotFoundError
15
18
from lp.code.errors import (
16
19
    CannotHaveLinkedBranch,
19
22
    NoSuchBranch,
20
23
    )
21
24
from lp.code.interfaces.branchlookup import IBranchLookup
 
25
from lp.bugs.interfaces.bugtask import BugTaskSearchParams, IBugTaskSet
 
26
from lp.registry.interfaces.person import IPerson
22
27
from lp.registry.interfaces.product import InvalidProductName
23
28
 
24
29
 
25
 
class LinkCheckerAPI:
 
30
class LinkCheckerAPI(LaunchpadView):
26
31
    """Validates Launchpad shortcut links.
27
32
 
28
33
    This class provides the endpoint of an Ajax call to .../+check-links.
47
52
        # Each link type has it's own validation method.
48
53
        self.link_checkers = dict(
49
54
            branch_links=self.check_branch_links,
 
55
            bug_links=self.check_bug_links,
50
56
        )
51
57
 
52
58
    def __call__(self):
78
84
                invalid_links[link] = self._error_message(e)
79
85
        return invalid_links
80
86
 
 
87
    def check_bug_links(self, links):
 
88
        """Checks links of the form /bugs/100"""
 
89
        invalid_links = {}
 
90
        user = self.user
 
91
        bugs = [int(link[len('/bugs/'):]) for link in links]
 
92
        if bugs:
 
93
            params = BugTaskSearchParams(
 
94
                user=user, status=None,
 
95
                bug=any(*bugs))
 
96
            bug_ids = getUtility(IBugTaskSet).searchBugIds(params)
 
97
            invalid = set(bugs) - set(bug_ids)
 
98
            for bug in invalid:
 
99
                invalid_links['/bugs/' + str(bug)] = "Bug %s cannot be found" % bug
 
100
        return invalid_links
 
101
         
81
102
    def _error_message(self, ex):
82
103
        if hasattr(ex, 'display_message'):
83
104
            return ex.display_message