Commits

Anonymous committed 6ec0a33

grid doesnt require pylons dependencies, added GridPylons class that supports sorting on column

Comments (0)

Files changed (3)

unfinished/grid.py

-from routes.util import url_for
 from webhelpers.html.builder import HTML, literal
 
 class Grid(object):
     
-    def default_column_format(self,i,record,column_name):
+    def default_column_format(self, i, record, column_name):
         return HTML.tag('td', record[column_name])
     
-    def numbered_column_format(self,i,record):
+    def numbered_column_format(self, i, record):
         return HTML.tag('td', i)
     
     def default_record_format(self, i, record, class_name, columns):
-        return HTML.tag('tr', columns,class_=class_name)
+        return HTML.tag('tr', columns, class_=class_name)
 
     def default_record_format(self, i, record, class_name, columns):
-        return HTML.tag('tr', columns,class_=class_name)
+        return HTML.tag('tr', columns, class_=class_name)
 
     def default_header_record_format(self, headers):
-        return HTML.tag('tr', headers,class_='header')
+        return HTML.tag('tr', headers, class_='header')
 
-    def default_header_ordered_column_format(self, column_number, order, header_label):
-        header_label = HTML(header_label,HTML.tag('span',class_='marker'))
-        class_name = 'column%s ordering' % (column_number)
+    def default_header_ordered_column_format(self, column_number, order, column_name, header_label):
+        header_label = HTML(header_label, HTML.tag('span', class_='marker'))
+        class_name = 'column%s ordering %s %s' % (column_number, order, column_name)
         return HTML.tag('td', header_label, class_=class_name)
 
-    def default_header_column_format(self, column_number, header_label):
-        class_name = 'column%s' % (column_number)
+    def default_header_column_format(self, column_number, column_name, header_label):
+        class_name = 'column%s %s' % (column_number, column_name)
         return HTML.tag('td', header_label, class_=class_name)
     
-    def default_header_link(self,url,content):
-        return HTML.tag("a", href=url, c=content)
+    def generate_header_link(self, column_number, column, label_text):
+        return None
     
-    def __init__(self, itemlist, columns=None, order_by=None, format=None,
-        start_number=1):
+    def __init__(self, itemlist, *args, **kwargs):
         self.custom_record_format = None
-        
         self.labels = {}
         self.exclude_ordering = ['_numbered']
         self.itemlist = itemlist
+        columns = kwargs['columns']
         if '_numbered' in columns:
             self.labels['_numbered'] = 'no.'            
         self.columns = columns
-        self.order_column = order_by
-        self.format = format or {}
-        self._start_number = start_number
+        self.format = kwargs.get('format', {})
+        self._start_number = kwargs.get('start_number', 1)
+        
 
     def make_headers(self):
         header_columns = []
+            
         for i, column in enumerate(self.columns):
-            #lets generate header column contents
+            # let's generate header column contents
             label_text = ''
             if column in self.labels:
                 label_text = self.labels[column]
             else:
                 label_text = column.replace('_', ' ').title()
-            if column not in self.exclude_ordering:
-                if self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'asc':
-                    new_ordering = column + '_dsc'
+            # handle non clickable columns
+            if column in self.exclude_ordering:
+                if column == '_numbered':
+                    header = self.default_header_column_format(i + 1, 'numbered', label_text)
                 else:
-                    new_ordering = column + '_asc'
-                label_text = self.default_header_link(url_for(order_by=new_ordering, **request_copy.GET), label_text)
-                #lets test if the column we try to order on is this column
-                if self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'asc':
-                    header_columns.append(self.default_header_ordered_column_format(i+1,'asc', label_text))
-                elif self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'dsc':
-                    header_columns.append(self.default_header_ordered_column_format(i+1,'dsc', label_text))
-                else:
-                    header_columns.append(self.default_header_column_format(i+1,label_text))    
+                    header = self.default_header_column_format(i + 1, column, label_text)
+            # handle clickable columns
             else:
-                header_columns.append(self.default_header_column_format(i+1,label_text))
+                header = self.generate_header_link(i + 1, column, label_text)
+                if header is None:
+                    header = self.default_header_column_format(i + 1, column, label_text)                    
+            header_columns.append(header)               
         return HTML(*header_columns)
     
     def make_columns(self, i, record):
                 else:
                     columns.append(self.default_column_format(i, record, column))     
         else:
-            for i,column in enumerate(self.columns):
+            for y, column in enumerate(self.columns):
                 if column in self.format:
                     columns.append(self.format[column](i, record))
                 else:
             else:
                 class_name = 'odd'
             if self.custom_record_format is None:
-                records.append(self.default_record_format(i, record, class_name,self.make_columns(i, record)))
+                records.append(self.default_record_format(i, record, class_name, self.make_columns(i, record)))
             else:
                 records.append(self.custom_record_format(i, record, class_name, self.make_columns(i, record)))
 
         return HTML(*records)
     
-    __str__ = __html__
+    def __str__(self):
+        return self.__html__()

unfinished/grid_pylons.py

+from pylons import request, url
+from webhelpers.html.builder import HTML, literal
+import grid
+
+class NoRequestException(Exception):
+    pass
+
+class GridPylons(grid.Grid):
+    
+    def generate_header_link(self, column_number, column, label_text):
+        # this will handle possible url generation
+        if not hasattr(self, 'request'):
+            raise NoRequestException('Could not find self.request for this grid')
+        request_copy = self.request.copy().GET
+        if 'order_by' in request_copy:
+            self.order_column = request_copy.pop('order_by')
+        else:
+            self.order_column = None
+            
+        if self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'asc':
+            new_ordering = column + '_dsc'
+        else:
+            new_ordering = column + '_asc'
+        url_href = url(order_by=new_ordering, **request_copy)        
+        label_text = HTML.tag("a", href=url_href, c=label_text)
+        #lets test if the column we try to order on is this column
+        if self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'asc':
+            return self.default_header_ordered_column_format(i, 'asc', column, label_text)
+        elif self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'dsc':
+            return self.default_header_ordered_column_format(i, 'dsc', column, label_text)
+        else:
+            return self.default_header_column_format(i, column, label_text)
+        

unfinished/grid_test.py

+import grid
+import grid_pylons
+from webhelpers.html.builder import HTML, literal
+
+test_data = [
+             {'group_name':'foo','options':'lalala'},
+             {'group_name':'foo2','options':'lalala2'},
+             {'group_name':'foo3','options':'lalala3'},
+             {'group_name':'foo4','options':'lalala4'},
+             ]
+
+test_grid = grid.Grid(test_data, columns=['_numbered','group_name','options'])
+test_grid.exclude_ordering = ['options']
+test_grid.format = {
+'options':lambda i,item: HTML.tag('td', 'baz')
+}
+
+
+print '<table>'
+print test_grid
+print '/<table>'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.