Commits

Mike Orr  committed 83d2ff8

Updates from James for unfinished/fields.py and unfinished/helpers.py

  • Participants
  • Parent commits 49c0161

Comments (0)

Files changed (3)

File unfinished/fields.py

     necessary for the use of the ``fields()`` helper. 
     
     >>> form_start('/action', method='post')
-    literal(u'<table><form action="/action" method="post">')
+    literal(u'<form action="/action" method="post"><table>')
     >>> form_start('/action', method='post', table_class='form')
-    literal(u'<table class="form"><form action="/action" method="post">')
+    literal(u'<form action="/action" method="post"><table class="form">')
     """
     if p.has_key('table_class'):
          table_class = p.get('table_class', 'form')
          del p['table_class']
-         return HTML.table(
+         return start_form(*k,**p)+ HTML.table(
              _closed=False, 
              class_=p.get('table_class', 'form'),
-             c=start_form(*k,**p)
          )
     else:
-         return HTML.table(_closed=False, c=start_form(*k,**p))
+         return start_form(*k,**p)+literal('<table>')
 
 def form_end(*k, **p):
     """\
     >>> form_end()
     literal(u'</form></table>')
     """
-    return end_form(*k, **p)+literal("</table>")
+    return literal("</table>")+end_form(*k, **p)
 
 def field(
     label='', 
                 return id
     raise Exception("Option %s not found or invalid option format"%id)
     
-def radio_group(name, options, value=None, align='horiz', cols=4):
-    """Radio Group Field."""
-    output=''
-    if len(options)>0:
-        if align <> 'table':
-            for option in options:
-                checked=''
-                if not isinstance(option, list) and not isinstance(option, tuple):
-                    k = option
-                    v = option
-                else:
-                    k, v = option
-                if unicode(v) == unicode(value):
-                    checked=" checked"
-                break_ = ''
-                if align == 'vert':
-                    break_='<br />'
-                output+='<input type="radio" name="%s" value="%s"%s /> %s%s\n'%(name, v, checked, k, break_)
-        else:
-            output += '<table border="0" width="100%" cellpadding="0" cellspacing="0">\n    <tr>\n'
-            counter = -1
-            for option in options:
-                counter += 1
-                if ((counter % cols) == 0) and (counter <> 0):
-                    output += '    </tr>\n    <tr>\n'
-                output += '      <td>'
-                checked=''
-                align=''
-                if not isinstance(option, list) and not isinstance(option, tuple):
-                    k = option
-                    v = option
-                else:
-                    k=option[0]
-                    v=option[1]
-                if unicode(v)==unicode(value):
-                    checked=" checked"
-                output += '<input type="radio" name="%s" value="%s"%s /> %s%s'%(name, v, checked, k,align)
-                output += '</td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
-            counter += 1
-            while (counter % cols):
-                counter += 1
-                output += '      <td></td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
-            output += '    </tr>\n</table>'
-    return output
+#def radio_group(name, options, value=None, align='horiz', cols=4):
+#    """Radio Group Field."""
+#    output=''
+#    if len(options)>0:
+#        if align <> 'table':
+#            for option in options:
+#                checked=''
+#                if not isinstance(option, list) and not isinstance(option, tuple):
+#                    k = option
+#                    v = option
+#                else:
+#                    k, v = option
+#                if unicode(v) == unicode(value):
+#                    checked=" checked"
+#                break_ = ''
+#                if align == 'vert':
+#                    break_='<br />'
+#                output+='<input type="radio" name="%s" value="%s"%s /> %s%s\n'%(name, v, checked, k, break_)
+#        else:
+#            output += '<table border="0" width="100%" cellpadding="0" cellspacing="0">\n    <tr>\n'
+#            counter = -1
+#            for option in options:
+#                counter += 1
+#                if ((counter % cols) == 0) and (counter <> 0):
+#                    output += '    </tr>\n    <tr>\n'
+#                output += '      <td>'
+#                checked=''
+#                align=''
+#                if not isinstance(option, list) and not isinstance(option, tuple):
+#                    k = option
+#                    v = option
+#                else:
+#                    k=option[0]
+#                    v=option[1]
+#                if unicode(v)==unicode(value):
+#                    checked=" checked"
+#                output += '<input type="radio" name="%s" value="%s"%s /> %s%s'%(name, v, checked, k,align)
+#                output += '</td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
+#            counter += 1
+#            while (counter % cols):
+#                counter += 1
+#                output += '      <td></td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
+#            output += '    </tr>\n</table>'
+#    return output
 
 def _format_values(values):
     if not isinstance(values, list) and not isinstance(values, tuple):
             values_.append(unicode(value))
         return values_
 
+def radio_group(name, options, values=None, align='horiz', cols=4):
+    return group(name, options, values, align, cols, 'radio')
+
 def checkbox_group(name, options, values=None, align='horiz', cols=4):
+    return group(name, options, values, align, cols, 'checkbox')
+
+def group(name, options, values=None, align='horiz', cols=4, group_type='checkbox'):
     """Check Box Group Field."""
+    if not group_type in ['checkbox','radio']:
+        raise Exception('Invalid group type %s'%group_type)
     values = _format_values(values)
     output = u''
     item_counter = 0
                 else:
                     k=option[0]
                     v=option[1]
-                checked=u''
+                checked=literal(u'')
                 if unicode(v) in values:
-                    checked=" checked"
+                    checked=literal(" checked")
                 break_ = u''
                 if align == 'vert':
-                    break_=u'<br />'
-                output+='<input type="checkbox" name="%s" value="%s"%s /> %s%s\n'%(name, v, checked, k, break_)
+                    break_=literal(u'<br />')
+                output+=literal('<input type="')+literal(group_type)+literal('" name="')+name+literal('" value="')+literal(unicode(v))+literal('" ')+checked+literal(' />')+unicode(k)+break_+literal('\n')
                 item_counter += 1
         else:
-            output += u'<table border="0" width="100%" cellpadding="0" cellspacing="0">\n    <tr>\n'
+            output += literal(u'<table border="0" width="100%" cellpadding="0" cellspacing="0">\n    <tr>\n')
             counter = -1
             for option in options:
                 counter += 1
                 if ((counter % cols) == 0) and (counter <> 0):
-                    output += u'    </tr>\n    <tr>\n'
-                output += '      <td>'
-                checked=u''
-                align=u''
+                    output += literal(u'    </tr>\n    <tr>\n')
+                output += literal('      <td>')
+                checked=literal(u'')
+                align=literal(u'')
                 if not isinstance(option, list) and not isinstance(option, tuple):
                     k = option
                     v = option
                     k=option[0]
                     v=option[1]
                 if unicode(v) in values:
-                    checked=" checked"
-                output += u'<input type="checkbox" name="%s" value="%s"%s />%s%s'%(name, v, checked, k, align)
+                    checked=literal(" checked")
+                output+=literal('<input type="checkbox" name="')+name+literal('" value="')+literal(unicode(v))+literal('" ')+checked+literal(' />')+unicode(k)
+                #output += u'<input type="checkbox" name="%s" value="%s"%s />%s%s'%(name, v, checked, k, align)
                 item_counter += 1
-                output += u'</td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
+                output += literal(u'</td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n')
             counter += 1
             while (counter % cols):
                 counter += 1
-                output += u'      <td></td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n'
-            output += u'    </tr>\n</table>\n'
-    if not type(output) == unicode:
+                output += literal(u'      <td></td>\n      <td>&nbsp;&nbsp;&nbsp;&nbsp;</td>\n')
+            output += literal(u'    </tr>\n</table>\n')
+    if not type(output) in [unicode, literal]:
         raise Exception(type(output))
     return output[:-1]
 

File unfinished/helpers.patch

-diff -r 5b974015c354 unfinished/helpers.py
---- a/unfinished/helpers.py	Thu Apr 24 10:59:10 2008 -0700
-+++ b/unfinished/helpers.py	Mon Apr 28 16:28:41 2008 +0100
-@@ -2,8 +2,6 @@
- A series of useful helper functions which aren't found in other libraries.
- """
- 
--from time import seconds
--import jimmyg.helpers.mail as mail
- 
- def divide(a,b):
-     """\
-@@ -12,9 +10,9 @@
-     For example::
- 
-         >>> divide(3,2)
--        >>> 1.5
-+        1.5
-         >>> divide(3,1)
--        >>> 3
-+        3
-     """
-     if a%b:
-         return a/float(b)
-@@ -79,8 +77,9 @@
-         '522.3 KB'
- 
-     >>> size_to_human(534813)
-+    '522.3 KB'
-     >>> size_to_human(534813, format='decimal')
--
-+    '534.8 KB'
-     """
-     if format == 'binary':  
-         format = 1024
-@@ -106,3 +105,7 @@
-         size = long(size)
-     return "%s %s"%(size, units[counter])
- 
-+if __name__ == '__main__':
-+    import doctest
-+    doctest.testmod()
-+

File unfinished/helpers.py

 A series of useful helper functions which aren't found in other libraries.
 """
 
-from time import seconds
-import jimmyg.helpers.mail as mail
 
 def divide(a,b):
     """\
     For example::
 
         >>> divide(3,2)
-        >>> 1.5
+        1.5
         >>> divide(3,1)
-        >>> 3
+        3
     """
     if a%b:
         return a/float(b)
         '522.3 KB'
 
     >>> size_to_human(534813)
+    '522.3 KB'
     >>> size_to_human(534813, format='decimal')
-
+    '534.8 KB'
     """
     if format == 'binary':  
         format = 1024
         size = long(size)
     return "%s %s"%(size, units[counter])
 
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
+