Fix bug
[plcapi.git] / PLC / Messages.py
1 #
2 # Functions for interacting with the messages table in the database
3 #
4 # Tony Mack <tmack@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 from PLC.Parameter import Parameter
9 from PLC.Table import Row, Table
10 from PLC.Filter import Filter
11 from PLC.Storage.AlchemyObject import AlchemyObj
12
13 class Message(AlchemyObj):
14     """
15     Representation of a row in the messages table.
16     """
17
18     tablename = 'messages'
19     fields = {
20         'message_id': Parameter(str, "Message identifier", primary_key=True),
21         'subject': Parameter(str, "Message summary", nullok = True),
22         'template': Parameter(str, "Message template", nullok = True),
23         'enabled': Parameter(bool, "Message is enabled"),
24         }
25
26     def sync(self, commit=True, validate=True):
27         AlchemyObj.sync(self, commit, validate)
28         if 'message_id' not in self:
29             # Before a new slice is added, delete expired slices
30             #expired = Slices(self.api, expires = -int(time.time()))
31             #for slice in expired:
32             #    slice.delete(commit)
33             AlchemyObj.insert(self, dict(self))
34         else:
35             AlchemyObj.update(self, {'message_id': self['message_id']}, dict(self))
36
37     def delete(self, commit = True):
38         assert 'message_id' in self
39         AlchemyObj.delete(self, dict(self))
40
41 class Messages(list):
42     """
43     Representation of row(s) from the messages table in the database.
44     """
45
46     def __init__(self, api, message_filter = None, columns = None, enabled = None):
47         if not message_filter:
48             messages = Message().select()
49         if isinstance(message_filter, (list, tuple, set, int, long)):
50             messages = Message().select(filter={'message_id': message_filter})
51         elif isinstance(message_filter, dict):
52             messages = Message().select(filter=message_filter)
53         else:
54             raise PLCInvalidArgument, "Wrong message filter %r"%message_filter
55     
56         for message in messages:
57             self.append(message)