Skip to content
This repository
Browse code

integrate template better, move in form

  • Loading branch information...
commit cbb981ef540bf371cf9231d686e74372589890c3 1 parent 55adf3a
aaronsw authored
3  trunk/web/__init__.py
@@ -11,7 +11,8 @@
11 11 # todo:
12 12 # - some sort of accounts system
13 13
14   -import utils, db, net, wsgi, http, webapi, request, httpserver, template
  14 +import utils, db, net, wsgi, http, webapi, request, httpserver
  15 +import template, form
15 16
16 17 from utils import *
17 18 from db import *
43 form.py → trunk/web/form.py
... ... @@ -1,10 +1,11 @@
1   -"""form.py: A simple Python form library."""
2   -__version__ = '0.22'
3   -__license__ = "Public domain"
4   -__author__ = ['Aaron Swartz <me@aaronsw.com>', 'Steve Huffman <http://spez.name/>']
  1 +"""
  2 +HTML forms
  3 +(part of web.py)
  4 +"""
5 5
6 6 import copy, re
7   -import web
  7 +import webapi as web
  8 +import utils, net
8 9
9 10 def attrget(obj, attr, value=None):
10 11 if hasattr(obj, 'has_key') and obj.has_key(attr): return obj[attr]
@@ -59,7 +60,7 @@ def __getitem__(self, i):
59 60 raise KeyError, i
60 61
61 62 def _get_d(self): #@@ should really be form.attr, no?
62   - return web.storage([(i.name, i.value) for i in self.inputs])
  63 + return utils.storage([(i.name, i.value) for i in self.inputs])
63 64 d = property(_get_d)
64 65
65 66 class Input(object):
@@ -86,33 +87,33 @@ def render(self): raise NotImplementedError
86 87 def addatts(self):
87 88 str = ""
88 89 for (n, v) in self.attrs.items():
89   - str += ' %s="%s"' % (n, web.websafe(v))
  90 + str += ' %s="%s"' % (n, net.websafe(v))
90 91 return str
91 92
92 93 #@@ quoting
93 94
94 95 class Textbox(Input):
95 96 def render(self):
96   - x = '<input type="text" name="%s"' % web.websafe(self.name)
97   - if self.value: x += ' value="%s"' % web.websafe(self.value)
  97 + x = '<input type="text" name="%s"' % net.websafe(self.name)
  98 + if self.value: x += ' value="%s"' % net.websafe(self.value)
98 99 x += self.addatts()
99 100 x += ' />'
100 101 return x
101 102
102 103 class Password(Input):
103 104 def render(self):
104   - x = '<input type="password" name="%s"' % web.websafe(self.name)
105   - if self.value: x += ' value="%s"' % web.websafe(self.value)
  105 + x = '<input type="password" name="%s"' % net.websafe(self.name)
  106 + if self.value: x += ' value="%s"' % net.websafe(self.value)
106 107 x += self.addatts()
107 108 x += ' />'
108 109 return x
109 110
110 111 class Textarea(Input):
111 112 def render(self):
112   - x = '<textarea name="%s"' % web.websafe(self.name)
  113 + x = '<textarea name="%s"' % net.websafe(self.name)
113 114 x += self.addatts()
114 115 x += '>'
115   - if self.value is not None: x += web.websafe(self.value)
  116 + if self.value is not None: x += net.websafe(self.value)
116 117 x += '</textarea>'
117 118 return x
118 119
@@ -122,11 +123,11 @@ def __init__(self, name, args, *validators, **attrs):
122 123 super(Dropdown, self).__init__(name, *validators, **attrs)
123 124
124 125 def render(self):
125   - x = '<select name="%s"%s>\n' % (web.websafe(self.name), self.addatts())
  126 + x = '<select name="%s"%s>\n' % (net.websafe(self.name), self.addatts())
126 127 for arg in self.args:
127 128 if self.value == arg: select_p = ' selected="selected"'
128 129 else: select_p = ''
129   - x += " <option"+select_p+">%s</option>\n" % web.websafe(arg)
  130 + x += " <option"+select_p+">%s</option>\n" % net.websafe(arg)
130 131 x += '</select>\n'
131 132 return x
132 133
@@ -140,12 +141,12 @@ def render(self):
140 141 for arg in self.args:
141 142 if self.value == arg: select_p = ' checked="checked"'
142 143 else: select_p = ''
143   - x += '<input type="radio" name="%s" value="%s"%s%s /> %s ' % (web.websafe(self.name), web.websafe(arg), select_p, self.addatts(), web.websafe(arg))
  144 + x += '<input type="radio" name="%s" value="%s"%s%s /> %s ' % (net.websafe(self.name), net.websafe(arg), select_p, self.addatts(), net.websafe(arg))
144 145 return x+'</span>'
145 146
146 147 class Checkbox(Input):
147 148 def render(self):
148   - x = '<input name="%s" type="checkbox"' % web.websafe(self.name)
  149 + x = '<input name="%s" type="checkbox"' % net.websafe(self.name)
149 150 if self.value: x += ' checked="checked"'
150 151 x += self.addatts()
151 152 x += ' />'
@@ -153,20 +154,20 @@ def render(self):
153 154
154 155 class Button(Input):
155 156 def render(self):
156   - safename = web.websafe(self.name)
  157 + safename = net.websafe(self.name)
157 158 x = '<button name="%s"%s>%s</button>' % (safename, self.addatts(), safename)
158 159 return x
159 160
160 161 class Hidden(Input):
161 162 def render(self):
162   - x = '<input type="hidden" name="%s' % web.websafe(self.name)
163   - if self.value: x += ' value="%s"' % web.websafe(self.value)
  163 + x = '<input type="hidden" name="%s' % net.websafe(self.name)
  164 + if self.value: x += ' value="%s"' % net.websafe(self.value)
164 165 x += ' />'
165 166 return x
166 167
167 168 class Validator:
168 169 def __deepcopy__(self, memo): return copy.copy(self)
169   - def __init__(self, msg, test, jstest=None): web.autoassign(self, locals())
  170 + def __init__(self, msg, test, jstest=None): utils.autoassign(self, locals())
170 171 def valid(self, value):
171 172 try: return self.test(value)
172 173 except: return False
8 trunk/web/template.py
... ... @@ -1,7 +1,7 @@
1   -"""template.py: simple, elegant templating"""
2   -__author__ = "Aaron Swartz <me@aaronsw.com>"
3   -__license__ = "Public domain"
4   -__version__ = 0.2
  1 +"""
  2 +simple, elegant templating
  3 +(part of web.py)
  4 +"""
5 5
6 6 import re, glob
7 7 from types import FunctionType as function

0 comments on commit cbb981e

Please sign in to comment.
Something went wrong with that request. Please try again.