Back to index

plone3  3.1.7
registry.py
Go to the documentation of this file.
00001 
00002 from kss.demo.interfaces import IKSSDemoRegistry
00003 from zope.publisher.interfaces.browser import IBrowserPublisher
00004 from zope.publisher.interfaces import NotFound
00005 from zope.component import getUtility
00006 from zope.interface import implements
00007 from kss.demo.selenium_utils.builder import cookSeleniumTests
00008 import os.path
00009 try:
00010     from Products.Five import BrowserView
00011     BrowserView         # make pyflakes happy
00012 except ImportError:
00013      from zope.publisher.browser import BrowserView
00014 
00015 class KSSDemoRegistryView(BrowserView):
00016 
00017     def getSortedDemos(self):
00018         """Get demos"""
00019         registry = getUtility(IKSSDemoRegistry)
00020         return registry.getSortedDemos()
00021 
00022     def getDemoGroups(self):
00023         """Get demos groupped by plugin_namespace, category"""
00024         demo_groups = []
00025         prev_plugin_namespace, prev_category = None, None
00026         group = None
00027         for demo in self.getSortedDemos():
00028             plugin_namespace = demo['plugin_namespace']
00029             category =  demo['category']
00030             # Set a flag on first rows in group, 
00031             # since z3 seems not to  handle rpeeat(..).first()
00032             # These will be used for grouping
00033             if prev_plugin_namespace != plugin_namespace:
00034                 is_first_plugin_namespace = True
00035                 prev_plugin_namespace = plugin_namespace
00036             else:
00037                 is_first_plugin_namespace = False
00038             if prev_category != category:
00039                 is_first_category = True
00040                 prev_category = category
00041             else:
00042                 is_first_category = False
00043             # If plugin_namespace or category changed, time to
00044             # start a new group.
00045             if is_first_plugin_namespace or is_first_category:
00046                 # Start a new group.
00047                 group = []
00048                 demo_groups.append(dict(
00049                     plugin_namespace = plugin_namespace,
00050                     category = category,
00051                     demos = group,
00052                     is_first_plugin_namespace = is_first_plugin_namespace,
00053                     is_first_category = is_first_category,
00054                     ))
00055             # In any case append our demo to the group
00056             group.append(demo)
00057         return demo_groups
00058 
00059     @staticmethod
00060     def _filename_to_title(filename):
00061         """Automatic conversion of filename to readable title"""
00062         if filename.lower().endswith('.html'):
00063             filename = filename[:-5]
00064         elif filename.lower().endswith('.htm'):
00065             filename = filename[:-4]
00066         words = os.path.basename(filename).split('_')
00067         # if the first word is a number: it is ignored in the title
00068         # This can be used to somelimited ordering, since filenames
00069         # are listed in alphabetical order, ie. 00_xxx.html will come first.
00070         if not words[0] or '0' <= words[0][0] <= '9':
00071             words = words[1:]
00072         # Compile the result.
00073         result = ' '.join([word.capitalize() for word in words])
00074         return result
00075 
00076     @staticmethod
00077     def _filename_to_href(filename):
00078         """Automatic conversion of filename to publication href"""
00079         return "@@resource?filename=%s" % (filename, )
00080 
00081     def getSeleniumTests(self):
00082         """Get selenium tests annotated with title and href."""
00083         return [dict(
00084                     href = self._filename_to_href(filename),
00085                     title=self._filename_to_title(filename),
00086                     )
00087                     for filename in self.selenium_test_filenames]
00088 
00089     @property
00090     def selenium_test_filenames(self):
00091         """A cached representation of all filenames."""
00092         try:
00093             return self._cooked_selenium_test_filenames
00094         except AttributeError:
00095             registry = getUtility(IKSSDemoRegistry)
00096             self._cooked_selenium_test_filenames =  filenames = \
00097                     registry.selenium_tests
00098             return filenames
00099 
00100     def getSeleniumTestResource(self, filename):
00101         """Return the html resource, whose absolute filename is given,"""
00102         # First of all, let's check if this is one of our file.
00103         # (Refuse otherwise - we don't want access to all files on the server.)
00104         if filename not in self.selenium_test_filenames:
00105             raise Exception, "Nonexistent resource"
00106         # Return the file's content.
00107         self.request.response.setHeader('Content-type', 'text/html;charset=utf-8')
00108         return file(filename).read()
00109 
00110     def getZuiteHomePage(self):
00111         """Redirects to the Zuite home page, Zuite object is found from path."""
00112         zuite = self.getZuite()
00113         html = "%s/core/TestRunner.html?test=%s/suite.html" % (zuite.absolute_url(), self.context.absolute_url())
00114         return self.request.response.redirect(html)
00115 
00116     def getZuite(self):
00117         """Finds a zuite in the same directory or under"""
00118         if not hasattr(self, 'zuites'):
00119             container = self.context.aq_inner.aq_parent
00120             self.zuites = self.context.ZopeFind(container, obj_metatypes=['Zuite'], search_sub=1)
00121         if self.zuites:
00122             return self.zuites[0][1]
00123         else:
00124             return None
00125 
00126 class KSSDemoRegistryAdminView(BrowserView):
00127     """Things that only admin should do"""
00128     implements(IBrowserPublisher)
00129 
00130     # Zope3 requires the implementation of
00131     # IBrowserPublisher, in order for the methods
00132     # to be traversable.
00133     #
00134     # An alternative would be:
00135     # <browser:pages class="...">
00136     #   <page name="..." attribute="..." />
00137     #   <page name="..." attribute="..." />
00138     # </browser:pages>
00139 
00140     def publishTraverse(self, request, name):
00141         try:
00142             return getattr(self, name)
00143         except AttributeError:
00144             raise NotFound(self.context, name, request)
00145 
00146     def browserDefault(self, request):
00147         # make ui the default method
00148         return self, ('cookSeleniumTests', )
00149 
00150     # --
00151     # Accessable methods
00152     # --
00153 
00154     def cookSeleniumTests(self):
00155         """Cook selenium tests
00156 
00157         The *.html tests from each plugin are produced
00158         into the file seltest_all.pty in the directory
00159         of kss.demo.selenium_utils .
00160         """
00161         registry = getUtility(IKSSDemoRegistry)
00162         filenames = registry.selenium_tests
00163         # Cook them. This will create seltest_all.py. 
00164         cookSeleniumTests(filenames)
00165         # We are done.
00166         return "Selenium tests cooked OK. (%i)" % (len(filenames), )