1 """Exceptions for the web interface."""
4 from invirt import database
5 from invirt.database import Machine, MachineAccess
7 class MyException(Exception):
8 """Base class for my exceptions"""
11 class InvalidInput(MyException):
12 """Exception for user-provided input is invalid but maybe in good faith.
14 This would include setting memory to negative (which might be a
15 typo) but not setting an invalid boot CD (which requires bypassing
18 def __init__(self, err_field, err_value, expl=None):
19 MyException.__init__(self, expl)
20 self.err_field = err_field
21 self.err_value = err_value
23 class CodeError(MyException):
24 """Exception for internal errors or bad faith input."""
29 def cachedproperty(func):
30 name = '__cache_' + func.__name__ + '_' + str(id(func))
33 return getattr(self, name)
34 except AttributeError:
36 setattr(self, name, value)
38 return property(getter)
41 """State for a request"""
42 def __init__(self, user, isadmin=False):
44 self.isadmin = isadmin
46 def getMachines(self):
48 return Machine.query().join('acl').select_by(
49 database.or_(MachineAccess.c.user == self.username,
50 Machine.c.adminable == True))
52 return Machine.query().join('acl').select_by(user=self.username)
54 machines = cachedproperty(getMachines)
55 xmlist_raw = cachedproperty(lambda self: controls.getList())
56 xmlist = cachedproperty(lambda self:
57 dict((m, self.xmlist_raw[m.name])
58 for m in self.machines
59 if m.name in self.xmlist_raw))
62 """Clear the state so future accesses reload it."""
63 for attr in list(self.__dict__):
64 if attr.startswith('__cache_'):