# # Functions for interacting with the messages table in the database # # Tony Mack # Copyright (C) 2006 The Trustees of Princeton University # from PLC.Parameter import Parameter from PLC.Table import Row, Table from PLC.Filter import Filter from PLC.Storage.AlchemyObject import AlchemyObj class Message(AlchemyObj): """ Representation of a row in the messages table. """ tablename = 'messages' fields = { 'message_id': Parameter(str, "Message identifier", primary_key=True), 'subject': Parameter(str, "Message summary", nullok = True), 'template': Parameter(str, "Message template", nullok = True), 'enabled': Parameter(bool, "Message is enabled"), } def sync(self, commit=True, validate=True): AlchemyObj.sync(self, commit, validate) if 'message_id' not in self: # Before a new slice is added, delete expired slices #expired = Slices(self.api, expires = -int(time.time())) #for slice in expired: # slice.delete(commit) AlchemyObj.insert(self, dict(self)) else: AlchemyObj.update(self, {'message_id': self['message_id']}, dict(self)) def delete(self, commit = True): assert 'message_id' in self AlchemyObj.delete(self, dict(self)) class Messages(list): """ Representation of row(s) from the messages table in the database. """ def __init__(self, api, message_filter = None, columns = None, enabled = None): if not message_filter: messages = Message().select() if isinstance(message_filter, (list, tuple, set, int, long)): messages = Message().select(filter={'message_id': message_filter}) elif isinstance(message_filter, dict): messages = Message().select(filter=message_filter) else: raise PLCInvalidArgument, "Wrong message filter %r"%message_filter for message in messages: self.append(message)