← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~andrey-fedoseev/launchpad:fix-cvereport into launchpad:master

 

Andrey Fedoseev has proposed merging ~andrey-fedoseev/launchpad:fix-cvereport into launchpad:master.

Commit message:
CVEReportView: use lazy loading for `open_cve_bugtasks` and `resolved_cve_bugtasks`


Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~andrey-fedoseev/launchpad/+git/launchpad/+merge/431036

Previously, these attributes were computed on view initialization, with the exception of the case when the context is a Distribution with no series.

Now, the attributes are always available, regardless of the context, but are computed on demand, when they are actually used.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~andrey-fedoseev/launchpad:fix-cvereport into launchpad:master.
diff --git a/lib/lp/bugs/browser/cvereport.py b/lib/lp/bugs/browser/cvereport.py
index 393cd91..e2f2f7d 100644
--- a/lib/lp/bugs/browser/cvereport.py
+++ b/lib/lp/bugs/browser/cvereport.py
@@ -8,15 +8,17 @@ __all__ = [
     "CVEReportView",
 ]
 
+from typing import List, NamedTuple
+
 from zope.component import getUtility
 
 from lp.bugs.browser.buglisting import BugTaskListingItem
 from lp.bugs.interfaces.bugtask import RESOLVED_BUGTASK_STATUSES, IBugTaskSet
 from lp.bugs.interfaces.bugtasksearch import BugTaskSearchParams
 from lp.bugs.interfaces.cve import ICveSet
-from lp.registry.interfaces.distribution import IDistribution
 from lp.registry.interfaces.person import IPersonSet
 from lp.services.helpers import shortlist
+from lp.services.propertycache import cachedproperty
 from lp.services.webapp import LaunchpadView
 from lp.services.webapp.escaping import structured
 from lp.services.webapp.publisher import canonical_url
@@ -37,6 +39,15 @@ class BugTaskCve:
         return self.bugtasks[0].bug
 
 
+BugTaskCves = NamedTuple(
+    "BugTaskCves",
+    (
+        ("open", List[BugTaskCve]),
+        ("resolved", List[BugTaskCve]),
+    ),
+)
+
+
 def get_cve_display_data(cve):
     """Return the data we need for display for the given CVE."""
     return {
@@ -63,18 +74,18 @@ class CVEReportView(LaunchpadView):
         """Update the search params for the context for a specific view."""
         raise NotImplementedError
 
-    def initialize(self):
-        """See `LaunchpadView`."""
-        super().initialize()
-        self.open_cve_bugtasks = []
-        self.resolved_cve_bugtasks = []
+    @property
+    def open_cve_bugtasks(self) -> List[BugTaskCve]:
+        return self._bugtaskcves.open
 
-        # If we are dealing with a distribution with one or more series,
-        # there is no need to deal with the open and resolved CVE bugtasks.
-        # This is because the template only renders links to the CVE report
-        # page of each available series.
-        if IDistribution.providedBy(self.context) and self.context.series:
-            return
+    @property
+    def resolved_cve_bugtasks(self) -> List[BugTaskCve]:
+        return self._bugtaskcves.resolved
+
+    @cachedproperty
+    def _bugtaskcves(self) -> BugTaskCves:
+
+        bugtaskcves = BugTaskCves(open=[], resolved=[])
 
         search_params = BugTaskSearchParams(self.user, has_cve=True)
         bugtasks = shortlist(
@@ -82,7 +93,7 @@ class CVEReportView(LaunchpadView):
         )
 
         if not bugtasks:
-            return
+            return bugtaskcves
 
         bugtask_set = getUtility(IBugTaskSet)
         badge_properties = bugtask_set.getBugTaskBadgeProperties(bugtasks)
@@ -121,13 +132,13 @@ class CVEReportView(LaunchpadView):
 
         # Order the dictionary items by bug ID and then store only the
         # bugtaskcve objects.
-        self.open_cve_bugtasks = [
+        bugtaskcves.open.extend(
             bugtaskcve for bug, bugtaskcve in sorted(open_bugtaskcves.items())
-        ]
-        self.resolved_cve_bugtasks = [
+        )
+        bugtaskcves.resolved.extend(
             bugtaskcve
             for bug, bugtaskcve in sorted(resolved_bugtaskcves.items())
-        ]
+        )
 
         # The page contains links to the bug task assignees:
         # Pre-load the related Person and ValidPersonCache records
@@ -138,6 +149,8 @@ class CVEReportView(LaunchpadView):
             )
         )
 
+        return bugtaskcves
+
     def renderCVELinks(self, cves):
         """Render the CVE links related to the given bug.