Mercurial > p > roundup > code
comparison roundup/cgi/templating.py @ 1696:38f9578de0c6
fix HTML file detection (hence history xref linking) [SF#741478]
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 24 Jun 2003 04:52:26 +0000 |
| parents | 7218be26cf85 |
| children | cd53f6238cae |
comparison
equal
deleted
inserted
replaced
| 1693:7218be26cf85 | 1696:38f9578de0c6 |
|---|---|
| 23 from roundup.cgi import ZTUtils | 23 from roundup.cgi import ZTUtils |
| 24 | 24 |
| 25 class NoTemplate(Exception): | 25 class NoTemplate(Exception): |
| 26 pass | 26 pass |
| 27 | 27 |
| 28 def find_template(dir, name, extension): | |
| 29 ''' Find a template in the nominated dir | |
| 30 ''' | |
| 31 # find the source | |
| 32 if extension: | |
| 33 filename = '%s.%s'%(name, extension) | |
| 34 else: | |
| 35 filename = name | |
| 36 | |
| 37 # try old-style | |
| 38 src = os.path.join(dir, filename) | |
| 39 if os.path.exists(src): | |
| 40 return (src, filename) | |
| 41 | |
| 42 # try with a .html extension (new-style) | |
| 43 filename = filename + '.html' | |
| 44 src = os.path.join(dir, filename) | |
| 45 if os.path.exists(src): | |
| 46 return (src, filename) | |
| 47 | |
| 48 # no extension == no generic template is possible | |
| 49 if not extension: | |
| 50 raise NoTemplate, 'Template file "%s" doesn\'t exist'%name | |
| 51 | |
| 52 # try for a _generic template | |
| 53 generic = '_generic.%s'%extension | |
| 54 src = os.path.join(dir, generic) | |
| 55 if os.path.exists(src): | |
| 56 return (src, generic) | |
| 57 | |
| 58 # finally, try _generic.html | |
| 59 generic = filename + '.html' | |
| 60 src = os.path.join(dir, generic) | |
| 61 if os.path.exists(src): | |
| 62 return (src, generic) | |
| 63 | |
| 64 raise NoTemplate, 'No template file exists for templating "%s" '\ | |
| 65 'with template "%s" (neither "%s" nor "%s")'%(name, extension, | |
| 66 filename, generic) | |
| 67 | |
| 28 class Templates: | 68 class Templates: |
| 29 templates = {} | 69 templates = {} |
| 30 | 70 |
| 31 def __init__(self, dir): | 71 def __init__(self, dir): |
| 32 self.dir = dir | 72 self.dir = dir |
| 57 name = 'home' | 97 name = 'home' |
| 58 elif extension is None and '.' in name: | 98 elif extension is None and '.' in name: |
| 59 # split name | 99 # split name |
| 60 name, extension = name.split('.') | 100 name, extension = name.split('.') |
| 61 | 101 |
| 62 # find the source, figure the time it was last modified | 102 # find the source |
| 63 if extension: | 103 src, filename = find_template(self.dir, name, extension) |
| 64 filename = '%s.%s'%(name, extension) | 104 |
| 65 else: | 105 # has it changed? |
| 66 filename = name | |
| 67 | |
| 68 src = os.path.join(self.dir, filename) | |
| 69 if not os.path.exists(src): | |
| 70 filename = filename + '.html' | |
| 71 src = os.path.join(self.dir, filename) | |
| 72 if not os.path.exists(src): | |
| 73 if not extension: | |
| 74 raise NoTemplate, 'Template file "%s" doesn\'t exist'%name | |
| 75 | |
| 76 # try for a generic template | |
| 77 generic = '_generic.%s'%extension | |
| 78 src = os.path.join(self.dir, generic) | |
| 79 if not os.path.exists(src): | |
| 80 generic = '_generic.%s.html'%extension | |
| 81 src = os.path.join(self.dir, generic) | |
| 82 if not os.path.exists(src): | |
| 83 raise NoTemplate, 'No template file exists for '\ | |
| 84 'templating "%s" with template "%s" (neither '\ | |
| 85 '"%s" nor "%s")'%(name, extension, filename, | |
| 86 generic) | |
| 87 filename = generic | |
| 88 | |
| 89 try: | 106 try: |
| 90 stime = os.stat(src)[os.path.stat.ST_MTIME] | 107 stime = os.stat(src)[os.path.stat.ST_MTIME] |
| 91 except os.error, error: | 108 except os.error, error: |
| 92 if error.errno != errno.ENOENT: | 109 if error.errno != errno.ENOENT: |
| 93 raise | 110 raise |
| 562 current[prop_n] = prop.plain() | 579 current[prop_n] = prop.plain() |
| 563 # make link if hrefable | 580 # make link if hrefable |
| 564 if (self._props.has_key(prop_n) and | 581 if (self._props.has_key(prop_n) and |
| 565 isinstance(self._props[prop_n], hyperdb.Link)): | 582 isinstance(self._props[prop_n], hyperdb.Link)): |
| 566 classname = self._props[prop_n].classname | 583 classname = self._props[prop_n].classname |
| 567 if os.path.exists(os.path.join(self._db.config.TEMPLATES, classname + '.item')): | 584 try: |
| 568 current[prop_n] = '<a href="%s%s">%s</a>'%(classname, | 585 find_template(self._db.config.TEMPLATES, |
| 569 self._klass.get(self._nodeid, prop_n, None), current[prop_n]) | 586 classname, 'item') |
| 587 except NoTemplate: | |
| 588 pass | |
| 589 else: | |
| 590 id = self._klass.get(self._nodeid, prop_n, None) | |
| 591 current[prop_n] = '<a href="%s%s">%s</a>'%( | |
| 592 classname, id, current[prop_n]) | |
| 570 | 593 |
| 571 for id, evt_date, user, action, args in history: | 594 for id, evt_date, user, action, args in history: |
| 572 date_s = str(evt_date.local(timezone)).replace("."," ") | 595 date_s = str(evt_date.local(timezone)).replace("."," ") |
| 573 arg_s = '' | 596 arg_s = '' |
| 574 if action == 'link' and type(args) == type(()): | 597 if action == 'link' and type(args) == type(()): |
