You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@bloodhound.apache.org by Anze Staric <an...@gmail.com> on 2013/04/12 09:48:57 UTC

Getting a permission filtered list of products

I need to get a list of products the user is allowed to view for
display in bhsearch breadcrumbs, but creating an instance of
BloodhoundTheme in order to use the existing method seems kind of
wrong. The following patch moves the code to a classmethod of
ProductModule. If someone knows a location better suited for such
helper functions, feel free to suggest it.


Anze

Index: bloodhound_theme/bhtheme/theme.py
===================================================================
--- bloodhound_theme/bhtheme/theme.py (revision 1466874)
+++ bloodhound_theme/bhtheme/theme.py (working copy)
@@ -52,7 +52,7 @@

 from multiproduct.model import Product
 from multiproduct.env import ProductEnvironment
-from multiproduct.web_ui import PRODUCT_RE
+from multiproduct.web_ui import PRODUCT_RE, ProductModule

 try:
     from multiproduct.ticket.web_ui import ProductTicketModule
@@ -262,7 +262,8 @@
         req.chrome['labels'] = self._get_whitelabelling()

         if data is not None:
-            data['product_list'] = self._get_product_list(req)
+            data['product_list'] = \
+                ProductModule.get_product_list(self.env, req)

         links = req.chrome.get('links',{})
         # replace favicon if appropriate
@@ -347,19 +348,6 @@
         self._modify_resource_breadcrumb(req, template, data, content_type,
                                          is_active)

-    def _get_product_list(self, req, href_fcn=None):
-        """Returns a list of products as (prefix, name, url) tuples
-        """
-        if href_fcn is None:
-            href_fcn = req.href.products
-        product_list = []
-        for product in Product.select(self.env):
-            if 'PRODUCT_VIEW' in req.perm(Neighborhood('product',
product.prefix).
-                                          child(product.resource)):
-                product_list.append((product.prefix, product.name,
-                    href_fcn(product.prefix)))
-        return product_list
-
     def _modify_resource_breadcrumb(self, req, template, data, content_type,
                                     is_active):
         """Provides logic for breadcrumb resource permissions
@@ -381,9 +369,9 @@
     def _modify_admin_breadcrumb(self, req, template, data,
content_type, is_active):
         # override 'normal' product list with the admin one
         glsettings = (None, _('(Global settings)'), req.href.admin())
+        admin_url = lambda x: req.href.products(x, 'admin')
         data['admin_product_list'] = [ glsettings, ] + \
-            self._get_product_list(req,
-                lambda x: req.href.products(x, 'admin'))
+            ProductModule.get_product_list(self.env, req, admin_url)

         if isinstance(req.perm.env, ProductEnvironment):
             product = req.perm.env.product
Index: bloodhound_multiproduct/multiproduct/web_ui.py
===================================================================
--- bloodhound_multiproduct/multiproduct/web_ui.py (revision 1466874)
+++ bloodhound_multiproduct/multiproduct/web_ui.py (working copy)
@@ -188,3 +188,17 @@
         if product and env.is_component_enabled(ProductModule):
             return req.href('products', product, itempath)
         return req.href(itempath)
+
+    @classmethod
+    def get_product_list(cls, env, req, href_fcn=None):
+        """Returns a list of products as (prefix, name, url) tuples
+        """
+        if href_fcn is None:
+            href_fcn = req.href.products
+        product_list = []
+        for product in Product.select(env):
+            if 'PRODUCT_VIEW' in req.perm(Neighborhood('product',
product.prefix).
+                                          child(product.resource)):
+                product_list.append((product.prefix, product.name,
+                    href_fcn(product.prefix)))
+        return product_list

Re: Getting a permission filtered list of products

Posted by Olemis Lang <ol...@gmail.com>.
On 4/12/13, Andrej Golcov <an...@digiverse.si> wrote:
> Agree, it looks like multiproduct plugin is more appropriate place for
> the get_product_list function.
>

FWIW , I agree . In a previous review I highlighted something like
this and mentioned that IMO BH Theme plugin should not depend upon BH
Multiproduct . Even if this fact is not explicit in
setup(install_require=...) MP modules and classes are imported in
there .

-- 
Regards,

Olemis.

Re: Getting a permission filtered list of products

Posted by Andrej Golcov <an...@digiverse.si>.
Agree, it looks like multiproduct plugin is more appropriate place for
the get_product_list function.

Cheers, Andrej