Left: | ||
Right: |
OLD | NEW |
---|---|
1 #This file is part of Tryton. The COPYRIGHT file at the top level of | 1 #This file is part of Tryton. The COPYRIGHT file at the top level of |
2 #this repository contains the full copyright notices and license terms. | 2 #this repository contains the full copyright notices and license terms. |
3 "Company" | 3 from __future__ import with_statement |
4 import copy | 4 import copy |
5 from trytond.model import ModelView, ModelSQL, fields | 5 from trytond.model import ModelView, ModelSQL, fields |
6 from trytond.wizard import Wizard | 6 from trytond.wizard import Wizard |
7 from trytond.report import Report | 7 from trytond.report import Report |
8 from trytond.pyson import Eval, If, In, Get | 8 from trytond.pyson import Eval, If, In, Get |
9 from trytond.transaction import Transaction | |
9 | 10 |
10 | 11 |
11 class Company(ModelSQL, ModelView): | 12 class Company(ModelSQL, ModelView): |
12 'Company' | 13 'Company' |
13 _name = 'company.company' | 14 _name = 'company.company' |
14 _description = __doc__ | 15 _description = __doc__ |
15 _inherits = {'party.party': 'party'} | 16 _inherits = {'party.party': 'party'} |
16 | 17 |
17 party = fields.Many2One('party.party', 'Party', required=True, | 18 party = fields.Many2One('party.party', 'Party', required=True, |
18 ondelete='CASCADE') | 19 ondelete='CASCADE') |
19 parent = fields.Many2One('company.company', 'Parent') | 20 parent = fields.Many2One('company.company', 'Parent') |
20 childs = fields.One2Many('company.company', 'parent', 'Children') | 21 childs = fields.One2Many('company.company', 'parent', 'Children') |
21 header = fields.Text('Header') | 22 header = fields.Text('Header') |
22 footer = fields.Text('Footer') | 23 footer = fields.Text('Footer') |
23 currency = fields.Many2One('currency.currency', 'Currency', required=True) | 24 currency = fields.Many2One('currency.currency', 'Currency', required=True) |
24 employees = fields.One2Many('company.employee', 'company', 'Employees') | 25 employees = fields.One2Many('company.employee', 'company', 'Employees') |
25 | 26 |
26 def __init__(self): | 27 def __init__(self): |
27 super(Company, self).__init__() | 28 super(Company, self).__init__() |
28 self._constraints += [ | 29 self._constraints += [ |
29 ('check_recursion', 'recursive_companies'), | 30 ('check_recursion', 'recursive_companies'), |
30 ] | 31 ] |
31 self._error_messages.update({ | 32 self._error_messages.update({ |
32 'recursive_companies': 'You can not create recursive companies!', | 33 'recursive_companies': 'You can not create recursive companies!', |
33 }) | 34 }) |
34 | 35 |
35 def copy(self, cursor, user, ids, default=None, context=None): | 36 def copy(self, ids, default=None): |
36 party_obj = self.pool.get('party.party') | 37 party_obj = self.pool.get('party.party') |
37 | 38 |
38 int_id = False | 39 int_id = False |
39 if isinstance(ids, (int, long)): | 40 if isinstance(ids, (int, long)): |
40 int_id = True | 41 int_id = True |
41 ids = [ids] | 42 ids = [ids] |
42 if default is None: | 43 if default is None: |
43 default = {} | 44 default = {} |
44 default = default.copy() | 45 default = default.copy() |
45 new_ids = [] | 46 new_ids = [] |
46 for company in self.browse(cursor, user, ids, context=context): | 47 for company in self.browse(ids): |
47 default['party'] = party_obj.copy(cursor, user, company.party.id, | 48 default['party'] = party_obj.copy(company.party.id) |
48 context=context) | 49 new_id = super(Company, self).copy(company.id, default=default) |
49 new_id = super(Company, self).copy(cursor, user, company.id, | |
50 default=default, context=context) | |
51 new_ids.append(new_id) | 50 new_ids.append(new_id) |
52 | 51 |
53 if int_id: | 52 if int_id: |
54 return new_ids[0] | 53 return new_ids[0] |
55 return new_ids | 54 return new_ids |
56 | 55 |
57 def write(self, cursor, user, ids, vals, context=None): | 56 def write(self, ids, vals): |
58 res = super(Company, self).write(cursor, user, ids, vals, | 57 res = super(Company, self).write(ids, vals) |
59 context=context) | |
60 # Restart the cache on the domain_get method | 58 # Restart the cache on the domain_get method |
61 self.pool.get('ir.rule').domain_get(cursor.dbname) | 59 self.pool.get('ir.rule').domain_get.reset() |
62 return res | 60 return res |
63 | 61 |
64 Company() | 62 Company() |
65 | 63 |
66 | 64 |
67 class Employee(ModelSQL, ModelView): | 65 class Employee(ModelSQL, ModelView): |
68 'Employee' | 66 'Employee' |
69 _name = 'company.employee' | 67 _name = 'company.employee' |
70 _description = __doc__ | 68 _description = __doc__ |
71 _inherits = {'party.party': 'party'} | 69 _inherits = {'party.party': 'party'} |
(...skipping 20 matching lines...) Expand all Loading... | |
92 super(User, self).__init__() | 90 super(User, self).__init__() |
93 self._context_fields.insert(0, 'company') | 91 self._context_fields.insert(0, 'company') |
94 self._constraints += [ | 92 self._constraints += [ |
95 ('check_company', 'child_company'), | 93 ('check_company', 'child_company'), |
96 ] | 94 ] |
97 self._error_messages.update({ | 95 self._error_messages.update({ |
98 'child_company': 'You can not set a company that is not ' \ | 96 'child_company': 'You can not set a company that is not ' \ |
99 'a child of your main company!', | 97 'a child of your main company!', |
100 }) | 98 }) |
101 | 99 |
102 def default_main_company(self, cursor, user, context=None): | 100 def default_main_company(self): |
103 if context is None: | 101 return Transaction().context.get['company'] or False |
ced
2010/07/31 19:43:07
get needs ()
| |
104 context = {} | |
105 if context.get('company'): | |
106 return context['company'] | |
107 return False | |
108 | 102 |
109 def default_company(self, cursor, user, context=None): | 103 def default_company(self): |
110 return self.default_main_company(cursor, user, context=context) | 104 return self.default_main_company() |
111 | 105 |
112 def get_companies(self, cursor, user_id, ids, name, context=None): | 106 def get_companies(self, ids, name): |
113 company_obj = self.pool.get('company.company') | 107 company_obj = self.pool.get('company.company') |
114 res = {} | 108 res = {} |
115 company_childs = {} | 109 company_childs = {} |
116 for user in self.browse(cursor, user_id, ids, context=context): | 110 for user in self.browse(ids): |
117 res[user.id] = [] | 111 res[user.id] = [] |
118 company_id = False | 112 company_id = False |
119 if user.company: | 113 if user.company: |
120 company_id = user.company.id | 114 company_id = user.company.id |
121 elif user.main_company: | 115 elif user.main_company: |
122 company_id = user.main_company.id | 116 company_id = user.main_company.id |
123 if company_id: | 117 if company_id: |
124 if company_id in company_childs: | 118 if company_id in company_childs: |
125 company_ids = company_childs[company_id] | 119 company_ids = company_childs[company_id] |
126 else: | 120 else: |
127 company_ids = company_obj.search(cursor, user_id, [ | 121 company_ids = company_obj.search([ |
128 ('parent', 'child_of', [company_id]), | 122 ('parent', 'child_of', [company_id]), |
129 ], context=context) | 123 ]) |
130 company_childs[company_id] = company_ids | 124 company_childs[company_id] = company_ids |
131 if company_ids: | 125 if company_ids: |
132 res[user.id].extend(company_ids) | 126 res[user.id].extend(company_ids) |
133 return res | 127 return res |
134 | 128 |
135 def get_status_bar(self, cursor, user_id, ids, name, context=None): | 129 def get_status_bar(self, ids, name): |
136 res = super(User, self).get_status_bar(cursor, user_id, ids, name, | 130 res = super(User, self).get_status_bar(ids, name) |
137 context=context) | 131 for user in self.browse(ids): |
138 for user in self.browse(cursor, user_id, ids, context=context): | |
139 if user.company: | 132 if user.company: |
140 res[user.id] += ' ' + user.company.name | 133 res[user.id] += ' ' + user.company.name |
141 return res | 134 return res |
142 | 135 |
143 def on_change_main_company(self, cursor, user, vals, context=None): | 136 def on_change_main_company(self, vals): |
144 return {'company': vals.get('main_company', False)} | 137 return {'company': vals.get('main_company', False)} |
145 | 138 |
146 def check_company(self, cursor, user_id, ids): | 139 def check_company(self, ids): |
147 company_obj = self.pool.get('company.company') | 140 company_obj = self.pool.get('company.company') |
148 for user in self.browse(cursor, user_id, ids): | 141 for user in self.browse(ids): |
149 if user.main_company: | 142 if user.main_company: |
150 companies = company_obj.search(cursor, user_id, [ | 143 companies = company_obj.search([ |
151 ('parent', 'child_of', [user.main_company.id]), | 144 ('parent', 'child_of', [user.main_company.id]), |
152 ]) | 145 ]) |
153 if user.company.id and (user.company.id not in companies): | 146 if user.company.id and (user.company.id not in companies): |
154 return False | 147 return False |
155 elif user.company: | 148 elif user.company: |
156 return False | 149 return False |
157 return True | 150 return True |
158 | 151 |
159 def _get_preferences(self, cursor, user_id, user, context_only=False, | 152 def _get_preferences(self, user, context_only=False): |
160 context=None): | 153 res = super(User, self)._get_preferences(user,· |
161 res = super(User, self)._get_preferences(cursor, user_id, user, | 154 context_only=context_only) |
ced
2010/07/31 19:43:07
Wrong indent
| |
162 context_only=context_only, context=context) | |
163 if not context_only: | 155 if not context_only: |
164 res['main_company'] = user.main_company.id | 156 res['main_company'] = user.main_company.id |
165 if user.employee: | 157 if user.employee: |
166 res['employee'] = user.employee.id | 158 res['employee'] = user.employee.id |
167 return res | 159 return res |
168 | 160 |
169 def get_preferences_fields_view(self, cursor, user_id, context=None): | 161 def get_preferences_fields_view(self): |
170 company_obj = self.pool.get('company.company') | 162 company_obj = self.pool.get('company.company') |
171 | 163 |
172 user = self.browse(cursor, user_id, user_id, context=context) | 164 user = self.browse(Transaction().user) |
173 | 165 |
174 res = super(User, self).get_preferences_fields_view(cursor, user_id, | 166 res = super(User, self).get_preferences_fields_view() |
175 context=context) | |
176 res = copy.deepcopy(res) | 167 res = copy.deepcopy(res) |
177 return res | 168 return res |
178 | 169 |
179 User() | 170 User() |
180 | 171 |
181 | 172 |
182 class Property(ModelSQL, ModelView): | 173 class Property(ModelSQL, ModelView): |
183 _name = 'ir.property' | 174 _name = 'ir.property' |
184 company = fields.Many2One('company.company', 'Company', | 175 company = fields.Many2One('company.company', 'Company', |
185 domain=[ | 176 domain=[ |
186 ('id', If(In('company', Eval('context', {})), '=', '!='), | 177 ('id', If(In('company', Eval('context', {})), '=', '!='), |
187 Get(Eval('context', {}), 'company', 0)), | 178 Get(Eval('context', {}), 'company', 0)), |
188 ]) | 179 ]) |
189 | 180 |
190 def _set_values(self, cursor, user_id, name, model, res_id, val, field_id, | 181 def _set_values(self, name, model, res_id, val, field_id): |
191 context=None): | |
192 user_obj = self.pool.get('res.user') | 182 user_obj = self.pool.get('res.user') |
193 user = user_obj.browse(cursor, user_id, user_id, context=context) | 183 user = user_obj.browse(Transaction().user) |
194 res = super(Property, self)._set_values(cursor, user_id, name, model, | 184 res = super(Property, self)._set_values(name, model, res_id, val, |
195 res_id, val, field_id, context=context) | 185 field_id) |
ced
2010/07/31 19:43:07
Wrong indent
pheller
2010/07/31 22:44:48
I thought the convention was a single indent of 4
ced
2010/08/01 06:53:03
8
pheller
2010/08/01 16:38:08
Ok, so this is why I'm confused:
Please see: htt
ced
2010/08/01 20:51:31
This is because there is double brackets. It is th
| |
196 if user_id: | 186 if user: |
197 res['company'] = user.company.id | 187 res['company'] = user.company.id |
198 return res | 188 return res |
199 | 189 |
200 def search(self, cursor, user, domain, offset=0, limit=None, order=None, | 190 def search(self, domain, offset=0, limit=None, order=None, count=False): |
201 context=None, count=False): | 191 if Transaction().user == 0: |
202 if user == 0: | |
203 domain = ['AND', domain[:], ('company', '=', False)] | 192 domain = ['AND', domain[:], ('company', '=', False)] |
204 return super(Property, self).search(cursor, user, domain, offset=offset, | 193 return super(Property, self).search(domain, offset=offset, |
205 limit=limit, order=order, context=context, count=count) | 194 limit=limit, order=order, count=count) |
206 | 195 |
207 Property() | 196 Property() |
208 | 197 |
209 | 198 |
210 class Sequence(ModelSQL, ModelView): | 199 class Sequence(ModelSQL, ModelView): |
211 _name = 'ir.sequence' | 200 _name = 'ir.sequence' |
212 company = fields.Many2One('company.company', 'Company', | 201 company = fields.Many2One('company.company', 'Company', |
213 domain=[ | 202 domain=[ |
214 ('id', If(In('company', Eval('context', {})), '=', '!='), | 203 ('id', If(In('company', Eval('context', {})), '=', '!='), |
215 Get(Eval('context', {}), 'company', 0)), | 204 Get(Eval('context', {}), 'company', 0)), |
216 ]) | 205 ]) |
217 | 206 |
218 def __init__(self): | 207 def __init__(self): |
219 super(Sequence, self).__init__() | 208 super(Sequence, self).__init__() |
220 self._order.insert(0, ('company', 'ASC')) | 209 self._order.insert(0, ('company', 'ASC')) |
221 | 210 |
222 def default_company(self, cursor, user, context=None): | 211 def default_company(self): |
223 if context is None: | 212 return Transaction().context.get['company'] or False |
ced
2010/07/31 19:43:07
get ()
| |
224 context = {} | |
225 if context.get('company'): | |
226 return context['company'] | |
227 return False | |
228 | 213 |
229 Sequence() | 214 Sequence() |
230 | 215 |
231 | 216 |
232 class SequenceStrict(Sequence): | 217 class SequenceStrict(Sequence): |
233 _name = 'ir.sequence.strict' | 218 _name = 'ir.sequence.strict' |
234 | 219 |
235 SequenceStrict() | 220 SequenceStrict() |
236 | 221 |
237 | 222 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
269 }, | 254 }, |
270 'add': { | 255 'add': { |
271 'result': { | 256 'result': { |
272 'type': 'action', | 257 'type': 'action', |
273 'action': '_add', | 258 'action': '_add', |
274 'state': 'end', | 259 'state': 'end', |
275 }, | 260 }, |
276 }, | 261 }, |
277 } | 262 } |
278 | 263 |
279 def _add(self, cursor, user, data, context=None): | 264 def _add(self, data): |
280 company_obj = self.pool.get('company.company') | 265 company_obj = self.pool.get('company.company') |
281 user_obj = self.pool.get('res.user') | 266 user_obj = self.pool.get('res.user') |
282 | 267 |
283 company_id = company_obj.create(cursor, user, data['form'], | 268 company_id = company_obj.create(data['form']) |
284 context=context) | 269 user_ids = user_obj.search([ |
285 user_ids = user_obj.search(cursor, user, [ | |
286 ('main_company', '=', False), | 270 ('main_company', '=', False), |
287 ], context=context) | 271 ]) |
288 user_obj.write(cursor, user, user_ids, { | 272 user_obj.write(user_ids, { |
289 'main_company': company_id, | 273 'main_company': company_id, |
290 'company': company_id, | 274 'company': company_id, |
291 }, context=context) | 275 }) |
292 return {} | 276 return {} |
293 | 277 |
294 CompanyConfig() | 278 CompanyConfig() |
295 | 279 |
296 | 280 |
297 class CompanyReport(Report): | 281 class CompanyReport(Report): |
298 | 282 |
299 def parse(self, cursor, user_id, report, objects, datas, context): | 283 def parse(self, report, objects, datas): |
300 user_obj = self.pool.get('res.user') | 284 user_obj = self.pool.get('res.user') |
301 | 285 |
302 user = user_obj.browse(cursor, user_id, user_id, context) | 286 user = user_obj.browse(Transaction().user) |
303 if context is None: | 287 with Transaction().set_context(company=user.company.id): |
304 context = {} | 288 return super(CompanyReport, self).parse(report, objects, datas) |
305 context = context.copy() | |
306 context['company'] = user.company | |
307 | |
308 return super(CompanyReport, self).parse(cursor, user_id, report, | |
309 objects, datas, context) | |
310 | 289 |
311 | 290 |
312 class LetterReport(CompanyReport): | 291 class LetterReport(CompanyReport): |
313 _name = 'party.letter' | 292 _name = 'party.letter' |
314 | 293 |
315 def parse(self, cursor, user_id, report, objects, datas, context): | 294 def parse(self, report, objects, datas): |
ced
2010/07/31 19:43:07
Report has localcontext
pheller
2010/07/31 22:44:48
Ok, I'm not exactly sure I understand what I must
ced
2010/08/01 06:53:03
You can look at account patch
pheller
2010/08/01 16:38:08
Ok, so I only have to add localcontext to the pars
ced
2010/08/01 20:51:31
Yes
| |
316 user_obj = self.pool.get('res.user') | 295 user_obj = self.pool.get('res.user') |
317 | 296 |
318 user = user_obj.browse(cursor, user_id, user_id, context) | 297 user = user_obj.browse(Transaction().user) |
319 if context is None: | 298 with Transaction().set_user(user): |
ced
2010/07/31 19:43:07
In fact it is localcontext that must be filled.
| |
320 context = {} | 299 return super(LetterReport, self).parse(report, objects, datas) |
321 context = context.copy() | |
322 context['user'] = user | |
323 | |
324 return super(LetterReport, self).parse(cursor, user_id, report, | |
325 objects, datas, context) | |
326 | 300 |
327 LetterReport() | 301 LetterReport() |
OLD | NEW |