add messages interface
[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 # $Id: Messages.py,v 1.4 2006/10/31 21:46:14 mlhuang Exp $
8 #
9
10 from PLC.Parameter import Parameter
11 from PLC.Table import Row, Table
12
13 class Message(Row):
14     """
15     Representation of a row in the messages table. 
16     """
17     
18     table_name = 'messages'
19     primary_key = 'message_id'
20     fields = {
21         'message_id': Parameter(str, "Message identifier"),
22         'template': Parameter(str, "Message template", nullok = True),
23         'enabled': Parameter(bool, "Message is enabled"),
24         }
25     
26 class Messages(Table):
27     """
28     Representation of row(s) from the messages table in the database. 
29     """
30
31     def __init__(self, api, message_ids, enabled = None):
32         self.api = api
33     
34         sql = "SELECT %s from messages" % ", ".join(Message.fields)
35
36         if enabled is not None:
37             sql += " WHERE enabled IS %(enabled)s"
38
39         rows = self.api.db.selectall(sql, locals())
40
41         for row in rows:
42             self[row['message_id']] = Message(api, row)