Mercurial > p > roundup > code
comparison roundup/cgi/templating.py @ 1956:1e2cccf6b53b
give access to the input() method all over the place
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Sun, 14 Dec 2003 21:40:57 +0000 |
| parents | c40ed9113285 |
| children | 75437090134e |
comparison
equal
deleted
inserted
replaced
| 1955:b00ad075bb2f | 1956:1e2cccf6b53b |
|---|---|
| 21 # bring in the templating support | 21 # bring in the templating support |
| 22 from roundup.cgi.PageTemplates import PageTemplate | 22 from roundup.cgi.PageTemplates import PageTemplate |
| 23 from roundup.cgi.PageTemplates.Expressions import getEngine | 23 from roundup.cgi.PageTemplates.Expressions import getEngine |
| 24 from roundup.cgi.TAL.TALInterpreter import TALInterpreter | 24 from roundup.cgi.TAL.TALInterpreter import TALInterpreter |
| 25 from roundup.cgi import ZTUtils | 25 from roundup.cgi import ZTUtils |
| 26 | |
| 27 def input_html4(**attrs): | |
| 28 """Generate an 'input' (html4) element with given attributes""" | |
| 29 return '<input %s>'%' '.join(['%s="%s"'%item for item in attrs.items()]) | |
| 30 | |
| 31 def input_xhtml(**attrs): | |
| 32 """Generate an 'input' (xhtml) element with given attributes""" | |
| 33 return '<input %s/>'%' '.join(['%s="%s"'%item for item in attrs.items()]) | |
| 34 | 26 |
| 35 class NoTemplate(Exception): | 27 class NoTemplate(Exception): |
| 36 pass | 28 pass |
| 37 | 29 |
| 38 def find_template(dir, name, extension): | 30 def find_template(dir, name, extension): |
| 287 def is_only_view_ok(self): | 279 def is_only_view_ok(self): |
| 288 ''' Is the user only allowed to View (ie. not Edit) the current class? | 280 ''' Is the user only allowed to View (ie. not Edit) the current class? |
| 289 ''' | 281 ''' |
| 290 return self.is_view_ok() and not self.is_edit_ok() | 282 return self.is_view_ok() and not self.is_edit_ok() |
| 291 | 283 |
| 292 class HTMLClass(HTMLPermissions): | 284 def input_html4(**attrs): |
| 285 """Generate an 'input' (html4) element with given attributes""" | |
| 286 return '<input %s>'%' '.join(['%s="%s"'%item for item in attrs.items()]) | |
| 287 | |
| 288 def input_xhtml(**attrs): | |
| 289 """Generate an 'input' (xhtml) element with given attributes""" | |
| 290 return '<input %s/>'%' '.join(['%s="%s"'%item for item in attrs.items()]) | |
| 291 | |
| 292 class HTMLInputMixin: | |
| 293 ''' requires a _client property ''' | |
| 294 def __init__(self): | |
| 295 html_version = 'html4' | |
| 296 if hasattr(self._client.instance.config, 'HTML_VERSION'): | |
| 297 html_version = self._client.instance.config.HTML_VERSION | |
| 298 if html_version == 'xhtml': | |
| 299 self.input = input_xhtml | |
| 300 else: | |
| 301 self.input = input_html4 | |
| 302 | |
| 303 class HTMLClass(HTMLInputMixin, HTMLPermissions): | |
| 293 ''' Accesses through a class (either through *class* or *db.<classname>*) | 304 ''' Accesses through a class (either through *class* or *db.<classname>*) |
| 294 ''' | 305 ''' |
| 295 def __init__(self, client, classname, anonymous=0): | 306 def __init__(self, client, classname, anonymous=0): |
| 296 self._client = client | 307 self._client = client |
| 297 self._db = client.db | 308 self._db = client.db |
| 301 # consistent API for extending Class/Item | 312 # consistent API for extending Class/Item |
| 302 self._classname = self.classname = classname | 313 self._classname = self.classname = classname |
| 303 self._klass = self._db.getclass(self.classname) | 314 self._klass = self._db.getclass(self.classname) |
| 304 self._props = self._klass.getprops() | 315 self._props = self._klass.getprops() |
| 305 | 316 |
| 306 html_version = 'html4' | 317 HTMLInputMixin.__init__(self) |
| 307 if hasattr(self._client.instance.config, 'HTML_VERSION'): | |
| 308 html_version = self._client.instance.config.HTML_VERSION | |
| 309 if html_version == 'xhtml': | |
| 310 self.input = input_xhtml | |
| 311 else: | |
| 312 self.input = input_html4 | |
| 313 | 318 |
| 314 def __repr__(self): | 319 def __repr__(self): |
| 315 return '<HTMLClass(0x%x) %s>'%(id(self), self.classname) | 320 return '<HTMLClass(0x%x) %s>'%(id(self), self.classname) |
| 316 | 321 |
| 317 def __getitem__(self, item): | 322 def __getitem__(self, item): |
| 512 pt = Templates(self._db.config.TEMPLATES).get(self.classname, name) | 517 pt = Templates(self._db.config.TEMPLATES).get(self.classname, name) |
| 513 | 518 |
| 514 # use our fabricated request | 519 # use our fabricated request |
| 515 return pt.render(self._client, self.classname, req) | 520 return pt.render(self._client, self.classname, req) |
| 516 | 521 |
| 517 class HTMLItem(HTMLPermissions): | 522 class HTMLItem(HTMLInputMixin, HTMLPermissions): |
| 518 ''' Accesses through an *item* | 523 ''' Accesses through an *item* |
| 519 ''' | 524 ''' |
| 520 def __init__(self, client, classname, nodeid, anonymous=0): | 525 def __init__(self, client, classname, nodeid, anonymous=0): |
| 521 self._client = client | 526 self._client = client |
| 522 self._db = client.db | 527 self._db = client.db |
| 525 self._klass = self._db.getclass(classname) | 530 self._klass = self._db.getclass(classname) |
| 526 self._props = self._klass.getprops() | 531 self._props = self._klass.getprops() |
| 527 | 532 |
| 528 # do we prefix the form items with the item's identification? | 533 # do we prefix the form items with the item's identification? |
| 529 self._anonymous = anonymous | 534 self._anonymous = anonymous |
| 535 | |
| 536 HTMLInputMixin.__init__(self) | |
| 530 | 537 |
| 531 def __repr__(self): | 538 def __repr__(self): |
| 532 return '<HTMLItem(0x%x) %s %s>'%(id(self), self._classname, | 539 return '<HTMLItem(0x%x) %s %s>'%(id(self), self._classname, |
| 533 self._nodeid) | 540 self._nodeid) |
| 534 | 541 |
| 834 ''' | 841 ''' |
| 835 return self._db.security.hasPermission('Edit', self._client.userid, | 842 return self._db.security.hasPermission('Edit', self._client.userid, |
| 836 self._classname) or (self._nodeid == self._client.userid and | 843 self._classname) or (self._nodeid == self._client.userid and |
| 837 self._db.user.get(self._client.userid, 'username') != 'anonymous') | 844 self._db.user.get(self._client.userid, 'username') != 'anonymous') |
| 838 | 845 |
| 839 class HTMLProperty: | 846 class HTMLProperty(HTMLInputMixin): |
| 840 ''' String, Number, Date, Interval HTMLProperty | 847 ''' String, Number, Date, Interval HTMLProperty |
| 841 | 848 |
| 842 Has useful attributes: | 849 Has useful attributes: |
| 843 | 850 |
| 844 _name the name of the property | 851 _name the name of the property |
| 859 if not anonymous: | 866 if not anonymous: |
| 860 self._formname = '%s%s@%s'%(classname, nodeid, name) | 867 self._formname = '%s%s@%s'%(classname, nodeid, name) |
| 861 else: | 868 else: |
| 862 self._formname = name | 869 self._formname = name |
| 863 | 870 |
| 864 html_version = 'html4' | 871 HTMLInputMixin.__init__(self) |
| 865 if hasattr(self._client.instance.config, 'HTML_VERSION'): | |
| 866 html_version = self._client.instance.config.HTML_VERSION | |
| 867 if html_version == 'xhtml': | |
| 868 self.input = input_xhtml | |
| 869 else: | |
| 870 self.input = input_html4 | |
| 871 | 872 |
| 872 def __repr__(self): | 873 def __repr__(self): |
| 873 return '<HTMLProperty(0x%x) %s %r %r>'%(id(self), self._formname, | 874 return '<HTMLProperty(0x%x) %s %r %r>'%(id(self), self._formname, |
| 874 self._prop, self._value) | 875 self._prop, self._value) |
| 875 def __str__(self): | 876 def __str__(self): |
| 1439 for col in columns: | 1440 for col in columns: |
| 1440 self.columns[col] = 1 | 1441 self.columns[col] = 1 |
| 1441 def __getitem__(self, name): | 1442 def __getitem__(self, name): |
| 1442 return self.columns.has_key(name) | 1443 return self.columns.has_key(name) |
| 1443 | 1444 |
| 1444 class HTMLRequest: | 1445 class HTMLRequest(HTMLInputMixin): |
| 1445 ''' The *request*, holding the CGI form and environment. | 1446 ''' The *request*, holding the CGI form and environment. |
| 1446 | 1447 |
| 1447 "form" the CGI form as a cgi.FieldStorage | 1448 "form" the CGI form as a cgi.FieldStorage |
| 1448 "env" the CGI environment variables | 1449 "env" the CGI environment variables |
| 1449 "base" the base URL for this instance | 1450 "base" the base URL for this instance |
| 1461 "filterspec" values to filter the index on | 1462 "filterspec" values to filter the index on |
| 1462 "search_text" text to perform a full-text search on for an index | 1463 "search_text" text to perform a full-text search on for an index |
| 1463 | 1464 |
| 1464 ''' | 1465 ''' |
| 1465 def __init__(self, client): | 1466 def __init__(self, client): |
| 1466 self.client = client | 1467 # _client is needed by HTMLInputMixin |
| 1468 self._client = self.client = client | |
| 1467 | 1469 |
| 1468 # easier access vars | 1470 # easier access vars |
| 1469 self.form = client.form | 1471 self.form = client.form |
| 1470 self.env = client.env | 1472 self.env = client.env |
| 1471 self.base = client.base | 1473 self.base = client.base |
| 1476 self.template = client.template | 1478 self.template = client.template |
| 1477 | 1479 |
| 1478 # the special char to use for special vars | 1480 # the special char to use for special vars |
| 1479 self.special_char = '@' | 1481 self.special_char = '@' |
| 1480 | 1482 |
| 1481 html_version = 'html4' | 1483 HTMLInputMixin.__init__(self) |
| 1482 if hasattr(self.client.instance.config, 'HTML_VERSION'): | |
| 1483 html_version = self.client.instance.config.HTML_VERSION | |
| 1484 if html_version == 'xhtml': | |
| 1485 self.input = input_xhtml | |
| 1486 else: | |
| 1487 self.input = input_html4 | |
| 1488 | 1484 |
| 1489 self._post_init() | 1485 self._post_init() |
| 1490 | 1486 |
| 1491 def _post_init(self): | 1487 def _post_init(self): |
| 1492 ''' Set attributes based on self.form | 1488 ''' Set attributes based on self.form |
