source: trunk/packages/invirt-web/code/webcommon.py @ 1799

Last change on this file since 1799 was 1644, checked in by broder, 16 years ago

I still want to see all VMs, even the ones without an ACL

File size: 1.3 KB
RevLine 
[209]1"""Exceptions for the web interface."""
2
[535]3import time
[874]4from invirt import database
[864]5from invirt.database import Machine, MachineAccess
[209]6
7import controls
8
[261]9def cachedproperty(func):
10    name = '__cache_' + func.__name__ + '_' + str(id(func))
11    def getter(self):
12        try:
13            return getattr(self, name)
14        except AttributeError:
15            value = func(self)
16            setattr(self, name, value)
17            return value
18    return property(getter)
19
[572]20class State(object):
21    """State for a request"""
[867]22    def __init__(self, user, isadmin=False):
[572]23        self.username = user
[867]24        self.isadmin = isadmin
[209]25
[632]26    def getMachines(self):
[867]27        if self.isadmin:
[1644]28            return Machine.query().all()
[632]29        else:
[1001]30            return Machine.query().join('acl').filter_by(user=self.username)
[632]31
32    machines = cachedproperty(getMachines)
[554]33    xmlist_raw = cachedproperty(lambda self: controls.getList())
[535]34    xmlist = cachedproperty(lambda self:
[554]35                                dict((m, self.xmlist_raw[m.name])
36                                     for m in self.machines
37                                     if m.name in self.xmlist_raw))
[535]38
[209]39    def clear(self):
40        """Clear the state so future accesses reload it."""
[264]41        for attr in list(self.__dict__):
[261]42            if attr.startswith('__cache_'):
[209]43                delattr(self, attr)
Note: See TracBrowser for help on using the repository browser.