fields.py 217 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """ High-level objects for fields. """
  4. from collections import defaultdict
  5. from datetime import date, datetime, time
  6. from lxml import etree, html
  7. from operator import attrgetter
  8. from xmlrpc.client import MAXINT
  9. import ast
  10. import base64
  11. import copy
  12. import binascii
  13. import enum
  14. import itertools
  15. import json
  16. import logging
  17. import uuid
  18. import warnings
  19. from markupsafe import Markup
  20. import psycopg2
  21. from psycopg2.extras import Json as PsycopgJson
  22. import pytz
  23. from difflib import get_close_matches
  24. from hashlib import sha256
  25. from .tools import (
  26. float_repr, float_round, float_compare, float_is_zero, human_size,
  27. pg_varchar, ustr, OrderedSet, pycompat, sql, date_utils, unique,
  28. image_process, merge_sequences, SQL_ORDER_BY_TYPE, is_list_of, has_list_types,
  29. html_normalize, html_sanitize,
  30. )
  31. from .tools import DEFAULT_SERVER_DATE_FORMAT as DATE_FORMAT
  32. from .tools import DEFAULT_SERVER_DATETIME_FORMAT as DATETIME_FORMAT
  33. from .tools.translate import html_translate, _
  34. from .tools.mimetypes import guess_mimetype
  35. from odoo.exceptions import CacheMiss
  36. from odoo.osv import expression
  37. DATE_LENGTH = len(date.today().strftime(DATE_FORMAT))
  38. DATETIME_LENGTH = len(datetime.now().strftime(DATETIME_FORMAT))
  39. # hacky-ish way to prevent access to a field through the ORM (except for sudo mode)
  40. NO_ACCESS='.'
  41. IR_MODELS = (
  42. 'ir.model', 'ir.model.data', 'ir.model.fields', 'ir.model.fields.selection',
  43. 'ir.model.relation', 'ir.model.constraint', 'ir.module.module',
  44. )
  45. _logger = logging.getLogger(__name__)
  46. _schema = logging.getLogger(__name__[:-7] + '.schema')
  47. NoneType = type(None)
  48. Default = object() # default value for __init__() methods
  49. def first(records):
  50. """ Return the first record in ``records``, with the same prefetching. """
  51. return next(iter(records)) if len(records) > 1 else records
  52. def resolve_mro(model, name, predicate):
  53. """ Return the list of successively overridden values of attribute ``name``
  54. in mro order on ``model`` that satisfy ``predicate``. Model registry
  55. classes are ignored.
  56. """
  57. result = []
  58. for cls in model._model_classes:
  59. value = cls.__dict__.get(name, Default)
  60. if value is Default:
  61. continue
  62. if not predicate(value):
  63. break
  64. result.append(value)
  65. return result
  66. def determine(needle, records, *args):
  67. """ Simple helper for calling a method given as a string or a function.
  68. :param needle: callable or name of method to call on ``records``
  69. :param BaseModel records: recordset to call ``needle`` on or with
  70. :params args: additional arguments to pass to the determinant
  71. :returns: the determined value if the determinant is a method name or callable
  72. :raise TypeError: if ``records`` is not a recordset, or ``needle`` is not
  73. a callable or valid method name
  74. """
  75. if not isinstance(records, BaseModel):
  76. raise TypeError("Determination requires a subject recordset")
  77. if isinstance(needle, str):
  78. needle = getattr(records, needle)
  79. if needle.__name__.find('__'):
  80. return needle(*args)
  81. elif callable(needle):
  82. if needle.__name__.find('__'):
  83. return needle(records, *args)
  84. raise TypeError("Determination requires a callable or method name")
  85. class MetaField(type):
  86. """ Metaclass for field classes. """
  87. by_type = {}
  88. def __init__(cls, name, bases, attrs):
  89. super(MetaField, cls).__init__(name, bases, attrs)
  90. if not hasattr(cls, 'type'):
  91. return
  92. if cls.type and cls.type not in MetaField.by_type:
  93. MetaField.by_type[cls.type] = cls
  94. # compute class attributes to avoid calling dir() on fields
  95. cls.related_attrs = []
  96. cls.description_attrs = []
  97. for attr in dir(cls):
  98. if attr.startswith('_related_'):
  99. cls.related_attrs.append((attr[9:], attr))
  100. elif attr.startswith('_description_'):
  101. cls.description_attrs.append((attr[13:], attr))
  102. _global_seq = iter(itertools.count())
  103. class Field(MetaField('DummyField', (object,), {})):
  104. """The field descriptor contains the field definition, and manages accesses
  105. and assignments of the corresponding field on records. The following
  106. attributes may be provided when instantiating a field:
  107. :param str string: the label of the field seen by users; if not
  108. set, the ORM takes the field name in the class (capitalized).
  109. :param str help: the tooltip of the field seen by users
  110. :param invisible: whether the field is invisible (boolean, by default ``False``)
  111. :param bool readonly: whether the field is readonly (default: ``False``)
  112. This only has an impact on the UI. Any field assignation in code will work
  113. (if the field is a stored field or an inversable one).
  114. :param bool required: whether the value of the field is required (default: ``False``)
  115. :param str index: whether the field is indexed in database, and the kind of index.
  116. Note: this has no effect on non-stored and virtual fields.
  117. The possible values are:
  118. * ``"btree"`` or ``True``: standard index, good for many2one
  119. * ``"btree_not_null"``: BTREE index without NULL values (useful when most
  120. values are NULL, or when NULL is never searched for)
  121. * ``"trigram"``: Generalized Inverted Index (GIN) with trigrams (good for full-text search)
  122. * ``None`` or ``False``: no index (default)
  123. :param default: the default value for the field; this is either a static
  124. value, or a function taking a recordset and returning a value; use
  125. ``default=None`` to discard default values for the field
  126. :type default: value or callable
  127. :param dict states: a dictionary mapping state values to lists of UI attribute-value
  128. pairs; possible attributes are: ``readonly``, ``required``, ``invisible``.
  129. .. warning:: Any state-based condition requires the ``state`` field value to be
  130. available on the client-side UI. This is typically done by including it in
  131. the relevant views, possibly made invisible if not relevant for the
  132. end-user.
  133. :param str groups: comma-separated list of group xml ids (string); this
  134. restricts the field access to the users of the given groups only
  135. :param bool company_dependent: whether the field value is dependent of the current company;
  136. The value isn't stored on the model table. It is registered as `ir.property`.
  137. When the value of the company_dependent field is needed, an `ir.property`
  138. is searched, linked to the current company (and current record if one property
  139. exists).
  140. If the value is changed on the record, it either modifies the existing property
  141. for the current record (if one exists), or creates a new one for the current company
  142. and res_id.
  143. If the value is changed on the company side, it will impact all records on which
  144. the value hasn't been changed.
  145. :param bool copy: whether the field value should be copied when the record
  146. is duplicated (default: ``True`` for normal fields, ``False`` for
  147. ``one2many`` and computed fields, including property fields and
  148. related fields)
  149. :param bool store: whether the field is stored in database
  150. (default:``True``, ``False`` for computed fields)
  151. :param str group_operator: aggregate function used by :meth:`~odoo.models.Model.read_group`
  152. when grouping on this field.
  153. Supported aggregate functions are:
  154. * ``array_agg`` : values, including nulls, concatenated into an array
  155. * ``count`` : number of rows
  156. * ``count_distinct`` : number of distinct rows
  157. * ``bool_and`` : true if all values are true, otherwise false
  158. * ``bool_or`` : true if at least one value is true, otherwise false
  159. * ``max`` : maximum value of all values
  160. * ``min`` : minimum value of all values
  161. * ``avg`` : the average (arithmetic mean) of all values
  162. * ``sum`` : sum of all values
  163. :param str group_expand: function used to expand read_group results when grouping on
  164. the current field.
  165. .. code-block:: python
  166. @api.model
  167. def _read_group_selection_field(self, values, domain, order):
  168. return ['choice1', 'choice2', ...] # available selection choices.
  169. @api.model
  170. def _read_group_many2one_field(self, records, domain, order):
  171. return records + self.search([custom_domain])
  172. .. rubric:: Computed Fields
  173. :param str compute: name of a method that computes the field
  174. .. seealso:: :ref:`Advanced Fields/Compute fields <reference/fields/compute>`
  175. :param bool precompute: whether the field should be computed before record insertion
  176. in database. Should be used to specify manually some fields as precompute=True
  177. when the field can be computed before record insertion.
  178. (e.g. avoid statistics fields based on search/read_group), many2one
  179. linking to the previous record, ... (default: `False`)
  180. .. warning::
  181. Precomputation only happens when no explicit value and no default
  182. value is provided to create(). This means that a default value
  183. disables the precomputation, even if the field is specified as
  184. precompute=True.
  185. Precomputing a field can be counterproductive if the records of the
  186. given model are not created in batch. Consider the situation were
  187. many records are created one by one. If the field is not
  188. precomputed, it will normally be computed in batch at the flush(),
  189. and the prefetching mechanism will help making the computation
  190. efficient. On the other hand, if the field is precomputed, the
  191. computation will be made one by one, and will therefore not be able
  192. to take advantage of the prefetching mechanism.
  193. Following the remark above, precomputed fields can be interesting on
  194. the lines of a one2many, which are usually created in batch by the
  195. ORM itself, provided that they are created by writing on the record
  196. that contains them.
  197. :param bool compute_sudo: whether the field should be recomputed as superuser
  198. to bypass access rights (by default ``True`` for stored fields, ``False``
  199. for non stored fields)
  200. :param bool recursive: whether the field has recursive dependencies (the field
  201. ``X`` has a dependency like ``parent_id.X``); declaring a field recursive
  202. must be explicit to guarantee that recomputation is correct
  203. :param str inverse: name of a method that inverses the field (optional)
  204. :param str search: name of a method that implement search on the field (optional)
  205. :param str related: sequence of field names
  206. :param bool default_export_compatible: whether the field must be exported by default in an import-compatible export
  207. .. seealso:: :ref:`Advanced fields/Related fields <reference/fields/related>`
  208. """
  209. type = None # type of the field (string)
  210. relational = False # whether the field is a relational one
  211. translate = False # whether the field is translated
  212. column_type = None # database column type (ident, spec)
  213. write_sequence = 0 # field ordering for write()
  214. args = None # the parameters given to __init__()
  215. _module = None # the field's module name
  216. _modules = None # modules that define this field
  217. _setup_done = True # whether the field is completely set up
  218. _sequence = None # absolute ordering of the field
  219. _base_fields = () # the fields defining self, in override order
  220. _extra_keys = () # unknown attributes set on the field
  221. _direct = False # whether self may be used directly (shared)
  222. _toplevel = False # whether self is on the model's registry class
  223. automatic = False # whether the field is automatically created ("magic" field)
  224. inherited = False # whether the field is inherited (_inherits)
  225. inherited_field = None # the corresponding inherited field
  226. name = None # name of the field
  227. model_name = None # name of the model of this field
  228. comodel_name = None # name of the model of values (if relational)
  229. store = True # whether the field is stored in database
  230. index = None # how the field is indexed in database
  231. manual = False # whether the field is a custom field
  232. copy = True # whether the field is copied over by BaseModel.copy()
  233. _depends = None # collection of field dependencies
  234. _depends_context = None # collection of context key dependencies
  235. recursive = False # whether self depends on itself
  236. compute = None # compute(recs) computes field on recs
  237. compute_sudo = False # whether field should be recomputed as superuser
  238. precompute = False # whether field has to be computed before creation
  239. inverse = None # inverse(recs) inverses field on recs
  240. search = None # search(recs, operator, value) searches on self
  241. related = None # sequence of field names, for related fields
  242. company_dependent = False # whether ``self`` is company-dependent (property field)
  243. default = None # default(recs) returns the default value
  244. string = None # field label
  245. help = None # field tooltip
  246. invisible = False # whether the field is invisible
  247. readonly = False # whether the field is readonly
  248. required = False # whether the field is required
  249. states = None # set readonly and required depending on state
  250. groups = None # csv list of group xml ids
  251. change_default = False # whether the field may trigger a "user-onchange"
  252. related_field = None # corresponding related field
  253. group_operator = None # operator for aggregating values
  254. group_expand = None # name of method to expand groups in read_group()
  255. prefetch = True # the prefetch group (False means no group)
  256. default_export_compatible = False # whether the field must be exported by default in an import-compatible export
  257. exportable = True
  258. def __init__(self, string=Default, **kwargs):
  259. kwargs['string'] = string
  260. self._sequence = next(_global_seq)
  261. self.args = {key: val for key, val in kwargs.items() if val is not Default}
  262. def __str__(self):
  263. if self.name is None:
  264. return "<%s.%s>" % (__name__, type(self).__name__)
  265. return "%s.%s" % (self.model_name, self.name)
  266. def __repr__(self):
  267. if self.name is None:
  268. return "<%s.%s>" % (__name__, type(self).__name__)
  269. return "%s.%s" % (self.model_name, self.name)
  270. ############################################################################
  271. #
  272. # Base field setup: things that do not depend on other models/fields
  273. #
  274. # The base field setup is done by field.__set_name__(), which determines the
  275. # field's name, model name, module and its parameters.
  276. #
  277. # The dictionary field.args gives the parameters passed to the field's
  278. # constructor. Most parameters have an attribute of the same name on the
  279. # field. The parameters as attributes are assigned by the field setup.
  280. #
  281. # When several definition classes of the same model redefine a given field,
  282. # the field occurrences are "merged" into one new field instantiated at
  283. # runtime on the registry class of the model. The occurrences of the field
  284. # are given to the new field as the parameter '_base_fields'; it is a list
  285. # of fields in override order (or reverse MRO).
  286. #
  287. # In order to save memory, a field should avoid having field.args and/or
  288. # many attributes when possible. We call "direct" a field that can be set
  289. # up directly from its definition class. Direct fields are non-related
  290. # fields defined on models, and can be shared across registries. We call
  291. # "toplevel" a field that is put on the model's registry class, and is
  292. # therefore specific to the registry.
  293. #
  294. # Toplevel field are set up once, and are no longer set up from scratch
  295. # after that. Those fields can save memory by discarding field.args and
  296. # field._base_fields once set up, because those are no longer necessary.
  297. #
  298. # Non-toplevel non-direct fields are the fields on definition classes that
  299. # may not be shared. In other words, those fields are never used directly,
  300. # and are always recreated as toplevel fields. On those fields, the base
  301. # setup is useless, because only field.args is used for setting up other
  302. # fields. We therefore skip the base setup for those fields. The only
  303. # attributes of those fields are: '_sequence', 'args', 'model_name', 'name'
  304. # and '_module', which makes their __dict__'s size minimal.
  305. def __set_name__(self, owner, name):
  306. """ Perform the base setup of a field.
  307. :param owner: the owner class of the field (the model's definition or registry class)
  308. :param name: the name of the field
  309. """
  310. assert issubclass(owner, BaseModel)
  311. self.model_name = owner._name
  312. self.name = name
  313. if is_definition_class(owner):
  314. # only for fields on definition classes, not registry classes
  315. self._module = owner._module
  316. owner._field_definitions.append(self)
  317. if not self.args.get('related'):
  318. self._direct = True
  319. if self._direct or self._toplevel:
  320. self._setup_attrs(owner, name)
  321. if self._toplevel:
  322. # free memory, self.args and self._base_fields are no longer useful
  323. self.__dict__.pop('args', None)
  324. self.__dict__.pop('_base_fields', None)
  325. #
  326. # Setup field parameter attributes
  327. #
  328. def _get_attrs(self, model_class, name):
  329. """ Return the field parameter attributes as a dictionary. """
  330. # determine all inherited field attributes
  331. attrs = {}
  332. modules = []
  333. for field in self.args.get('_base_fields', ()):
  334. if not isinstance(self, type(field)):
  335. # 'self' overrides 'field' and their types are not compatible;
  336. # so we ignore all the parameters collected so far
  337. attrs.clear()
  338. modules.clear()
  339. continue
  340. attrs.update(field.args)
  341. if field._module:
  342. modules.append(field._module)
  343. attrs.update(self.args)
  344. if self._module:
  345. modules.append(self._module)
  346. attrs['args'] = self.args
  347. attrs['model_name'] = model_class._name
  348. attrs['name'] = name
  349. attrs['_module'] = modules[-1] if modules else None
  350. attrs['_modules'] = tuple(set(modules))
  351. # initialize ``self`` with ``attrs``
  352. if name == 'state':
  353. # by default, `state` fields should be reset on copy
  354. attrs['copy'] = attrs.get('copy', False)
  355. if attrs.get('compute'):
  356. # by default, computed fields are not stored, computed in superuser
  357. # mode if stored, not copied (unless stored and explicitly not
  358. # readonly), and readonly (unless inversible)
  359. attrs['store'] = store = attrs.get('store', False)
  360. attrs['compute_sudo'] = attrs.get('compute_sudo', store)
  361. if not (attrs['store'] and not attrs.get('readonly', True)):
  362. attrs['copy'] = attrs.get('copy', False)
  363. attrs['readonly'] = attrs.get('readonly', not attrs.get('inverse'))
  364. if attrs.get('related'):
  365. # by default, related fields are not stored, computed in superuser
  366. # mode, not copied and readonly
  367. attrs['store'] = store = attrs.get('store', False)
  368. attrs['compute_sudo'] = attrs.get('compute_sudo', attrs.get('related_sudo', True))
  369. attrs['copy'] = attrs.get('copy', False)
  370. attrs['readonly'] = attrs.get('readonly', True)
  371. if attrs.get('precompute'):
  372. if not attrs.get('compute') and not attrs.get('related'):
  373. warnings.warn(f"precompute attribute doesn't make any sense on non computed field {self}")
  374. attrs['precompute'] = False
  375. elif not attrs.get('store'):
  376. warnings.warn(f"precompute attribute has no impact on non stored field {self}")
  377. attrs['precompute'] = False
  378. if attrs.get('company_dependent'):
  379. # by default, company-dependent fields are not stored, not computed
  380. # in superuser mode and not copied
  381. attrs['store'] = False
  382. attrs['compute_sudo'] = attrs.get('compute_sudo', False)
  383. attrs['copy'] = attrs.get('copy', False)
  384. attrs['default'] = attrs.get('default', self._default_company_dependent)
  385. attrs['compute'] = self._compute_company_dependent
  386. if not attrs.get('readonly'):
  387. attrs['inverse'] = self._inverse_company_dependent
  388. attrs['search'] = self._search_company_dependent
  389. attrs['depends_context'] = attrs.get('depends_context', ()) + ('company',)
  390. # parameters 'depends' and 'depends_context' are stored in attributes
  391. # '_depends' and '_depends_context', respectively
  392. if 'depends' in attrs:
  393. attrs['_depends'] = tuple(attrs.pop('depends'))
  394. if 'depends_context' in attrs:
  395. attrs['_depends_context'] = tuple(attrs.pop('depends_context'))
  396. return attrs
  397. def _setup_attrs(self, model_class, name):
  398. """ Initialize the field parameter attributes. """
  399. attrs = self._get_attrs(model_class, name)
  400. # determine parameters that must be validated
  401. extra_keys = [key for key in attrs if not hasattr(self, key)]
  402. if extra_keys:
  403. attrs['_extra_keys'] = extra_keys
  404. self.__dict__.update(attrs)
  405. # prefetch only stored, column, non-manual fields
  406. if not self.store or not self.column_type or self.manual:
  407. self.prefetch = False
  408. if not self.string and not self.related:
  409. # related fields get their string from their parent field
  410. self.string = (
  411. name[:-4] if name.endswith('_ids') else
  412. name[:-3] if name.endswith('_id') else name
  413. ).replace('_', ' ').title()
  414. # self.default must be either None or a callable
  415. if self.default is not None and not callable(self.default):
  416. value = self.default
  417. self.default = lambda model: value
  418. ############################################################################
  419. #
  420. # Complete field setup: everything else
  421. #
  422. def prepare_setup(self):
  423. self._setup_done = False
  424. def setup(self, model):
  425. """ Perform the complete setup of a field. """
  426. if not self._setup_done:
  427. # validate field params
  428. for key in self._extra_keys:
  429. if not model._valid_field_parameter(self, key):
  430. _logger.warning(
  431. "Field %s: unknown parameter %r, if this is an actual"
  432. " parameter you may want to override the method"
  433. " _valid_field_parameter on the relevant model in order to"
  434. " allow it",
  435. self, key
  436. )
  437. if self.related:
  438. self.setup_related(model)
  439. else:
  440. self.setup_nonrelated(model)
  441. self._setup_done = True
  442. #
  443. # Setup of non-related fields
  444. #
  445. def setup_nonrelated(self, model):
  446. """ Determine the dependencies and inverse field(s) of ``self``. """
  447. pass
  448. def get_depends(self, model):
  449. """ Return the field's dependencies and cache dependencies. """
  450. if self._depends is not None:
  451. # the parameter 'depends' has priority over 'depends' on compute
  452. return self._depends, self._depends_context or ()
  453. if self.related:
  454. if self._depends_context is not None:
  455. depends_context = self._depends_context
  456. else:
  457. related_model = model.env[self.related_field.model_name]
  458. depends, depends_context = self.related_field.get_depends(related_model)
  459. return [self.related], depends_context
  460. if not self.compute:
  461. return (), self._depends_context or ()
  462. # determine the functions implementing self.compute
  463. if isinstance(self.compute, str):
  464. funcs = resolve_mro(model, self.compute, callable)
  465. else:
  466. funcs = [self.compute]
  467. # collect depends and depends_context
  468. depends = []
  469. depends_context = list(self._depends_context or ())
  470. for func in funcs:
  471. deps = getattr(func, '_depends', ())
  472. depends.extend(deps(model) if callable(deps) else deps)
  473. depends_context.extend(getattr(func, '_depends_context', ()))
  474. # display_name may depend on context['lang'] (`test_lp1071710`)
  475. if self.automatic and self.name == 'display_name' and model._rec_name:
  476. if model._fields[model._rec_name].base_field.translate:
  477. if 'lang' not in depends_context:
  478. depends_context.append('lang')
  479. return depends, depends_context
  480. #
  481. # Setup of related fields
  482. #
  483. def setup_related(self, model):
  484. """ Setup the attributes of a related field. """
  485. assert isinstance(self.related, str), self.related
  486. # determine the chain of fields, and make sure they are all set up
  487. model_name = self.model_name
  488. for name in self.related.split('.'):
  489. field = model.pool[model_name]._fields.get(name)
  490. if field is None:
  491. raise KeyError(
  492. f"Field {name} referenced in related field definition {self} does not exist."
  493. )
  494. if not field._setup_done:
  495. field.setup(model.env[model_name])
  496. model_name = field.comodel_name
  497. self.related_field = field
  498. # check type consistency
  499. if self.type != field.type:
  500. raise TypeError("Type of related field %s is inconsistent with %s" % (self, field))
  501. # determine dependencies, compute, inverse, and search
  502. self.compute = self._compute_related
  503. if self.inherited or not (self.readonly or field.readonly):
  504. self.inverse = self._inverse_related
  505. if field._description_searchable:
  506. # allow searching on self only if the related field is searchable
  507. self.search = self._search_related
  508. # A readonly related field without an inverse method should not have a
  509. # default value, as it does not make sense.
  510. if self.default and self.readonly and not self.inverse:
  511. _logger.warning("Redundant default on %s", self)
  512. # copy attributes from field to self (string, help, etc.)
  513. for attr, prop in self.related_attrs:
  514. # check whether 'attr' is explicitly set on self (from its field
  515. # definition), and ignore its class-level value (only a default)
  516. if attr not in self.__dict__ and prop.startswith('_related_'):
  517. setattr(self, attr, getattr(field, prop))
  518. for attr in field._extra_keys:
  519. if not hasattr(self, attr) and model._valid_field_parameter(self, attr):
  520. setattr(self, attr, getattr(field, attr))
  521. # special cases of inherited fields
  522. if self.inherited:
  523. self.inherited_field = field
  524. if not self.states:
  525. self.states = field.states
  526. if field.required:
  527. self.required = True
  528. # add modules from delegate and target fields; the first one ensures
  529. # that inherited fields introduced via an abstract model (_inherits
  530. # being on the abstract model) are assigned an XML id
  531. delegate_field = model._fields[self.related.split('.')[0]]
  532. self._modules = tuple({*self._modules, *delegate_field._modules, *field._modules})
  533. def traverse_related(self, record):
  534. """ Traverse the fields of the related field `self` except for the last
  535. one, and return it as a pair `(last_record, last_field)`. """
  536. for name in self.related.split('.')[:-1]:
  537. record = first(record[name])
  538. return record, self.related_field
  539. def _compute_related(self, records):
  540. """ Compute the related field ``self`` on ``records``. """
  541. #
  542. # Traverse fields one by one for all records, in order to take advantage
  543. # of prefetching for each field access. In order to clarify the impact
  544. # of the algorithm, consider traversing 'foo.bar' for records a1 and a2,
  545. # where 'foo' is already present in cache for a1, a2. Initially, both a1
  546. # and a2 are marked for prefetching. As the commented code below shows,
  547. # traversing all fields one record at a time will fetch 'bar' one record
  548. # at a time.
  549. #
  550. # b1 = a1.foo # mark b1 for prefetching
  551. # v1 = b1.bar # fetch/compute bar for b1
  552. # b2 = a2.foo # mark b2 for prefetching
  553. # v2 = b2.bar # fetch/compute bar for b2
  554. #
  555. # On the other hand, traversing all records one field at a time ensures
  556. # maximal prefetching for each field access.
  557. #
  558. # b1 = a1.foo # mark b1 for prefetching
  559. # b2 = a2.foo # mark b2 for prefetching
  560. # v1 = b1.bar # fetch/compute bar for b1, b2
  561. # v2 = b2.bar # value already in cache
  562. #
  563. # This difference has a major impact on performance, in particular in
  564. # the case where 'bar' is a computed field that takes advantage of batch
  565. # computation.
  566. #
  567. values = list(records)
  568. for name in self.related.split('.')[:-1]:
  569. try:
  570. values = [first(value[name]) for value in values]
  571. except AccessError as e:
  572. description = records.env['ir.model']._get(records._name).name
  573. raise AccessError(
  574. _("%(previous_message)s\n\nImplicitly accessed through '%(document_kind)s' (%(document_model)s).") % {
  575. 'previous_message': e.args[0],
  576. 'document_kind': description,
  577. 'document_model': records._name,
  578. }
  579. )
  580. # assign final values to records
  581. for record, value in zip(records, values):
  582. record[self.name] = self._process_related(value[self.related_field.name])
  583. def _process_related(self, value):
  584. """No transformation by default, but allows override."""
  585. return value
  586. def _inverse_related(self, records):
  587. """ Inverse the related field ``self`` on ``records``. """
  588. # store record values, otherwise they may be lost by cache invalidation!
  589. record_value = {record: record[self.name] for record in records}
  590. for record in records:
  591. target, field = self.traverse_related(record)
  592. # update 'target' only if 'record' and 'target' are both real or
  593. # both new (see `test_base_objects.py`, `test_basic`)
  594. if target and bool(target.id) == bool(record.id):
  595. target[field.name] = record_value[record]
  596. def _search_related(self, records, operator, value):
  597. """ Determine the domain to search on field ``self``. """
  598. return [(self.related, operator, value)]
  599. # properties used by setup_related() to copy values from related field
  600. _related_comodel_name = property(attrgetter('comodel_name'))
  601. _related_string = property(attrgetter('string'))
  602. _related_help = property(attrgetter('help'))
  603. _related_groups = property(attrgetter('groups'))
  604. _related_group_operator = property(attrgetter('group_operator'))
  605. @property
  606. def base_field(self):
  607. """ Return the base field of an inherited field, or ``self``. """
  608. return self.inherited_field.base_field if self.inherited_field else self
  609. @property
  610. def groupable(self):
  611. """
  612. Return whether the field may be used for grouping in :meth:`~odoo.models.BaseModel.read_group`.
  613. """
  614. return self.store and self.column_type
  615. #
  616. # Company-dependent fields
  617. #
  618. def _default_company_dependent(self, model):
  619. return model.env['ir.property']._get(self.name, self.model_name)
  620. def _compute_company_dependent(self, records):
  621. # read property as superuser, as the current user may not have access
  622. Property = records.env['ir.property'].sudo()
  623. values = Property._get_multi(self.name, self.model_name, records.ids)
  624. for record in records:
  625. record[self.name] = values.get(record.id)
  626. def _inverse_company_dependent(self, records):
  627. # update property as superuser, as the current user may not have access
  628. Property = records.env['ir.property'].sudo()
  629. values = {
  630. record.id: self.convert_to_write(record[self.name], record)
  631. for record in records
  632. }
  633. Property._set_multi(self.name, self.model_name, values)
  634. def _search_company_dependent(self, records, operator, value):
  635. Property = records.env['ir.property'].sudo()
  636. return Property.search_multi(self.name, self.model_name, operator, value)
  637. #
  638. # Setup of field triggers
  639. #
  640. def resolve_depends(self, registry):
  641. """ Return the dependencies of `self` as a collection of field tuples. """
  642. Model0 = registry[self.model_name]
  643. for dotnames in registry.field_depends[self]:
  644. field_seq = []
  645. model_name = self.model_name
  646. check_precompute = self.precompute
  647. for index, fname in enumerate(dotnames.split('.')):
  648. Model = registry[model_name]
  649. if Model0._transient and not Model._transient:
  650. # modifying fields on regular models should not trigger
  651. # recomputations of fields on transient models
  652. break
  653. try:
  654. field = Model._fields[fname]
  655. except KeyError:
  656. raise ValueError(
  657. f"Wrong @depends on '{self.compute}' (compute method of field {self}). "
  658. f"Dependency field '{fname}' not found in model {model_name}."
  659. )
  660. if field is self and index and not self.recursive:
  661. self.recursive = True
  662. warnings.warn(f"Field {self} should be declared with recursive=True")
  663. # precomputed fields can depend on non-precomputed ones, as long
  664. # as they are reachable through at least one many2one field
  665. if check_precompute and field.store and field.compute and not field.precompute:
  666. warnings.warn(f"Field {self} cannot be precomputed as it depends on non-precomputed field {field}")
  667. self.precompute = False
  668. if field_seq and not field_seq[-1]._description_searchable:
  669. # the field before this one is not searchable, so there is
  670. # no way to know which on records to recompute self
  671. warnings.warn(
  672. f"Field {field_seq[-1]!r} in dependency of {self} should be searchable. "
  673. f"This is necessary to determine which records to recompute when {field} is modified. "
  674. f"You should either make the field searchable, or simplify the field dependency."
  675. )
  676. field_seq.append(field)
  677. # do not make self trigger itself: for instance, a one2many
  678. # field line_ids with domain [('foo', ...)] will have
  679. # 'line_ids.foo' as a dependency
  680. if not (field is self and not index):
  681. yield tuple(field_seq)
  682. if field.type == 'one2many':
  683. for inv_field in Model.pool.field_inverses[field]:
  684. yield tuple(field_seq) + (inv_field,)
  685. if check_precompute and field.type == 'many2one':
  686. check_precompute = False
  687. model_name = field.comodel_name
  688. ############################################################################
  689. #
  690. # Field description
  691. #
  692. def get_description(self, env, attributes=None):
  693. """ Return a dictionary that describes the field ``self``. """
  694. desc = {}
  695. for attr, prop in self.description_attrs:
  696. if attributes is not None and attr not in attributes:
  697. continue
  698. if not prop.startswith('_description_'):
  699. continue
  700. value = getattr(self, prop)
  701. if callable(value):
  702. value = value(env)
  703. if value is not None:
  704. desc[attr] = value
  705. return desc
  706. # properties used by get_description()
  707. _description_name = property(attrgetter('name'))
  708. _description_type = property(attrgetter('type'))
  709. _description_store = property(attrgetter('store'))
  710. _description_manual = property(attrgetter('manual'))
  711. _description_related = property(attrgetter('related'))
  712. _description_company_dependent = property(attrgetter('company_dependent'))
  713. _description_readonly = property(attrgetter('readonly'))
  714. _description_required = property(attrgetter('required'))
  715. _description_states = property(attrgetter('states'))
  716. _description_groups = property(attrgetter('groups'))
  717. _description_change_default = property(attrgetter('change_default'))
  718. _description_group_operator = property(attrgetter('group_operator'))
  719. _description_default_export_compatible = property(attrgetter('default_export_compatible'))
  720. _description_exportable = property(attrgetter('exportable'))
  721. def _description_depends(self, env):
  722. return env.registry.field_depends[self]
  723. @property
  724. def _description_searchable(self):
  725. return bool(self.store or self.search)
  726. @property
  727. def _description_sortable(self):
  728. return (self.column_type and self.store) or (self.inherited and self.related_field._description_sortable)
  729. def _description_string(self, env):
  730. if self.string and env.lang:
  731. model_name = self.base_field.model_name
  732. field_string = env['ir.model.fields'].get_field_string(model_name)
  733. return field_string.get(self.name) or self.string
  734. return self.string
  735. def _description_help(self, env):
  736. if self.help and env.lang:
  737. model_name = self.base_field.model_name
  738. field_help = env['ir.model.fields'].get_field_help(model_name)
  739. return field_help.get(self.name) or self.help
  740. return self.help
  741. def is_editable(self):
  742. """ Return whether the field can be editable in a view. """
  743. return not self.readonly or self.states and any(
  744. 'readonly' in item for items in self.states.values() for item in items
  745. )
  746. ############################################################################
  747. #
  748. # Conversion of values
  749. #
  750. def null(self, record):
  751. """ Return the null value for this field in the record format. """
  752. return False
  753. def convert_to_column(self, value, record, values=None, validate=True):
  754. """ Convert ``value`` from the ``write`` format to the SQL format. """
  755. if value is None or value is False:
  756. return None
  757. return pycompat.to_text(value)
  758. def convert_to_cache(self, value, record, validate=True):
  759. """ Convert ``value`` to the cache format; ``value`` may come from an
  760. assignment, or have the format of methods :meth:`BaseModel.read` or
  761. :meth:`BaseModel.write`. If the value represents a recordset, it should
  762. be added for prefetching on ``record``.
  763. :param value:
  764. :param record:
  765. :param bool validate: when True, field-specific validation of ``value``
  766. will be performed
  767. """
  768. return value
  769. def convert_to_record(self, value, record):
  770. """ Convert ``value`` from the cache format to the record format.
  771. If the value represents a recordset, it should share the prefetching of
  772. ``record``.
  773. """
  774. return False if value is None else value
  775. def convert_to_record_multi(self, values, records):
  776. """ Convert a list of values from the cache format to the record format.
  777. Some field classes may override this method to add optimizations for
  778. batch processing.
  779. """
  780. # spare the method lookup overhead
  781. convert = self.convert_to_record
  782. return [convert(value, record) for value, record in zip(values, records)]
  783. def convert_to_read(self, value, record, use_name_get=True):
  784. """ Convert ``value`` from the record format to the format returned by
  785. method :meth:`BaseModel.read`.
  786. :param value:
  787. :param record:
  788. :param bool use_name_get: when True, the value's display name will be
  789. computed using :meth:`BaseModel.name_get`, if relevant for the field
  790. """
  791. return False if value is None else value
  792. def convert_to_write(self, value, record):
  793. """ Convert ``value`` from any format to the format of method
  794. :meth:`BaseModel.write`.
  795. """
  796. cache_value = self.convert_to_cache(value, record, validate=False)
  797. record_value = self.convert_to_record(cache_value, record)
  798. return self.convert_to_read(record_value, record)
  799. def convert_to_onchange(self, value, record, names):
  800. """ Convert ``value`` from the record format to the format returned by
  801. method :meth:`BaseModel.onchange`.
  802. :param value:
  803. :param record:
  804. :param names: a tree of field names (for relational fields only)
  805. """
  806. return self.convert_to_read(value, record)
  807. def convert_to_export(self, value, record):
  808. """ Convert ``value`` from the record format to the export format. """
  809. if not value:
  810. return ''
  811. return value
  812. def convert_to_display_name(self, value, record):
  813. """ Convert ``value`` from the record format to a suitable display name. """
  814. return ustr(value) if value else False
  815. ############################################################################
  816. #
  817. # Update database schema
  818. #
  819. @property
  820. def column_order(self):
  821. """ Prescribed column order in table. """
  822. return 0 if self.column_type is None else SQL_ORDER_BY_TYPE[self.column_type[0]]
  823. def update_db(self, model, columns):
  824. """ Update the database schema to implement this field.
  825. :param model: an instance of the field's model
  826. :param columns: a dict mapping column names to their configuration in database
  827. :return: ``True`` if the field must be recomputed on existing rows
  828. """
  829. if not self.column_type:
  830. return
  831. column = columns.get(self.name)
  832. # create/update the column, not null constraint; the index will be
  833. # managed by registry.check_indexes()
  834. self.update_db_column(model, column)
  835. self.update_db_notnull(model, column)
  836. # optimization for computing simple related fields like 'foo_id.bar'
  837. if (
  838. not column
  839. and self.related and self.related.count('.') == 1
  840. and self.related_field.store and not self.related_field.compute
  841. and not (self.related_field.type == 'binary' and self.related_field.attachment)
  842. and self.related_field.type not in ('one2many', 'many2many')
  843. ):
  844. join_field = model._fields[self.related.split('.')[0]]
  845. if (
  846. join_field.type == 'many2one'
  847. and join_field.store and not join_field.compute
  848. ):
  849. model.pool.post_init(self.update_db_related, model)
  850. # discard the "classical" computation
  851. return False
  852. return not column
  853. def update_db_column(self, model, column):
  854. """ Create/update the column corresponding to ``self``.
  855. :param model: an instance of the field's model
  856. :param column: the column's configuration (dict) if it exists, or ``None``
  857. """
  858. if not column:
  859. # the column does not exist, create it
  860. sql.create_column(model._cr, model._table, self.name, self.column_type[1], self.string)
  861. return
  862. if column['udt_name'] == self.column_type[0]:
  863. return
  864. if column['is_nullable'] == 'NO':
  865. sql.drop_not_null(model._cr, model._table, self.name)
  866. self._convert_db_column(model, column)
  867. def _convert_db_column(self, model, column):
  868. """ Convert the given database column to the type of the field. """
  869. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  870. def update_db_notnull(self, model, column):
  871. """ Add or remove the NOT NULL constraint on ``self``.
  872. :param model: an instance of the field's model
  873. :param column: the column's configuration (dict) if it exists, or ``None``
  874. """
  875. has_notnull = column and column['is_nullable'] == 'NO'
  876. if not column or (self.required and not has_notnull):
  877. # the column is new or it becomes required; initialize its values
  878. if model._table_has_rows():
  879. model._init_column(self.name)
  880. if self.required and not has_notnull:
  881. # _init_column may delay computations in post-init phase
  882. @model.pool.post_init
  883. def add_not_null():
  884. # flush values before adding NOT NULL constraint
  885. model.flush_model([self.name])
  886. model.pool.post_constraint(apply_required, model, self.name)
  887. elif not self.required and has_notnull:
  888. sql.drop_not_null(model._cr, model._table, self.name)
  889. def update_db_related(self, model):
  890. """ Compute a stored related field directly in SQL. """
  891. comodel = model.env[self.related_field.model_name]
  892. join_field, comodel_field = self.related.split('.')
  893. model.env.cr.execute("""
  894. UPDATE "{model_table}" AS x
  895. SET "{model_field}" = y."{comodel_field}"
  896. FROM "{comodel_table}" AS y
  897. WHERE x."{join_field}" = y.id
  898. """.format(
  899. model_table=model._table,
  900. model_field=self.name,
  901. comodel_table=comodel._table,
  902. comodel_field=comodel_field,
  903. join_field=join_field,
  904. ))
  905. ############################################################################
  906. #
  907. # Alternatively stored fields: if fields don't have a `column_type` (not
  908. # stored as regular db columns) they go through a read/create/write
  909. # protocol instead
  910. #
  911. def read(self, records):
  912. """ Read the value of ``self`` on ``records``, and store it in cache. """
  913. if not self.column_type:
  914. raise NotImplementedError("Method read() undefined on %s" % self)
  915. def create(self, record_values):
  916. """ Write the value of ``self`` on the given records, which have just
  917. been created.
  918. :param record_values: a list of pairs ``(record, value)``, where
  919. ``value`` is in the format of method :meth:`BaseModel.write`
  920. """
  921. for record, value in record_values:
  922. self.write(record, value)
  923. def write(self, records, value):
  924. """ Write the value of ``self`` on ``records``. This method must update
  925. the cache and prepare database updates.
  926. :param records:
  927. :param value: a value in any format
  928. :return: the subset of `records` that have been modified
  929. """
  930. # discard recomputation of self on records
  931. records.env.remove_to_compute(self, records)
  932. # discard the records that are not modified
  933. cache = records.env.cache
  934. cache_value = self.convert_to_cache(value, records)
  935. records = cache.get_records_different_from(records, self, cache_value)
  936. if not records:
  937. return records
  938. # update the cache
  939. dirty = self.store and any(records._ids)
  940. cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  941. return records
  942. ############################################################################
  943. #
  944. # Descriptor methods
  945. #
  946. def __get__(self, record, owner):
  947. """ return the value of field ``self`` on ``record`` """
  948. if record is None:
  949. return self # the field is accessed through the owner class
  950. if not record._ids:
  951. # null record -> return the null value for this field
  952. value = self.convert_to_cache(False, record, validate=False)
  953. return self.convert_to_record(value, record)
  954. env = record.env
  955. # only a single record may be accessed
  956. record.ensure_one()
  957. if self.compute and self.store:
  958. # process pending computations
  959. self.recompute(record)
  960. try:
  961. value = env.cache.get(record, self)
  962. except KeyError:
  963. # behavior in case of cache miss:
  964. #
  965. # on a real record:
  966. # stored -> fetch from database (computation done above)
  967. # not stored and computed -> compute
  968. # not stored and not computed -> default
  969. #
  970. # on a new record w/ origin:
  971. # stored and not (computed and readonly) -> fetch from origin
  972. # stored and computed and readonly -> compute
  973. # not stored and computed -> compute
  974. # not stored and not computed -> default
  975. #
  976. # on a new record w/o origin:
  977. # stored and computed -> compute
  978. # stored and not computed -> new delegate or default
  979. # not stored and computed -> compute
  980. # not stored and not computed -> default
  981. #
  982. if self.store and record.id:
  983. # real record: fetch from database
  984. recs = record._in_cache_without(self)
  985. try:
  986. recs._fetch_field(self)
  987. except AccessError:
  988. record._fetch_field(self)
  989. if not env.cache.contains(record, self):
  990. raise MissingError("\n".join([
  991. _("Record does not exist or has been deleted."),
  992. _("(Record: %s, User: %s)") % (record, env.uid),
  993. ]))
  994. value = env.cache.get(record, self)
  995. elif self.store and record._origin and not (self.compute and self.readonly):
  996. # new record with origin: fetch from origin
  997. value = self.convert_to_cache(record._origin[self.name], record)
  998. env.cache.set(record, self, value)
  999. elif self.compute:
  1000. # non-stored field or new record without origin: compute
  1001. if env.is_protected(self, record):
  1002. value = self.convert_to_cache(False, record, validate=False)
  1003. env.cache.set(record, self, value)
  1004. else:
  1005. recs = record if self.recursive else record._in_cache_without(self)
  1006. try:
  1007. self.compute_value(recs)
  1008. except (AccessError, MissingError):
  1009. self.compute_value(record)
  1010. try:
  1011. value = env.cache.get(record, self)
  1012. except CacheMiss:
  1013. if self.readonly and not self.store:
  1014. raise ValueError("Compute method failed to assign %s.%s" % (record, self.name))
  1015. # fallback to null value if compute gives nothing
  1016. value = self.convert_to_cache(False, record, validate=False)
  1017. env.cache.set(record, self, value)
  1018. elif self.type == 'many2one' and self.delegate and not record.id:
  1019. # parent record of a new record: new record, with the same
  1020. # values as record for the corresponding inherited fields
  1021. def is_inherited_field(name):
  1022. field = record._fields[name]
  1023. return field.inherited and field.related.split('.')[0] == self.name
  1024. parent = record.env[self.comodel_name].new({
  1025. name: value
  1026. for name, value in record._cache.items()
  1027. if is_inherited_field(name)
  1028. })
  1029. # in case the delegate field has inverse one2many fields, this
  1030. # updates the inverse fields as well
  1031. record._update_cache({self.name: parent}, validate=False)
  1032. value = env.cache.get(record, self)
  1033. else:
  1034. # non-stored field or stored field on new record: default value
  1035. value = self.convert_to_cache(False, record, validate=False)
  1036. env.cache.set(record, self, value)
  1037. defaults = record.default_get([self.name])
  1038. if self.name in defaults:
  1039. # The null value above is necessary to convert x2many field
  1040. # values. For instance, converting [(Command.LINK, id)]
  1041. # accesses the field's current value, then adds the given
  1042. # id. Without an initial value, the conversion ends up here
  1043. # to determine the field's value, and generates an infinite
  1044. # recursion.
  1045. value = self.convert_to_cache(defaults[self.name], record)
  1046. env.cache.set(record, self, value)
  1047. return self.convert_to_record(value, record)
  1048. def mapped(self, records):
  1049. """ Return the values of ``self`` for ``records``, either as a list
  1050. (scalar fields), or as a recordset (relational fields).
  1051. This method is meant to be used internally and has very little benefit
  1052. over a simple call to `~odoo.models.BaseModel.mapped()` on a recordset.
  1053. """
  1054. if self.name == 'id':
  1055. # not stored in cache
  1056. return list(records._ids)
  1057. if self.compute and self.store:
  1058. # process pending computations
  1059. self.recompute(records)
  1060. # retrieve values in cache, and fetch missing ones
  1061. vals = records.env.cache.get_until_miss(records, self)
  1062. while len(vals) < len(records):
  1063. # It is important to construct a 'remaining' recordset with the
  1064. # _prefetch_ids of the original recordset, in order to prefetch as
  1065. # many records as possible. If not done this way, scenarios such as
  1066. # [rec.line_ids.mapped('name') for rec in recs] would generate one
  1067. # query per record in `recs`!
  1068. remaining = records.__class__(records.env, records._ids[len(vals):], records._prefetch_ids)
  1069. self.__get__(first(remaining), type(remaining))
  1070. vals += records.env.cache.get_until_miss(remaining, self)
  1071. return self.convert_to_record_multi(vals, records)
  1072. def __set__(self, records, value):
  1073. """ set the value of field ``self`` on ``records`` """
  1074. protected_ids = []
  1075. new_ids = []
  1076. other_ids = []
  1077. for record_id in records._ids:
  1078. if record_id in records.env._protected.get(self, ()):
  1079. protected_ids.append(record_id)
  1080. elif not record_id:
  1081. new_ids.append(record_id)
  1082. else:
  1083. other_ids.append(record_id)
  1084. if protected_ids:
  1085. # records being computed: no business logic, no recomputation
  1086. protected_records = records.browse(protected_ids)
  1087. self.write(protected_records, value)
  1088. if new_ids:
  1089. # new records: no business logic
  1090. new_records = records.browse(new_ids)
  1091. with records.env.protecting(records.pool.field_computed.get(self, [self]), records):
  1092. if self.relational:
  1093. new_records.modified([self.name], before=True)
  1094. self.write(new_records, value)
  1095. new_records.modified([self.name])
  1096. if self.inherited:
  1097. # special case: also assign parent records if they are new
  1098. parents = records[self.related.split('.')[0]]
  1099. parents.filtered(lambda r: not r.id)[self.name] = value
  1100. if other_ids:
  1101. # base case: full business logic
  1102. records = records.browse(other_ids)
  1103. write_value = self.convert_to_write(value, records)
  1104. records.write({self.name: write_value})
  1105. ############################################################################
  1106. #
  1107. # Computation of field values
  1108. #
  1109. def recompute(self, records):
  1110. """ Process the pending computations of ``self`` on ``records``. This
  1111. should be called only if ``self`` is computed and stored.
  1112. """
  1113. to_compute_ids = records.env.all.tocompute.get(self)
  1114. if not to_compute_ids:
  1115. return
  1116. def apply_except_missing(func, records):
  1117. """ Apply `func` on `records`, with a fallback ignoring non-existent records. """
  1118. try:
  1119. func(records)
  1120. except MissingError:
  1121. existing = records.exists()
  1122. if existing:
  1123. func(existing)
  1124. # mark the field as computed on missing records, otherwise they
  1125. # remain to compute forever, which may lead to an infinite loop
  1126. missing = records - existing
  1127. for f in records.pool.field_computed[self]:
  1128. records.env.remove_to_compute(f, missing)
  1129. if self.recursive:
  1130. # recursive computed fields are computed record by record, in order
  1131. # to recursively handle dependencies inside records
  1132. def recursive_compute(records):
  1133. for record in records:
  1134. if record.id in to_compute_ids:
  1135. self.compute_value(record)
  1136. apply_except_missing(recursive_compute, records)
  1137. return
  1138. for record in records:
  1139. if record.id in to_compute_ids:
  1140. ids = expand_ids(record.id, to_compute_ids)
  1141. recs = record.browse(itertools.islice(ids, PREFETCH_MAX))
  1142. try:
  1143. apply_except_missing(self.compute_value, recs)
  1144. except AccessError:
  1145. self.compute_value(record)
  1146. def compute_value(self, records):
  1147. """ Invoke the compute method on ``records``; the results are in cache. """
  1148. env = records.env
  1149. if self.compute_sudo:
  1150. records = records.sudo()
  1151. fields = records.pool.field_computed[self]
  1152. # Just in case the compute method does not assign a value, we already
  1153. # mark the computation as done. This is also necessary if the compute
  1154. # method accesses the old value of the field: the field will be fetched
  1155. # with _read(), which will flush() it. If the field is still to compute,
  1156. # the latter flush() will recursively compute this field!
  1157. for field in fields:
  1158. if field.store:
  1159. env.remove_to_compute(field, records)
  1160. try:
  1161. with records.env.protecting(fields, records):
  1162. records._compute_field_value(self)
  1163. except Exception:
  1164. for field in fields:
  1165. if field.store:
  1166. env.add_to_compute(field, records)
  1167. raise
  1168. def determine_inverse(self, records):
  1169. """ Given the value of ``self`` on ``records``, inverse the computation. """
  1170. determine(self.inverse, records)
  1171. def determine_domain(self, records, operator, value):
  1172. """ Return a domain representing a condition on ``self``. """
  1173. return determine(self.search, records, operator, value)
  1174. class Boolean(Field):
  1175. """ Encapsulates a :class:`bool`. """
  1176. type = 'boolean'
  1177. column_type = ('bool', 'bool')
  1178. def convert_to_column(self, value, record, values=None, validate=True):
  1179. return bool(value)
  1180. def convert_to_cache(self, value, record, validate=True):
  1181. return bool(value)
  1182. def convert_to_export(self, value, record):
  1183. return value
  1184. class Integer(Field):
  1185. """ Encapsulates an :class:`int`. """
  1186. type = 'integer'
  1187. column_type = ('int4', 'int4')
  1188. group_operator = 'sum'
  1189. def convert_to_column(self, value, record, values=None, validate=True):
  1190. return int(value or 0)
  1191. def convert_to_cache(self, value, record, validate=True):
  1192. if isinstance(value, dict):
  1193. # special case, when an integer field is used as inverse for a one2many
  1194. return value.get('id', None)
  1195. return int(value or 0)
  1196. def convert_to_record(self, value, record):
  1197. return value or 0
  1198. def convert_to_read(self, value, record, use_name_get=True):
  1199. # Integer values greater than 2^31-1 are not supported in pure XMLRPC,
  1200. # so we have to pass them as floats :-(
  1201. if value and value > MAXINT:
  1202. return float(value)
  1203. return value
  1204. def _update(self, records, value):
  1205. # special case, when an integer field is used as inverse for a one2many
  1206. cache = records.env.cache
  1207. for record in records:
  1208. cache.set(record, self, value.id or 0)
  1209. def convert_to_export(self, value, record):
  1210. if value or value == 0:
  1211. return value
  1212. return ''
  1213. class Float(Field):
  1214. """ Encapsulates a :class:`float`.
  1215. The precision digits are given by the (optional) ``digits`` attribute.
  1216. :param digits: a pair (total, decimal) or a string referencing a
  1217. :class:`~odoo.addons.base.models.decimal_precision.DecimalPrecision` record name.
  1218. :type digits: tuple(int,int) or str
  1219. When a float is a quantity associated with an unit of measure, it is important
  1220. to use the right tool to compare or round values with the correct precision.
  1221. The Float class provides some static methods for this purpose:
  1222. :func:`~odoo.fields.Float.round()` to round a float with the given precision.
  1223. :func:`~odoo.fields.Float.is_zero()` to check if a float equals zero at the given precision.
  1224. :func:`~odoo.fields.Float.compare()` to compare two floats at the given precision.
  1225. .. admonition:: Example
  1226. To round a quantity with the precision of the unit of measure::
  1227. fields.Float.round(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)
  1228. To check if the quantity is zero with the precision of the unit of measure::
  1229. fields.Float.is_zero(self.product_uom_qty, precision_rounding=self.product_uom_id.rounding)
  1230. To compare two quantities::
  1231. field.Float.compare(self.product_uom_qty, self.qty_done, precision_rounding=self.product_uom_id.rounding)
  1232. The compare helper uses the __cmp__ semantics for historic purposes, therefore
  1233. the proper, idiomatic way to use this helper is like so:
  1234. if result == 0, the first and second floats are equal
  1235. if result < 0, the first float is lower than the second
  1236. if result > 0, the first float is greater than the second
  1237. """
  1238. type = 'float'
  1239. _digits = None # digits argument passed to class initializer
  1240. group_operator = 'sum'
  1241. def __init__(self, string=Default, digits=Default, **kwargs):
  1242. super(Float, self).__init__(string=string, _digits=digits, **kwargs)
  1243. @property
  1244. def column_type(self):
  1245. # Explicit support for "falsy" digits (0, False) to indicate a NUMERIC
  1246. # field with no fixed precision. The values are saved in the database
  1247. # with all significant digits.
  1248. # FLOAT8 type is still the default when there is no precision because it
  1249. # is faster for most operations (sums, etc.)
  1250. return ('numeric', 'numeric') if self._digits is not None else \
  1251. ('float8', 'double precision')
  1252. def get_digits(self, env):
  1253. if isinstance(self._digits, str):
  1254. precision = env['decimal.precision'].precision_get(self._digits)
  1255. return 16, precision
  1256. else:
  1257. return self._digits
  1258. _related__digits = property(attrgetter('_digits'))
  1259. def _description_digits(self, env):
  1260. return self.get_digits(env)
  1261. def convert_to_column(self, value, record, values=None, validate=True):
  1262. result = float(value or 0.0)
  1263. digits = self.get_digits(record.env)
  1264. if digits:
  1265. precision, scale = digits
  1266. result = float_repr(float_round(result, precision_digits=scale), precision_digits=scale)
  1267. return result
  1268. def convert_to_cache(self, value, record, validate=True):
  1269. # apply rounding here, otherwise value in cache may be wrong!
  1270. value = float(value or 0.0)
  1271. digits = self.get_digits(record.env)
  1272. return float_round(value, precision_digits=digits[1]) if digits else value
  1273. def convert_to_record(self, value, record):
  1274. return value or 0.0
  1275. def convert_to_export(self, value, record):
  1276. if value or value == 0.0:
  1277. return value
  1278. return ''
  1279. round = staticmethod(float_round)
  1280. is_zero = staticmethod(float_is_zero)
  1281. compare = staticmethod(float_compare)
  1282. class Monetary(Field):
  1283. """ Encapsulates a :class:`float` expressed in a given
  1284. :class:`res_currency<odoo.addons.base.models.res_currency.Currency>`.
  1285. The decimal precision and currency symbol are taken from the ``currency_field`` attribute.
  1286. :param str currency_field: name of the :class:`Many2one` field
  1287. holding the :class:`res_currency <odoo.addons.base.models.res_currency.Currency>`
  1288. this monetary field is expressed in (default: `\'currency_id\'`)
  1289. """
  1290. type = 'monetary'
  1291. write_sequence = 10
  1292. column_type = ('numeric', 'numeric')
  1293. currency_field = None
  1294. group_operator = 'sum'
  1295. def __init__(self, string=Default, currency_field=Default, **kwargs):
  1296. super(Monetary, self).__init__(string=string, currency_field=currency_field, **kwargs)
  1297. def _description_currency_field(self, env):
  1298. return self.get_currency_field(env[self.model_name])
  1299. def get_currency_field(self, model):
  1300. """ Return the name of the currency field. """
  1301. return self.currency_field or (
  1302. 'currency_id' if 'currency_id' in model._fields else
  1303. 'x_currency_id' if 'x_currency_id' in model._fields else
  1304. None
  1305. )
  1306. def setup_nonrelated(self, model):
  1307. super().setup_nonrelated(model)
  1308. assert self.get_currency_field(model) in model._fields, \
  1309. "Field %s with unknown currency_field %r" % (self, self.get_currency_field(model))
  1310. def setup_related(self, model):
  1311. super().setup_related(model)
  1312. if self.inherited:
  1313. self.currency_field = self.related_field.get_currency_field(model.env[self.related_field.model_name])
  1314. assert self.get_currency_field(model) in model._fields, \
  1315. "Field %s with unknown currency_field %r" % (self, self.get_currency_field(model))
  1316. def convert_to_column(self, value, record, values=None, validate=True):
  1317. # retrieve currency from values or record
  1318. currency_field_name = self.get_currency_field(record)
  1319. currency_field = record._fields[currency_field_name]
  1320. if values and currency_field_name in values:
  1321. dummy = record.new({currency_field_name: values[currency_field_name]})
  1322. currency = dummy[currency_field_name]
  1323. elif values and currency_field.related and currency_field.related.split('.')[0] in values:
  1324. related_field_name = currency_field.related.split('.')[0]
  1325. dummy = record.new({related_field_name: values[related_field_name]})
  1326. currency = dummy[currency_field_name]
  1327. else:
  1328. # Note: this is wrong if 'record' is several records with different
  1329. # currencies, which is functional nonsense and should not happen
  1330. # BEWARE: do not prefetch other fields, because 'value' may be in
  1331. # cache, and would be overridden by the value read from database!
  1332. currency = record[:1].with_context(prefetch_fields=False)[currency_field_name]
  1333. currency = currency.with_env(record.env)
  1334. value = float(value or 0.0)
  1335. if currency:
  1336. return float_repr(currency.round(value), currency.decimal_places)
  1337. return value
  1338. def convert_to_cache(self, value, record, validate=True):
  1339. # cache format: float
  1340. value = float(value or 0.0)
  1341. if value and validate:
  1342. # FIXME @rco-odoo: currency may not be already initialized if it is
  1343. # a function or related field!
  1344. # BEWARE: do not prefetch other fields, because 'value' may be in
  1345. # cache, and would be overridden by the value read from database!
  1346. currency_field = self.get_currency_field(record)
  1347. currency = record.sudo().with_context(prefetch_fields=False)[currency_field]
  1348. if len(currency) > 1:
  1349. raise ValueError("Got multiple currencies while assigning values of monetary field %s" % str(self))
  1350. elif currency:
  1351. value = currency.with_env(record.env).round(value)
  1352. return value
  1353. def convert_to_record(self, value, record):
  1354. return value or 0.0
  1355. def convert_to_read(self, value, record, use_name_get=True):
  1356. return value
  1357. def convert_to_write(self, value, record):
  1358. return value
  1359. class _String(Field):
  1360. """ Abstract class for string fields. """
  1361. translate = False # whether the field is translated
  1362. unaccent = True
  1363. def __init__(self, string=Default, **kwargs):
  1364. # translate is either True, False, or a callable
  1365. if 'translate' in kwargs and not callable(kwargs['translate']):
  1366. kwargs['translate'] = bool(kwargs['translate'])
  1367. super(_String, self).__init__(string=string, **kwargs)
  1368. _related_translate = property(attrgetter('translate'))
  1369. def _description_translate(self, env):
  1370. return bool(self.translate)
  1371. def _convert_db_column(self, model, column):
  1372. # specialized implementation for converting from/to translated fields
  1373. if self.translate or column['udt_name'] == 'jsonb':
  1374. sql.convert_column_translatable(model._cr, model._table, self.name, self.column_type[1])
  1375. else:
  1376. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  1377. def get_trans_terms(self, value):
  1378. """ Return the sequence of terms to translate found in `value`. """
  1379. if not callable(self.translate):
  1380. return [value] if value else []
  1381. terms = []
  1382. self.translate(terms.append, value)
  1383. return terms
  1384. def get_text_content(self, term):
  1385. """ Return the textual content for the given term. """
  1386. func = getattr(self.translate, 'get_text_content', lambda term: term)
  1387. return func(term)
  1388. def convert_to_column(self, value, record, values=None, validate=True):
  1389. cache_value = self.convert_to_cache(value, record)
  1390. if cache_value is None:
  1391. return None
  1392. if callable(self.translate):
  1393. # pylint: disable=not-callable
  1394. cache_value = self.translate(lambda t: None, cache_value)
  1395. if self.translate:
  1396. cache_value = {'en_US': cache_value, record.env.lang or 'en_US': cache_value}
  1397. return self._convert_from_cache_to_column(cache_value)
  1398. def _convert_from_cache_to_column(self, value):
  1399. """ Convert from cache_raw value to column value """
  1400. if value is None:
  1401. return None
  1402. return PsycopgJson(value) if self.translate else value
  1403. def convert_to_cache(self, value, record, validate=True):
  1404. if value is None or value is False:
  1405. return None
  1406. return value
  1407. def convert_to_record(self, value, record):
  1408. if value is None:
  1409. return False
  1410. if callable(self.translate) and record.env.context.get('edit_translations'):
  1411. terms = self.get_trans_terms(value)
  1412. base_lang = record._get_base_lang()
  1413. if base_lang != (record.env.lang or 'en_US'):
  1414. base_value = record.with_context(edit_translations=None, lang=base_lang)[self.name]
  1415. base_terms = self.get_trans_terms(base_value)
  1416. term_to_state = {term: "translated" if base_term != term else "to_translate" for term, base_term in zip(terms, base_terms)}
  1417. else:
  1418. term_to_state = defaultdict(lambda: 'translated')
  1419. # use a wrapper to let the frontend js code identify each term and its metadata in the 'edit_translations' context
  1420. # pylint: disable=not-callable
  1421. value = self.translate(
  1422. lambda term: f'''<span data-oe-model="{record._name}" data-oe-id="{record.id}" data-oe-field="{self.name}" data-oe-translation-state="{term_to_state[term]}" data-oe-translation-initial-sha="{sha256(term.encode()).hexdigest()}">{term}</span>''',
  1423. value
  1424. )
  1425. return value
  1426. def convert_to_write(self, value, record):
  1427. return value
  1428. def get_translation_dictionary(self, from_lang_value, to_lang_values):
  1429. """ Build a dictionary from terms in from_lang_value to terms in to_lang_values
  1430. :param str from_lang_value: from xml/html
  1431. :param dict to_lang_values: {lang: lang_value}
  1432. :return: {from_lang_term: {lang: lang_term}}
  1433. :rtype: dict
  1434. """
  1435. from_lang_terms = self.get_trans_terms(from_lang_value)
  1436. dictionary = defaultdict(lambda: defaultdict(dict))
  1437. for lang, to_lang_value in to_lang_values.items():
  1438. to_lang_terms = self.get_trans_terms(to_lang_value)
  1439. if len(from_lang_terms) != len(to_lang_terms):
  1440. for from_lang_term in from_lang_terms:
  1441. dictionary[from_lang_term][lang] = from_lang_term
  1442. else:
  1443. for from_lang_term, to_lang_term in zip(from_lang_terms, to_lang_terms):
  1444. dictionary[from_lang_term][lang] = to_lang_term
  1445. return dictionary
  1446. def _get_stored_translations(self, record):
  1447. """
  1448. : return: {'en_US': 'value_en_US', 'fr_FR': 'French'}
  1449. """
  1450. # assert (self.translate and self.store and record)
  1451. record.flush_recordset([self.name])
  1452. cr = record.env.cr
  1453. cr.execute(f'SELECT "{self.name}" FROM "{record._table}" WHERE id = %s', (record.id,))
  1454. res = cr.fetchone()
  1455. return res[0] if res else None
  1456. def write(self, records, value):
  1457. if not self.translate or value is False or value is None:
  1458. return super().write(records, value)
  1459. cache = records.env.cache
  1460. cache_value = self.convert_to_cache(value, records)
  1461. records = cache.get_records_different_from(records, self, cache_value)
  1462. if not records:
  1463. return records
  1464. # flush dirty None values
  1465. dirty_records = records & cache.get_dirty_records(records, self)
  1466. if any(v is None for v in cache.get_values(dirty_records, self)):
  1467. dirty_records.flush_recordset([self.name])
  1468. dirty = self.store and any(records._ids)
  1469. lang = records.env.lang or 'en_US'
  1470. # not dirty fields
  1471. if not dirty:
  1472. cache.update_raw(records, self, [{lang: cache_value} for _id in records._ids], dirty=False)
  1473. return records
  1474. # model translation
  1475. if not callable(self.translate):
  1476. # invalidate clean fields because them may contain fallback value
  1477. clean_records = records - cache.get_dirty_records(records, self)
  1478. clean_records.invalidate_recordset([self.name])
  1479. cache.update(records, self, itertools.repeat(cache_value), dirty=True)
  1480. if lang != 'en_US' and not records.env['res.lang']._lang_get_id('en_US'):
  1481. # if 'en_US' is not active, we always write en_US to make sure value_en is meaningful
  1482. cache.update(records.with_context(lang='en_US'), self, itertools.repeat(cache_value), dirty=True)
  1483. return records
  1484. # model term translation
  1485. new_translations_list = []
  1486. # pylint: disable=not-callable
  1487. cache_value = self.translate(lambda t: None, cache_value)
  1488. new_terms = set(self.get_trans_terms(cache_value))
  1489. for record in records:
  1490. # shortcut when no term needs to be translated
  1491. if not new_terms:
  1492. new_translations_list.append({'en_US': cache_value, lang: cache_value})
  1493. continue
  1494. # _get_stored_translations can be refactored and prefetches translations for multi records,
  1495. # but it is really rare to write the same non-False/None/no-term value to multi records
  1496. old_translations = self._get_stored_translations(record)
  1497. if not old_translations:
  1498. new_translations_list.append({'en_US': cache_value, lang: cache_value})
  1499. continue
  1500. from_lang_value = old_translations.get(lang, old_translations['en_US'])
  1501. translation_dictionary = self.get_translation_dictionary(from_lang_value, old_translations)
  1502. text2terms = defaultdict(list)
  1503. for term in new_terms:
  1504. text2terms[self.get_text_content(term)].append(term)
  1505. for old_term in list(translation_dictionary.keys()):
  1506. if old_term not in new_terms:
  1507. old_term_text = self.get_text_content(old_term)
  1508. matches = get_close_matches(old_term_text, text2terms, 1, 0.9)
  1509. if matches:
  1510. closest_term = get_close_matches(old_term, text2terms[matches[0]], 1, 0)[0]
  1511. old_is_text = old_term == self.get_text_content(old_term)
  1512. closest_is_text = closest_term == self.get_text_content(closest_term)
  1513. if old_is_text or not closest_is_text:
  1514. translation_dictionary[closest_term] = translation_dictionary.pop(old_term)
  1515. # pylint: disable=not-callable
  1516. new_translations = {
  1517. l: self.translate(lambda term: translation_dictionary.get(term, {l: None})[l], cache_value)
  1518. for l in old_translations.keys()
  1519. }
  1520. new_translations[lang] = cache_value
  1521. if not records.env['res.lang']._lang_get_id('en_US'):
  1522. new_translations['en_US'] = cache_value
  1523. new_translations_list.append(new_translations)
  1524. # Maybe we can use Cache.update(records.with_context(cache_update_raw=True), self, new_translations_list, dirty=True)
  1525. cache.update_raw(records, self, new_translations_list, dirty=True)
  1526. return records
  1527. class Char(_String):
  1528. """ Basic string field, can be length-limited, usually displayed as a
  1529. single-line string in clients.
  1530. :param int size: the maximum size of values stored for that field
  1531. :param bool trim: states whether the value is trimmed or not (by default,
  1532. ``True``). Note that the trim operation is applied only by the web client.
  1533. :param translate: enable the translation of the field's values; use
  1534. ``translate=True`` to translate field values as a whole; ``translate``
  1535. may also be a callable such that ``translate(callback, value)``
  1536. translates ``value`` by using ``callback(term)`` to retrieve the
  1537. translation of terms.
  1538. :type translate: bool or callable
  1539. """
  1540. type = 'char'
  1541. size = None # maximum size of values (deprecated)
  1542. trim = True # whether value is trimmed (only by web client)
  1543. def _setup_attrs(self, model_class, name):
  1544. super()._setup_attrs(model_class, name)
  1545. assert self.size is None or isinstance(self.size, int), \
  1546. "Char field %s with non-integer size %r" % (self, self.size)
  1547. @property
  1548. def column_type(self):
  1549. return ('jsonb', 'jsonb') if self.translate else ('varchar', pg_varchar(self.size))
  1550. def update_db_column(self, model, column):
  1551. if (
  1552. column and self.column_type[0] == 'varchar' and
  1553. column['udt_name'] == 'varchar' and column['character_maximum_length'] and
  1554. (self.size is None or column['character_maximum_length'] < self.size)
  1555. ):
  1556. # the column's varchar size does not match self.size; convert it
  1557. sql.convert_column(model._cr, model._table, self.name, self.column_type[1])
  1558. super().update_db_column(model, column)
  1559. _related_size = property(attrgetter('size'))
  1560. _related_trim = property(attrgetter('trim'))
  1561. _description_size = property(attrgetter('size'))
  1562. _description_trim = property(attrgetter('trim'))
  1563. def convert_to_column(self, value, record, values=None, validate=True):
  1564. if value is None or value is False:
  1565. return None
  1566. # we need to convert the string to a unicode object to be able
  1567. # to evaluate its length (and possibly truncate it) reliably
  1568. return super().convert_to_column(pycompat.to_text(value)[:self.size], record, values, validate)
  1569. def convert_to_cache(self, value, record, validate=True):
  1570. if value is None or value is False:
  1571. return None
  1572. return pycompat.to_text(value)[:self.size]
  1573. class Text(_String):
  1574. """ Very similar to :class:`Char` but used for longer contents, does not
  1575. have a size and usually displayed as a multiline text box.
  1576. :param translate: enable the translation of the field's values; use
  1577. ``translate=True`` to translate field values as a whole; ``translate``
  1578. may also be a callable such that ``translate(callback, value)``
  1579. translates ``value`` by using ``callback(term)`` to retrieve the
  1580. translation of terms.
  1581. :type translate: bool or callable
  1582. """
  1583. type = 'text'
  1584. @property
  1585. def column_type(self):
  1586. return ('jsonb', 'jsonb') if self.translate else ('text', 'text')
  1587. def convert_to_cache(self, value, record, validate=True):
  1588. if value is None or value is False:
  1589. return None
  1590. return ustr(value)
  1591. class Html(_String):
  1592. """ Encapsulates an html code content.
  1593. :param bool sanitize: whether value must be sanitized (default: ``True``)
  1594. :param bool sanitize_overridable: whether the sanitation can be bypassed by
  1595. the users part of the `base.group_sanitize_override` group (default: ``False``)
  1596. :param bool sanitize_tags: whether to sanitize tags
  1597. (only a white list of attributes is accepted, default: ``True``)
  1598. :param bool sanitize_attributes: whether to sanitize attributes
  1599. (only a white list of attributes is accepted, default: ``True``)
  1600. :param bool sanitize_style: whether to sanitize style attributes (default: ``False``)
  1601. :param bool strip_style: whether to strip style attributes
  1602. (removed and therefore not sanitized, default: ``False``)
  1603. :param bool strip_classes: whether to strip classes attributes (default: ``False``)
  1604. """
  1605. type = 'html'
  1606. sanitize = True # whether value must be sanitized
  1607. sanitize_overridable = False # whether the sanitation can be bypassed by the users part of the `base.group_sanitize_override` group
  1608. sanitize_tags = True # whether to sanitize tags (only a white list of attributes is accepted)
  1609. sanitize_attributes = True # whether to sanitize attributes (only a white list of attributes is accepted)
  1610. sanitize_style = False # whether to sanitize style attributes
  1611. sanitize_form = True # whether to sanitize forms
  1612. strip_style = False # whether to strip style attributes (removed and therefore not sanitized)
  1613. strip_classes = False # whether to strip classes attributes
  1614. def _get_attrs(self, model_class, name):
  1615. # called by _setup_attrs(), working together with _String._setup_attrs()
  1616. attrs = super()._get_attrs(model_class, name)
  1617. # Translated sanitized html fields must use html_translate or a callable.
  1618. if attrs.get('translate') is True and attrs.get('sanitize', True):
  1619. attrs['translate'] = html_translate
  1620. return attrs
  1621. @property
  1622. def column_type(self):
  1623. return ('jsonb', 'jsonb') if self.translate else ('text', 'text')
  1624. _related_sanitize = property(attrgetter('sanitize'))
  1625. _related_sanitize_tags = property(attrgetter('sanitize_tags'))
  1626. _related_sanitize_attributes = property(attrgetter('sanitize_attributes'))
  1627. _related_sanitize_style = property(attrgetter('sanitize_style'))
  1628. _related_strip_style = property(attrgetter('strip_style'))
  1629. _related_strip_classes = property(attrgetter('strip_classes'))
  1630. _description_sanitize = property(attrgetter('sanitize'))
  1631. _description_sanitize_tags = property(attrgetter('sanitize_tags'))
  1632. _description_sanitize_attributes = property(attrgetter('sanitize_attributes'))
  1633. _description_sanitize_style = property(attrgetter('sanitize_style'))
  1634. _description_strip_style = property(attrgetter('strip_style'))
  1635. _description_strip_classes = property(attrgetter('strip_classes'))
  1636. def convert_to_column(self, value, record, values=None, validate=True):
  1637. return super().convert_to_column(self._convert(value, record, True), record, values, validate)
  1638. def convert_to_cache(self, value, record, validate=True):
  1639. return self._convert(value, record, validate)
  1640. def _convert(self, value, record, validate):
  1641. if value is None or value is False:
  1642. return None
  1643. if not validate or not self.sanitize:
  1644. return value
  1645. sanitize_vals = {
  1646. 'silent': True,
  1647. 'sanitize_tags': self.sanitize_tags,
  1648. 'sanitize_attributes': self.sanitize_attributes,
  1649. 'sanitize_style': self.sanitize_style,
  1650. 'sanitize_form': self.sanitize_form,
  1651. 'strip_style': self.strip_style,
  1652. 'strip_classes': self.strip_classes
  1653. }
  1654. if self.sanitize_overridable:
  1655. if record.user_has_groups('base.group_sanitize_override'):
  1656. return value
  1657. original_value = record[self.name]
  1658. if original_value:
  1659. # Note that sanitize also normalize
  1660. original_value_sanitized = html_sanitize(original_value, **sanitize_vals)
  1661. original_value_normalized = html_normalize(original_value)
  1662. if (
  1663. not original_value_sanitized # sanitizer could empty it
  1664. or original_value_normalized != original_value_sanitized
  1665. ):
  1666. # The field contains element(s) that would be removed if
  1667. # sanitized. It means that someone who was part of a group
  1668. # allowing to bypass the sanitation saved that field
  1669. # previously.
  1670. raise UserError(_(
  1671. "The field value you're saving (%s %s) includes content that is "
  1672. "restricted for security reasons. It is possible that someone "
  1673. "with higher privileges previously modified it, and you are therefore "
  1674. "not able to modify it yourself while preserving the content.",
  1675. record._description, self.string,
  1676. ))
  1677. return html_sanitize(value, **sanitize_vals)
  1678. def convert_to_record(self, value, record):
  1679. r = super().convert_to_record(value, record)
  1680. if isinstance(r, bytes):
  1681. r = r.decode()
  1682. return r and Markup(r)
  1683. def convert_to_read(self, value, record, use_name_get=True):
  1684. r = super().convert_to_read(value, record, use_name_get)
  1685. if isinstance(r, bytes):
  1686. r = r.decode()
  1687. return r and Markup(r)
  1688. def get_trans_terms(self, value):
  1689. # ensure the translation terms are stringified, otherwise we can break the PO file
  1690. return list(map(str, super().get_trans_terms(value)))
  1691. class Date(Field):
  1692. """ Encapsulates a python :class:`date <datetime.date>` object. """
  1693. type = 'date'
  1694. column_type = ('date', 'date')
  1695. start_of = staticmethod(date_utils.start_of)
  1696. end_of = staticmethod(date_utils.end_of)
  1697. add = staticmethod(date_utils.add)
  1698. subtract = staticmethod(date_utils.subtract)
  1699. @staticmethod
  1700. def today(*args):
  1701. """Return the current day in the format expected by the ORM.
  1702. .. note:: This function may be used to compute default values.
  1703. """
  1704. return date.today()
  1705. @staticmethod
  1706. def context_today(record, timestamp=None):
  1707. """Return the current date as seen in the client's timezone in a format
  1708. fit for date fields.
  1709. .. note:: This method may be used to compute default values.
  1710. :param record: recordset from which the timezone will be obtained.
  1711. :param datetime timestamp: optional datetime value to use instead of
  1712. the current date and time (must be a datetime, regular dates
  1713. can't be converted between timezones).
  1714. :rtype: date
  1715. """
  1716. today = timestamp or datetime.now()
  1717. context_today = None
  1718. tz_name = record._context.get('tz') or record.env.user.tz
  1719. if tz_name:
  1720. try:
  1721. today_utc = pytz.timezone('UTC').localize(today, is_dst=False) # UTC = no DST
  1722. context_today = today_utc.astimezone(pytz.timezone(tz_name))
  1723. except Exception:
  1724. _logger.debug("failed to compute context/client-specific today date, using UTC value for `today`",
  1725. exc_info=True)
  1726. return (context_today or today).date()
  1727. @staticmethod
  1728. def to_date(value):
  1729. """Attempt to convert ``value`` to a :class:`date` object.
  1730. .. warning::
  1731. If a datetime object is given as value,
  1732. it will be converted to a date object and all
  1733. datetime-specific information will be lost (HMS, TZ, ...).
  1734. :param value: value to convert.
  1735. :type value: str or date or datetime
  1736. :return: an object representing ``value``.
  1737. :rtype: date or None
  1738. """
  1739. if not value:
  1740. return None
  1741. if isinstance(value, date):
  1742. if isinstance(value, datetime):
  1743. return value.date()
  1744. return value
  1745. value = value[:DATE_LENGTH]
  1746. return datetime.strptime(value, DATE_FORMAT).date()
  1747. # kept for backwards compatibility, but consider `from_string` as deprecated, will probably
  1748. # be removed after V12
  1749. from_string = to_date
  1750. @staticmethod
  1751. def to_string(value):
  1752. """
  1753. Convert a :class:`date` or :class:`datetime` object to a string.
  1754. :param value: value to convert.
  1755. :return: a string representing ``value`` in the server's date format, if ``value`` is of
  1756. type :class:`datetime`, the hours, minute, seconds, tzinfo will be truncated.
  1757. :rtype: str
  1758. """
  1759. return value.strftime(DATE_FORMAT) if value else False
  1760. def convert_to_cache(self, value, record, validate=True):
  1761. if not value:
  1762. return None
  1763. if isinstance(value, datetime):
  1764. # TODO: better fix data files (crm demo data)
  1765. value = value.date()
  1766. # raise TypeError("%s (field %s) must be string or date, not datetime." % (value, self))
  1767. return self.to_date(value)
  1768. def convert_to_export(self, value, record):
  1769. if not value:
  1770. return ''
  1771. return self.from_string(value)
  1772. class Datetime(Field):
  1773. """ Encapsulates a python :class:`datetime <datetime.datetime>` object. """
  1774. type = 'datetime'
  1775. column_type = ('timestamp', 'timestamp')
  1776. start_of = staticmethod(date_utils.start_of)
  1777. end_of = staticmethod(date_utils.end_of)
  1778. add = staticmethod(date_utils.add)
  1779. subtract = staticmethod(date_utils.subtract)
  1780. @staticmethod
  1781. def now(*args):
  1782. """Return the current day and time in the format expected by the ORM.
  1783. .. note:: This function may be used to compute default values.
  1784. """
  1785. # microseconds must be annihilated as they don't comply with the server datetime format
  1786. return datetime.now().replace(microsecond=0)
  1787. @staticmethod
  1788. def today(*args):
  1789. """Return the current day, at midnight (00:00:00)."""
  1790. return Datetime.now().replace(hour=0, minute=0, second=0)
  1791. @staticmethod
  1792. def context_timestamp(record, timestamp):
  1793. """Return the given timestamp converted to the client's timezone.
  1794. .. note:: This method is *not* meant for use as a default initializer,
  1795. because datetime fields are automatically converted upon
  1796. display on client side. For default values, :meth:`now`
  1797. should be used instead.
  1798. :param record: recordset from which the timezone will be obtained.
  1799. :param datetime timestamp: naive datetime value (expressed in UTC)
  1800. to be converted to the client timezone.
  1801. :return: timestamp converted to timezone-aware datetime in context timezone.
  1802. :rtype: datetime
  1803. """
  1804. assert isinstance(timestamp, datetime), 'Datetime instance expected'
  1805. tz_name = record._context.get('tz') or record.env.user.tz
  1806. utc_timestamp = pytz.utc.localize(timestamp, is_dst=False) # UTC = no DST
  1807. if tz_name:
  1808. try:
  1809. context_tz = pytz.timezone(tz_name)
  1810. return utc_timestamp.astimezone(context_tz)
  1811. except Exception:
  1812. _logger.debug("failed to compute context/client-specific timestamp, "
  1813. "using the UTC value",
  1814. exc_info=True)
  1815. return utc_timestamp
  1816. @staticmethod
  1817. def to_datetime(value):
  1818. """Convert an ORM ``value`` into a :class:`datetime` value.
  1819. :param value: value to convert.
  1820. :type value: str or date or datetime
  1821. :return: an object representing ``value``.
  1822. :rtype: datetime or None
  1823. """
  1824. if not value:
  1825. return None
  1826. if isinstance(value, date):
  1827. if isinstance(value, datetime):
  1828. if value.tzinfo:
  1829. raise ValueError("Datetime field expects a naive datetime: %s" % value)
  1830. return value
  1831. return datetime.combine(value, time.min)
  1832. # TODO: fix data files
  1833. return datetime.strptime(value, DATETIME_FORMAT[:len(value)-2])
  1834. # kept for backwards compatibility, but consider `from_string` as deprecated, will probably
  1835. # be removed after V12
  1836. from_string = to_datetime
  1837. @staticmethod
  1838. def to_string(value):
  1839. """Convert a :class:`datetime` or :class:`date` object to a string.
  1840. :param value: value to convert.
  1841. :type value: datetime or date
  1842. :return: a string representing ``value`` in the server's datetime format,
  1843. if ``value`` is of type :class:`date`,
  1844. the time portion will be midnight (00:00:00).
  1845. :rtype: str
  1846. """
  1847. return value.strftime(DATETIME_FORMAT) if value else False
  1848. def convert_to_cache(self, value, record, validate=True):
  1849. return self.to_datetime(value)
  1850. def convert_to_export(self, value, record):
  1851. if not value:
  1852. return ''
  1853. value = self.convert_to_display_name(value, record)
  1854. return self.from_string(value)
  1855. def convert_to_display_name(self, value, record):
  1856. assert record, 'Record expected'
  1857. return Datetime.to_string(Datetime.context_timestamp(record, Datetime.from_string(value)))
  1858. # http://initd.org/psycopg/docs/usage.html#binary-adaptation
  1859. # Received data is returned as buffer (in Python 2) or memoryview (in Python 3).
  1860. _BINARY = memoryview
  1861. class Binary(Field):
  1862. """Encapsulates a binary content (e.g. a file).
  1863. :param bool attachment: whether the field should be stored as `ir_attachment`
  1864. or in a column of the model's table (default: ``True``).
  1865. """
  1866. type = 'binary'
  1867. prefetch = False # not prefetched by default
  1868. _depends_context = ('bin_size',) # depends on context (content or size)
  1869. attachment = True # whether value is stored in attachment
  1870. @property
  1871. def column_type(self):
  1872. return None if self.attachment else ('bytea', 'bytea')
  1873. def _get_attrs(self, model_class, name):
  1874. attrs = super()._get_attrs(model_class, name)
  1875. if not attrs.get('store', True):
  1876. attrs['attachment'] = False
  1877. return attrs
  1878. _description_attachment = property(attrgetter('attachment'))
  1879. def convert_to_column(self, value, record, values=None, validate=True):
  1880. # Binary values may be byte strings (python 2.6 byte array), but
  1881. # the legacy OpenERP convention is to transfer and store binaries
  1882. # as base64-encoded strings. The base64 string may be provided as a
  1883. # unicode in some circumstances, hence the str() cast here.
  1884. # This str() coercion will only work for pure ASCII unicode strings,
  1885. # on purpose - non base64 data must be passed as a 8bit byte strings.
  1886. if not value:
  1887. return None
  1888. # Detect if the binary content is an SVG for restricting its upload
  1889. # only to system users.
  1890. magic_bytes = {
  1891. b'P', # first 6 bits of '<' (0x3C) b64 encoded
  1892. b'<', # plaintext XML tag opening
  1893. }
  1894. if isinstance(value, str):
  1895. value = value.encode()
  1896. if value[:1] in magic_bytes:
  1897. try:
  1898. decoded_value = base64.b64decode(value.translate(None, delete=b'\r\n'), validate=True)
  1899. except binascii.Error:
  1900. decoded_value = value
  1901. # Full mimetype detection
  1902. if (guess_mimetype(decoded_value).startswith('image/svg') and
  1903. not record.env.is_system()):
  1904. raise UserError(_("Only admins can upload SVG files."))
  1905. if isinstance(value, bytes):
  1906. return psycopg2.Binary(value)
  1907. try:
  1908. return psycopg2.Binary(str(value).encode('ascii'))
  1909. except UnicodeEncodeError:
  1910. raise UserError(_("ASCII characters are required for %s in %s") % (value, self.name))
  1911. def convert_to_cache(self, value, record, validate=True):
  1912. if isinstance(value, _BINARY):
  1913. return bytes(value)
  1914. if isinstance(value, str):
  1915. # the cache must contain bytes or memoryview, but sometimes a string
  1916. # is given when assigning a binary field (test `TestFileSeparator`)
  1917. return value.encode()
  1918. if isinstance(value, int) and \
  1919. (record._context.get('bin_size') or
  1920. record._context.get('bin_size_' + self.name)):
  1921. # If the client requests only the size of the field, we return that
  1922. # instead of the content. Presumably a separate request will be done
  1923. # to read the actual content, if necessary.
  1924. value = human_size(value)
  1925. # human_size can return False (-> None) or a string (-> encoded)
  1926. return value.encode() if value else None
  1927. return None if value is False else value
  1928. def convert_to_record(self, value, record):
  1929. if isinstance(value, _BINARY):
  1930. return bytes(value)
  1931. return False if value is None else value
  1932. def compute_value(self, records):
  1933. bin_size_name = 'bin_size_' + self.name
  1934. if records.env.context.get('bin_size') or records.env.context.get(bin_size_name):
  1935. # always compute without bin_size
  1936. records_no_bin_size = records.with_context(**{'bin_size': False, bin_size_name: False})
  1937. super().compute_value(records_no_bin_size)
  1938. # manually update the bin_size cache
  1939. cache = records.env.cache
  1940. for record_no_bin_size, record in zip(records_no_bin_size, records):
  1941. try:
  1942. value = cache.get(record_no_bin_size, self)
  1943. try:
  1944. value = base64.b64decode(value)
  1945. except (TypeError, binascii.Error):
  1946. pass
  1947. try:
  1948. if isinstance(value, (bytes, _BINARY)):
  1949. value = human_size(len(value))
  1950. except (TypeError):
  1951. pass
  1952. cache_value = self.convert_to_cache(value, record)
  1953. dirty = self.column_type and self.store and any(records._ids)
  1954. cache.set(record, self, cache_value, dirty=dirty)
  1955. except CacheMiss:
  1956. pass
  1957. else:
  1958. super().compute_value(records)
  1959. def read(self, records):
  1960. # values are stored in attachments, retrieve them
  1961. assert self.attachment
  1962. domain = [
  1963. ('res_model', '=', records._name),
  1964. ('res_field', '=', self.name),
  1965. ('res_id', 'in', records.ids),
  1966. ]
  1967. # Note: the 'bin_size' flag is handled by the field 'datas' itself
  1968. data = {
  1969. att.res_id: att.datas
  1970. for att in records.env['ir.attachment'].sudo().search(domain)
  1971. }
  1972. records.env.cache.insert_missing(records, self, map(data.get, records._ids))
  1973. def create(self, record_values):
  1974. assert self.attachment
  1975. if not record_values:
  1976. return
  1977. # create the attachments that store the values
  1978. env = record_values[0][0].env
  1979. with env.norecompute():
  1980. env['ir.attachment'].sudo().with_context(
  1981. binary_field_real_user=env.user,
  1982. ).create([{
  1983. 'name': self.name,
  1984. 'res_model': self.model_name,
  1985. 'res_field': self.name,
  1986. 'res_id': record.id,
  1987. 'type': 'binary',
  1988. 'datas': value,
  1989. }
  1990. for record, value in record_values
  1991. if value
  1992. ])
  1993. def write(self, records, value):
  1994. if not self.attachment:
  1995. return super().write(records, value)
  1996. # discard recomputation of self on records
  1997. records.env.remove_to_compute(self, records)
  1998. # update the cache, and discard the records that are not modified
  1999. cache = records.env.cache
  2000. cache_value = self.convert_to_cache(value, records)
  2001. records = cache.get_records_different_from(records, self, cache_value)
  2002. if not records:
  2003. return records
  2004. if self.store:
  2005. # determine records that are known to be not null
  2006. not_null = cache.get_records_different_from(records, self, None)
  2007. cache.update(records, self, itertools.repeat(cache_value))
  2008. # retrieve the attachments that store the values, and adapt them
  2009. if self.store and any(records._ids):
  2010. real_records = records.filtered('id')
  2011. atts = records.env['ir.attachment'].sudo()
  2012. if not_null:
  2013. atts = atts.search([
  2014. ('res_model', '=', self.model_name),
  2015. ('res_field', '=', self.name),
  2016. ('res_id', 'in', real_records.ids),
  2017. ])
  2018. if value:
  2019. # update the existing attachments
  2020. atts.write({'datas': value})
  2021. atts_records = records.browse(atts.mapped('res_id'))
  2022. # create the missing attachments
  2023. missing = (real_records - atts_records)
  2024. if missing:
  2025. atts.create([{
  2026. 'name': self.name,
  2027. 'res_model': record._name,
  2028. 'res_field': self.name,
  2029. 'res_id': record.id,
  2030. 'type': 'binary',
  2031. 'datas': value,
  2032. }
  2033. for record in missing
  2034. ])
  2035. else:
  2036. atts.unlink()
  2037. return records
  2038. class Image(Binary):
  2039. """Encapsulates an image, extending :class:`Binary`.
  2040. If image size is greater than the ``max_width``/``max_height`` limit of pixels, the image will be
  2041. resized to the limit by keeping aspect ratio.
  2042. :param int max_width: the maximum width of the image (default: ``0``, no limit)
  2043. :param int max_height: the maximum height of the image (default: ``0``, no limit)
  2044. :param bool verify_resolution: whether the image resolution should be verified
  2045. to ensure it doesn't go over the maximum image resolution (default: ``True``).
  2046. See :class:`odoo.tools.image.ImageProcess` for maximum image resolution (default: ``50e6``).
  2047. .. note::
  2048. If no ``max_width``/``max_height`` is specified (or is set to 0) and ``verify_resolution`` is False,
  2049. the field content won't be verified at all and a :class:`Binary` field should be used.
  2050. """
  2051. max_width = 0
  2052. max_height = 0
  2053. verify_resolution = True
  2054. def create(self, record_values):
  2055. new_record_values = []
  2056. for record, value in record_values:
  2057. # strange behavior when setting related image field, when `self`
  2058. # does not resize the same way as its related field
  2059. new_value = self._image_process(value)
  2060. new_record_values.append((record, new_value))
  2061. cache_value = self.convert_to_cache(value if self.related else new_value, record)
  2062. record.env.cache.update(record, self, itertools.repeat(cache_value))
  2063. super(Image, self).create(new_record_values)
  2064. def write(self, records, value):
  2065. try:
  2066. new_value = self._image_process(value)
  2067. except UserError:
  2068. if not any(records._ids):
  2069. # Some crap is assigned to a new record. This can happen in an
  2070. # onchange, where the client sends the "bin size" value of the
  2071. # field instead of its full value (this saves bandwidth). In
  2072. # this case, we simply don't assign the field: its value will be
  2073. # taken from the records' origin.
  2074. return
  2075. raise
  2076. super(Image, self).write(records, new_value)
  2077. cache_value = self.convert_to_cache(value if self.related else new_value, records)
  2078. dirty = self.column_type and self.store and any(records._ids)
  2079. records.env.cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  2080. def _image_process(self, value):
  2081. if self.readonly and not self.max_width and not self.max_height:
  2082. # no need to process images for computed fields, or related fields
  2083. return value
  2084. try:
  2085. img = base64.b64decode(value or '') or False
  2086. except:
  2087. raise UserError(_("Image is not encoded in base64."))
  2088. return base64.b64encode(image_process(img,
  2089. size=(self.max_width, self.max_height),
  2090. verify_resolution=self.verify_resolution,
  2091. ) or b'') or False
  2092. def _process_related(self, value):
  2093. """Override to resize the related value before saving it on self."""
  2094. try:
  2095. return self._image_process(super()._process_related(value))
  2096. except UserError:
  2097. # Avoid the following `write` to fail if the related image was saved
  2098. # invalid, which can happen for pre-existing databases.
  2099. return False
  2100. class Selection(Field):
  2101. """ Encapsulates an exclusive choice between different values.
  2102. :param selection: specifies the possible values for this field.
  2103. It is given as either a list of pairs ``(value, label)``, or a model
  2104. method, or a method name.
  2105. :type selection: list(tuple(str,str)) or callable or str
  2106. :param selection_add: provides an extension of the selection in the case
  2107. of an overridden field. It is a list of pairs ``(value, label)`` or
  2108. singletons ``(value,)``, where singleton values must appear in the
  2109. overridden selection. The new values are inserted in an order that is
  2110. consistent with the overridden selection and this list::
  2111. selection = [('a', 'A'), ('b', 'B')]
  2112. selection_add = [('c', 'C'), ('b',)]
  2113. > result = [('a', 'A'), ('c', 'C'), ('b', 'B')]
  2114. :type selection_add: list(tuple(str,str))
  2115. :param ondelete: provides a fallback mechanism for any overridden
  2116. field with a selection_add. It is a dict that maps every option
  2117. from the selection_add to a fallback action.
  2118. This fallback action will be applied to all records whose
  2119. selection_add option maps to it.
  2120. The actions can be any of the following:
  2121. - 'set null' -- the default, all records with this option
  2122. will have their selection value set to False.
  2123. - 'cascade' -- all records with this option will be
  2124. deleted along with the option itself.
  2125. - 'set default' -- all records with this option will be
  2126. set to the default of the field definition
  2127. - 'set VALUE' -- all records with this option will be
  2128. set to the given value
  2129. - <callable> -- a callable whose first and only argument will be
  2130. the set of records containing the specified Selection option,
  2131. for custom processing
  2132. The attribute ``selection`` is mandatory except in the case of
  2133. ``related`` or extended fields.
  2134. """
  2135. type = 'selection'
  2136. column_type = ('varchar', pg_varchar())
  2137. selection = None # [(value, string), ...], function or method name
  2138. validate = True # whether validating upon write
  2139. ondelete = None # {value: policy} (what to do when value is deleted)
  2140. def __init__(self, selection=Default, string=Default, **kwargs):
  2141. super(Selection, self).__init__(selection=selection, string=string, **kwargs)
  2142. def setup_nonrelated(self, model):
  2143. super().setup_nonrelated(model)
  2144. assert self.selection is not None, "Field %s without selection" % self
  2145. def setup_related(self, model):
  2146. super().setup_related(model)
  2147. # selection must be computed on related field
  2148. field = self.related_field
  2149. self.selection = lambda model: field._description_selection(model.env)
  2150. def _get_attrs(self, model_class, name):
  2151. attrs = super()._get_attrs(model_class, name)
  2152. # arguments 'selection' and 'selection_add' are processed below
  2153. attrs.pop('selection_add', None)
  2154. # Selection fields have an optional default implementation of a group_expand function
  2155. if attrs.get('group_expand') is True:
  2156. attrs['group_expand'] = self._default_group_expand
  2157. return attrs
  2158. def _setup_attrs(self, model_class, name):
  2159. super()._setup_attrs(model_class, name)
  2160. if not self._base_fields:
  2161. return
  2162. # determine selection (applying 'selection_add' extensions)
  2163. values = None
  2164. labels = {}
  2165. for field in self._base_fields:
  2166. # We cannot use field.selection or field.selection_add here
  2167. # because those attributes are overridden by ``_setup_attrs``.
  2168. if 'selection' in field.args:
  2169. if self.related:
  2170. _logger.warning("%s: selection attribute will be ignored as the field is related", self)
  2171. selection = field.args['selection']
  2172. if isinstance(selection, list):
  2173. if values is not None and values != [kv[0] for kv in selection]:
  2174. _logger.warning("%s: selection=%r overrides existing selection; use selection_add instead", self, selection)
  2175. values = [kv[0] for kv in selection]
  2176. labels = dict(selection)
  2177. self.ondelete = {}
  2178. else:
  2179. values = None
  2180. labels = {}
  2181. self.selection = selection
  2182. self.ondelete = None
  2183. if 'selection_add' in field.args:
  2184. if self.related:
  2185. _logger.warning("%s: selection_add attribute will be ignored as the field is related", self)
  2186. selection_add = field.args['selection_add']
  2187. assert isinstance(selection_add, list), \
  2188. "%s: selection_add=%r must be a list" % (self, selection_add)
  2189. assert values is not None, \
  2190. "%s: selection_add=%r on non-list selection %r" % (self, selection_add, self.selection)
  2191. ondelete = field.args.get('ondelete') or {}
  2192. new_values = [kv[0] for kv in selection_add if kv[0] not in values]
  2193. for key in new_values:
  2194. ondelete.setdefault(key, 'set null')
  2195. if self.required and new_values and 'set null' in ondelete.values():
  2196. raise ValueError(
  2197. "%r: required selection fields must define an ondelete policy that "
  2198. "implements the proper cleanup of the corresponding records upon "
  2199. "module uninstallation. Please use one or more of the following "
  2200. "policies: 'set default' (if the field has a default defined), 'cascade', "
  2201. "or a single-argument callable where the argument is the recordset "
  2202. "containing the specified option." % self
  2203. )
  2204. # check ondelete values
  2205. for key, val in ondelete.items():
  2206. if callable(val) or val in ('set null', 'cascade'):
  2207. continue
  2208. if val == 'set default':
  2209. assert self.default is not None, (
  2210. "%r: ondelete policy of type 'set default' is invalid for this field "
  2211. "as it does not define a default! Either define one in the base "
  2212. "field, or change the chosen ondelete policy" % self
  2213. )
  2214. elif val.startswith('set '):
  2215. assert val[4:] in values, (
  2216. "%s: ondelete policy of type 'set %%' must be either 'set null', "
  2217. "'set default', or 'set value' where value is a valid selection value."
  2218. ) % self
  2219. else:
  2220. raise ValueError(
  2221. "%r: ondelete policy %r for selection value %r is not a valid ondelete"
  2222. " policy, please choose one of 'set null', 'set default', "
  2223. "'set [value]', 'cascade' or a callable" % (self, val, key)
  2224. )
  2225. values = merge_sequences(values, [kv[0] for kv in selection_add])
  2226. labels.update(kv for kv in selection_add if len(kv) == 2)
  2227. self.ondelete.update(ondelete)
  2228. if values is not None:
  2229. self.selection = [(value, labels[value]) for value in values]
  2230. if isinstance(self.selection, list):
  2231. assert all(isinstance(v, str) for v, _ in self.selection), \
  2232. "Field %s with non-str value in selection" % self
  2233. def _selection_modules(self, model):
  2234. """ Return a mapping from selection values to modules defining each value. """
  2235. if not isinstance(self.selection, list):
  2236. return {}
  2237. value_modules = defaultdict(set)
  2238. for field in reversed(resolve_mro(model, self.name, type(self).__instancecheck__)):
  2239. module = field._module
  2240. if not module:
  2241. continue
  2242. if 'selection' in field.args:
  2243. value_modules.clear()
  2244. if isinstance(field.args['selection'], list):
  2245. for value, label in field.args['selection']:
  2246. value_modules[value].add(module)
  2247. if 'selection_add' in field.args:
  2248. for value_label in field.args['selection_add']:
  2249. if len(value_label) > 1:
  2250. value_modules[value_label[0]].add(module)
  2251. return value_modules
  2252. def _description_selection(self, env):
  2253. """ return the selection list (pairs (value, label)); labels are
  2254. translated according to context language
  2255. """
  2256. selection = self.selection
  2257. if isinstance(selection, str) or callable(selection):
  2258. return determine(selection, env[self.model_name])
  2259. # translate selection labels
  2260. if env.lang:
  2261. return env['ir.model.fields'].get_field_selection(self.model_name, self.name)
  2262. else:
  2263. return selection
  2264. def _default_group_expand(self, records, groups, domain, order):
  2265. # return a group per selection option, in definition order
  2266. return self.get_values(records.env)
  2267. def get_values(self, env):
  2268. """Return a list of the possible values."""
  2269. selection = self.selection
  2270. if isinstance(selection, str) or callable(selection):
  2271. selection = determine(selection, env[self.model_name].with_context(lang=None))
  2272. return [value for value, _ in selection]
  2273. def convert_to_column(self, value, record, values=None, validate=True):
  2274. if validate and self.validate:
  2275. value = self.convert_to_cache(value, record)
  2276. return super(Selection, self).convert_to_column(value, record, values, validate)
  2277. def convert_to_cache(self, value, record, validate=True):
  2278. if not validate:
  2279. return value or None
  2280. if value and self.column_type[0] == 'int4':
  2281. value = int(value)
  2282. if value in self.get_values(record.env):
  2283. return value
  2284. elif not value:
  2285. return None
  2286. raise ValueError("Wrong value for %s: %r" % (self, value))
  2287. def convert_to_export(self, value, record):
  2288. if not isinstance(self.selection, list):
  2289. # FIXME: this reproduces an existing buggy behavior!
  2290. return value if value else ''
  2291. for item in self._description_selection(record.env):
  2292. if item[0] == value:
  2293. return item[1]
  2294. return ''
  2295. class Reference(Selection):
  2296. """ Pseudo-relational field (no FK in database).
  2297. The field value is stored as a :class:`string <str>` following the pattern
  2298. ``"res_model,res_id"`` in database.
  2299. """
  2300. type = 'reference'
  2301. @property
  2302. def column_type(self):
  2303. return ('varchar', pg_varchar())
  2304. def convert_to_column(self, value, record, values=None, validate=True):
  2305. return Field.convert_to_column(self, value, record, values, validate)
  2306. def convert_to_cache(self, value, record, validate=True):
  2307. # cache format: str ("model,id") or None
  2308. if isinstance(value, BaseModel):
  2309. if not validate or (value._name in self.get_values(record.env) and len(value) <= 1):
  2310. return "%s,%s" % (value._name, value.id) if value else None
  2311. elif isinstance(value, str):
  2312. res_model, res_id = value.split(',')
  2313. if not validate or res_model in self.get_values(record.env):
  2314. if record.env[res_model].browse(int(res_id)).exists():
  2315. return value
  2316. else:
  2317. return None
  2318. elif not value:
  2319. return None
  2320. raise ValueError("Wrong value for %s: %r" % (self, value))
  2321. def convert_to_record(self, value, record):
  2322. if value:
  2323. res_model, res_id = value.split(',')
  2324. return record.env[res_model].browse(int(res_id))
  2325. return None
  2326. def convert_to_read(self, value, record, use_name_get=True):
  2327. return "%s,%s" % (value._name, value.id) if value else False
  2328. def convert_to_export(self, value, record):
  2329. return value.display_name if value else ''
  2330. def convert_to_display_name(self, value, record):
  2331. return value.display_name if value else False
  2332. class _Relational(Field):
  2333. """ Abstract class for relational fields. """
  2334. relational = True
  2335. domain = [] # domain for searching values
  2336. context = {} # context for searching values
  2337. check_company = False
  2338. def __get__(self, records, owner):
  2339. # base case: do the regular access
  2340. if records is None or len(records._ids) <= 1:
  2341. return super().__get__(records, owner)
  2342. # multirecord case: use mapped
  2343. return self.mapped(records)
  2344. def setup_nonrelated(self, model):
  2345. super().setup_nonrelated(model)
  2346. if self.comodel_name not in model.pool:
  2347. _logger.warning("Field %s with unknown comodel_name %r", self, self.comodel_name)
  2348. self.comodel_name = '_unknown'
  2349. def get_domain_list(self, model):
  2350. """ Return a list domain from the domain parameter. """
  2351. domain = self.domain
  2352. if callable(domain):
  2353. domain = domain(model)
  2354. return domain if isinstance(domain, list) else []
  2355. @property
  2356. def _related_domain(self):
  2357. if callable(self.domain):
  2358. # will be called with another model than self's
  2359. return lambda recs: self.domain(recs.env[self.model_name])
  2360. else:
  2361. # maybe not correct if domain is a string...
  2362. return self.domain
  2363. _related_context = property(attrgetter('context'))
  2364. _description_relation = property(attrgetter('comodel_name'))
  2365. _description_context = property(attrgetter('context'))
  2366. def _description_domain(self, env):
  2367. if self.check_company and not self.domain:
  2368. if self.company_dependent:
  2369. if self.comodel_name == "res.users":
  2370. # user needs access to current company (self.env.company)
  2371. return "[('company_ids', 'in', allowed_company_ids[0])]"
  2372. else:
  2373. return "[('company_id', 'in', [allowed_company_ids[0], False])]"
  2374. else:
  2375. # when using check_company=True on a field on 'res.company', the
  2376. # company_id comes from the id of the current record
  2377. cid = "id" if self.model_name == "res.company" else "company_id"
  2378. if self.comodel_name == "res.users":
  2379. # User allowed company ids = user.company_ids
  2380. return f"['|', (not {cid}, '=', True), ('company_ids', 'in', [{cid}])]"
  2381. else:
  2382. return f"[('company_id', 'in', [{cid}, False])]"
  2383. return self.domain(env[self.model_name]) if callable(self.domain) else self.domain
  2384. def null(self, record):
  2385. return record.env[self.comodel_name]
  2386. class Many2one(_Relational):
  2387. """ The value of such a field is a recordset of size 0 (no
  2388. record) or 1 (a single record).
  2389. :param str comodel_name: name of the target model
  2390. ``Mandatory`` except for related or extended fields.
  2391. :param domain: an optional domain to set on candidate values on the
  2392. client side (domain or string)
  2393. :param dict context: an optional context to use on the client side when
  2394. handling that field
  2395. :param str ondelete: what to do when the referred record is deleted;
  2396. possible values are: ``'set null'``, ``'restrict'``, ``'cascade'``
  2397. :param bool auto_join: whether JOINs are generated upon search through that
  2398. field (default: ``False``)
  2399. :param bool delegate: set it to ``True`` to make fields of the target model
  2400. accessible from the current model (corresponds to ``_inherits``)
  2401. :param bool check_company: Mark the field to be verified in
  2402. :meth:`~odoo.models.Model._check_company`. Add a default company
  2403. domain depending on the field attributes.
  2404. """
  2405. type = 'many2one'
  2406. column_type = ('int4', 'int4')
  2407. ondelete = None # what to do when value is deleted
  2408. auto_join = False # whether joins are generated upon search
  2409. delegate = False # whether self implements delegation
  2410. def __init__(self, comodel_name=Default, string=Default, **kwargs):
  2411. super(Many2one, self).__init__(comodel_name=comodel_name, string=string, **kwargs)
  2412. def _setup_attrs(self, model_class, name):
  2413. super()._setup_attrs(model_class, name)
  2414. # determine self.delegate
  2415. if not self.delegate and name in model_class._inherits.values():
  2416. self.delegate = True
  2417. # self.delegate implies self.auto_join
  2418. if self.delegate:
  2419. self.auto_join = True
  2420. def setup_nonrelated(self, model):
  2421. super().setup_nonrelated(model)
  2422. # 3 cases:
  2423. # 1) The ondelete attribute is not defined, we assign it a sensible default
  2424. # 2) The ondelete attribute is defined and its definition makes sense
  2425. # 3) The ondelete attribute is explicitly defined as 'set null' for a required m2o,
  2426. # this is considered a programming error.
  2427. if not self.ondelete:
  2428. comodel = model.env[self.comodel_name]
  2429. if model.is_transient() and not comodel.is_transient():
  2430. # Many2one relations from TransientModel Model are annoying because
  2431. # they can block deletion due to foreign keys. So unless stated
  2432. # otherwise, we default them to ondelete='cascade'.
  2433. self.ondelete = 'cascade' if self.required else 'set null'
  2434. else:
  2435. self.ondelete = 'restrict' if self.required else 'set null'
  2436. if self.ondelete == 'set null' and self.required:
  2437. raise ValueError(
  2438. "The m2o field %s of model %s is required but declares its ondelete policy "
  2439. "as being 'set null'. Only 'restrict' and 'cascade' make sense."
  2440. % (self.name, model._name)
  2441. )
  2442. if self.ondelete == 'restrict' and self.comodel_name in IR_MODELS:
  2443. raise ValueError(
  2444. f"Field {self.name} of model {model._name} is defined as ondelete='restrict' "
  2445. f"while having {self.comodel_name} as comodel, the 'restrict' mode is not "
  2446. f"supported for this type of field as comodel."
  2447. )
  2448. def update_db(self, model, columns):
  2449. comodel = model.env[self.comodel_name]
  2450. if not model.is_transient() and comodel.is_transient():
  2451. raise ValueError('Many2one %s from Model to TransientModel is forbidden' % self)
  2452. return super(Many2one, self).update_db(model, columns)
  2453. def update_db_column(self, model, column):
  2454. super(Many2one, self).update_db_column(model, column)
  2455. model.pool.post_init(self.update_db_foreign_key, model, column)
  2456. def update_db_foreign_key(self, model, column):
  2457. comodel = model.env[self.comodel_name]
  2458. # foreign keys do not work on views, and users can define custom models on sql views.
  2459. if not model._is_an_ordinary_table() or not comodel._is_an_ordinary_table():
  2460. return
  2461. # ir_actions is inherited, so foreign key doesn't work on it
  2462. if not comodel._auto or comodel._table == 'ir_actions':
  2463. return
  2464. # create/update the foreign key, and reflect it in 'ir.model.constraint'
  2465. model.pool.add_foreign_key(
  2466. model._table, self.name, comodel._table, 'id', self.ondelete or 'set null',
  2467. model, self._module
  2468. )
  2469. def _update(self, records, value):
  2470. """ Update the cached value of ``self`` for ``records`` with ``value``. """
  2471. cache = records.env.cache
  2472. for record in records:
  2473. cache.set(record, self, self.convert_to_cache(value, record, validate=False))
  2474. def convert_to_column(self, value, record, values=None, validate=True):
  2475. return value or None
  2476. def convert_to_cache(self, value, record, validate=True):
  2477. # cache format: id or None
  2478. if type(value) in IdType:
  2479. id_ = value
  2480. elif isinstance(value, BaseModel):
  2481. if validate and (value._name != self.comodel_name or len(value) > 1):
  2482. raise ValueError("Wrong value for %s: %r" % (self, value))
  2483. id_ = value._ids[0] if value._ids else None
  2484. elif isinstance(value, tuple):
  2485. # value is either a pair (id, name), or a tuple of ids
  2486. id_ = value[0] if value else None
  2487. elif isinstance(value, dict):
  2488. # return a new record (with the given field 'id' as origin)
  2489. comodel = record.env[self.comodel_name]
  2490. origin = comodel.browse(value.get('id'))
  2491. id_ = comodel.new(value, origin=origin).id
  2492. else:
  2493. id_ = None
  2494. if self.delegate and record and not any(record._ids):
  2495. # if all records are new, then so is the parent
  2496. id_ = id_ and NewId(id_)
  2497. return id_
  2498. def convert_to_record(self, value, record):
  2499. # use registry to avoid creating a recordset for the model
  2500. ids = () if value is None else (value,)
  2501. prefetch_ids = PrefetchMany2one(record, self)
  2502. return record.pool[self.comodel_name](record.env, ids, prefetch_ids)
  2503. def convert_to_record_multi(self, values, records):
  2504. # return the ids as a recordset without duplicates
  2505. prefetch_ids = PrefetchMany2one(records, self)
  2506. ids = tuple(unique(id_ for id_ in values if id_ is not None))
  2507. return records.pool[self.comodel_name](records.env, ids, prefetch_ids)
  2508. def convert_to_read(self, value, record, use_name_get=True):
  2509. if use_name_get and value:
  2510. # evaluate name_get() as superuser, because the visibility of a
  2511. # many2one field value (id and name) depends on the current record's
  2512. # access rights, and not the value's access rights.
  2513. try:
  2514. # performance: value.sudo() prefetches the same records as value
  2515. return (value.id, value.sudo().display_name)
  2516. except MissingError:
  2517. # Should not happen, unless the foreign key is missing.
  2518. return False
  2519. else:
  2520. return value.id
  2521. def convert_to_write(self, value, record):
  2522. if type(value) in IdType:
  2523. return value
  2524. if not value:
  2525. return False
  2526. if isinstance(value, BaseModel) and value._name == self.comodel_name:
  2527. return value.id
  2528. if isinstance(value, tuple):
  2529. # value is either a pair (id, name), or a tuple of ids
  2530. return value[0] if value else False
  2531. if isinstance(value, dict):
  2532. return record.env[self.comodel_name].new(value).id
  2533. raise ValueError("Wrong value for %s: %r" % (self, value))
  2534. def convert_to_export(self, value, record):
  2535. return value.display_name if value else ''
  2536. def convert_to_display_name(self, value, record):
  2537. return value.display_name
  2538. def convert_to_onchange(self, value, record, names):
  2539. # if value is a new record, serialize its origin instead
  2540. return super().convert_to_onchange(value._origin, record, names)
  2541. def write(self, records, value):
  2542. # discard recomputation of self on records
  2543. records.env.remove_to_compute(self, records)
  2544. # discard the records that are not modified
  2545. cache = records.env.cache
  2546. cache_value = self.convert_to_cache(value, records)
  2547. records = cache.get_records_different_from(records, self, cache_value)
  2548. if not records:
  2549. return records
  2550. # remove records from the cache of one2many fields of old corecords
  2551. self._remove_inverses(records, cache_value)
  2552. # update the cache of self
  2553. dirty = self.store and any(records._ids)
  2554. cache.update(records, self, itertools.repeat(cache_value), dirty=dirty)
  2555. # update the cache of one2many fields of new corecord
  2556. self._update_inverses(records, cache_value)
  2557. return records
  2558. def _remove_inverses(self, records, value):
  2559. """ Remove `records` from the cached values of the inverse fields of `self`. """
  2560. cache = records.env.cache
  2561. record_ids = set(records._ids)
  2562. # align(id) returns a NewId if records are new, a real id otherwise
  2563. align = (lambda id_: id_) if all(record_ids) else (lambda id_: id_ and NewId(id_))
  2564. for invf in records.pool.field_inverses[self]:
  2565. corecords = records.env[self.comodel_name].browse(
  2566. align(id_) for id_ in cache.get_values(records, self)
  2567. )
  2568. for corecord in corecords:
  2569. ids0 = cache.get(corecord, invf, None)
  2570. if ids0 is not None:
  2571. ids1 = tuple(id_ for id_ in ids0 if id_ not in record_ids)
  2572. cache.set(corecord, invf, ids1)
  2573. def _update_inverses(self, records, value):
  2574. """ Add `records` to the cached values of the inverse fields of `self`. """
  2575. if value is None:
  2576. return
  2577. cache = records.env.cache
  2578. corecord = self.convert_to_record(value, records)
  2579. for invf in records.pool.field_inverses[self]:
  2580. valid_records = records.filtered_domain(invf.get_domain_list(corecord))
  2581. if not valid_records:
  2582. continue
  2583. ids0 = cache.get(corecord, invf, None)
  2584. # if the value for the corecord is not in cache, but this is a new
  2585. # record, assign it anyway, as you won't be able to fetch it from
  2586. # database (see `test_sale_order`)
  2587. if ids0 is not None or not corecord.id:
  2588. ids1 = tuple(unique((ids0 or ()) + valid_records._ids))
  2589. cache.set(corecord, invf, ids1)
  2590. class Many2oneReference(Integer):
  2591. """ Pseudo-relational field (no FK in database).
  2592. The field value is stored as an :class:`integer <int>` id in database.
  2593. Contrary to :class:`Reference` fields, the model has to be specified
  2594. in a :class:`Char` field, whose name has to be specified in the
  2595. `model_field` attribute for the current :class:`Many2oneReference` field.
  2596. :param str model_field: name of the :class:`Char` where the model name is stored.
  2597. """
  2598. type = 'many2one_reference'
  2599. model_field = None
  2600. _related_model_field = property(attrgetter('model_field'))
  2601. def convert_to_cache(self, value, record, validate=True):
  2602. # cache format: id or None
  2603. if isinstance(value, BaseModel):
  2604. value = value._ids[0] if value._ids else None
  2605. return super().convert_to_cache(value, record, validate)
  2606. def _remove_inverses(self, records, value):
  2607. # TODO: unused
  2608. # remove records from the cache of one2many fields of old corecords
  2609. cache = records.env.cache
  2610. record_ids = set(records._ids)
  2611. model_ids = self._record_ids_per_res_model(records)
  2612. for invf in records.pool.field_inverses[self]:
  2613. records = records.browse(model_ids[invf.model_name])
  2614. if not records:
  2615. continue
  2616. corecords = records.env[invf.model_name].browse(
  2617. id_ for id_ in cache.get_values(records, self)
  2618. )
  2619. for corecord in corecords:
  2620. ids0 = cache.get(corecord, invf, None)
  2621. if ids0 is not None:
  2622. ids1 = tuple(id_ for id_ in ids0 if id_ not in record_ids)
  2623. cache.set(corecord, invf, ids1)
  2624. def _update_inverses(self, records, value):
  2625. """ Add `records` to the cached values of the inverse fields of `self`. """
  2626. if not value:
  2627. return
  2628. cache = records.env.cache
  2629. model_ids = self._record_ids_per_res_model(records)
  2630. for invf in records.pool.field_inverses[self]:
  2631. records = records.browse(model_ids[invf.model_name])
  2632. if not records:
  2633. continue
  2634. corecord = records.env[invf.model_name].browse(value)
  2635. records = records.filtered_domain(invf.get_domain_list(corecord))
  2636. if not records:
  2637. continue
  2638. ids0 = cache.get(corecord, invf, None)
  2639. # if the value for the corecord is not in cache, but this is a new
  2640. # record, assign it anyway, as you won't be able to fetch it from
  2641. # database (see `test_sale_order`)
  2642. if ids0 is not None or not corecord.id:
  2643. ids1 = tuple(unique((ids0 or ()) + records._ids))
  2644. cache.set(corecord, invf, ids1)
  2645. def _record_ids_per_res_model(self, records):
  2646. model_ids = defaultdict(set)
  2647. for record in records:
  2648. model = record[self.model_field]
  2649. if not model and record._fields[self.model_field].compute:
  2650. # fallback when the model field is computed :-/
  2651. record._fields[self.model_field].compute_value(record)
  2652. model = record[self.model_field]
  2653. if not model:
  2654. continue
  2655. model_ids[model].add(record.id)
  2656. return model_ids
  2657. class Json(Field):
  2658. """ JSON Field that contain unstructured information in jsonb PostgreSQL column.
  2659. This field is still in beta
  2660. Some features have not been implemented and won't be implemented in stable versions, including:
  2661. * searching
  2662. * indexing
  2663. * mutating the values.
  2664. """
  2665. type = 'json'
  2666. column_type = ('jsonb', 'jsonb')
  2667. def convert_to_record(self, value, record):
  2668. """ Return a copy of the value """
  2669. return False if value is None else copy.deepcopy(value)
  2670. def convert_to_cache(self, value, record, validate=True):
  2671. if not value:
  2672. return None
  2673. return json.loads(json.dumps(value))
  2674. def convert_to_column(self, value, record, values=None, validate=True):
  2675. if not value:
  2676. return None
  2677. return PsycopgJson(value)
  2678. def convert_to_export(self, value, record):
  2679. if not value:
  2680. return ''
  2681. return json.dumps(value)
  2682. class Properties(Field):
  2683. """ Field that contains a list of properties (aka "sub-field") based on
  2684. a definition defined on a container. Properties are pseudo-fields, acting
  2685. like Odoo fields but without being independently stored in database.
  2686. This field allows a light customization based on a container record. Used
  2687. for relationships such as <project.project> / <project.task>,... New
  2688. properties can be created on the fly without changing the structure of the
  2689. database.
  2690. The "definition_record" define the field used to find the container of the
  2691. current record. The container must have a :class:`~odoo.fields.PropertiesDefinition`
  2692. field "definition_record_field" that contains the properties definition
  2693. (type of each property, default value)...
  2694. Only the value of each property is stored on the child. When we read the
  2695. properties field, we read the definition on the container and merge it with
  2696. the value of the child. That way the web client has access to the full
  2697. field definition (property type, ...).
  2698. """
  2699. type = 'properties'
  2700. column_type = ('jsonb', 'jsonb')
  2701. copy = False
  2702. prefetch = False
  2703. write_sequence = 10 # because it must be written after the definition field
  2704. # the field is computed editable by design (see the compute method below)
  2705. store = True
  2706. readonly = False
  2707. precompute = True
  2708. definition = None
  2709. definition_record = None # field on the current model that point to the definition record
  2710. definition_record_field = None # field on the definition record which defined the Properties field definition
  2711. _description_definition_record = property(attrgetter('definition_record'))
  2712. _description_definition_record_field = property(attrgetter('definition_record_field'))
  2713. ALLOWED_TYPES = (
  2714. 'boolean', 'integer', 'float', 'char', 'date',
  2715. 'datetime', 'many2one', 'many2many', 'selection', 'tags',
  2716. )
  2717. def _setup_attrs(self, model_class, name):
  2718. super()._setup_attrs(model_class, name)
  2719. if self.definition:
  2720. # determine definition_record and definition_record_field
  2721. assert self.definition.count(".") == 1
  2722. self.definition_record, self.definition_record_field = self.definition.rsplit('.', 1)
  2723. # make the field computed, and set its dependencies
  2724. self._depends = (self.definition_record, )
  2725. self.compute = self._compute
  2726. # Database/cache format: a value is either None, or a dict mapping property
  2727. # names to their corresponding value, like
  2728. #
  2729. # {
  2730. # '3adf37f3258cfe40': 'red',
  2731. # 'aa34746a6851ee4e': 1337,
  2732. # }
  2733. #
  2734. def convert_to_column(self, value, record, values=None, validate=True):
  2735. if not value:
  2736. return None
  2737. value = self.convert_to_cache(value, record, validate=validate)
  2738. return json.dumps(value)
  2739. def convert_to_cache(self, value, record, validate=True):
  2740. # any format -> cache format {name: value} or None
  2741. if not value:
  2742. return None
  2743. if isinstance(value, dict):
  2744. # avoid accidental side effects from shared mutable data
  2745. return copy.deepcopy(value)
  2746. if isinstance(value, str):
  2747. value = json.loads(value)
  2748. if not isinstance(value, dict):
  2749. raise ValueError(f"Wrong property value {value!r}")
  2750. return value
  2751. if isinstance(value, list):
  2752. # Convert the list with all definitions into a simple dict
  2753. # {name: value} to store the strict minimum on the child
  2754. self._remove_display_name(value)
  2755. return self._list_to_dict(value)
  2756. raise ValueError(f"Wrong property type {type(value)!r}")
  2757. # Record format: the value is a list, where each element is a dict
  2758. # containing the definition of a property, together with the property's
  2759. # corresponding value, like
  2760. #
  2761. # [{
  2762. # 'name': '3adf37f3258cfe40',
  2763. # 'string': 'Color Code',
  2764. # 'type': 'char',
  2765. # 'default': 'blue',
  2766. # 'value': 'red',
  2767. # }, {
  2768. # 'name': 'aa34746a6851ee4e',
  2769. # 'string': 'Partner',
  2770. # 'type': 'many2one',
  2771. # 'comodel': 'test_new_api.partner',
  2772. # 'value': 1337,
  2773. # }]
  2774. #
  2775. def convert_to_record(self, value, record):
  2776. # value is in cache format
  2777. definition = self._get_properties_definition(record)
  2778. if not value or not definition:
  2779. return definition or []
  2780. assert isinstance(value, dict), f"Wrong type {value!r}"
  2781. value = self._dict_to_list(value, definition)
  2782. self._parse_json_types(value, record.env)
  2783. return value
  2784. # Read format: almost identical to the record format, except that relational
  2785. # field values have a display name.
  2786. #
  2787. # [{
  2788. # 'name': '3adf37f3258cfe40',
  2789. # 'string': 'Color Code',
  2790. # 'type': 'char',
  2791. # 'default': 'blue',
  2792. # 'value': 'red',
  2793. # }, {
  2794. # 'name': 'aa34746a6851ee4e',
  2795. # 'string': 'Partner',
  2796. # 'type': 'many2one',
  2797. # 'comodel': 'test_new_api.partner',
  2798. # 'value': [1337, 'Bob'],
  2799. # }]
  2800. #
  2801. def convert_to_read(self, value, record, use_name_get=True):
  2802. # value is in record format
  2803. if use_name_get:
  2804. self._add_display_name(value, record.env)
  2805. return value
  2806. def convert_to_write(self, value, record):
  2807. """If we write a list on the child, update the definition record."""
  2808. if isinstance(value, list):
  2809. # will update the definition record
  2810. self._remove_display_name(value)
  2811. return value
  2812. return super().convert_to_write(value, record)
  2813. def convert_to_onchange(self, value, record, names):
  2814. self._add_display_name(value, record.env)
  2815. return value
  2816. def read(self, records):
  2817. """Read everything needed in batch for the given records.
  2818. To retrieve relational properties names, or to check their existence,
  2819. we need to do some SQL queries. To reduce the number of queries when we read
  2820. in batch, we put in cache everything needed before calling
  2821. convert_to_record / convert_to_read.
  2822. """
  2823. definition_records_map = {
  2824. record: record[self.definition_record].sudo()[self.definition_record_field]
  2825. for record in records
  2826. }
  2827. # ids per model we need to fetch in batch to put in cache
  2828. ids_per_model = defaultdict(OrderedSet)
  2829. records_cached_values = list(records.env.cache.get_values(records, self))
  2830. for record, record_values in zip(records, records_cached_values):
  2831. definition = definition_records_map.get(record)
  2832. if not record_values or not definition:
  2833. continue
  2834. for property_definition in definition:
  2835. comodel = property_definition.get('comodel')
  2836. type_ = property_definition.get('type')
  2837. name = property_definition.get('name')
  2838. if not comodel or type_ not in ('many2one', 'many2many') or name not in record_values:
  2839. continue
  2840. default = property_definition.get('default') or []
  2841. property_value = record_values[name] or []
  2842. if type_ == 'many2one':
  2843. default = [default] if default else []
  2844. property_value = [property_value] if property_value else []
  2845. ids_per_model[comodel].update(default)
  2846. ids_per_model[comodel].update(property_value)
  2847. # check existence and pre-fetch in batch
  2848. existing_ids_per_model = {}
  2849. for model, ids in ids_per_model.items():
  2850. recs = records.env[model].browse(ids).exists()
  2851. existing_ids_per_model[model] = set(recs.ids)
  2852. for record in recs:
  2853. # read a field to pre-fetch the recordset
  2854. try:
  2855. record.display_name
  2856. except AccessError:
  2857. pass
  2858. # update the cache and remove non-existing ids
  2859. for record, record_values in zip(records, records_cached_values):
  2860. definition = definition_records_map.get(record)
  2861. if not record_values or not definition:
  2862. continue
  2863. for property_definition in definition:
  2864. comodel = property_definition.get('comodel')
  2865. type_ = property_definition.get('type')
  2866. name = property_definition.get('name')
  2867. if not comodel or type_ not in ('many2one', 'many2many') or not record_values.get(name):
  2868. continue
  2869. property_value = record_values[name]
  2870. if type_ == 'many2one':
  2871. record_values[name] = property_value if property_value in existing_ids_per_model[comodel] else False
  2872. else:
  2873. record_values[name] = [id_ for id_ in property_value if id_ in existing_ids_per_model[comodel]]
  2874. records.env.cache.update(record, self, [record_values], check_dirty=False)
  2875. def write(self, records, value):
  2876. """Check if the properties definition has been changed.
  2877. To avoid extra SQL queries used to detect definition change, we add a
  2878. flag in the properties list. Parent update is done only when this flag
  2879. is present, delegating the check to the caller (generally web client).
  2880. For deletion, we need to keep the removed property definition in the
  2881. list to be able to put the delete flag in it. Otherwise we have no way
  2882. to know that a property has been removed.
  2883. """
  2884. if isinstance(value, str):
  2885. value = json.loads(value)
  2886. if isinstance(value, dict):
  2887. # don't need to write on the container definition
  2888. return super().write(records, value)
  2889. definition_changed = any(
  2890. definition.get('definition_changed')
  2891. or definition.get('definition_deleted')
  2892. for definition in value
  2893. )
  2894. if definition_changed:
  2895. value = [
  2896. definition for definition in value
  2897. if not definition.get('definition_deleted')
  2898. ]
  2899. for definition in value:
  2900. definition.pop('definition_changed', None)
  2901. # update the properties definition on the container
  2902. container = records[self.definition_record]
  2903. if container:
  2904. properties_definition = copy.deepcopy(value)
  2905. for property_definition in properties_definition:
  2906. property_definition.pop('value', None)
  2907. container[self.definition_record_field] = properties_definition
  2908. _logger.info('Properties field: User #%i changed definition of %r', records.env.user.id, container)
  2909. return super().write(records, value)
  2910. def _compute(self, records):
  2911. """Add the default properties value when the container is changed."""
  2912. for record in records:
  2913. record[self.name] = self._add_default_values(
  2914. record.env,
  2915. {self.name: record[self.name], self.definition_record: record[self.definition_record]},
  2916. )
  2917. def _add_default_values(self, env, values):
  2918. """Read the properties definition to add default values.
  2919. Default values are defined on the container in the 'default' key of
  2920. the definition.
  2921. :param env: environment
  2922. :param values: All values that will be written on the record
  2923. :return: Return the default values in the "dict" format
  2924. """
  2925. properties_values = values.get(self.name) or {}
  2926. if not values.get(self.definition_record):
  2927. # container is not given in the value, can not find properties definition
  2928. return properties_values
  2929. container_id = values[self.definition_record]
  2930. if not isinstance(container_id, (int, BaseModel)):
  2931. raise ValueError(f"Wrong container value {container_id!r}")
  2932. if isinstance(container_id, int):
  2933. # retrieve the container record
  2934. current_model = env[self.model_name]
  2935. definition_record_field = current_model._fields[self.definition_record]
  2936. container_model_name = definition_record_field.comodel_name
  2937. container_id = env[container_model_name].browse(container_id)
  2938. properties_definition = container_id[self.definition_record_field]
  2939. if not properties_definition:
  2940. return properties_values
  2941. assert isinstance(properties_values, (list, dict))
  2942. if isinstance(properties_values, list):
  2943. self._remove_display_name(properties_values)
  2944. properties_list_values = properties_values
  2945. else:
  2946. properties_list_values = self._dict_to_list(properties_values, properties_definition)
  2947. for properties_value in properties_list_values:
  2948. if properties_value.get('value') is None:
  2949. default = properties_value.get('default') or False
  2950. properties_value['value'] = default
  2951. return properties_list_values
  2952. def _get_properties_definition(self, record):
  2953. """Return the properties definition of the given record."""
  2954. container = record[self.definition_record]
  2955. if container:
  2956. return container.sudo()[self.definition_record_field]
  2957. @classmethod
  2958. def _add_display_name(cls, values_list, env, value_keys=('value', 'default')):
  2959. """Add the "name_get" for each many2one / many2many properties.
  2960. Modify in place "values_list".
  2961. :param values_list: List of properties definition and values
  2962. :param env: environment
  2963. """
  2964. for property_definition in values_list:
  2965. property_type = property_definition.get('type')
  2966. property_model = property_definition.get('comodel')
  2967. if not property_model:
  2968. continue
  2969. for value_key in value_keys:
  2970. property_value = property_definition.get(value_key)
  2971. if property_type == 'many2one' and property_value and isinstance(property_value, int):
  2972. try:
  2973. display_name = env[property_model].browse(property_value).display_name
  2974. property_definition[value_key] = (property_value, display_name)
  2975. except AccessError:
  2976. # protect from access error message, show an empty name
  2977. property_definition[value_key] = (property_value, None)
  2978. except MissingError:
  2979. property_definition[value_key] = False
  2980. elif property_type == 'many2many' and property_value and is_list_of(property_value, int):
  2981. property_definition[value_key] = []
  2982. records = env[property_model].browse(property_value)
  2983. for record in records:
  2984. try:
  2985. property_definition[value_key].append((record.id, record.display_name))
  2986. except AccessError:
  2987. property_definition[value_key].append((record.id, None))
  2988. except MissingError:
  2989. continue
  2990. @classmethod
  2991. def _remove_display_name(cls, values_list, value_key='value'):
  2992. """Remove the display name received by the web client for the relational properties.
  2993. Modify in place "values_list".
  2994. - many2one: (35, 'Bob') -> 35
  2995. - many2many: [(35, 'Bob'), (36, 'Alice')] -> [35, 36]
  2996. :param values_list: List of properties definition with properties value
  2997. :param value_key: In which dict key we need to remove the display name
  2998. """
  2999. for property_definition in values_list:
  3000. if not isinstance(property_definition, dict) or not property_definition.get('name'):
  3001. continue
  3002. property_value = property_definition.get(value_key)
  3003. if not property_value:
  3004. continue
  3005. property_type = property_definition.get('type')
  3006. if property_type == 'many2one' and has_list_types(property_value, [int, (str, NoneType)]):
  3007. property_definition[value_key] = property_value[0]
  3008. elif property_type == 'many2many':
  3009. if is_list_of(property_value, (list, tuple)):
  3010. # [(35, 'Admin'), (36, 'Demo')] -> [35, 36]
  3011. property_definition[value_key] = [
  3012. many2many_value[0]
  3013. for many2many_value in property_value
  3014. ]
  3015. @classmethod
  3016. def _add_missing_names(cls, values_list):
  3017. """Generate new properties name if needed.
  3018. Modify in place "values_list".
  3019. :param values_list: List of properties definition with properties value
  3020. """
  3021. for definition in values_list:
  3022. if definition.get('definition_changed') and not definition.get('name'):
  3023. # keep only the first 64 bits
  3024. definition['name'] = str(uuid.uuid4()).replace('-', '')[:16]
  3025. @classmethod
  3026. def _parse_json_types(cls, values_list, env):
  3027. """Parse the value stored in the JSON.
  3028. Check for records existence, if we removed a selection option, ...
  3029. Modify in place "values_list".
  3030. :param values_list: List of properties definition and values
  3031. :param env: environment
  3032. """
  3033. for property_definition in values_list:
  3034. property_value = property_definition.get('value')
  3035. property_type = property_definition.get('type')
  3036. res_model = property_definition.get('comodel')
  3037. if property_type not in cls.ALLOWED_TYPES:
  3038. raise ValueError(f'Wrong property type {property_type!r}')
  3039. if property_type == 'boolean':
  3040. # E.G. convert zero to False
  3041. property_value = bool(property_value)
  3042. elif property_type == 'char' and not isinstance(property_value, str) \
  3043. and property_value is not None:
  3044. property_value = False
  3045. elif property_value and property_type == 'selection':
  3046. # check if the selection option still exists
  3047. options = property_definition.get('selection') or []
  3048. options = {option[0] for option in options if option or ()} # always length 2
  3049. if property_value not in options:
  3050. # maybe the option has been removed on the container
  3051. property_value = False
  3052. elif property_value and property_type == 'tags':
  3053. # remove all tags that are not defined on the container
  3054. all_tags = {tag[0] for tag in property_definition.get('tags') or ()}
  3055. property_value = [tag for tag in property_value if tag in all_tags]
  3056. elif property_type == 'many2one' and property_value and res_model in env:
  3057. if not isinstance(property_value, int):
  3058. raise ValueError(f'Wrong many2one value: {property_value!r}.')
  3059. elif property_type == 'many2many' and property_value and res_model in env:
  3060. if not is_list_of(property_value, int):
  3061. raise ValueError(f'Wrong many2many value: {property_value!r}.')
  3062. if len(property_value) != len(set(property_value)):
  3063. # remove duplicated value and preserve order
  3064. property_value = list(dict.fromkeys(property_value))
  3065. property_definition['value'] = property_value
  3066. @classmethod
  3067. def _list_to_dict(cls, values_list):
  3068. """Convert a list of properties with definition into a dict {name: value}.
  3069. To not repeat data in database, we only store the value of each property on
  3070. the child. The properties definition is stored on the container.
  3071. E.G.
  3072. Input list:
  3073. [{
  3074. 'name': '3adf37f3258cfe40',
  3075. 'string': 'Color Code',
  3076. 'type': 'char',
  3077. 'default': 'blue',
  3078. 'value': 'red',
  3079. }, {
  3080. 'name': 'aa34746a6851ee4e',
  3081. 'string': 'Partner',
  3082. 'type': 'many2one',
  3083. 'comodel': 'test_new_api.partner',
  3084. 'value': [1337, 'Bob'],
  3085. }]
  3086. Output dict:
  3087. {
  3088. '3adf37f3258cfe40': 'red',
  3089. 'aa34746a6851ee4e': 1337,
  3090. }
  3091. :param values_list: List of properties definition and value
  3092. :return: Generate a dict {name: value} from this definitions / values list
  3093. """
  3094. if not is_list_of(values_list, dict):
  3095. raise ValueError(f'Wrong properties value {values_list!r}')
  3096. cls._add_missing_names(values_list)
  3097. dict_value = {}
  3098. for property_definition in values_list:
  3099. property_value = property_definition.get('value')
  3100. property_type = property_definition.get('type')
  3101. property_model = property_definition.get('comodel')
  3102. if property_type not in ('integer', 'float') or property_value != 0:
  3103. property_value = property_value or False
  3104. if property_type in ('many2one', 'many2many') and property_model and property_value:
  3105. # check that value are correct before storing them in database
  3106. if property_type == 'many2many' and property_value and not is_list_of(property_value, int):
  3107. raise ValueError(f"Wrong many2many value {property_value!r}")
  3108. if property_type == 'many2one' and not isinstance(property_value, int):
  3109. raise ValueError(f"Wrong many2one value {property_value!r}")
  3110. dict_value[property_definition['name']] = property_value
  3111. return dict_value
  3112. @classmethod
  3113. def _dict_to_list(cls, values_dict, properties_definition):
  3114. """Convert a dict of {property: value} into a list of property definition with values.
  3115. :param values_dict: JSON value coming from the child table
  3116. :param properties_definition: Properties definition coming from the container table
  3117. :return: Merge both value into a list of properties with value
  3118. Ignore every values in the child that is not defined on the container.
  3119. """
  3120. if not is_list_of(properties_definition, dict):
  3121. raise ValueError(f'Wrong properties value {properties_definition!r}')
  3122. values_list = copy.deepcopy(properties_definition)
  3123. for property_definition in values_list:
  3124. property_definition['value'] = values_dict.get(property_definition['name'])
  3125. return values_list
  3126. class PropertiesDefinition(Field):
  3127. """ Field used to define the properties definition (see :class:`~odoo.fields.Properties`
  3128. field). This field is used on the container record to define the structure
  3129. of expected properties on subrecords. It is used to check the properties
  3130. definition. """
  3131. type = 'properties_definition'
  3132. column_type = ('jsonb', 'jsonb')
  3133. copy = True # containers may act like templates, keep definitions to ease usage
  3134. readonly = False
  3135. prefetch = True
  3136. REQUIRED_KEYS = ('name', 'type')
  3137. ALLOWED_KEYS = (
  3138. 'name', 'string', 'type', 'comodel', 'default',
  3139. 'selection', 'tags', 'domain', 'view_in_kanban',
  3140. )
  3141. # those keys will be removed if the types does not match
  3142. PROPERTY_PARAMETERS_MAP = {
  3143. 'comodel': {'many2one', 'many2many'},
  3144. 'domain': {'many2one', 'many2many'},
  3145. 'selection': {'selection'},
  3146. 'tags': {'tags'},
  3147. }
  3148. def convert_to_column(self, value, record, values=None, validate=True):
  3149. """Convert the value before inserting it in database.
  3150. This method accepts a list properties definition.
  3151. The relational properties (many2one / many2many) default value
  3152. might contain the name_get of those records (and will be removed).
  3153. [{
  3154. 'name': '3adf37f3258cfe40',
  3155. 'string': 'Color Code',
  3156. 'type': 'char',
  3157. 'default': 'blue',
  3158. 'default': 'red',
  3159. }, {
  3160. 'name': 'aa34746a6851ee4e',
  3161. 'string': 'Partner',
  3162. 'type': 'many2one',
  3163. 'comodel': 'test_new_api.partner',
  3164. 'default': [1337, 'Bob'],
  3165. }]
  3166. """
  3167. if not value:
  3168. return None
  3169. if isinstance(value, str):
  3170. value = json.loads(value)
  3171. if not isinstance(value, list):
  3172. raise ValueError(f'Wrong properties definition type {type(value)!r}')
  3173. Properties._remove_display_name(value, value_key='default')
  3174. self._validate_properties_definition(value, record.env)
  3175. return json.dumps(value)
  3176. def convert_to_cache(self, value, record, validate=True):
  3177. # any format -> cache format (list of dicts or None)
  3178. if not value:
  3179. return None
  3180. if isinstance(value, list):
  3181. # avoid accidental side effects from shared mutable data, and make
  3182. # the value strict with respect to JSON (tuple -> list, etc)
  3183. value = json.dumps(value)
  3184. if isinstance(value, str):
  3185. value = json.loads(value)
  3186. if not isinstance(value, list):
  3187. raise ValueError(f'Wrong properties definition type {type(value)!r}')
  3188. Properties._remove_display_name(value, value_key='default')
  3189. self._validate_properties_definition(value, record.env)
  3190. return value
  3191. def convert_to_record(self, value, record):
  3192. # cache format -> record format (list of dicts)
  3193. if not value:
  3194. return []
  3195. # return a copy of the definition in cache where all property
  3196. # definitions have been cleaned up
  3197. result = []
  3198. for property_definition in value:
  3199. if not all(property_definition.get(key) for key in self.REQUIRED_KEYS):
  3200. # some required keys are missing, ignore this property definition
  3201. continue
  3202. # don't modify the value in cache
  3203. property_definition = dict(property_definition)
  3204. # check if the model still exists in the environment, the module of the
  3205. # model might have been uninstalled so the model might not exist anymore
  3206. property_model = property_definition.get('comodel')
  3207. if property_model and property_model not in record.env:
  3208. property_definition['comodel'] = property_model = False
  3209. if not property_model and 'domain' in property_definition:
  3210. del property_definition['domain']
  3211. property_domain = property_definition.get('domain')
  3212. if property_domain:
  3213. # some fields in the domain might have been removed
  3214. # (e.g. if the module has been uninstalled)
  3215. # check if the domain is still valid
  3216. try:
  3217. expression.expression(
  3218. ast.literal_eval(property_domain),
  3219. record.env[property_model],
  3220. )
  3221. except ValueError:
  3222. del property_definition['domain']
  3223. result.append(property_definition)
  3224. for property_parameter, allowed_types in self.PROPERTY_PARAMETERS_MAP.items():
  3225. if property_definition.get('type') not in allowed_types:
  3226. property_definition.pop(property_parameter, None)
  3227. return result
  3228. def convert_to_read(self, value, record, use_name_get=True):
  3229. # record format -> read format (list of dicts with display names)
  3230. if not value:
  3231. return value
  3232. if use_name_get:
  3233. Properties._add_display_name(value, record.env, value_keys=('default',))
  3234. return value
  3235. @classmethod
  3236. def _validate_properties_definition(cls, properties_definition, env):
  3237. """Raise an error if the property definition is not valid."""
  3238. properties_names = set()
  3239. for property_definition in properties_definition:
  3240. property_definition_keys = set(property_definition.keys())
  3241. invalid_keys = property_definition_keys - set(cls.ALLOWED_KEYS)
  3242. if invalid_keys:
  3243. raise ValueError(
  3244. 'Some key are not allowed for a properties definition [%s].' %
  3245. ', '.join(invalid_keys),
  3246. )
  3247. required_keys = set(cls.REQUIRED_KEYS) - property_definition_keys
  3248. if required_keys:
  3249. raise ValueError(
  3250. 'Some key are missing for a properties definition [%s].' %
  3251. ', '.join(required_keys),
  3252. )
  3253. property_name = property_definition.get('name')
  3254. if not property_name or property_name in properties_names:
  3255. raise ValueError(f'The property name {property_name!r} is not set or duplicated.')
  3256. properties_names.add(property_name)
  3257. property_type = property_definition.get('type')
  3258. if property_type and property_type not in Properties.ALLOWED_TYPES:
  3259. raise ValueError(f'Wrong property type {property_type!r}.')
  3260. model = property_definition.get('comodel')
  3261. if model and model not in env:
  3262. raise ValueError(f'Invalid model name {model!r}')
  3263. property_selection = property_definition.get('selection')
  3264. if property_selection:
  3265. if (not is_list_of(property_selection, (list, tuple))
  3266. or not all(len(selection) == 2 for selection in property_selection)):
  3267. raise ValueError(f'Wrong options {property_selection!r}.')
  3268. all_options = [option[0] for option in property_selection]
  3269. if len(all_options) != len(set(all_options)):
  3270. duplicated = set(filter(lambda x: all_options.count(x) > 1, all_options))
  3271. raise ValueError(f'Some options are duplicated: {", ".join(duplicated)}.')
  3272. property_tags = property_definition.get('tags')
  3273. if property_tags:
  3274. if (not is_list_of(property_tags, (list, tuple))
  3275. or not all(len(tag) == 3 and isinstance(tag[2], int) for tag in property_tags)):
  3276. raise ValueError(f'Wrong tags definition {property_tags!r}.')
  3277. all_tags = [tag[0] for tag in property_tags]
  3278. if len(all_tags) != len(set(all_tags)):
  3279. duplicated = set(filter(lambda x: all_tags.count(x) > 1, all_tags))
  3280. raise ValueError(f'Some tags are duplicated: {", ".join(duplicated)}.')
  3281. class Command(enum.IntEnum):
  3282. """
  3283. :class:`~odoo.fields.One2many` and :class:`~odoo.fields.Many2many` fields
  3284. expect a special command to manipulate the relation they implement.
  3285. Internally, each command is a 3-elements tuple where the first element is a
  3286. mandatory integer that identifies the command, the second element is either
  3287. the related record id to apply the command on (commands update, delete,
  3288. unlink and link) either 0 (commands create, clear and set), the third
  3289. element is either the ``values`` to write on the record (commands create
  3290. and update) either the new ``ids`` list of related records (command set),
  3291. either 0 (commands delete, unlink, link, and clear).
  3292. Via Python, we encourage developers craft new commands via the various
  3293. functions of this namespace. We also encourage developers to use the
  3294. command identifier constant names when comparing the 1st element of
  3295. existing commands.
  3296. Via RPC, it is impossible nor to use the functions nor the command constant
  3297. names. It is required to instead write the literal 3-elements tuple where
  3298. the first element is the integer identifier of the command.
  3299. """
  3300. CREATE = 0
  3301. UPDATE = 1
  3302. DELETE = 2
  3303. UNLINK = 3
  3304. LINK = 4
  3305. CLEAR = 5
  3306. SET = 6
  3307. @classmethod
  3308. def create(cls, values: dict):
  3309. """
  3310. Create new records in the comodel using ``values``, link the created
  3311. records to ``self``.
  3312. In case of a :class:`~odoo.fields.Many2many` relation, one unique
  3313. new record is created in the comodel such that all records in `self`
  3314. are linked to the new record.
  3315. In case of a :class:`~odoo.fields.One2many` relation, one new record
  3316. is created in the comodel for every record in ``self`` such that every
  3317. record in ``self`` is linked to exactly one of the new records.
  3318. Return the command triple :samp:`(CREATE, 0, {values})`
  3319. """
  3320. return (cls.CREATE, 0, values)
  3321. @classmethod
  3322. def update(cls, id: int, values: dict):
  3323. """
  3324. Write ``values`` on the related record.
  3325. Return the command triple :samp:`(UPDATE, {id}, {values})`
  3326. """
  3327. return (cls.UPDATE, id, values)
  3328. @classmethod
  3329. def delete(cls, id: int):
  3330. """
  3331. Remove the related record from the database and remove its relation
  3332. with ``self``.
  3333. In case of a :class:`~odoo.fields.Many2many` relation, removing the
  3334. record from the database may be prevented if it is still linked to
  3335. other records.
  3336. Return the command triple :samp:`(DELETE, {id}, 0)`
  3337. """
  3338. return (cls.DELETE, id, 0)
  3339. @classmethod
  3340. def unlink(cls, id: int):
  3341. """
  3342. Remove the relation between ``self`` and the related record.
  3343. In case of a :class:`~odoo.fields.One2many` relation, the given record
  3344. is deleted from the database if the inverse field is set as
  3345. ``ondelete='cascade'``. Otherwise, the value of the inverse field is
  3346. set to False and the record is kept.
  3347. Return the command triple :samp:`(UNLINK, {id}, 0)`
  3348. """
  3349. return (cls.UNLINK, id, 0)
  3350. @classmethod
  3351. def link(cls, id: int):
  3352. """
  3353. Add a relation between ``self`` and the related record.
  3354. Return the command triple :samp:`(LINK, {id}, 0)`
  3355. """
  3356. return (cls.LINK, id, 0)
  3357. @classmethod
  3358. def clear(cls):
  3359. """
  3360. Remove all records from the relation with ``self``. It behaves like
  3361. executing the `unlink` command on every record.
  3362. Return the command triple :samp:`(CLEAR, 0, 0)`
  3363. """
  3364. return (cls.CLEAR, 0, 0)
  3365. @classmethod
  3366. def set(cls, ids: list):
  3367. """
  3368. Replace the current relations of ``self`` by the given ones. It behaves
  3369. like executing the ``unlink`` command on every removed relation then
  3370. executing the ``link`` command on every new relation.
  3371. Return the command triple :samp:`(SET, 0, {ids})`
  3372. """
  3373. return (cls.SET, 0, ids)
  3374. class _RelationalMulti(_Relational):
  3375. r"Abstract class for relational fields \*2many."
  3376. write_sequence = 20
  3377. # Important: the cache contains the ids of all the records in the relation,
  3378. # including inactive records. Inactive records are filtered out by
  3379. # convert_to_record(), depending on the context.
  3380. def _update(self, records, value):
  3381. """ Update the cached value of ``self`` for ``records`` with ``value``,
  3382. and return whether everything is in cache.
  3383. """
  3384. if not isinstance(records, BaseModel):
  3385. # the inverse of self is a non-relational field; `value` is a
  3386. # corecord that refers to `records` by an integer field
  3387. model = value.env[self.model_name]
  3388. domain = self.domain(model) if callable(self.domain) else self.domain
  3389. if not value.filtered_domain(domain):
  3390. return
  3391. records = model.browse(records)
  3392. result = True
  3393. if value:
  3394. cache = records.env.cache
  3395. for record in records:
  3396. if cache.contains(record, self):
  3397. val = self.convert_to_cache(record[self.name] | value, record, validate=False)
  3398. cache.set(record, self, val)
  3399. else:
  3400. result = False
  3401. records.modified([self.name])
  3402. return result
  3403. def convert_to_cache(self, value, record, validate=True):
  3404. # cache format: tuple(ids)
  3405. if isinstance(value, BaseModel):
  3406. if validate and value._name != self.comodel_name:
  3407. raise ValueError("Wrong value for %s: %s" % (self, value))
  3408. ids = value._ids
  3409. if record and not record.id:
  3410. # x2many field value of new record is new records
  3411. ids = tuple(it and NewId(it) for it in ids)
  3412. return ids
  3413. elif isinstance(value, (list, tuple)):
  3414. # value is a list/tuple of commands, dicts or record ids
  3415. comodel = record.env[self.comodel_name]
  3416. # if record is new, the field's value is new records
  3417. if record and not record.id:
  3418. browse = lambda it: comodel.browse((it and NewId(it),))
  3419. else:
  3420. browse = comodel.browse
  3421. # determine the value ids
  3422. ids = OrderedSet(record[self.name]._ids if validate else ())
  3423. # modify ids with the commands
  3424. for command in value:
  3425. if isinstance(command, (tuple, list)):
  3426. if command[0] == Command.CREATE:
  3427. ids.add(comodel.new(command[2], ref=command[1]).id)
  3428. elif command[0] == Command.UPDATE:
  3429. line = browse(command[1])
  3430. if validate:
  3431. line.update(command[2])
  3432. else:
  3433. line._update_cache(command[2], validate=False)
  3434. ids.add(line.id)
  3435. elif command[0] in (Command.DELETE, Command.UNLINK):
  3436. ids.discard(browse(command[1]).id)
  3437. elif command[0] == Command.LINK:
  3438. ids.add(browse(command[1]).id)
  3439. elif command[0] == Command.CLEAR:
  3440. ids.clear()
  3441. elif command[0] == Command.SET:
  3442. ids = OrderedSet(browse(it).id for it in command[2])
  3443. elif isinstance(command, dict):
  3444. ids.add(comodel.new(command).id)
  3445. else:
  3446. ids.add(browse(command).id)
  3447. # return result as a tuple
  3448. return tuple(ids)
  3449. elif not value:
  3450. return ()
  3451. raise ValueError("Wrong value for %s: %s" % (self, value))
  3452. def convert_to_record(self, value, record):
  3453. # use registry to avoid creating a recordset for the model
  3454. prefetch_ids = PrefetchX2many(record, self)
  3455. Comodel = record.pool[self.comodel_name]
  3456. corecords = Comodel(record.env, value, prefetch_ids)
  3457. if (
  3458. Comodel._active_name
  3459. and self.context.get('active_test', record.env.context.get('active_test', True))
  3460. ):
  3461. corecords = corecords.filtered(Comodel._active_name).with_prefetch(prefetch_ids)
  3462. return corecords
  3463. def convert_to_record_multi(self, values, records):
  3464. # return the list of ids as a recordset without duplicates
  3465. prefetch_ids = PrefetchX2many(records, self)
  3466. Comodel = records.pool[self.comodel_name]
  3467. ids = tuple(unique(id_ for ids in values for id_ in ids))
  3468. corecords = Comodel(records.env, ids, prefetch_ids)
  3469. if (
  3470. Comodel._active_name
  3471. and self.context.get('active_test', records.env.context.get('active_test', True))
  3472. ):
  3473. corecords = corecords.filtered(Comodel._active_name).with_prefetch(prefetch_ids)
  3474. return corecords
  3475. def convert_to_read(self, value, record, use_name_get=True):
  3476. return value.ids
  3477. def convert_to_write(self, value, record):
  3478. if isinstance(value, tuple):
  3479. # a tuple of ids, this is the cache format
  3480. value = record.env[self.comodel_name].browse(value)
  3481. if isinstance(value, BaseModel) and value._name == self.comodel_name:
  3482. def get_origin(val):
  3483. return val._origin if isinstance(val, BaseModel) else val
  3484. # make result with new and existing records
  3485. inv_names = {field.name for field in record.pool.field_inverses[self]}
  3486. result = [Command.set([])]
  3487. for record in value:
  3488. origin = record._origin
  3489. if not origin:
  3490. values = record._convert_to_write({
  3491. name: record[name]
  3492. for name in record._cache
  3493. if name not in inv_names
  3494. })
  3495. result.append(Command.create(values))
  3496. else:
  3497. result[0][2].append(origin.id)
  3498. if record != origin:
  3499. values = record._convert_to_write({
  3500. name: record[name]
  3501. for name in record._cache
  3502. if name not in inv_names and get_origin(record[name]) != origin[name]
  3503. })
  3504. if values:
  3505. result.append(Command.update(origin.id, values))
  3506. return result
  3507. if value is False or value is None:
  3508. return [Command.clear()]
  3509. if isinstance(value, list):
  3510. return value
  3511. raise ValueError("Wrong value for %s: %s" % (self, value))
  3512. def convert_to_export(self, value, record):
  3513. return ','.join(name for id, name in value.name_get()) if value else ''
  3514. def convert_to_display_name(self, value, record):
  3515. raise NotImplementedError()
  3516. def get_depends(self, model):
  3517. depends, depends_context = super().get_depends(model)
  3518. if not self.compute and isinstance(self.domain, list):
  3519. depends = unique(itertools.chain(depends, (
  3520. self.name + '.' + arg[0]
  3521. for arg in self.domain
  3522. if isinstance(arg, (tuple, list)) and isinstance(arg[0], str)
  3523. )))
  3524. return depends, depends_context
  3525. def create(self, record_values):
  3526. """ Write the value of ``self`` on the given records, which have just
  3527. been created.
  3528. :param record_values: a list of pairs ``(record, value)``, where
  3529. ``value`` is in the format of method :meth:`BaseModel.write`
  3530. """
  3531. self.write_batch(record_values, True)
  3532. def write(self, records, value):
  3533. # discard recomputation of self on records
  3534. records.env.remove_to_compute(self, records)
  3535. return self.write_batch([(records, value)])
  3536. def write_batch(self, records_commands_list, create=False):
  3537. if not records_commands_list:
  3538. return False
  3539. for idx, (recs, value) in enumerate(records_commands_list):
  3540. if isinstance(value, tuple):
  3541. value = [Command.set(value)]
  3542. elif isinstance(value, BaseModel) and value._name == self.comodel_name:
  3543. value = [Command.set(value._ids)]
  3544. elif value is False or value is None:
  3545. value = [Command.clear()]
  3546. elif isinstance(value, list) and value and not isinstance(value[0], (tuple, list)):
  3547. value = [Command.set(tuple(value))]
  3548. if not isinstance(value, list):
  3549. raise ValueError("Wrong value for %s: %s" % (self, value))
  3550. records_commands_list[idx] = (recs, value)
  3551. record_ids = {rid for recs, cs in records_commands_list for rid in recs._ids}
  3552. if all(record_ids):
  3553. return self.write_real(records_commands_list, create)
  3554. else:
  3555. assert not any(record_ids)
  3556. return self.write_new(records_commands_list)
  3557. class One2many(_RelationalMulti):
  3558. """One2many field; the value of such a field is the recordset of all the
  3559. records in ``comodel_name`` such that the field ``inverse_name`` is equal to
  3560. the current record.
  3561. :param str comodel_name: name of the target model
  3562. :param str inverse_name: name of the inverse ``Many2one`` field in
  3563. ``comodel_name``
  3564. :param domain: an optional domain to set on candidate values on the
  3565. client side (domain or string)
  3566. :param dict context: an optional context to use on the client side when
  3567. handling that field
  3568. :param bool auto_join: whether JOINs are generated upon search through that
  3569. field (default: ``False``)
  3570. The attributes ``comodel_name`` and ``inverse_name`` are mandatory except in
  3571. the case of related fields or field extensions.
  3572. """
  3573. type = 'one2many'
  3574. inverse_name = None # name of the inverse field
  3575. auto_join = False # whether joins are generated upon search
  3576. copy = False # o2m are not copied by default
  3577. def __init__(self, comodel_name=Default, inverse_name=Default, string=Default, **kwargs):
  3578. super(One2many, self).__init__(
  3579. comodel_name=comodel_name,
  3580. inverse_name=inverse_name,
  3581. string=string,
  3582. **kwargs
  3583. )
  3584. def setup_nonrelated(self, model):
  3585. super(One2many, self).setup_nonrelated(model)
  3586. if self.inverse_name:
  3587. # link self to its inverse field and vice-versa
  3588. comodel = model.env[self.comodel_name]
  3589. invf = comodel._fields[self.inverse_name]
  3590. if isinstance(invf, (Many2one, Many2oneReference)):
  3591. # setting one2many fields only invalidates many2one inverses;
  3592. # integer inverses (res_model/res_id pairs) are not supported
  3593. model.pool.field_inverses.add(self, invf)
  3594. comodel.pool.field_inverses.add(invf, self)
  3595. _description_relation_field = property(attrgetter('inverse_name'))
  3596. def update_db(self, model, columns):
  3597. if self.comodel_name in model.env:
  3598. comodel = model.env[self.comodel_name]
  3599. if self.inverse_name not in comodel._fields:
  3600. raise UserError(_("No inverse field %r found for %r") % (self.inverse_name, self.comodel_name))
  3601. def get_domain_list(self, records):
  3602. comodel = records.env.registry[self.comodel_name]
  3603. inverse_field = comodel._fields[self.inverse_name]
  3604. domain = super(One2many, self).get_domain_list(records)
  3605. if inverse_field.type == 'many2one_reference':
  3606. domain = domain + [(inverse_field.model_field, '=', records._name)]
  3607. return domain
  3608. def __get__(self, records, owner):
  3609. if records is not None and self.inverse_name is not None:
  3610. # force the computation of the inverse field to ensure that the
  3611. # cache value of self is consistent
  3612. inverse_field = records.pool[self.comodel_name]._fields[self.inverse_name]
  3613. if inverse_field.compute:
  3614. records.env[self.comodel_name]._recompute_model([self.inverse_name])
  3615. return super().__get__(records, owner)
  3616. def read(self, records):
  3617. # retrieve the lines in the comodel
  3618. context = {'active_test': False}
  3619. context.update(self.context)
  3620. comodel = records.env[self.comodel_name].with_context(**context)
  3621. inverse = self.inverse_name
  3622. inverse_field = comodel._fields[inverse]
  3623. domain = self.get_domain_list(records) + [(inverse, 'in', records.ids)]
  3624. lines = comodel.search(domain)
  3625. get_id = (lambda rec: rec.id) if inverse_field.type == 'many2one' else int
  3626. if len(records) == 1:
  3627. # optimization: all lines have the same value for 'inverse_field',
  3628. # so we don't need to fetch it from database
  3629. if not inverse_field._description_searchable:
  3630. # fix: if the field is not searchable maybe we got some lines that are not linked to records
  3631. lines = lines.with_context(prefetch_fields=False).filtered(
  3632. lambda line: get_id(line[inverse]) == records.id
  3633. )
  3634. records.env.cache.insert_missing(records, self, [lines._ids])
  3635. records.env.cache.insert_missing(lines, inverse_field, itertools.repeat(records.id))
  3636. return
  3637. # group lines by inverse field (without prefetching other fields)
  3638. group = defaultdict(list)
  3639. for line in lines.with_context(prefetch_fields=False):
  3640. # line[inverse] may be a record or an integer
  3641. group[get_id(line[inverse])].append(line.id)
  3642. # store result in cache
  3643. values = [tuple(group[id_]) for id_ in records._ids]
  3644. records.env.cache.insert_missing(records, self, values)
  3645. def write_real(self, records_commands_list, create=False):
  3646. """ Update real records. """
  3647. # records_commands_list = [(records, commands), ...]
  3648. if not records_commands_list:
  3649. return
  3650. model = records_commands_list[0][0].browse()
  3651. comodel = model.env[self.comodel_name].with_context(**self.context)
  3652. ids = OrderedSet(rid for recs, cs in records_commands_list for rid in recs.ids)
  3653. records = records_commands_list[0][0].browse(ids)
  3654. if self.store:
  3655. inverse = self.inverse_name
  3656. to_create = [] # line vals to create
  3657. to_delete = [] # line ids to delete
  3658. to_link = defaultdict(OrderedSet) # {record: line_ids}
  3659. allow_full_delete = not create
  3660. def unlink(lines):
  3661. if getattr(comodel._fields[inverse], 'ondelete', False) == 'cascade':
  3662. to_delete.extend(lines._ids)
  3663. else:
  3664. lines[inverse] = False
  3665. def flush():
  3666. if to_link:
  3667. before = {record: record[self.name] for record in to_link}
  3668. if to_delete:
  3669. # unlink() will remove the lines from the cache
  3670. comodel.browse(to_delete).unlink()
  3671. to_delete.clear()
  3672. if to_create:
  3673. # create() will add the new lines to the cache of records
  3674. comodel.create(to_create)
  3675. to_create.clear()
  3676. if to_link:
  3677. for record, line_ids in to_link.items():
  3678. lines = comodel.browse(line_ids) - before[record]
  3679. # linking missing lines should fail
  3680. lines.mapped(inverse)
  3681. lines[inverse] = record
  3682. to_link.clear()
  3683. for recs, commands in records_commands_list:
  3684. for command in (commands or ()):
  3685. if command[0] == Command.CREATE:
  3686. for record in recs:
  3687. to_create.append(dict(command[2], **{inverse: record.id}))
  3688. allow_full_delete = False
  3689. elif command[0] == Command.UPDATE:
  3690. comodel.browse(command[1]).write(command[2])
  3691. elif command[0] == Command.DELETE:
  3692. to_delete.append(command[1])
  3693. elif command[0] == Command.UNLINK:
  3694. unlink(comodel.browse(command[1]))
  3695. elif command[0] == Command.LINK:
  3696. to_link[recs[-1]].add(command[1])
  3697. allow_full_delete = False
  3698. elif command[0] in (Command.CLEAR, Command.SET):
  3699. line_ids = command[2] if command[0] == Command.SET else []
  3700. if not allow_full_delete:
  3701. # do not try to delete anything in creation mode if nothing has been created before
  3702. if line_ids:
  3703. # equivalent to Command.LINK
  3704. if line_ids.__class__ is int:
  3705. line_ids = [line_ids]
  3706. to_link[recs[-1]].update(line_ids)
  3707. allow_full_delete = False
  3708. continue
  3709. flush()
  3710. # assign the given lines to the last record only
  3711. lines = comodel.browse(line_ids)
  3712. domain = self.get_domain_list(model) + \
  3713. [(inverse, 'in', recs.ids), ('id', 'not in', lines.ids)]
  3714. unlink(comodel.search(domain))
  3715. lines[inverse] = recs[-1]
  3716. flush()
  3717. else:
  3718. cache = records.env.cache
  3719. def link(record, lines):
  3720. ids = record[self.name]._ids
  3721. cache.set(record, self, tuple(unique(ids + lines._ids)))
  3722. def unlink(lines):
  3723. for record in records:
  3724. cache.set(record, self, (record[self.name] - lines)._ids)
  3725. for recs, commands in records_commands_list:
  3726. for command in (commands or ()):
  3727. if command[0] == Command.CREATE:
  3728. for record in recs:
  3729. link(record, comodel.new(command[2], ref=command[1]))
  3730. elif command[0] == Command.UPDATE:
  3731. comodel.browse(command[1]).write(command[2])
  3732. elif command[0] == Command.DELETE:
  3733. unlink(comodel.browse(command[1]))
  3734. elif command[0] == Command.UNLINK:
  3735. unlink(comodel.browse(command[1]))
  3736. elif command[0] == Command.LINK:
  3737. link(recs[-1], comodel.browse(command[1]))
  3738. elif command[0] in (Command.CLEAR, Command.SET):
  3739. # assign the given lines to the last record only
  3740. cache.update(recs, self, itertools.repeat(()))
  3741. lines = comodel.browse(command[2] if command[0] == Command.SET else [])
  3742. cache.set(recs[-1], self, lines._ids)
  3743. return records
  3744. def write_new(self, records_commands_list):
  3745. if not records_commands_list:
  3746. return
  3747. model = records_commands_list[0][0].browse()
  3748. cache = model.env.cache
  3749. comodel = model.env[self.comodel_name].with_context(**self.context)
  3750. ids = {record.id for records, _ in records_commands_list for record in records}
  3751. records = model.browse(ids)
  3752. def browse(ids):
  3753. return comodel.browse([id_ and NewId(id_) for id_ in ids])
  3754. # make sure self is in cache
  3755. records[self.name]
  3756. if self.store:
  3757. inverse = self.inverse_name
  3758. # make sure self's inverse is in cache
  3759. inverse_field = comodel._fields[inverse]
  3760. for record in records:
  3761. cache.update(record[self.name], inverse_field, itertools.repeat(record.id))
  3762. for recs, commands in records_commands_list:
  3763. for command in commands:
  3764. if command[0] == Command.CREATE:
  3765. for record in recs:
  3766. line = comodel.new(command[2], ref=command[1])
  3767. line[inverse] = record
  3768. elif command[0] == Command.UPDATE:
  3769. browse([command[1]]).update(command[2])
  3770. elif command[0] == Command.DELETE:
  3771. browse([command[1]])[inverse] = False
  3772. elif command[0] == Command.UNLINK:
  3773. browse([command[1]])[inverse] = False
  3774. elif command[0] == Command.LINK:
  3775. browse([command[1]])[inverse] = recs[-1]
  3776. elif command[0] == Command.CLEAR:
  3777. cache.update(recs, self, itertools.repeat(()))
  3778. elif command[0] == Command.SET:
  3779. # assign the given lines to the last record only
  3780. cache.update(recs, self, itertools.repeat(()))
  3781. last, lines = recs[-1], browse(command[2])
  3782. cache.set(last, self, lines._ids)
  3783. cache.update(lines, inverse_field, itertools.repeat(last.id))
  3784. else:
  3785. def link(record, lines):
  3786. ids = record[self.name]._ids
  3787. cache.set(record, self, tuple(unique(ids + lines._ids)))
  3788. def unlink(lines):
  3789. for record in records:
  3790. cache.set(record, self, (record[self.name] - lines)._ids)
  3791. for recs, commands in records_commands_list:
  3792. for command in commands:
  3793. if command[0] == Command.CREATE:
  3794. for record in recs:
  3795. link(record, comodel.new(command[2], ref=command[1]))
  3796. elif command[0] == Command.UPDATE:
  3797. browse([command[1]]).update(command[2])
  3798. elif command[0] == Command.DELETE:
  3799. unlink(browse([command[1]]))
  3800. elif command[0] == Command.UNLINK:
  3801. unlink(browse([command[1]]))
  3802. elif command[0] == Command.LINK:
  3803. link(recs[-1], browse([command[1]]))
  3804. elif command[0] in (Command.CLEAR, Command.SET):
  3805. # assign the given lines to the last record only
  3806. cache.update(recs, self, itertools.repeat(()))
  3807. lines = comodel.browse(command[2] if command[0] == Command.SET else [])
  3808. cache.set(recs[-1], self, lines._ids)
  3809. return records
  3810. class Many2many(_RelationalMulti):
  3811. """ Many2many field; the value of such a field is the recordset.
  3812. :param comodel_name: name of the target model (string)
  3813. mandatory except in the case of related or extended fields
  3814. :param str relation: optional name of the table that stores the relation in
  3815. the database
  3816. :param str column1: optional name of the column referring to "these" records
  3817. in the table ``relation``
  3818. :param str column2: optional name of the column referring to "those" records
  3819. in the table ``relation``
  3820. The attributes ``relation``, ``column1`` and ``column2`` are optional.
  3821. If not given, names are automatically generated from model names,
  3822. provided ``model_name`` and ``comodel_name`` are different!
  3823. Note that having several fields with implicit relation parameters on a
  3824. given model with the same comodel is not accepted by the ORM, since
  3825. those field would use the same table. The ORM prevents two many2many
  3826. fields to use the same relation parameters, except if
  3827. - both fields use the same model, comodel, and relation parameters are
  3828. explicit; or
  3829. - at least one field belongs to a model with ``_auto = False``.
  3830. :param domain: an optional domain to set on candidate values on the
  3831. client side (domain or string)
  3832. :param dict context: an optional context to use on the client side when
  3833. handling that field
  3834. :param bool check_company: Mark the field to be verified in
  3835. :meth:`~odoo.models.Model._check_company`. Add a default company
  3836. domain depending on the field attributes.
  3837. """
  3838. type = 'many2many'
  3839. _explicit = True # whether schema is explicitly given
  3840. relation = None # name of table
  3841. column1 = None # column of table referring to model
  3842. column2 = None # column of table referring to comodel
  3843. auto_join = False # whether joins are generated upon search
  3844. ondelete = 'cascade' # optional ondelete for the column2 fkey
  3845. def __init__(self, comodel_name=Default, relation=Default, column1=Default,
  3846. column2=Default, string=Default, **kwargs):
  3847. super(Many2many, self).__init__(
  3848. comodel_name=comodel_name,
  3849. relation=relation,
  3850. column1=column1,
  3851. column2=column2,
  3852. string=string,
  3853. **kwargs
  3854. )
  3855. def setup_nonrelated(self, model):
  3856. super().setup_nonrelated(model)
  3857. # 2 cases:
  3858. # 1) The ondelete attribute is defined and its definition makes sense
  3859. # 2) The ondelete attribute is explicitly defined as 'set null' for a m2m,
  3860. # this is considered a programming error.
  3861. if self.ondelete not in ('cascade', 'restrict'):
  3862. raise ValueError(
  3863. "The m2m field %s of model %s declares its ondelete policy "
  3864. "as being %r. Only 'restrict' and 'cascade' make sense."
  3865. % (self.name, model._name, self.ondelete)
  3866. )
  3867. if self.store:
  3868. if not (self.relation and self.column1 and self.column2):
  3869. if not self.relation:
  3870. self._explicit = False
  3871. # table name is based on the stable alphabetical order of tables
  3872. comodel = model.env[self.comodel_name]
  3873. if not self.relation:
  3874. tables = sorted([model._table, comodel._table])
  3875. assert tables[0] != tables[1], \
  3876. "%s: Implicit/canonical naming of many2many relationship " \
  3877. "table is not possible when source and destination models " \
  3878. "are the same" % self
  3879. self.relation = '%s_%s_rel' % tuple(tables)
  3880. if not self.column1:
  3881. self.column1 = '%s_id' % model._table
  3882. if not self.column2:
  3883. self.column2 = '%s_id' % comodel._table
  3884. # check validity of table name
  3885. check_pg_name(self.relation)
  3886. else:
  3887. self.relation = self.column1 = self.column2 = None
  3888. if self.relation:
  3889. m2m = model.pool._m2m
  3890. # check whether other fields use the same schema
  3891. fields = m2m[(self.relation, self.column1, self.column2)]
  3892. for field in fields:
  3893. if ( # same model: relation parameters must be explicit
  3894. self.model_name == field.model_name and
  3895. self.comodel_name == field.comodel_name and
  3896. self._explicit and field._explicit
  3897. ) or ( # different models: one model must be _auto=False
  3898. self.model_name != field.model_name and
  3899. not (model._auto and model.env[field.model_name]._auto)
  3900. ):
  3901. continue
  3902. msg = "Many2many fields %s and %s use the same table and columns"
  3903. raise TypeError(msg % (self, field))
  3904. fields.append(self)
  3905. # retrieve inverse fields, and link them in field_inverses
  3906. for field in m2m[(self.relation, self.column2, self.column1)]:
  3907. model.pool.field_inverses.add(self, field)
  3908. model.pool.field_inverses.add(field, self)
  3909. def update_db(self, model, columns):
  3910. cr = model._cr
  3911. # Do not reflect relations for custom fields, as they do not belong to a
  3912. # module. They are automatically removed when dropping the corresponding
  3913. # 'ir.model.field'.
  3914. if not self.manual:
  3915. model.pool.post_init(model.env['ir.model.relation']._reflect_relation,
  3916. model, self.relation, self._module)
  3917. comodel = model.env[self.comodel_name]
  3918. if not sql.table_exists(cr, self.relation):
  3919. query = """
  3920. CREATE TABLE "{rel}" ("{id1}" INTEGER NOT NULL,
  3921. "{id2}" INTEGER NOT NULL,
  3922. PRIMARY KEY("{id1}","{id2}"));
  3923. COMMENT ON TABLE "{rel}" IS %s;
  3924. CREATE INDEX ON "{rel}" ("{id2}","{id1}");
  3925. """.format(rel=self.relation, id1=self.column1, id2=self.column2)
  3926. cr.execute(query, ['RELATION BETWEEN %s AND %s' % (model._table, comodel._table)])
  3927. _schema.debug("Create table %r: m2m relation between %r and %r", self.relation, model._table, comodel._table)
  3928. model.pool.post_init(self.update_db_foreign_keys, model)
  3929. return True
  3930. model.pool.post_init(self.update_db_foreign_keys, model)
  3931. def update_db_foreign_keys(self, model):
  3932. """ Add the foreign keys corresponding to the field's relation table. """
  3933. comodel = model.env[self.comodel_name]
  3934. if model._is_an_ordinary_table():
  3935. model.pool.add_foreign_key(
  3936. self.relation, self.column1, model._table, 'id', 'cascade',
  3937. model, self._module, force=False,
  3938. )
  3939. if comodel._is_an_ordinary_table():
  3940. model.pool.add_foreign_key(
  3941. self.relation, self.column2, comodel._table, 'id', self.ondelete,
  3942. model, self._module,
  3943. )
  3944. @property
  3945. def groupable(self):
  3946. return self.store
  3947. def read(self, records):
  3948. context = {'active_test': False}
  3949. context.update(self.context)
  3950. comodel = records.env[self.comodel_name].with_context(**context)
  3951. domain = self.get_domain_list(records)
  3952. comodel._flush_search(domain)
  3953. wquery = comodel._where_calc(domain)
  3954. comodel._apply_ir_rules(wquery, 'read')
  3955. order_by = comodel._generate_order_by(None, wquery)
  3956. from_c, where_c, where_params = wquery.get_sql()
  3957. query = """ SELECT {rel}.{id1}, {rel}.{id2} FROM {rel}, {from_c}
  3958. WHERE {where_c} AND {rel}.{id1} IN %s AND {rel}.{id2} = {tbl}.id
  3959. {order_by}
  3960. """.format(rel=self.relation, id1=self.column1, id2=self.column2,
  3961. tbl=comodel._table, from_c=from_c, where_c=where_c or '1=1',
  3962. order_by=order_by)
  3963. where_params.append(tuple(records.ids))
  3964. # retrieve lines and group them by record
  3965. group = defaultdict(list)
  3966. records._cr.execute(query, where_params)
  3967. for row in records._cr.fetchall():
  3968. group[row[0]].append(row[1])
  3969. # store result in cache
  3970. values = [tuple(group[id_]) for id_ in records._ids]
  3971. records.env.cache.insert_missing(records, self, values)
  3972. def write_real(self, records_commands_list, create=False):
  3973. # records_commands_list = [(records, commands), ...]
  3974. if not records_commands_list:
  3975. return
  3976. model = records_commands_list[0][0].browse()
  3977. comodel = model.env[self.comodel_name].with_context(**self.context)
  3978. cr = model.env.cr
  3979. # determine old and new relation {x: ys}
  3980. set = OrderedSet
  3981. ids = set(rid for recs, cs in records_commands_list for rid in recs.ids)
  3982. records = model.browse(ids)
  3983. if self.store:
  3984. # Using `record[self.name]` generates 2 SQL queries when the value
  3985. # is not in cache: one that actually checks access rules for
  3986. # records, and the other one fetching the actual data. We use
  3987. # `self.read` instead to shortcut the first query.
  3988. missing_ids = list(records.env.cache.get_missing_ids(records, self))
  3989. if missing_ids:
  3990. self.read(records.browse(missing_ids))
  3991. # determine new relation {x: ys}
  3992. old_relation = {record.id: set(record[self.name]._ids) for record in records}
  3993. new_relation = {x: set(ys) for x, ys in old_relation.items()}
  3994. # operations on new relation
  3995. def relation_add(xs, y):
  3996. for x in xs:
  3997. new_relation[x].add(y)
  3998. def relation_remove(xs, y):
  3999. for x in xs:
  4000. new_relation[x].discard(y)
  4001. def relation_set(xs, ys):
  4002. for x in xs:
  4003. new_relation[x] = set(ys)
  4004. def relation_delete(ys):
  4005. # the pairs (x, y) have been cascade-deleted from relation
  4006. for ys1 in old_relation.values():
  4007. ys1 -= ys
  4008. for ys1 in new_relation.values():
  4009. ys1 -= ys
  4010. for recs, commands in records_commands_list:
  4011. to_create = [] # line vals to create
  4012. to_delete = [] # line ids to delete
  4013. for command in (commands or ()):
  4014. if not isinstance(command, (list, tuple)) or not command:
  4015. continue
  4016. if command[0] == Command.CREATE:
  4017. to_create.append((recs._ids, command[2]))
  4018. elif command[0] == Command.UPDATE:
  4019. comodel.browse(command[1]).write(command[2])
  4020. elif command[0] == Command.DELETE:
  4021. to_delete.append(command[1])
  4022. elif command[0] == Command.UNLINK:
  4023. relation_remove(recs._ids, command[1])
  4024. elif command[0] == Command.LINK:
  4025. relation_add(recs._ids, command[1])
  4026. elif command[0] in (Command.CLEAR, Command.SET):
  4027. # new lines must no longer be linked to records
  4028. to_create = [(set(ids) - set(recs._ids), vals) for (ids, vals) in to_create]
  4029. relation_set(recs._ids, command[2] if command[0] == Command.SET else ())
  4030. if to_create:
  4031. # create lines in batch, and link them
  4032. lines = comodel.create([vals for ids, vals in to_create])
  4033. for line, (ids, vals) in zip(lines, to_create):
  4034. relation_add(ids, line.id)
  4035. if to_delete:
  4036. # delete lines in batch
  4037. comodel.browse(to_delete).unlink()
  4038. relation_delete(to_delete)
  4039. # update the cache of self
  4040. cache = records.env.cache
  4041. for record in records:
  4042. cache.set(record, self, tuple(new_relation[record.id]))
  4043. # determine the corecords for which the relation has changed
  4044. modified_corecord_ids = set()
  4045. # process pairs to add (beware of duplicates)
  4046. pairs = [(x, y) for x, ys in new_relation.items() for y in ys - old_relation[x]]
  4047. if pairs:
  4048. if self.store:
  4049. query = "INSERT INTO {} ({}, {}) VALUES {} ON CONFLICT DO NOTHING".format(
  4050. self.relation, self.column1, self.column2, ", ".join(["%s"] * len(pairs)),
  4051. )
  4052. cr.execute(query, pairs)
  4053. # update the cache of inverse fields
  4054. y_to_xs = defaultdict(set)
  4055. for x, y in pairs:
  4056. y_to_xs[y].add(x)
  4057. modified_corecord_ids.add(y)
  4058. for invf in records.pool.field_inverses[self]:
  4059. domain = invf.get_domain_list(comodel)
  4060. valid_ids = set(records.filtered_domain(domain)._ids)
  4061. if not valid_ids:
  4062. continue
  4063. for y, xs in y_to_xs.items():
  4064. corecord = comodel.browse(y)
  4065. try:
  4066. ids0 = cache.get(corecord, invf)
  4067. ids1 = tuple(set(ids0) | (xs & valid_ids))
  4068. cache.set(corecord, invf, ids1)
  4069. except KeyError:
  4070. pass
  4071. # process pairs to remove
  4072. pairs = [(x, y) for x, ys in old_relation.items() for y in ys - new_relation[x]]
  4073. if pairs:
  4074. y_to_xs = defaultdict(set)
  4075. for x, y in pairs:
  4076. y_to_xs[y].add(x)
  4077. modified_corecord_ids.add(y)
  4078. if self.store:
  4079. # express pairs as the union of cartesian products:
  4080. # pairs = [(1, 11), (1, 12), (1, 13), (2, 11), (2, 12), (2, 14)]
  4081. # -> y_to_xs = {11: {1, 2}, 12: {1, 2}, 13: {1}, 14: {2}}
  4082. # -> xs_to_ys = {{1, 2}: {11, 12}, {2}: {14}, {1}: {13}}
  4083. xs_to_ys = defaultdict(set)
  4084. for y, xs in y_to_xs.items():
  4085. xs_to_ys[frozenset(xs)].add(y)
  4086. # delete the rows where (id1 IN xs AND id2 IN ys) OR ...
  4087. COND = "{} IN %s AND {} IN %s".format(self.column1, self.column2)
  4088. query = "DELETE FROM {} WHERE {}".format(
  4089. self.relation, " OR ".join([COND] * len(xs_to_ys)),
  4090. )
  4091. params = [arg for xs, ys in xs_to_ys.items() for arg in [tuple(xs), tuple(ys)]]
  4092. cr.execute(query, params)
  4093. # update the cache of inverse fields
  4094. for invf in records.pool.field_inverses[self]:
  4095. for y, xs in y_to_xs.items():
  4096. corecord = comodel.browse(y)
  4097. try:
  4098. ids0 = cache.get(corecord, invf)
  4099. ids1 = tuple(id_ for id_ in ids0 if id_ not in xs)
  4100. cache.set(corecord, invf, ids1)
  4101. except KeyError:
  4102. pass
  4103. if modified_corecord_ids:
  4104. # trigger the recomputation of fields that depend on the inverse
  4105. # fields of self on the modified corecords
  4106. corecords = comodel.browse(modified_corecord_ids)
  4107. corecords.modified([
  4108. invf.name
  4109. for invf in model.pool.field_inverses[self]
  4110. if invf.model_name == self.comodel_name
  4111. ])
  4112. return records.filtered(
  4113. lambda record: new_relation[record.id] != old_relation[record.id]
  4114. )
  4115. def write_new(self, records_commands_list):
  4116. """ Update self on new records. """
  4117. if not records_commands_list:
  4118. return
  4119. model = records_commands_list[0][0].browse()
  4120. comodel = model.env[self.comodel_name].with_context(**self.context)
  4121. new = lambda id_: id_ and NewId(id_)
  4122. # determine old and new relation {x: ys}
  4123. set = OrderedSet
  4124. old_relation = {record.id: set(record[self.name]._ids) for records, _ in records_commands_list for record in records}
  4125. new_relation = {x: set(ys) for x, ys in old_relation.items()}
  4126. ids = set(old_relation.keys())
  4127. records = model.browse(ids)
  4128. for recs, commands in records_commands_list:
  4129. for command in commands:
  4130. if not isinstance(command, (list, tuple)) or not command:
  4131. continue
  4132. if command[0] == Command.CREATE:
  4133. line_id = comodel.new(command[2], ref=command[1]).id
  4134. for line_ids in new_relation.values():
  4135. line_ids.add(line_id)
  4136. elif command[0] == Command.UPDATE:
  4137. line_id = new(command[1])
  4138. comodel.browse([line_id]).update(command[2])
  4139. elif command[0] == Command.DELETE:
  4140. line_id = new(command[1])
  4141. for line_ids in new_relation.values():
  4142. line_ids.discard(line_id)
  4143. elif command[0] == Command.UNLINK:
  4144. line_id = new(command[1])
  4145. for line_ids in new_relation.values():
  4146. line_ids.discard(line_id)
  4147. elif command[0] == Command.LINK:
  4148. line_id = new(command[1])
  4149. for line_ids in new_relation.values():
  4150. line_ids.add(line_id)
  4151. elif command[0] in (Command.CLEAR, Command.SET):
  4152. # new lines must no longer be linked to records
  4153. line_ids = command[2] if command[0] == Command.SET else ()
  4154. line_ids = set(new(line_id) for line_id in line_ids)
  4155. for id_ in recs._ids:
  4156. new_relation[id_] = set(line_ids)
  4157. if new_relation == old_relation:
  4158. return records.browse()
  4159. # update the cache of self
  4160. cache = records.env.cache
  4161. for record in records:
  4162. cache.set(record, self, tuple(new_relation[record.id]))
  4163. # determine the corecords for which the relation has changed
  4164. modified_corecord_ids = set()
  4165. # process pairs to add (beware of duplicates)
  4166. pairs = [(x, y) for x, ys in new_relation.items() for y in ys - old_relation[x]]
  4167. if pairs:
  4168. # update the cache of inverse fields
  4169. y_to_xs = defaultdict(set)
  4170. for x, y in pairs:
  4171. y_to_xs[y].add(x)
  4172. modified_corecord_ids.add(y)
  4173. for invf in records.pool.field_inverses[self]:
  4174. domain = invf.get_domain_list(comodel)
  4175. valid_ids = set(records.filtered_domain(domain)._ids)
  4176. if not valid_ids:
  4177. continue
  4178. for y, xs in y_to_xs.items():
  4179. corecord = comodel.browse([y])
  4180. try:
  4181. ids0 = cache.get(corecord, invf)
  4182. ids1 = tuple(set(ids0) | (xs & valid_ids))
  4183. cache.set(corecord, invf, ids1)
  4184. except KeyError:
  4185. pass
  4186. # process pairs to remove
  4187. pairs = [(x, y) for x, ys in old_relation.items() for y in ys - new_relation[x]]
  4188. if pairs:
  4189. # update the cache of inverse fields
  4190. y_to_xs = defaultdict(set)
  4191. for x, y in pairs:
  4192. y_to_xs[y].add(x)
  4193. modified_corecord_ids.add(y)
  4194. for invf in records.pool.field_inverses[self]:
  4195. for y, xs in y_to_xs.items():
  4196. corecord = comodel.browse([y])
  4197. try:
  4198. ids0 = cache.get(corecord, invf)
  4199. ids1 = tuple(id_ for id_ in ids0 if id_ not in xs)
  4200. cache.set(corecord, invf, ids1)
  4201. except KeyError:
  4202. pass
  4203. if modified_corecord_ids:
  4204. # trigger the recomputation of fields that depend on the inverse
  4205. # fields of self on the modified corecords
  4206. corecords = comodel.browse(modified_corecord_ids)
  4207. corecords.modified([
  4208. invf.name
  4209. for invf in model.pool.field_inverses[self]
  4210. if invf.model_name == self.comodel_name
  4211. ])
  4212. return records.filtered(
  4213. lambda record: new_relation[record.id] != old_relation[record.id]
  4214. )
  4215. class Id(Field):
  4216. """ Special case for field 'id'. """
  4217. type = 'integer'
  4218. column_type = ('int4', 'int4')
  4219. string = 'ID'
  4220. store = True
  4221. readonly = True
  4222. prefetch = False
  4223. def update_db(self, model, columns):
  4224. pass # this column is created with the table
  4225. def __get__(self, record, owner):
  4226. if record is None:
  4227. return self # the field is accessed through the class owner
  4228. # the code below is written to make record.id as quick as possible
  4229. ids = record._ids
  4230. size = len(ids)
  4231. if size == 0:
  4232. return False
  4233. elif size == 1:
  4234. return ids[0]
  4235. raise ValueError("Expected singleton: %s" % record)
  4236. def __set__(self, record, value):
  4237. raise TypeError("field 'id' cannot be assigned")
  4238. class PrefetchMany2one:
  4239. """ Iterable for the values of a many2one field on the prefetch set of a given record. """
  4240. __slots__ = 'record', 'field'
  4241. def __init__(self, record, field):
  4242. self.record = record
  4243. self.field = field
  4244. def __iter__(self):
  4245. records = self.record.browse(self.record._prefetch_ids)
  4246. ids = self.record.env.cache.get_values(records, self.field)
  4247. return unique(id_ for id_ in ids if id_ is not None)
  4248. def __reversed__(self):
  4249. records = self.record.browse(reversed(self.record._prefetch_ids))
  4250. ids = self.record.env.cache.get_values(records, self.field)
  4251. return unique(id_ for id_ in ids if id_ is not None)
  4252. class PrefetchX2many:
  4253. """ Iterable for the values of an x2many field on the prefetch set of a given record. """
  4254. __slots__ = 'record', 'field'
  4255. def __init__(self, record, field):
  4256. self.record = record
  4257. self.field = field
  4258. def __iter__(self):
  4259. records = self.record.browse(self.record._prefetch_ids)
  4260. ids_list = self.record.env.cache.get_values(records, self.field)
  4261. return unique(id_ for ids in ids_list for id_ in ids)
  4262. def __reversed__(self):
  4263. records = self.record.browse(reversed(self.record._prefetch_ids))
  4264. ids_list = self.record.env.cache.get_values(records, self.field)
  4265. return unique(id_ for ids in ids_list for id_ in ids)
  4266. def apply_required(model, field_name):
  4267. """ Set a NOT NULL constraint on the given field, if necessary. """
  4268. # At the time this function is called, the model's _fields may have been reset, although
  4269. # the model's class is still the same. Retrieve the field to see whether the NOT NULL
  4270. # constraint still applies
  4271. field = model._fields[field_name]
  4272. if field.store and field.required:
  4273. sql.set_not_null(model.env.cr, model._table, field_name)
  4274. # imported here to avoid dependency cycle issues
  4275. # pylint: disable=wrong-import-position
  4276. from .exceptions import AccessError, MissingError, UserError
  4277. from .models import (
  4278. check_pg_name, expand_ids, is_definition_class,
  4279. BaseModel, IdType, NewId, PREFETCH_MAX,
  4280. )