Mercurial > p > roundup > code
comparison roundup/cgi/templating.py @ 1067:73e7bbb8a1fa
allow hasPermission checks on normal user class iterations
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 06 Sep 2002 22:47:13 +0000 |
| parents | 0f9aa62917bd |
| children | 665730c27d29 |
comparison
equal
deleted
inserted
replaced
| 1066:cf22c87d6fce | 1067:73e7bbb8a1fa |
|---|---|
| 156 'db': HTMLDatabase(client), | 156 'db': HTMLDatabase(client), |
| 157 'instance': client.instance | 157 'instance': client.instance |
| 158 } | 158 } |
| 159 # add in the item if there is one | 159 # add in the item if there is one |
| 160 if client.nodeid: | 160 if client.nodeid: |
| 161 c['item'] = HTMLItem(client.db, classname, client.nodeid) | 161 c['item'] = HTMLItem(client, classname, client.nodeid) |
| 162 c[classname] = c['item'] | 162 c[classname] = c['item'] |
| 163 else: | 163 else: |
| 164 c[classname] = c['klass'] | 164 c[classname] = c['klass'] |
| 165 return c | 165 return c |
| 166 | 166 |
| 217 | 217 |
| 218 def __repr__(self): | 218 def __repr__(self): |
| 219 return '<HTMLClass(0x%x) %s>'%(id(self), self.classname) | 219 return '<HTMLClass(0x%x) %s>'%(id(self), self.classname) |
| 220 | 220 |
| 221 def __getitem__(self, item): | 221 def __getitem__(self, item): |
| 222 ''' return an HTMLItem instance''' | 222 ''' return an HTMLProperty instance |
| 223 #print 'getitem', (self, attr) | 223 ''' |
| 224 #print 'getitem', (self, item) | |
| 224 if item == 'creator': | 225 if item == 'creator': |
| 225 return HTMLUser(self.client) | 226 return HTMLUser(self.client, 'user', client.userid) |
| 226 | 227 |
| 227 if not self.props.has_key(item): | 228 if not self.props.has_key(item): |
| 228 raise KeyError, item | 229 raise KeyError, item |
| 229 prop = self.props[item] | 230 prop = self.props[item] |
| 230 | 231 |
| 233 if isinstance(prop, hyperdb.Multilink): | 234 if isinstance(prop, hyperdb.Multilink): |
| 234 value = [] | 235 value = [] |
| 235 else: | 236 else: |
| 236 value = None | 237 value = None |
| 237 if isinstance(prop, klass): | 238 if isinstance(prop, klass): |
| 238 return htmlklass(self.db, '', prop, item, value) | 239 return htmlklass(self.client, '', prop, item, value) |
| 239 | 240 |
| 240 # no good | 241 # no good |
| 241 raise KeyError, item | 242 raise KeyError, item |
| 242 | 243 |
| 243 def __getattr__(self, attr): | 244 def __getattr__(self, attr): |
| 256 if isinstance(prop, hyperdb.Multilink): | 257 if isinstance(prop, hyperdb.Multilink): |
| 257 value = [] | 258 value = [] |
| 258 else: | 259 else: |
| 259 value = None | 260 value = None |
| 260 if isinstance(prop, klass): | 261 if isinstance(prop, klass): |
| 261 l.append(htmlklass(self.db, '', prop, name, value)) | 262 l.append(htmlklass(self.client, '', prop, name, value)) |
| 262 return l | 263 return l |
| 263 | 264 |
| 264 def list(self): | 265 def list(self): |
| 265 l = [HTMLItem(self.db, self.classname, x) for x in self.klass.list()] | 266 if self.classname == 'user': |
| 267 klass = HTMLUser | |
| 268 else: | |
| 269 klass = HTMLItem | |
| 270 l = [klass(self.client, self.classname, x) for x in self.klass.list()] | |
| 266 return l | 271 return l |
| 267 | 272 |
| 268 def csv(self): | 273 def csv(self): |
| 269 ''' Return the items of this class as a chunk of CSV text. | 274 ''' Return the items of this class as a chunk of CSV text. |
| 270 ''' | 275 ''' |
| 305 ''' | 310 ''' |
| 306 if request is not None: | 311 if request is not None: |
| 307 filterspec = request.filterspec | 312 filterspec = request.filterspec |
| 308 sort = request.sort | 313 sort = request.sort |
| 309 group = request.group | 314 group = request.group |
| 310 l = [HTMLItem(self.db, self.classname, x) | 315 if self.classname == 'user': |
| 316 klass = HTMLUser | |
| 317 else: | |
| 318 klass = HTMLItem | |
| 319 l = [klass(self.client, self.classname, x) | |
| 311 for x in self.klass.filter(None, filterspec, sort, group)] | 320 for x in self.klass.filter(None, filterspec, sort, group)] |
| 312 return l | 321 return l |
| 313 | 322 |
| 314 def classhelp(self, properties, label='?', width='400', height='400'): | 323 def classhelp(self, properties, label='?', width='400', height='400'): |
| 315 '''pop up a javascript window with class help | 324 '''pop up a javascript window with class help |
| 355 cgi.escape('<li>'.join(pt._v_errors))) | 364 cgi.escape('<li>'.join(pt._v_errors))) |
| 356 | 365 |
| 357 class HTMLItem: | 366 class HTMLItem: |
| 358 ''' Accesses through an *item* | 367 ''' Accesses through an *item* |
| 359 ''' | 368 ''' |
| 360 def __init__(self, db, classname, nodeid): | 369 def __init__(self, client, classname, nodeid): |
| 361 self.db = db | 370 self.client = client |
| 371 self.db = client.db | |
| 362 self.classname = classname | 372 self.classname = classname |
| 363 self.nodeid = nodeid | 373 self.nodeid = nodeid |
| 364 self.klass = self.db.getclass(classname) | 374 self.klass = self.db.getclass(classname) |
| 365 self.props = self.klass.getprops() | 375 self.props = self.klass.getprops() |
| 366 | 376 |
| 367 def __repr__(self): | 377 def __repr__(self): |
| 368 return '<HTMLItem(0x%x) %s %s>'%(id(self), self.classname, self.nodeid) | 378 return '<HTMLItem(0x%x) %s %s>'%(id(self), self.classname, self.nodeid) |
| 369 | 379 |
| 370 def __getitem__(self, item): | 380 def __getitem__(self, item): |
| 371 ''' return an HTMLItem instance''' | 381 ''' return an HTMLProperty instance |
| 382 ''' | |
| 383 #print 'getitem', (self, item) | |
| 372 if item == 'id': | 384 if item == 'id': |
| 373 return self.nodeid | 385 return self.nodeid |
| 374 if not self.props.has_key(item): | 386 if not self.props.has_key(item): |
| 375 raise KeyError, item | 387 raise KeyError, item |
| 376 prop = self.props[item] | 388 prop = self.props[item] |
| 382 value = [] | 394 value = [] |
| 383 | 395 |
| 384 # look up the correct HTMLProperty class | 396 # look up the correct HTMLProperty class |
| 385 for klass, htmlklass in propclasses: | 397 for klass, htmlklass in propclasses: |
| 386 if isinstance(prop, klass): | 398 if isinstance(prop, klass): |
| 387 return htmlklass(self.db, self.nodeid, prop, item, value) | 399 return htmlklass(self.client, self.nodeid, prop, item, value) |
| 388 | 400 |
| 389 raise KeyErorr, item | 401 raise KeyErorr, item |
| 390 | 402 |
| 391 def __getattr__(self, attr): | 403 def __getattr__(self, attr): |
| 392 ''' convenience access to properties ''' | 404 ''' convenience access to properties ''' |
| 554 return '' | 566 return '' |
| 555 | 567 |
| 556 class HTMLUser(HTMLItem): | 568 class HTMLUser(HTMLItem): |
| 557 ''' Accesses through the *user* (a special case of item) | 569 ''' Accesses through the *user* (a special case of item) |
| 558 ''' | 570 ''' |
| 559 def __init__(self, client): | 571 def __init__(self, client, classname, nodeid): |
| 560 HTMLItem.__init__(self, client.db, 'user', client.userid) | 572 HTMLItem.__init__(self, client, 'user', nodeid) |
| 561 self.default_classname = client.classname | 573 self.default_classname = client.classname |
| 562 self.userid = client.userid | |
| 563 | 574 |
| 564 # used for security checks | 575 # used for security checks |
| 565 self.security = client.db.security | 576 self.security = client.db.security |
| 566 _marker = [] | 577 _marker = [] |
| 567 def hasPermission(self, role, classname=_marker): | 578 def hasPermission(self, role, classname=_marker): |
| 570 The class being tested defaults to the template's class, but may | 581 The class being tested defaults to the template's class, but may |
| 571 be overidden for this test by suppling an alternate classname. | 582 be overidden for this test by suppling an alternate classname. |
| 572 ''' | 583 ''' |
| 573 if classname is self._marker: | 584 if classname is self._marker: |
| 574 classname = self.default_classname | 585 classname = self.default_classname |
| 575 return self.security.hasPermission(role, self.userid, classname) | 586 return self.security.hasPermission(role, self.nodeid, classname) |
| 576 | 587 |
| 577 class HTMLProperty: | 588 class HTMLProperty: |
| 578 ''' String, Number, Date, Interval HTMLProperty | 589 ''' String, Number, Date, Interval HTMLProperty |
| 579 | 590 |
| 580 A wrapper object which may be stringified for the plain() behaviour. | 591 A wrapper object which may be stringified for the plain() behaviour. |
| 581 ''' | 592 ''' |
| 582 def __init__(self, db, nodeid, prop, name, value): | 593 def __init__(self, client, nodeid, prop, name, value): |
| 583 self.db = db | 594 self.client = client |
| 595 self.db = client.db | |
| 584 self.nodeid = nodeid | 596 self.nodeid = nodeid |
| 585 self.prop = prop | 597 self.prop = prop |
| 586 self.name = name | 598 self.name = name |
| 587 self.value = value | 599 self.value = value |
| 588 def __repr__(self): | 600 def __repr__(self): |
| 728 def __getattr__(self, attr): | 740 def __getattr__(self, attr): |
| 729 ''' return a new HTMLItem ''' | 741 ''' return a new HTMLItem ''' |
| 730 #print 'getattr', (self, attr, self.value) | 742 #print 'getattr', (self, attr, self.value) |
| 731 if not self.value: | 743 if not self.value: |
| 732 raise AttributeError, "Can't access missing value" | 744 raise AttributeError, "Can't access missing value" |
| 733 i = HTMLItem(self.db, self.prop.classname, self.value) | 745 if self.prop.classname == 'user': |
| 746 klass = HTMLItem | |
| 747 else: | |
| 748 klass = HTMLUser | |
| 749 i = klass(self.client, self.prop.classname, self.value) | |
| 734 return getattr(i, attr) | 750 return getattr(i, attr) |
| 735 | 751 |
| 736 def plain(self, escape=0): | 752 def plain(self, escape=0): |
| 737 if self.value is None: | 753 if self.value is None: |
| 738 return _('[unselected]') | 754 return _('[unselected]') |
| 851 def __getattr__(self, attr): | 867 def __getattr__(self, attr): |
| 852 ''' no extended attribute accesses make sense here ''' | 868 ''' no extended attribute accesses make sense here ''' |
| 853 raise AttributeError, attr | 869 raise AttributeError, attr |
| 854 | 870 |
| 855 def __getitem__(self, num): | 871 def __getitem__(self, num): |
| 856 ''' iterate and return a new HTMLItem ''' | 872 ''' iterate and return a new HTMLItem |
| 873 ''' | |
| 857 #print 'getitem', (self, num) | 874 #print 'getitem', (self, num) |
| 858 value = self.value[num] | 875 value = self.value[num] |
| 859 return HTMLItem(self.db, self.prop.classname, value) | 876 if self.prop.classname == 'user': |
| 877 klass = HTMLUser | |
| 878 else: | |
| 879 klass = HTMLItem | |
| 880 return klass(self.client, self.prop.classname, value) | |
| 860 | 881 |
| 861 def reverse(self): | 882 def reverse(self): |
| 862 ''' return the list in reverse order ''' | 883 ''' return the list in reverse order |
| 884 ''' | |
| 863 l = self.value[:] | 885 l = self.value[:] |
| 864 l.reverse() | 886 l.reverse() |
| 865 return [HTMLItem(self.db, self.prop.classname, value) for value in l] | 887 if self.prop.classname == 'user': |
| 888 klass = HTMLUser | |
| 889 else: | |
| 890 klass = HTMLItem | |
| 891 return [klass(self.client, self.prop.classname, value) for value in l] | |
| 866 | 892 |
| 867 def plain(self, escape=0): | 893 def plain(self, escape=0): |
| 868 linkcl = self.db.classes[self.prop.classname] | 894 linkcl = self.db.classes[self.prop.classname] |
| 869 k = linkcl.labelprop(1) | 895 k = linkcl.labelprop(1) |
| 870 labels = [] | 896 labels = [] |
| 1001 # easier access vars | 1027 # easier access vars |
| 1002 self.form = client.form | 1028 self.form = client.form |
| 1003 self.env = client.env | 1029 self.env = client.env |
| 1004 self.base = client.base | 1030 self.base = client.base |
| 1005 self.url = client.url | 1031 self.url = client.url |
| 1006 self.user = HTMLUser(client) | 1032 self.user = HTMLUser(client, 'user', client.userid) |
| 1007 | 1033 |
| 1008 # store the current class name and action | 1034 # store the current class name and action |
| 1009 self.classname = client.classname | 1035 self.classname = client.classname |
| 1010 self.template = client.template | 1036 self.template = client.template |
| 1011 | 1037 |
| 1238 if index != self.last_index: | 1264 if index != self.last_index: |
| 1239 self.last_item = self.current_item | 1265 self.last_item = self.current_item |
| 1240 self.last_index = index | 1266 self.last_index = index |
| 1241 | 1267 |
| 1242 # wrap the return in an HTMLItem | 1268 # wrap the return in an HTMLItem |
| 1243 self.current_item = HTMLItem(self.client.db, self.classname, | 1269 if self.classname == 'user': |
| 1270 klass = HTMLUser | |
| 1271 else: | |
| 1272 klass = HTMLItem | |
| 1273 self.current_item = klass(self.client, self.classname, | |
| 1244 self._sequence[index+self.first]) | 1274 self._sequence[index+self.first]) |
| 1245 return self.current_item | 1275 return self.current_item |
| 1246 | 1276 |
| 1247 def propchanged(self, property): | 1277 def propchanged(self, property): |
| 1248 ''' Detect if the property marked as being the group property | 1278 ''' Detect if the property marked as being the group property |
