[FIX] website_forum: fixed controller creating a new forum tha was crashing.
[odoo/odoo.git] / openerpcommand / client.py
1 """
2 Define a few common arguments for client-side command-line tools.
3 """
4 import os
5 import sys
6 import time
7 import xmlrpclib
8
9 import common
10
11 class Client(common.Command):
12     """
13     Base class for XML-RPC command-line clients. It must be inherited and the
14     work() method overriden.
15     """
16
17     def __init__(self, subparsers=None):
18         super(Client, self).__init__(subparsers)
19         required_or_default = common.required_or_default
20         self.parser.add_argument('-H', '--host', metavar='HOST',
21             **required_or_default('HOST', 'the server host'))
22         self.parser.add_argument('-P', '--port', metavar='PORT',
23             **required_or_default('PORT', 'the server port'))
24
25     def execute(self, *args):
26         return self.object_proxy.execute(self.database, self.uid, self.password, *args)
27
28     def initialize(self):
29         self.host = self.args.host
30         self.port = int(self.args.port)
31         self.database = self.args.database
32         self.user = self.args.user
33         self.password = self.args.password
34
35         self.url = 'http://%s:%d/xmlrpc/' % (self.host, self.port)
36         self.common_proxy = xmlrpclib.ServerProxy(self.url + 'common')
37         self.object_proxy = xmlrpclib.ServerProxy(self.url + 'object')
38
39         try:
40             self.uid = int(self.user)
41         except ValueError, e:
42             self.uid = self.common_proxy.login(self.database, self.user, self.password)
43
44     def run(self, *args):
45         self.initialize()
46         self.work(*args)
47
48     def work(self, *args):
49         pass
50
51 class Open(Client):
52     """Get the web client's URL to view a specific model."""
53
54     command_name = 'open'
55
56     def __init__(self, subparsers=None):
57         super(Open, self).__init__(subparsers)
58         self.parser.add_argument('-m', '--model', metavar='MODEL',
59             required=True, help='the view type')
60         self.parser.add_argument('-v', '--view-mode', metavar='VIEWMODE',
61             default='tree', help='the view mode')
62
63     def work(self):
64         ids = self.execute('ir.actions.act_window', 'search', [
65             ('res_model', '=', self.args.model),
66             ('view_mode', 'like', self.args.view_mode),
67             ])
68         xs = self.execute('ir.actions.act_window', 'read', ids, [])
69         for x in xs:
70             print x['id'], x['name']
71             d = {}
72             d['host'] = self.host
73             d['port'] = self.port
74             d['action_id'] = x['id']
75             print "  http://%(host)s:%(port)s/web/webclient/home#action_id=%(action_id)s" % d
76
77 class Show(Client):
78     """Display a record."""
79
80     command_name = 'show'
81
82     def __init__(self, subparsers=None):
83         super(Show, self).__init__(subparsers)
84         self.parser.add_argument('-m', '--model', metavar='MODEL',
85             required=True, help='the model')
86         self.parser.add_argument('-i', '--id', metavar='RECORDID',
87             required=True, help='the record id')
88
89     def work(self):
90         xs = self.execute(self.args.model, 'read', [self.args.id], [])
91         if xs:
92             x = xs[0]
93             print x['name']
94         else:
95             print "Record not found."
96
97 class ConsumeNothing(Client):
98     """Call test.limits.model.consume_nothing()."""
99
100     command_name = 'consume-nothing'
101
102     def work(self):
103         xs = self.execute('test.limits.model', 'consume_nothing')
104
105 class ConsumeMemory(Client):
106     """Call test.limits.model.consume_memory()."""
107
108     command_name = 'consume-memory'
109
110     def __init__(self, subparsers=None):
111         super(ConsumeMemory, self).__init__(subparsers)
112         self.parser.add_argument('--size', metavar='SIZE',
113             required=True, help='size of the list to allocate')
114
115     def work(self):
116         xs = self.execute('test.limits.model', 'consume_memory', int(self.args.size))
117
118 class LeakMemory(ConsumeMemory):
119     """Call test.limits.model.leak_memory()."""
120
121     command_name = 'leak-memory'
122
123     def work(self):
124         xs = self.execute('test.limits.model', 'leak_memory', int(self.args.size))
125
126 class ConsumeCPU(Client):
127     """Call test.limits.model.consume_cpu_time()."""
128
129     command_name = 'consume-cpu'
130
131     def __init__(self, subparsers=None):
132         super(ConsumeCPU, self).__init__(subparsers)
133         self.parser.add_argument('--seconds', metavar='INT',
134             required=True, help='how much CPU time to consume')
135
136     def work(self):
137         xs = self.execute('test.limits.model', 'consume_cpu_time', int(self.args.seconds))