Back to index

plone3  3.1.7
tableview.py
Go to the documentation of this file.
00001 from zope.app.pagetemplate import ViewPageTemplateFile
00002 from plone.app.content.batching import Batch
00003 from plone.memoize import instance
00004 from kss.core import KSSView
00005 
00006 class Table(object):
00007     """   
00008     The table renders a table with sortable columns etc.
00009 
00010     It is meant to be subclassed to provide methods for getting specific table info.
00011     """                
00012 
00013     def __init__(self, request, base_url, view_url, items, show_sort_column=False,
00014                  buttons=[], pagesize=20):
00015         self.request = request
00016         self.context = None # Need for view pagetemplate
00017 
00018         self.base_url = base_url
00019         self.view_url = view_url
00020         self.url = view_url
00021         self.items = items
00022         self.show_sort_column = show_sort_column
00023         self.buttons = buttons
00024         self.default_page_size = 20
00025         self.pagesize = pagesize
00026         self.show_all = request.get('show_all', '').lower() == 'true'
00027 
00028         selection = request.get('select')
00029         if selection == 'screen':
00030             self.selectcurrentbatch=True
00031         elif selection == 'all':
00032             self.selectall = True
00033 
00034         self.pagenumber =  int(request.get('pagenumber', 1))
00035 
00036     @property
00037     def within_batch_size(self):
00038         return len(self.items) < self.pagesize
00039 
00040     def set_checked(self, item):
00041         selected = self.selected(item)
00042         item['checked'] = selected and 'checked' or None
00043         item['table_row_class'] = item.get('table_row_class', '')
00044         if selected:
00045             item['table_row_class'] += ' selected'
00046 
00047     @property
00048     @instance.memoize
00049     def batch(self):
00050         pagesize = self.pagesize
00051         if self.show_all:
00052             pagesize = len(self.items)
00053         b = Batch(self.items,
00054                   pagesize=pagesize,
00055                   pagenumber=self.pagenumber)
00056         map(self.set_checked, b)
00057         return b
00058 
00059     render = ViewPageTemplateFile("table.pt")
00060     batching = ViewPageTemplateFile("batching.pt")
00061 
00062     # options
00063     _selectcurrentbatch = False
00064     _select_all = False
00065 
00066     def _get_select_currentbatch(self):
00067         return self._selectcurrentbatch
00068 
00069     def _set_select_currentbatch(self, value):
00070         self._selectcurrentbatch = value
00071         if self._selectcurrentbatch and self.show_all or (
00072             len(self.items) <= self.pagesize):
00073             self.selectall = True
00074 
00075     selectcurrentbatch = property(_get_select_currentbatch,
00076                                   _set_select_currentbatch)
00077 
00078     def _get_select_all(self):
00079         return self._select_all
00080 
00081     def _set_select_all(self, value):
00082         self._select_all = bool(value)
00083         if self._select_all:
00084             self._selectcurrentbatch = True
00085 
00086     selectall = property(_get_select_all, _set_select_all)
00087 
00088     @property
00089     def show_select_all_items(self):
00090         return self.selectcurrentbatch and not self.selectall
00091 
00092     def get_nosort_class(self):
00093         """
00094         """
00095         return "nosort"
00096 
00097     @property
00098     def selectall_url(self):
00099         return self.selectnone_url+'&select=all'
00100 
00101     @property
00102     def selectscreen_url(self):
00103         return self.selectnone_url+'&select=screen'
00104 
00105     @property
00106     def selectnone_url(self):
00107         base = self.view_url + '?pagenumber=%s' % (self.pagenumber)
00108         if self.show_all:
00109             base += '&show_all=true'
00110         return base
00111 
00112     @property
00113     def show_all_url(self):
00114         return self.view_url + '?show_all=true'
00115 
00116     def selected(self, item):
00117         if self.selectcurrentbatch:
00118             return True
00119         return False
00120 
00121     @property
00122     def viewname(self):
00123         return self.view_url.split('?')[0].split('/')[-1]
00124 
00125 class TableKSSView(KSSView):
00126     '''Base class which can be used from a KSS view
00127 
00128     Subclasses only need to set the table property to a different
00129     class.'''
00130     
00131     table = None
00132 
00133     def update_table(self, pagenumber='1', sort_on='getObjPositionInParent', show_all=False):
00134         self.request.set('sort_on', sort_on)
00135         self.request.set('pagenumber', pagenumber)
00136         table = self.table(self.context, self.request,
00137                                     contentFilter={'sort_on':sort_on})
00138         core = self.getCommandSet('core')
00139         core.replaceHTML('#folderlisting-main-table', table.render())
00140         return self.render()