Back to index

plone3  3.1.7
addressbook.py
Go to the documentation of this file.
00001 # Copyright (c) 2005-2007
00002 # Authors: KSS Project Contributors (see docs/CREDITS.txt)
00003 #
00004 # This program is free software; you can redistribute it and/or modify
00005 # it under the terms of the GNU General Public License version 2 as published
00006 # by the Free Software Foundation.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program; if not, write to the Free Software
00015 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00016 # 02111-1307, USA.
00017 
00018 from kss.core import KSSView, force_unicode, KSSExplicitError, kssaction
00019 from time import time
00020 from persistent.list import PersistentList
00021 
00022 class KSSDynamicTableDemo(KSSView):
00023 
00024     def getPersons(self):
00025         persons = self.request.SESSION.get('persons', PersistentList())
00026         if not persons or not isinstance(persons, PersistentList):
00027             persons = self.request.SESSION['persons'] = PersistentList()
00028         return persons
00029 
00030     def cleanPersons(self):
00031         self.request.SESSION['persons'] = PersistentList()
00032         self.getCommandSet('core').replaceHTML('table#personTable tbody', '<tbody></tbody>')
00033         return self.render()
00034 
00035     def addPerson(self, name, address, city, country, telephone):
00036         """ adds a person"""
00037         persons = self.getPersons()
00038         person = {
00039             'pid' : str(time()).replace('.',''),
00040             'name' : name,
00041             'address' : address,
00042             'city' : city,
00043             'country' : country,
00044             'telephone' : telephone
00045         }
00046         persons.append(person)
00047         html = self.renderPersonAsTr(person)
00048         self.getCommandSet('core').insertHTMLAsLastChild('table#personTable tbody', html)
00049         return self.render()
00050     
00051 
00052     def removePerson(self, pid):
00053         persons = self.getPersons()
00054         for p in persons:
00055             if p['pid'] == pid:
00056                 persons.remove(p)
00057         self.getCommandSet('core').deleteNode('tr#pid%s' % pid)
00058         return self.render()
00059 
00060 
00061     def moveUp(self, pid):
00062         persons = self.getPersons()
00063         for i in range(1, len(persons)):
00064             pid1 = persons[i]['pid']
00065             pid2 = persons[i-1]['pid']
00066             if pid==pid1:
00067                 self.switchPersons(pid1, pid2)
00068                 break
00069         return self.render()
00070 
00071     def moveDown(self, pid):
00072         persons = self.getPersons()
00073         for i in range(0, len(persons)-1):
00074             pid1 = persons[i]['pid']
00075             pid2 = persons[i+1]['pid']
00076             if pid==pid1:
00077                 self.switchPersons(pid1, pid2)
00078                 break
00079         return self.render()
00080 
00081     def toTop(self, pid):
00082         persons = self.getPersons()
00083         firstPid = persons[0]['pid']
00084         if firstPid == pid:
00085             return self.render()
00086         self.getCommandSet('core').moveNodeBefore('tr#pid%s' % pid, 'pid%s' % firstPid)
00087         for p in persons:
00088             if p['pid'] == pid:
00089                 persons.remove(p)
00090                 persons.insert(0, p)
00091                 break
00092         return self.render()
00093 
00094     def toBottom(self, pid):
00095         persons = self.getPersons()
00096         lastPid = persons[-1]['pid']
00097         if lastPid == pid:
00098             return self.render()
00099         self.getCommandSet('core').moveNodeAfter('tr#pid%s' % pid, 'pid%s' % lastPid)
00100         for p in persons:
00101             if p['pid'] == pid:
00102                 persons.remove(p)
00103                 persons.append(p)
00104                 break
00105         return self.render()
00106 
00107 
00108     def switchPersons(self, pid1, pid2):
00109         persons = self.getPersons()
00110         p1 = None
00111         i1 = None
00112         p2 = None
00113         i2 = None
00114         for i in range(0, len(persons)):
00115             if persons[i]['pid'] == pid1:
00116                 p1 = persons[i]
00117                 i1 = i
00118             if persons[i]['pid'] == pid2:
00119                 p2 = persons[i]
00120                 i2 = i
00121         if p1 and p2:
00122             self.getCommandSet('core').replaceHTML('tr#pid%s' % p2['pid'], '<tr class="placeholder"></tr>')
00123             self.getCommandSet('core').replaceHTML('tr#pid%s' % p1['pid'], self.renderPersonAsTr(p2))
00124             self.getCommandSet('core').replaceHTML('tr.placeholder', self.renderPersonAsTr(p1))
00125             persons[i1] = p2
00126             persons[i2] = p1
00127 
00128 
00129 
00130     def renderPersonAsTr(self, person):
00131         pid = person['pid']
00132         buttons = '''
00133                     <input type="button" class="button remove kssattr-pid-%(pid)s" value="Remove">
00134                     <input type="button" class="button moveUp kssattr-pid-%(pid)s" value="Up">
00135                     <input type="button" class="button moveDown kssattr-pid-%(pid)s" value="Down">
00136                     <input type="button" class="button toTop kssattr-pid-%(pid)s" value="Top">
00137                     <input type="button" class="button toBottom kssattr-pid-%(pid)s" value="Bottom">
00138                 ''' % {'pid' : pid}
00139         html = """<tr id="pid%(pid)s"><td>%(name)s</td><td>%(address)s</td><td>%(city)s</td><td>%(country)s</td><td>%(telephone)s</td><td>%(buttons)s</td></tr>""" % {
00140                 'pid' : person['pid'],
00141                 'name' : person['name'],
00142                 'address' : person['address'],
00143                 'city' : person['city'],
00144                 'country' : person['country'],
00145                 'telephone' : person['telephone'],
00146                 'buttons' : buttons
00147         }
00148         return html
00149 
00150