|
25 | 25 | class NoTemplate(Exception): |
26 | 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 | 68 | class Templates: |
29 | 69 | templates = {} |
30 | 70 |
|
@@ -59,33 +99,10 @@ def get(self, name, extension=None): |
59 | 99 | # split name |
60 | 100 | name, extension = name.split('.') |
61 | 101 |
|
62 | | - # find the source, figure the time it was last modified |
63 | | - if extension: |
64 | | - filename = '%s.%s'%(name, extension) |
65 | | - else: |
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 |
| 102 | + # find the source |
| 103 | + src, filename = find_template(self.dir, name, extension) |
88 | 104 |
|
| 105 | + # has it changed? |
89 | 106 | try: |
90 | 107 | stime = os.stat(src)[os.path.stat.ST_MTIME] |
91 | 108 | except os.error, error: |
@@ -564,9 +581,15 @@ def history(self, direction='descending', dre=re.compile('\d+')): |
564 | 581 | if (self._props.has_key(prop_n) and |
565 | 582 | isinstance(self._props[prop_n], hyperdb.Link)): |
566 | 583 | classname = self._props[prop_n].classname |
567 | | - if os.path.exists(os.path.join(self._db.config.TEMPLATES, classname + '.item')): |
568 | | - current[prop_n] = '<a href="%s%s">%s</a>'%(classname, |
569 | | - self._klass.get(self._nodeid, prop_n, None), current[prop_n]) |
| 584 | + try: |
| 585 | + find_template(self._db.config.TEMPLATES, |
| 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 | 594 | for id, evt_date, user, action, args in history: |
572 | 595 | date_s = str(evt_date.local(timezone)).replace("."," ") |
|
0 commit comments