-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
995 lines (773 loc) · 34.3 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
""" This module provides the elements for managing two different API styles,
namely the "traditional" and "record" styles.
In the "traditional" style, parameters like the database cursor, user id,
context dictionary and record ids (usually denoted as ``cr``, ``uid``,
``context``, ``ids``) are passed explicitly to all methods. In the "record"
style, those parameters are hidden into model instances, which gives it a
more object-oriented feel.
For instance, the statements::
model = self.pool.get(MODEL)
ids = model.search(cr, uid, DOMAIN, context=context)
for rec in model.browse(cr, uid, ids, context=context):
print rec.name
model.write(cr, uid, ids, VALUES, context=context)
may also be written as::
env = Environment(cr, uid, context) # cr, uid, context wrapped in env
model = env[MODEL] # retrieve an instance of MODEL
recs = model.search(DOMAIN) # search returns a recordset
for rec in recs: # iterate over the records
print rec.name
recs.write(VALUES) # update all records in recs
Methods written in the "traditional" style are automatically decorated,
following some heuristics based on parameter names.
"""
__all__ = [
'Environment',
'Meta', 'guess', 'noguess',
'model', 'multi', 'one',
'cr', 'cr_context', 'cr_uid', 'cr_uid_context',
'cr_uid_id', 'cr_uid_id_context', 'cr_uid_ids', 'cr_uid_ids_context',
'constrains', 'depends', 'onchange', 'returns',
]
import logging
import operator
from inspect import currentframe, getargspec
from collections import defaultdict, MutableMapping
from contextlib import contextmanager
from pprint import pformat
from weakref import WeakSet
from werkzeug.local import Local, release_local
from openerp.tools import frozendict, classproperty
_logger = logging.getLogger(__name__)
# The following attributes are used, and reflected on wrapping methods:
# - method._constrains: set by @constrains, specifies constraint dependencies
# - method._depends: set by @depends, specifies compute dependencies
# - method._returns: set by @returns, specifies return model
# - method._onchange: set by @onchange, specifies onchange fields
# - method.clear_cache: set by @ormcache, used to clear the cache
#
# On wrapping method only:
# - method._api: decorator function, used for re-applying decorator
# - method._orig: original method
#
WRAPPED_ATTRS = ('__module__', '__name__', '__doc__', '_constrains',
'_depends', '_onchange', '_returns', 'clear_cache')
INHERITED_ATTRS = ('_returns',)
class Meta(type):
""" Metaclass that automatically decorates traditional-style methods by
guessing their API. It also implements the inheritance of the
:func:`returns` decorators.
"""
def __new__(meta, name, bases, attrs):
# dummy parent class to catch overridden methods decorated with 'returns'
parent = type.__new__(meta, name, bases, {})
for key, value in attrs.items():
if not key.startswith('__') and callable(value):
# make the method inherit from decorators
value = propagate(getattr(parent, key, None), value)
# guess calling convention if none is given
if not hasattr(value, '_api'):
try:
value = guess(value)
except TypeError:
pass
attrs[key] = value
return type.__new__(meta, name, bases, attrs)
identity = lambda x: x
def decorate(method, attr, value):
""" Decorate ``method`` or its original method. """
if getattr(method, '_api', False):
# decorate the original method, and re-apply the api decorator
setattr(method._orig, attr, value)
return method._api(method._orig)
else:
# simply decorate the method itself
setattr(method, attr, value)
return method
def propagate(from_method, to_method):
""" Propagate decorators from ``from_method`` to ``to_method``, and return the
resulting method.
"""
if from_method:
for attr in INHERITED_ATTRS:
if hasattr(from_method, attr) and not hasattr(to_method, attr):
to_method = decorate(to_method, attr, getattr(from_method, attr))
return to_method
def constrains(*args):
""" Decorates a constraint checker. Each argument must be a field name
used in the check::
@api.one
@api.constrains('name', 'description')
def _check_description(self):
if self.name == self.description:
raise ValidationError("Fields name and description must be different")
Invoked on the records on which one of the named fields has been modified.
Should raise :class:`~openerp.exceptions.ValidationError` if the
validation failed.
.. warning::
``@constrains`` only supports simple field names, dotted names
(fields of relational fields e.g. ``partner_id.customer``) are not
supported and will be ignored
"""
return lambda method: decorate(method, '_constrains', args)
def onchange(*args):
""" Return a decorator to decorate an onchange method for given fields.
Each argument must be a field name::
@api.onchange('partner_id')
def _onchange_partner(self):
self.message = "Dear %s" % (self.partner_id.name or "")
In the form views where the field appears, the method will be called
when one of the given fields is modified. The method is invoked on a
pseudo-record that contains the values present in the form. Field
assignments on that record are automatically sent back to the client.
The method may return a dictionary for changing field domains and pop up
a warning message, like in the old API::
return {
'domain': {'other_id': [('partner_id', '=', partner_id)]},
'warning': {'title': "Warning", 'message': "What is this?"},
}
.. warning::
``@onchange`` only supports simple field names, dotted names
(fields of relational fields e.g. ``partner_id.tz``) are not
supported and will be ignored
"""
return lambda method: decorate(method, '_onchange', args)
def depends(*args):
""" Return a decorator that specifies the field dependencies of a "compute"
method (for new-style function fields). Each argument must be a string
that consists in a dot-separated sequence of field names::
pname = fields.Char(compute='_compute_pname')
@api.one
@api.depends('partner_id.name', 'partner_id.is_company')
def _compute_pname(self):
if self.partner_id.is_company:
self.pname = (self.partner_id.name or "").upper()
else:
self.pname = self.partner_id.name
One may also pass a single function as argument. In that case, the
dependencies are given by calling the function with the field's model.
"""
if args and callable(args[0]):
args = args[0]
elif any('id' in arg.split('.') for arg in args):
raise NotImplementedError("Compute method cannot depend on field 'id'.")
return lambda method: decorate(method, '_depends', args)
def returns(model, downgrade=None, upgrade=None):
""" Return a decorator for methods that return instances of ``model``.
:param model: a model name, or ``'self'`` for the current model
:param downgrade: a function ``downgrade(self, value, *args, **kwargs)``
to convert the record-style ``value`` to a traditional-style output
:param upgrade: a function ``upgrade(self, value, *args, **kwargs)``
to convert the traditional-style ``value`` to a record-style output
The arguments ``self``, ``*args`` and ``**kwargs`` are the ones passed
to the method in the record-style.
The decorator adapts the method output to the api style: ``id``, ``ids`` or
``False`` for the traditional style, and recordset for the record style::
@model
@returns('res.partner')
def find_partner(self, arg):
... # return some record
# output depends on call style: traditional vs record style
partner_id = model.find_partner(cr, uid, arg, context=context)
# recs = model.browse(cr, uid, ids, context)
partner_record = recs.find_partner(arg)
Note that the decorated method must satisfy that convention.
Those decorators are automatically *inherited*: a method that overrides
a decorated existing method will be decorated with the same
``@returns(model)``.
"""
return lambda method: decorate(method, '_returns', (model, downgrade, upgrade))
def make_wrapper(decorator, method, old_api, new_api):
""" Return a wrapper method for ``method``. """
def wrapper(self, *args, **kwargs):
# avoid hasattr(self, '_ids') because __getattr__() is overridden
if '_ids' in self.__dict__:
return new_api(self, *args, **kwargs)
else:
return old_api(self, *args, **kwargs)
# propagate specific openerp attributes from method to wrapper
for attr in WRAPPED_ATTRS:
if hasattr(method, attr):
setattr(wrapper, attr, getattr(method, attr))
wrapper._api = decorator
wrapper._orig = method
return wrapper
def get_downgrade(method):
""" Return a function `downgrade(self, value, *args, **kwargs)` that adapts
``value`` from record-style to traditional-style, following the
convention of ``method``.
"""
spec = getattr(method, '_returns', None)
if spec:
_, downgrade, _ = spec
if downgrade and len(getargspec(downgrade).args) > 1:
return downgrade
elif downgrade:
return lambda self, *args, **kwargs: downgrade(args[0])
else:
return lambda self, *args, **kwargs: args[0].ids
else:
return lambda self, *args, **kwargs: args[0]
def get_upgrade(method):
""" Return a function `upgrade(self, value, *args, **kwargs)` that adapts
``value`` from traditional-style to record-style, following the
convention of ``method``.
"""
spec = getattr(method, '_returns', None)
if spec:
model, _, upgrade = spec
if upgrade:
return upgrade
elif model == 'self':
return lambda self, *args, **kwargs: self.browse(args[0])
else:
return lambda self, *args, **kwargs: self.env[model].browse(args[0])
else:
return lambda self, *args, **kwargs: args[0]
def get_aggregate(method):
""" Return a function `aggregate(self, value)` that aggregates record-style
``value`` for a method decorated with ``@one``.
"""
spec = getattr(method, '_returns', None)
if spec:
# value is a list of instances, concatenate them
model, _, _ = spec
if model == 'self':
return lambda self, value: sum(value, self.browse())
else:
return lambda self, value: sum(value, self.env[model].browse())
else:
return lambda self, value: value
def get_context_split(method):
""" Return a function ``split`` that extracts the context from a pair of
positional and keyword arguments::
context, args, kwargs = split(args, kwargs)
"""
pos = len(getargspec(method).args) - 1
def split(args, kwargs):
if pos < len(args):
return args[pos], args[:pos], kwargs
else:
return kwargs.pop('context', None), args, kwargs
return split
def model(method):
""" Decorate a record-style method where ``self`` is a recordset, but its
contents is not relevant, only the model is. Such a method::
@api.model
def method(self, args):
...
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, uid, args, context=context)
Notice that no ``ids`` are passed to the method in the traditional style.
"""
split = get_context_split(method)
downgrade = get_downgrade(method)
def old_api(self, cr, uid, *args, **kwargs):
context, args, kwargs = split(args, kwargs)
recs = self.browse(cr, uid, [], context)
result = method(recs, *args, **kwargs)
return downgrade(recs, result, *args, **kwargs)
return make_wrapper(model, method, old_api, method)
def multi(method):
""" Decorate a record-style method where ``self`` is a recordset. The method
typically defines an operation on records. Such a method::
@api.multi
def method(self, args):
...
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, uid, ids, args, context=context)
"""
split = get_context_split(method)
downgrade = get_downgrade(method)
def old_api(self, cr, uid, ids, *args, **kwargs):
context, args, kwargs = split(args, kwargs)
recs = self.browse(cr, uid, ids, context)
result = method(recs, *args, **kwargs)
return downgrade(recs, result, *args, **kwargs)
return make_wrapper(multi, method, old_api, method)
def one(method):
""" Decorate a record-style method where ``self`` is expected to be a
singleton instance. The decorated method automatically loops on records,
and makes a list with the results. In case the method is decorated with
:func:`returns`, it concatenates the resulting instances. Such a
method::
@api.one
def method(self, args):
return self.name
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
names = recs.method(args)
names = model.method(cr, uid, ids, args, context=context)
.. deprecated:: 9.0
:func:`~.one` often makes the code less clear and behaves in ways
developers and readers may not expect.
It is strongly recommended to use :func:`~.multi` and either
iterate on the ``self`` recordset or ensure that the recordset
is a single record with :meth:`~openerp.models.Model.ensure_one`.
"""
split = get_context_split(method)
downgrade = get_downgrade(method)
aggregate = get_aggregate(method)
def old_api(self, cr, uid, ids, *args, **kwargs):
context, args, kwargs = split(args, kwargs)
recs = self.browse(cr, uid, ids, context)
result = new_api(recs, *args, **kwargs)
return downgrade(recs, result, *args, **kwargs)
def new_api(self, *args, **kwargs):
result = [method(rec, *args, **kwargs) for rec in self]
return aggregate(self, result)
return make_wrapper(one, method, old_api, new_api)
def cr(method):
""" Decorate a traditional-style method that takes ``cr`` as a parameter.
Such a method may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, args)
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
result = method(self._model, cr, *args, **kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr, method, method, new_api)
def cr_context(method):
""" Decorate a traditional-style method that takes ``cr``, ``context`` as parameters. """
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
old_kwargs = dict(kwargs, context=context)
result = method(self._model, cr, *args, **old_kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_context, method, method, new_api)
def cr_uid(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid`` as parameters. """
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
result = method(self._model, cr, uid, *args, **kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid, method, method, new_api)
def cr_uid_context(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid``, ``context`` as
parameters. Such a method may be called in both record and traditional
styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, uid, args, context=context)
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
old_kwargs = dict(kwargs, context=context)
result = method(self._model, cr, uid, *args, **old_kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid_context, method, method, new_api)
def cr_uid_id(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid``, ``id`` as
parameters. Such a method may be called in both record and traditional
styles. In the record style, the method automatically loops on records.
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
result = [method(self._model, cr, uid, id, *args, **kwargs) for id in self.ids]
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid_id, method, method, new_api)
def cr_uid_id_context(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid``, ``id``,
``context`` as parameters. Such a method::
@api.cr_uid_id
def method(self, cr, uid, id, args, context=None):
...
may be called in both record and traditional styles, like::
# rec = model.browse(cr, uid, id, context)
rec.method(args)
model.method(cr, uid, id, args, context=context)
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
old_kwargs = dict(kwargs, context=context)
result = [method(self._model, cr, uid, id, *args, **old_kwargs) for id in self.ids]
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid_id_context, method, method, new_api)
def cr_uid_ids(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid``, ``ids`` as
parameters. Such a method may be called in both record and traditional
styles.
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
result = method(self._model, cr, uid, self.ids, *args, **kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid_ids, method, method, new_api)
def cr_uid_ids_context(method):
""" Decorate a traditional-style method that takes ``cr``, ``uid``, ``ids``,
``context`` as parameters. Such a method::
@api.cr_uid_ids_context
def method(self, cr, uid, ids, args, context=None):
...
may be called in both record and traditional styles, like::
# recs = model.browse(cr, uid, ids, context)
recs.method(args)
model.method(cr, uid, ids, args, context=context)
It is generally not necessary, see :func:`guess`.
"""
upgrade = get_upgrade(method)
def new_api(self, *args, **kwargs):
cr, uid, context = self.env.args
old_kwargs = dict(kwargs, context=context)
result = method(self._model, cr, uid, self.ids, *args, **old_kwargs)
return upgrade(self, result, *args, **kwargs)
return make_wrapper(cr_uid_ids_context, method, method, new_api)
def v7(method_v7):
""" Decorate a method that supports the old-style api only. A new-style api
may be provided by redefining a method with the same name and decorated
with :func:`~.v8`::
@api.v7
def foo(self, cr, uid, ids, context=None):
...
@api.v8
def foo(self):
...
Special care must be taken if one method calls the other one, because
the method may be overridden! In that case, one should call the method
from the current class (say ``MyClass``), for instance::
@api.v7
def foo(self, cr, uid, ids, context=None):
# Beware: records.foo() may call an overriding of foo()
records = self.browse(cr, uid, ids, context)
return MyClass.foo(records)
Note that the wrapper method uses the docstring of the first method.
"""
# retrieve method_v8 from the caller's frame
frame = currentframe().f_back
method = frame.f_locals.get(method_v7.__name__)
method_v8 = getattr(method, '_v8', method)
wrapper = make_wrapper(v7, method_v7, method_v7, method_v8)
wrapper._v7 = method_v7
wrapper._v8 = method_v8
return wrapper
def v8(method_v8):
""" Decorate a method that supports the new-style api only. An old-style api
may be provided by redefining a method with the same name and decorated
with :func:`~.v7`::
@api.v8
def foo(self):
...
@api.v7
def foo(self, cr, uid, ids, context=None):
...
Note that the wrapper method uses the docstring of the first method.
"""
# retrieve method_v7 from the caller's frame
frame = currentframe().f_back
method = frame.f_locals.get(method_v8.__name__)
method_v7 = getattr(method, '_v7', method)
wrapper = make_wrapper(v8, method_v8, method_v7, method_v8)
wrapper._v7 = method_v7
wrapper._v8 = method_v8
return wrapper
def noguess(method):
""" Decorate a method to prevent any effect from :func:`guess`. """
method._api = False
return method
def guess(method):
""" Decorate ``method`` to make it callable in both traditional and record
styles. This decorator is applied automatically by the model's
metaclass, and has no effect on already-decorated methods.
The API style is determined by heuristics on the parameter names: ``cr``
or ``cursor`` for the cursor, ``uid`` or ``user`` for the user id,
``id`` or ``ids`` for a list of record ids, and ``context`` for the
context dictionary. If a traditional API is recognized, one of the
decorators :func:`cr`, :func:`cr_context`, :func:`cr_uid`,
:func:`cr_uid_context`, :func:`cr_uid_id`, :func:`cr_uid_id_context`,
:func:`cr_uid_ids`, :func:`cr_uid_ids_context` is applied on the method.
Method calls are considered traditional style when their first parameter
is a database cursor.
"""
if hasattr(method, '_api'):
return method
# introspection on argument names to determine api style
args, vname, kwname, defaults = getargspec(method)
names = tuple(args) + (None,) * 4
if names[0] == 'self':
if names[1] in ('cr', 'cursor'):
if names[2] in ('uid', 'user'):
if names[3] == 'ids':
if 'context' in names or kwname:
return cr_uid_ids_context(method)
else:
return cr_uid_ids(method)
elif names[3] == 'id' or names[3] == 'res_id':
if 'context' in names or kwname:
return cr_uid_id_context(method)
else:
return cr_uid_id(method)
elif 'context' in names or kwname:
return cr_uid_context(method)
else:
return cr_uid(method)
elif 'context' in names:
return cr_context(method)
else:
return cr(method)
# no wrapping by default
return noguess(method)
def expected(decorator, func):
""" Decorate ``func`` with ``decorator`` if ``func`` is not wrapped yet. """
return decorator(func) if not hasattr(func, '_api') else func
class Environment(object):
""" An environment wraps data for ORM records:
- :attr:`cr`, the current database cursor;
- :attr:`uid`, the current user id;
- :attr:`context`, the current context dictionary.
It provides access to the registry by implementing a mapping from model
names to new api models. It also holds a cache for records, and a data
structure to manage recomputations.
"""
_local = Local()
@classproperty
def envs(cls):
return cls._local.environments
@classmethod
@contextmanager
def manage(cls):
""" Context manager for a set of environments. """
if hasattr(cls._local, 'environments'):
yield
else:
try:
cls._local.environments = Environments()
yield
finally:
release_local(cls._local)
@classmethod
def reset(cls):
""" Clear the set of environments.
This may be useful when recreating a registry inside a transaction.
"""
cls._local.environments = Environments()
def __new__(cls, cr, uid, context):
assert context is not None
args = (cr, uid, context)
# if env already exists, return it
env, envs = None, cls.envs
for env in envs:
if env.args == args:
return env
# otherwise create environment, and add it in the set
self = object.__new__(cls)
self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context))
self.registry = RegistryManager.get(cr.dbname)
self.cache = defaultdict(dict) # {field: {id: value, ...}, ...}
self.prefetch = defaultdict(set) # {model_name: set(id), ...}
self.computed = defaultdict(set) # {field: set(id), ...}
self.dirty = defaultdict(set) # {record: set(field_name), ...}
self.all = envs
envs.add(self)
return self
def __contains__(self, model_name):
""" Test whether the given model exists. """
return model_name in self.registry
def __getitem__(self, model_name):
""" Return an empty recordset from the given model. """
return self.registry[model_name]._browse(self, ())
def __iter__(self):
""" Return an iterator on model names. """
return iter(self.registry)
def __len__(self):
""" Return the size of the model registry. """
return len(self.registry)
def __call__(self, cr=None, user=None, context=None):
""" Return an environment based on ``self`` with modified parameters.
:param cr: optional database cursor to change the current cursor
:param user: optional user/user id to change the current user
:param context: optional context dictionary to change the current context
"""
cr = self.cr if cr is None else cr
uid = self.uid if user is None else int(user)
context = self.context if context is None else context
return Environment(cr, uid, context)
def ref(self, xml_id, raise_if_not_found=True):
""" return the record corresponding to the given ``xml_id`` """
return self['ir.model.data'].xmlid_to_object(xml_id, raise_if_not_found=raise_if_not_found)
@property
def user(self):
""" return the current user (as an instance) """
return self(user=SUPERUSER_ID)['res.users'].browse(self.uid)
@property
def lang(self):
""" return the current language code """
return self.context.get('lang')
@contextmanager
def _do_in_mode(self, mode):
if self.all.mode:
yield
else:
try:
self.all.mode = mode
yield
finally:
self.all.mode = False
self.dirty.clear()
def do_in_draft(self):
""" Context-switch to draft mode, where all field updates are done in
cache only.
"""
return self._do_in_mode(True)
@property
def in_draft(self):
""" Return whether we are in draft mode. """
return bool(self.all.mode)
def do_in_onchange(self):
""" Context-switch to 'onchange' draft mode, which is a specialized
draft mode used during execution of onchange methods.
"""
return self._do_in_mode('onchange')
@property
def in_onchange(self):
""" Return whether we are in 'onchange' draft mode. """
return self.all.mode == 'onchange'
def invalidate(self, spec):
""" Invalidate some fields for some records in the cache of all
environments.
:param spec: what to invalidate, a list of `(field, ids)` pair,
where ``field`` is a field object, and ``ids`` is a list of record
ids or ``None`` (to invalidate all records).
"""
if not spec:
return
for env in list(self.all):
c = env.cache
for field, ids in spec:
if ids is None:
if field in c:
del c[field]
else:
field_cache = c[field]
for id in ids:
field_cache.pop(id, None)
def invalidate_all(self):
""" Clear the cache of all environments. """
for env in list(self.all):
env.cache.clear()
env.prefetch.clear()
env.computed.clear()
env.dirty.clear()
def clear(self):
""" Clear all record caches, and discard all fields to recompute.
This may be useful when recovering from a failed ORM operation.
"""
self.invalidate_all()
self.all.todo.clear()
@contextmanager
def clear_upon_failure(self):
""" Context manager that clears the environments (caches and fields to
recompute) upon exception.
"""
try:
yield
except Exception:
self.clear()
raise
def field_todo(self, field):
""" Return a recordset with all records to recompute for ``field``. """
ids = {rid for recs in self.all.todo.get(field, ()) for rid in recs.ids}
return self[field.model_name].browse(ids)
def check_todo(self, field, record):
""" Check whether ``field`` must be recomputed on ``record``, and if so,
return the corresponding recordset to recompute.
"""
for recs in self.all.todo.get(field, []):
if recs & record:
return recs
def add_todo(self, field, records):
""" Mark ``field`` to be recomputed on ``records``. """
recs_list = self.all.todo.setdefault(field, [])
for i, recs in enumerate(recs_list):
if recs.env == records.env:
recs_list[i] |= records
break
else:
recs_list.append(records)
def remove_todo(self, field, records):
""" Mark ``field`` as recomputed on ``records``. """
recs_list = [recs - records for recs in self.all.todo.pop(field, [])]
recs_list = filter(None, recs_list)
if recs_list:
self.all.todo[field] = recs_list
def has_todo(self):
""" Return whether some fields must be recomputed. """
return bool(self.all.todo)
def get_todo(self):
""" Return a pair ``(field, records)`` to recompute.
The field is such that none of its dependencies must be recomputed.
"""
field = min(self.all.todo, key=self.registry.field_sequence)
return field, self.all.todo[field][0]
def check_cache(self):
""" Check the cache consistency. """
# make a full copy of the cache, and invalidate it
cache_dump = dict(
(field, dict(field_cache))
for field, field_cache in self.cache.iteritems()
)
self.invalidate_all()
# re-fetch the records, and compare with their former cache
invalids = []
for field, field_dump in cache_dump.iteritems():
ids = filter(None, field_dump)
records = self[field.model_name].browse(ids)
for record in records:
try:
cached = field_dump[record.id]
fetched = record[field.name]
if fetched != cached:
info = {'cached': cached, 'fetched': fetched}
invalids.append((field, record, info))
except (AccessError, MissingError):
pass
if invalids:
raise UserError('Invalid cache for fields\n' + pformat(invalids))
@property
def recompute(self):
return self.all.recompute
@contextmanager
def norecompute(self):
tmp = self.all.recompute
self.all.recompute = False
try:
yield
finally:
self.all.recompute = tmp
@property
def recompute_old(self):
return self.all.recompute_old
def clear_recompute_old(self):
del self.all.recompute_old[:]
class Environments(object):
""" A common object for all environments in a request. """
def __init__(self):
self.envs = WeakSet() # weak set of environments
self.todo = {} # recomputations {field: [records]}
self.mode = False # flag for draft/onchange
self.recompute = True
self.recompute_old = [] # list of old api compute fields to recompute
def add(self, env):
""" Add the environment ``env``. """
self.envs.add(env)
def __iter__(self):
""" Iterate over environments. """
return iter(self.envs)
# keep those imports here in order to handle cyclic dependencies correctly
from openerp import SUPERUSER_ID
from openerp.exceptions import UserError, AccessError, MissingError
from openerp.modules.registry import RegistryManager