models.py 297 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. """
  4. Object Relational Mapping module:
  5. * Hierarchical structure
  6. * Constraints consistency and validation
  7. * Object metadata depends on its status
  8. * Optimised processing by complex query (multiple actions at once)
  9. * Default field values
  10. * Permissions optimisation
  11. * Persistent object: DB postgresql
  12. * Data conversion
  13. * Multi-level caching system
  14. * Two different inheritance mechanisms
  15. * Rich set of field types:
  16. - classical (varchar, integer, boolean, ...)
  17. - relational (one2many, many2one, many2many)
  18. - functional
  19. """
  20. import collections
  21. import contextlib
  22. import copy
  23. import datetime
  24. import dateutil
  25. import fnmatch
  26. import functools
  27. import inspect
  28. import itertools
  29. import io
  30. import logging
  31. import operator
  32. import pytz
  33. import re
  34. import uuid
  35. import warnings
  36. from collections import defaultdict, OrderedDict
  37. from collections.abc import MutableMapping
  38. from contextlib import closing
  39. from inspect import getmembers, currentframe
  40. from operator import attrgetter, itemgetter
  41. import babel.dates
  42. import dateutil.relativedelta
  43. import psycopg2
  44. import psycopg2.extensions
  45. from psycopg2.extras import Json
  46. import odoo
  47. from . import SUPERUSER_ID
  48. from . import api
  49. from . import tools
  50. from .exceptions import AccessError, MissingError, ValidationError, UserError
  51. from .tools import (
  52. clean_context, config, CountingStream, date_utils, discardattr,
  53. DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT, frozendict,
  54. get_lang, LastOrderedSet, lazy_classproperty, OrderedSet, ormcache,
  55. partition, populate, Query, ReversedIterable, split_every, unique,
  56. )
  57. from .tools.func import frame_codeinfo
  58. from .tools.lru import LRU
  59. from .tools.translate import _, _lt
  60. _logger = logging.getLogger(__name__)
  61. _unlink = logging.getLogger(__name__ + '.unlink')
  62. regex_order = re.compile(r'^(\s*([a-z0-9:_]+|"[a-z0-9:_]+")(\.id)?(\s+(desc|asc))?\s*(,|$))+(?<!,)$', re.I)
  63. regex_object_name = re.compile(r'^[a-z0-9_.]+$')
  64. regex_pg_name = re.compile(r'^[a-z_][a-z0-9_$]*$', re.I)
  65. regex_field_agg = re.compile(r'(\w+)(?::(\w+)(?:\((\w+)\))?)?')
  66. AUTOINIT_RECALCULATE_STORED_FIELDS = 1000
  67. INSERT_BATCH_SIZE = 100
  68. SQL_DEFAULT = psycopg2.extensions.AsIs("DEFAULT")
  69. def check_object_name(name):
  70. """ Check if the given name is a valid model name.
  71. The _name attribute in osv and osv_memory object is subject to
  72. some restrictions. This function returns True or False whether
  73. the given name is allowed or not.
  74. TODO: this is an approximation. The goal in this approximation
  75. is to disallow uppercase characters (in some places, we quote
  76. table/column names and in other not, which leads to this kind
  77. of errors:
  78. psycopg2.ProgrammingError: relation "xxx" does not exist).
  79. The same restriction should apply to both osv and osv_memory
  80. objects for consistency.
  81. """
  82. if regex_object_name.match(name) is None:
  83. return False
  84. return True
  85. def raise_on_invalid_object_name(name):
  86. if not check_object_name(name):
  87. msg = "The _name attribute %s is not valid." % name
  88. raise ValueError(msg)
  89. def check_pg_name(name):
  90. """ Check whether the given name is a valid PostgreSQL identifier name. """
  91. if not regex_pg_name.match(name):
  92. raise ValidationError("Invalid characters in table name %r" % name)
  93. if len(name) > 63:
  94. raise ValidationError("Table name %r is too long" % name)
  95. # match private methods, to prevent their remote invocation
  96. regex_private = re.compile(r'^(_.*|init)$')
  97. def check_method_name(name):
  98. """ Raise an ``AccessError`` if ``name`` is a private method name. """
  99. if regex_private.match(name):
  100. raise AccessError(_('Private methods (such as %s) cannot be called remotely.', name))
  101. def fix_import_export_id_paths(fieldname):
  102. """
  103. Fixes the id fields in import and exports, and splits field paths
  104. on '/'.
  105. :param str fieldname: name of the field to import/export
  106. :return: split field name
  107. :rtype: list of str
  108. """
  109. fixed_db_id = re.sub(r'([^/])\.id', r'\1/.id', fieldname)
  110. fixed_external_id = re.sub(r'([^/]):id', r'\1/id', fixed_db_id)
  111. return fixed_external_id.split('/')
  112. class MetaModel(api.Meta):
  113. """ The metaclass of all model classes.
  114. Its main purpose is to register the models per module.
  115. """
  116. module_to_models = defaultdict(list)
  117. def __new__(meta, name, bases, attrs):
  118. # this prevents assignment of non-fields on recordsets
  119. attrs.setdefault('__slots__', ())
  120. # this collects the fields defined on the class (via Field.__set_name__())
  121. attrs.setdefault('_field_definitions', [])
  122. if attrs.get('_register', True):
  123. # determine '_module'
  124. if '_module' not in attrs:
  125. module = attrs['__module__']
  126. assert module.startswith('odoo.addons.'), \
  127. f"Invalid import of {module}.{name}, it should start with 'odoo.addons'."
  128. attrs['_module'] = module.split('.')[2]
  129. # determine model '_name' and normalize '_inherits'
  130. inherit = attrs.get('_inherit', ())
  131. if isinstance(inherit, str):
  132. inherit = attrs['_inherit'] = [inherit]
  133. if '_name' not in attrs:
  134. attrs['_name'] = inherit[0] if len(inherit) == 1 else name
  135. return super().__new__(meta, name, bases, attrs)
  136. def __init__(self, name, bases, attrs):
  137. super().__init__(name, bases, attrs)
  138. if '__init__' in attrs and len(inspect.signature(attrs['__init__']).parameters) != 4:
  139. _logger.warning("The method %s.__init__ doesn't match the new signature in module %s", name, attrs.get('__module__'))
  140. if not attrs.get('_register', True):
  141. return
  142. # Remember which models to instantiate for this module.
  143. if self._module:
  144. self.module_to_models[self._module].append(self)
  145. if not self._abstract and self._name not in self._inherit:
  146. # this class defines a model: add magic fields
  147. def add(name, field):
  148. setattr(self, name, field)
  149. field.__set_name__(self, name)
  150. def add_default(name, field):
  151. if name not in attrs:
  152. setattr(self, name, field)
  153. field.__set_name__(self, name)
  154. add('id', fields.Id(automatic=True))
  155. add(self.CONCURRENCY_CHECK_FIELD, fields.Datetime(
  156. string='Last Modified on', automatic=True,
  157. compute='_compute_concurrency_field', compute_sudo=False))
  158. add_default('display_name', fields.Char(
  159. string='Display Name', automatic=True, compute='_compute_display_name'))
  160. if attrs.get('_log_access', self._auto):
  161. add_default('create_uid', fields.Many2one(
  162. 'res.users', string='Created by', automatic=True, readonly=True))
  163. add_default('create_date', fields.Datetime(
  164. string='Created on', automatic=True, readonly=True))
  165. add_default('write_uid', fields.Many2one(
  166. 'res.users', string='Last Updated by', automatic=True, readonly=True))
  167. add_default('write_date', fields.Datetime(
  168. string='Last Updated on', automatic=True, readonly=True))
  169. class NewId(object):
  170. """ Pseudo-ids for new records, encapsulating an optional origin id (actual
  171. record id) and an optional reference (any value).
  172. """
  173. __slots__ = ['origin', 'ref']
  174. def __init__(self, origin=None, ref=None):
  175. self.origin = origin
  176. self.ref = ref
  177. def __bool__(self):
  178. return False
  179. def __eq__(self, other):
  180. return isinstance(other, NewId) and (
  181. (self.origin and other.origin and self.origin == other.origin)
  182. or (self.ref and other.ref and self.ref == other.ref)
  183. )
  184. def __hash__(self):
  185. return hash(self.origin or self.ref or id(self))
  186. def __repr__(self):
  187. return (
  188. "<NewId origin=%r>" % self.origin if self.origin else
  189. "<NewId ref=%r>" % self.ref if self.ref else
  190. "<NewId 0x%x>" % id(self)
  191. )
  192. def __str__(self):
  193. if self.origin or self.ref:
  194. id_part = repr(self.origin or self.ref)
  195. else:
  196. id_part = hex(id(self))
  197. return "NewId_%s" % id_part
  198. def origin_ids(ids):
  199. """ Return an iterator over the origin ids corresponding to ``ids``.
  200. Actual ids are returned as is, and ids without origin are not returned.
  201. """
  202. return ((id_ or id_.origin) for id_ in ids if (id_ or getattr(id_, "origin", None)))
  203. class OriginIds:
  204. """ A reversible iterable returning the origin ids of a collection of ``ids``. """
  205. __slots__ = ['ids']
  206. def __init__(self, ids):
  207. self.ids = ids
  208. def __iter__(self):
  209. return origin_ids(self.ids)
  210. def __reversed__(self):
  211. return origin_ids(reversed(self.ids))
  212. def expand_ids(id0, ids):
  213. """ Return an iterator of unique ids from the concatenation of ``[id0]`` and
  214. ``ids``, and of the same kind (all real or all new).
  215. """
  216. yield id0
  217. seen = {id0}
  218. kind = bool(id0)
  219. for id_ in ids:
  220. if id_ not in seen and bool(id_) == kind:
  221. yield id_
  222. seen.add(id_)
  223. IdType = (int, NewId)
  224. # maximum number of prefetched records
  225. PREFETCH_MAX = 1000
  226. # special columns automatically created by the ORM
  227. LOG_ACCESS_COLUMNS = ['create_uid', 'create_date', 'write_uid', 'write_date']
  228. MAGIC_COLUMNS = ['id'] + LOG_ACCESS_COLUMNS
  229. # valid SQL aggregation functions
  230. VALID_AGGREGATE_FUNCTIONS = {
  231. 'array_agg', 'count', 'count_distinct',
  232. 'bool_and', 'bool_or', 'max', 'min', 'avg', 'sum',
  233. }
  234. # THE DEFINITION AND REGISTRY CLASSES
  235. #
  236. # The framework deals with two kinds of classes for models: the "definition"
  237. # classes and the "registry" classes.
  238. #
  239. # The "definition" classes are the ones defined in modules source code: they
  240. # define models and extend them. Those classes are essentially "static", for
  241. # whatever that means in Python. The only exception is custom models: their
  242. # definition class is created dynamically.
  243. #
  244. # The "registry" classes are the ones you find in the registry. They are the
  245. # actual classes of the recordsets of their model. The "registry" class of a
  246. # model is created dynamically when the registry is built. It inherits (in the
  247. # Python sense) from all the definition classes of the model, and possibly other
  248. # registry classes (when the model inherits from another model). It also
  249. # carries model metadata inferred from its parent classes.
  250. #
  251. #
  252. # THE REGISTRY CLASS OF A MODEL
  253. #
  254. # In the simplest case, a model's registry class inherits from all the classes
  255. # that define the model in a flat hierarchy. Consider the model definition
  256. # below. The registry class of model 'a' inherits from the definition classes
  257. # A1, A2, A3, in reverse order, to match the expected overriding order. The
  258. # registry class carries inferred metadata that is shared between all the
  259. # model's instances for a given registry.
  260. #
  261. # class A1(Model): Model
  262. # _name = 'a' / | \
  263. # A3 A2 A1 <- definition classes
  264. # class A2(Model): \ | /
  265. # _inherit = 'a' a <- registry class: registry['a']
  266. # |
  267. # class A3(Model): records <- model instances, like env['a']
  268. # _inherit = 'a'
  269. #
  270. # Note that when the model inherits from another model, we actually make the
  271. # registry classes inherit from each other, so that extensions to an inherited
  272. # model are visible in the registry class of the child model, like in the
  273. # following example.
  274. #
  275. # class A1(Model):
  276. # _name = 'a' Model
  277. # / / \ \
  278. # class B1(Model): / / \ \
  279. # _name = 'b' / A2 A1 \
  280. # B2 \ / B1
  281. # class B2(Model): \ \ / /
  282. # _name = 'b' \ a /
  283. # _inherit = ['a', 'b'] \ | /
  284. # \ | /
  285. # class A2(Model): b
  286. # _inherit = 'a'
  287. #
  288. #
  289. # THE FIELDS OF A MODEL
  290. #
  291. # The fields of a model are given by the model's definition classes, inherited
  292. # models ('_inherit' and '_inherits') and other parties, like custom fields.
  293. # Note that a field can be partially overridden when it appears on several
  294. # definition classes of its model. In that case, the field's final definition
  295. # depends on the presence or absence of each definition class, which itself
  296. # depends on the modules loaded in the registry.
  297. #
  298. # By design, the registry class has access to all the fields on the model's
  299. # definition classes. When possible, the field is used directly from the
  300. # model's registry class. There are a number of cases where the field cannot be
  301. # used directly:
  302. # - the field is related (and bits may not be shared);
  303. # - the field is overridden on definition classes;
  304. # - the field is defined for another model (and accessible by mixin).
  305. #
  306. # The last case prevents sharing the field, because the field object is specific
  307. # to a model, and is used as a key in several key dictionaries, like the record
  308. # cache and pending computations.
  309. #
  310. # Setting up a field on its definition class helps saving memory and time.
  311. # Indeed, when sharing is possible, the field's setup is almost entirely done
  312. # where the field was defined. It is thus done when the definition class was
  313. # created, and it may be reused across registries.
  314. #
  315. # In the example below, the field 'foo' appears once on its model's definition
  316. # classes. Assuming that it is not related, that field can be set up directly
  317. # on its definition class. If the model appears in several registries, the
  318. # field 'foo' is effectively shared across registries.
  319. #
  320. # class A1(Model): Model
  321. # _name = 'a' / \
  322. # foo = ... / \
  323. # bar = ... A2 A1
  324. # bar foo, bar
  325. # class A2(Model): \ /
  326. # _inherit = 'a' \ /
  327. # bar = ... a
  328. # bar
  329. #
  330. # On the other hand, the field 'bar' is overridden in its model's definition
  331. # classes. In that case, the framework recreates the field on the model's
  332. # registry class. The field's setup will be based on its definitions, and will
  333. # not be shared across registries.
  334. #
  335. # The so-called magic fields ('id', 'display_name', ...) used to be added on
  336. # registry classes. But doing so prevents them from being shared. So instead,
  337. # we add them on definition classes that define a model without extending it.
  338. # This increases the number of fields that are shared across registries.
  339. def is_definition_class(cls):
  340. """ Return whether ``cls`` is a model definition class. """
  341. return isinstance(cls, MetaModel) and getattr(cls, 'pool', None) is None
  342. def is_registry_class(cls):
  343. """ Return whether ``cls`` is a model registry class. """
  344. return getattr(cls, 'pool', None) is not None
  345. class BaseModel(metaclass=MetaModel):
  346. """Base class for Odoo models.
  347. Odoo models are created by inheriting one of the following:
  348. * :class:`Model` for regular database-persisted models
  349. * :class:`TransientModel` for temporary data, stored in the database but
  350. automatically vacuumed every so often
  351. * :class:`AbstractModel` for abstract super classes meant to be shared by
  352. multiple inheriting models
  353. The system automatically instantiates every model once per database. Those
  354. instances represent the available models on each database, and depend on
  355. which modules are installed on that database. The actual class of each
  356. instance is built from the Python classes that create and inherit from the
  357. corresponding model.
  358. Every model instance is a "recordset", i.e., an ordered collection of
  359. records of the model. Recordsets are returned by methods like
  360. :meth:`~.browse`, :meth:`~.search`, or field accesses. Records have no
  361. explicit representation: a record is represented as a recordset of one
  362. record.
  363. To create a class that should not be instantiated,
  364. the :attr:`~odoo.models.BaseModel._register` attribute may be set to False.
  365. """
  366. __slots__ = ['env', '_ids', '_prefetch_ids']
  367. _auto = False
  368. """Whether a database table should be created.
  369. If set to ``False``, override :meth:`~odoo.models.BaseModel.init`
  370. to create the database table.
  371. Automatically defaults to `True` for :class:`Model` and
  372. :class:`TransientModel`, `False` for :class:`AbstractModel`.
  373. .. tip:: To create a model without any table, inherit
  374. from :class:`~odoo.models.AbstractModel`.
  375. """
  376. _register = False #: registry visibility
  377. _abstract = True
  378. """ Whether the model is *abstract*.
  379. .. seealso:: :class:`AbstractModel`
  380. """
  381. _transient = False
  382. """ Whether the model is *transient*.
  383. .. seealso:: :class:`TransientModel`
  384. """
  385. _name = None #: the model name (in dot-notation, module namespace)
  386. _description = None #: the model's informal name
  387. _module = None #: the model's module (in the Odoo sense)
  388. _custom = False #: should be True for custom models only
  389. _inherit = ()
  390. """Python-inherited models:
  391. :type: str or list(str)
  392. .. note::
  393. * If :attr:`._name` is set, name(s) of parent models to inherit from
  394. * If :attr:`._name` is unset, name of a single model to extend in-place
  395. """
  396. _inherits = frozendict()
  397. """dictionary {'parent_model': 'm2o_field'} mapping the _name of the parent business
  398. objects to the names of the corresponding foreign key fields to use::
  399. _inherits = {
  400. 'a.model': 'a_field_id',
  401. 'b.model': 'b_field_id'
  402. }
  403. implements composition-based inheritance: the new model exposes all
  404. the fields of the inherited models but stores none of them:
  405. the values themselves remain stored on the linked record.
  406. .. warning::
  407. if multiple fields with the same name are defined in the
  408. :attr:`~odoo.models.Model._inherits`-ed models, the inherited field will
  409. correspond to the last one (in the inherits list order).
  410. """
  411. _table = None #: SQL table name used by model if :attr:`_auto`
  412. _table_query = None #: SQL expression of the table's content (optional)
  413. _sql_constraints = [] #: SQL constraints [(name, sql_def, message)]
  414. _rec_name = None #: field to use for labeling records, default: ``name``
  415. _rec_names_search = None #: fields to consider in ``name_search``
  416. _order = 'id' #: default order field for searching results
  417. _parent_name = 'parent_id' #: the many2one field used as parent field
  418. _parent_store = False
  419. """set to True to compute parent_path field.
  420. Alongside a :attr:`~.parent_path` field, sets up an indexed storage
  421. of the tree structure of records, to enable faster hierarchical queries
  422. on the records of the current model using the ``child_of`` and
  423. ``parent_of`` domain operators.
  424. """
  425. _active_name = None
  426. """field to use for active records, automatically set to either ``"active"``
  427. or ``"x_active"``.
  428. """
  429. _fold_name = 'fold' #: field to determine folded groups in kanban views
  430. _translate = True # False disables translations export for this model (Old API)
  431. _check_company_auto = False
  432. """On write and create, call ``_check_company`` to ensure companies
  433. consistency on the relational fields having ``check_company=True``
  434. as attribute.
  435. """
  436. _depends = frozendict()
  437. """dependencies of models backed up by SQL views
  438. ``{model_name: field_names}``, where ``field_names`` is an iterable.
  439. This is only used to determine the changes to flush to database before
  440. executing ``search()`` or ``read_group()``. It won't be used for cache
  441. invalidation or recomputing fields.
  442. """
  443. # default values for _transient_vacuum()
  444. _transient_max_count = lazy_classproperty(lambda _: config.get('osv_memory_count_limit'))
  445. "maximum number of transient records, unlimited if ``0``"
  446. _transient_max_hours = lazy_classproperty(lambda _: config.get('transient_age_limit'))
  447. "maximum idle lifetime (in hours), unlimited if ``0``"
  448. CONCURRENCY_CHECK_FIELD = '__last_update'
  449. def _valid_field_parameter(self, field, name):
  450. """ Return whether the given parameter name is valid for the field. """
  451. return name == 'related_sudo'
  452. @api.model
  453. def _add_field(self, name, field):
  454. """ Add the given ``field`` under the given ``name`` in the class """
  455. cls = type(self)
  456. # add field as an attribute and in cls._fields (for reflection)
  457. if not isinstance(getattr(cls, name, field), Field):
  458. _logger.warning("In model %r, field %r overriding existing value", cls._name, name)
  459. setattr(cls, name, field)
  460. field._toplevel = True
  461. field.__set_name__(cls, name)
  462. cls._fields[name] = field
  463. @api.model
  464. def _pop_field(self, name):
  465. """ Remove the field with the given ``name`` from the model.
  466. This method should only be used for manual fields.
  467. """
  468. cls = type(self)
  469. field = cls._fields.pop(name, None)
  470. discardattr(cls, name)
  471. if cls._rec_name == name:
  472. # fixup _rec_name and display_name's dependencies
  473. cls._rec_name = None
  474. if cls.display_name in cls.pool.field_depends:
  475. cls.pool.field_depends[cls.display_name] = tuple(
  476. dep for dep in cls.pool.field_depends[cls.display_name] if dep != name
  477. )
  478. return field
  479. @api.depends(lambda model: ('create_date', 'write_date') if model._log_access else ())
  480. def _compute_concurrency_field(self):
  481. fname = self.CONCURRENCY_CHECK_FIELD
  482. if self._log_access:
  483. for record in self:
  484. record[fname] = record.write_date or record.create_date or Datetime.now()
  485. else:
  486. self[fname] = odoo.fields.Datetime.now()
  487. #
  488. # Goal: try to apply inheritance at the instantiation level and
  489. # put objects in the pool var
  490. #
  491. @classmethod
  492. def _build_model(cls, pool, cr):
  493. """ Instantiate a given model in the registry.
  494. This method creates or extends a "registry" class for the given model.
  495. This "registry" class carries inferred model metadata, and inherits (in
  496. the Python sense) from all classes that define the model, and possibly
  497. other registry classes.
  498. """
  499. if getattr(cls, '_constraints', None):
  500. _logger.warning("Model attribute '_constraints' is no longer supported, "
  501. "please use @api.constrains on methods instead.")
  502. # Keep links to non-inherited constraints in cls; this is useful for
  503. # instance when exporting translations
  504. cls._local_sql_constraints = cls.__dict__.get('_sql_constraints', [])
  505. # all models except 'base' implicitly inherit from 'base'
  506. name = cls._name
  507. parents = list(cls._inherit)
  508. if name != 'base':
  509. parents.append('base')
  510. # create or retrieve the model's class
  511. if name in parents:
  512. if name not in pool:
  513. raise TypeError("Model %r does not exist in registry." % name)
  514. ModelClass = pool[name]
  515. ModelClass._build_model_check_base(cls)
  516. check_parent = ModelClass._build_model_check_parent
  517. else:
  518. ModelClass = type(name, (cls,), {
  519. '_name': name,
  520. '_register': False,
  521. '_original_module': cls._module,
  522. '_inherit_module': {}, # map parent to introducing module
  523. '_inherit_children': OrderedSet(), # names of children models
  524. '_inherits_children': set(), # names of children models
  525. '_fields': {}, # populated in _setup_base()
  526. })
  527. check_parent = cls._build_model_check_parent
  528. # determine all the classes the model should inherit from
  529. bases = LastOrderedSet([cls])
  530. for parent in parents:
  531. if parent not in pool:
  532. raise TypeError("Model %r inherits from non-existing model %r." % (name, parent))
  533. parent_class = pool[parent]
  534. if parent == name:
  535. for base in parent_class.__base_classes:
  536. bases.add(base)
  537. else:
  538. check_parent(cls, parent_class)
  539. bases.add(parent_class)
  540. ModelClass._inherit_module[parent] = cls._module
  541. parent_class._inherit_children.add(name)
  542. # ModelClass.__bases__ must be assigned those classes; however, this
  543. # operation is quite slow, so we do it once in method _prepare_setup()
  544. ModelClass.__base_classes = tuple(bases)
  545. # determine the attributes of the model's class
  546. ModelClass._build_model_attributes(pool)
  547. check_pg_name(ModelClass._table)
  548. # Transience
  549. if ModelClass._transient:
  550. assert ModelClass._log_access, \
  551. "TransientModels must have log_access turned on, " \
  552. "in order to implement their vacuum policy"
  553. # link the class to the registry, and update the registry
  554. ModelClass.pool = pool
  555. pool[name] = ModelClass
  556. return ModelClass
  557. @classmethod
  558. def _build_model_check_base(model_class, cls):
  559. """ Check whether ``model_class`` can be extended with ``cls``. """
  560. if model_class._abstract and not cls._abstract:
  561. msg = ("%s transforms the abstract model %r into a non-abstract model. "
  562. "That class should either inherit from AbstractModel, or set a different '_name'.")
  563. raise TypeError(msg % (cls, model_class._name))
  564. if model_class._transient != cls._transient:
  565. if model_class._transient:
  566. msg = ("%s transforms the transient model %r into a non-transient model. "
  567. "That class should either inherit from TransientModel, or set a different '_name'.")
  568. else:
  569. msg = ("%s transforms the model %r into a transient model. "
  570. "That class should either inherit from Model, or set a different '_name'.")
  571. raise TypeError(msg % (cls, model_class._name))
  572. @classmethod
  573. def _build_model_check_parent(model_class, cls, parent_class):
  574. """ Check whether ``model_class`` can inherit from ``parent_class``. """
  575. if model_class._abstract and not parent_class._abstract:
  576. msg = ("In %s, the abstract model %r cannot inherit from the non-abstract model %r.")
  577. raise TypeError(msg % (cls, model_class._name, parent_class._name))
  578. @classmethod
  579. def _build_model_attributes(cls, pool):
  580. """ Initialize base model attributes. """
  581. cls._description = cls._name
  582. cls._table = cls._name.replace('.', '_')
  583. cls._log_access = cls._auto
  584. inherits = {}
  585. depends = {}
  586. cls._sql_constraints = {}
  587. for base in reversed(cls.__base_classes):
  588. if is_definition_class(base):
  589. # the following attributes are not taken from registry classes
  590. if cls._name not in base._inherit and not base._description:
  591. _logger.warning("The model %s has no _description", cls._name)
  592. cls._description = base._description or cls._description
  593. cls._table = base._table or cls._table
  594. cls._log_access = getattr(base, '_log_access', cls._log_access)
  595. inherits.update(base._inherits)
  596. for mname, fnames in base._depends.items():
  597. depends.setdefault(mname, []).extend(fnames)
  598. for cons in base._sql_constraints:
  599. cls._sql_constraints[cons[0]] = cons
  600. cls._sql_constraints = list(cls._sql_constraints.values())
  601. # avoid assigning an empty dict to save memory
  602. if inherits:
  603. cls._inherits = inherits
  604. if depends:
  605. cls._depends = depends
  606. # update _inherits_children of parent models
  607. for parent_name in cls._inherits:
  608. pool[parent_name]._inherits_children.add(cls._name)
  609. # recompute attributes of _inherit_children models
  610. for child_name in cls._inherit_children:
  611. child_class = pool[child_name]
  612. child_class._build_model_attributes(pool)
  613. @classmethod
  614. def _init_constraints_onchanges(cls):
  615. # store list of sql constraint qualified names
  616. for (key, _, _) in cls._sql_constraints:
  617. cls.pool._sql_constraints.add(cls._table + '_' + key)
  618. # reset properties memoized on cls
  619. cls._constraint_methods = BaseModel._constraint_methods
  620. cls._ondelete_methods = BaseModel._ondelete_methods
  621. cls._onchange_methods = BaseModel._onchange_methods
  622. @property
  623. def _constraint_methods(self):
  624. """ Return a list of methods implementing Python constraints. """
  625. def is_constraint(func):
  626. return callable(func) and hasattr(func, '_constrains')
  627. def wrap(func, names):
  628. # wrap func into a proxy function with explicit '_constrains'
  629. @api.constrains(*names)
  630. def wrapper(self):
  631. return func(self)
  632. return wrapper
  633. cls = type(self)
  634. methods = []
  635. for attr, func in getmembers(cls, is_constraint):
  636. if callable(func._constrains):
  637. func = wrap(func, func._constrains(self))
  638. for name in func._constrains:
  639. field = cls._fields.get(name)
  640. if not field:
  641. _logger.warning("method %s.%s: @constrains parameter %r is not a field name", cls._name, attr, name)
  642. elif not (field.store or field.inverse or field.inherited):
  643. _logger.warning("method %s.%s: @constrains parameter %r is not writeable", cls._name, attr, name)
  644. methods.append(func)
  645. # optimization: memoize result on cls, it will not be recomputed
  646. cls._constraint_methods = methods
  647. return methods
  648. @property
  649. def _ondelete_methods(self):
  650. """ Return a list of methods implementing checks before unlinking. """
  651. def is_ondelete(func):
  652. return callable(func) and hasattr(func, '_ondelete')
  653. cls = type(self)
  654. methods = [func for _, func in getmembers(cls, is_ondelete)]
  655. # optimization: memoize results on cls, it will not be recomputed
  656. cls._ondelete_methods = methods
  657. return methods
  658. @property
  659. def _onchange_methods(self):
  660. """ Return a dictionary mapping field names to onchange methods. """
  661. def is_onchange(func):
  662. return callable(func) and hasattr(func, '_onchange')
  663. # collect onchange methods on the model's class
  664. cls = type(self)
  665. methods = defaultdict(list)
  666. for attr, func in getmembers(cls, is_onchange):
  667. missing = []
  668. for name in func._onchange:
  669. if name not in cls._fields:
  670. missing.append(name)
  671. methods[name].append(func)
  672. if missing:
  673. _logger.warning(
  674. "@api.onchange%r parameters must be field names -> not valid: %s",
  675. func._onchange, missing
  676. )
  677. # add onchange methods to implement "change_default" on fields
  678. def onchange_default(field, self):
  679. value = field.convert_to_write(self[field.name], self)
  680. condition = "%s=%s" % (field.name, value)
  681. defaults = self.env['ir.default'].get_model_defaults(self._name, condition)
  682. self.update(defaults)
  683. for name, field in cls._fields.items():
  684. if field.change_default:
  685. methods[name].append(functools.partial(onchange_default, field))
  686. # optimization: memoize result on cls, it will not be recomputed
  687. cls._onchange_methods = methods
  688. return methods
  689. def _is_an_ordinary_table(self):
  690. return self.pool.is_an_ordinary_table(self)
  691. def __ensure_xml_id(self, skip=False):
  692. """ Create missing external ids for records in ``self``, and return an
  693. iterator of pairs ``(record, xmlid)`` for the records in ``self``.
  694. :rtype: Iterable[Model, str | None]
  695. """
  696. if skip:
  697. return ((record, None) for record in self)
  698. if not self:
  699. return iter([])
  700. if not self._is_an_ordinary_table():
  701. raise Exception(
  702. "You can not export the column ID of model %s, because the "
  703. "table %s is not an ordinary table."
  704. % (self._name, self._table))
  705. modname = '__export__'
  706. cr = self.env.cr
  707. cr.execute("""
  708. SELECT res_id, module, name
  709. FROM ir_model_data
  710. WHERE model = %s AND res_id in %s
  711. """, (self._name, tuple(self.ids)))
  712. xids = {
  713. res_id: (module, name)
  714. for res_id, module, name in cr.fetchall()
  715. }
  716. def to_xid(record_id):
  717. (module, name) = xids[record_id]
  718. return ('%s.%s' % (module, name)) if module else name
  719. # create missing xml ids
  720. missing = self.filtered(lambda r: r.id not in xids)
  721. if not missing:
  722. return (
  723. (record, to_xid(record.id))
  724. for record in self
  725. )
  726. xids.update(
  727. (r.id, (modname, '%s_%s_%s' % (
  728. r._table,
  729. r.id,
  730. uuid.uuid4().hex[:8],
  731. )))
  732. for r in missing
  733. )
  734. fields = ['module', 'model', 'name', 'res_id']
  735. # disable eventual async callback / support for the extent of
  736. # the COPY FROM, as these are apparently incompatible
  737. callback = psycopg2.extensions.get_wait_callback()
  738. psycopg2.extensions.set_wait_callback(None)
  739. try:
  740. cr.copy_from(io.StringIO(
  741. u'\n'.join(
  742. u"%s\t%s\t%s\t%d" % (
  743. modname,
  744. record._name,
  745. xids[record.id][1],
  746. record.id,
  747. )
  748. for record in missing
  749. )),
  750. table='ir_model_data',
  751. columns=fields,
  752. )
  753. finally:
  754. psycopg2.extensions.set_wait_callback(callback)
  755. self.env['ir.model.data'].invalidate_model(fields)
  756. return (
  757. (record, to_xid(record.id))
  758. for record in self
  759. )
  760. def _export_rows(self, fields, *, _is_toplevel_call=True):
  761. """ Export fields of the records in ``self``.
  762. :param list fields: list of lists of fields to traverse
  763. :param bool _is_toplevel_call:
  764. used when recursing, avoid using when calling from outside
  765. :return: list of lists of corresponding values
  766. """
  767. import_compatible = self.env.context.get('import_compat', True)
  768. lines = []
  769. def splittor(rs):
  770. """ Splits the self recordset in batches of 1000 (to avoid
  771. entire-recordset-prefetch-effects) & removes the previous batch
  772. from the cache after it's been iterated in full
  773. """
  774. for idx in range(0, len(rs), 1000):
  775. sub = rs[idx:idx+1000]
  776. for rec in sub:
  777. yield rec
  778. sub.invalidate_recordset()
  779. if not _is_toplevel_call:
  780. splittor = lambda rs: rs
  781. # memory stable but ends up prefetching 275 fields (???)
  782. for record in splittor(self):
  783. # main line of record, initially empty
  784. current = [''] * len(fields)
  785. lines.append(current)
  786. # list of primary fields followed by secondary field(s)
  787. primary_done = []
  788. # process column by column
  789. for i, path in enumerate(fields):
  790. if not path:
  791. continue
  792. name = path[0]
  793. if name in primary_done:
  794. continue
  795. if name == '.id':
  796. current[i] = str(record.id)
  797. elif name == 'id':
  798. current[i] = (record._name, record.id)
  799. else:
  800. field = record._fields[name]
  801. value = record[name]
  802. # this part could be simpler, but it has to be done this way
  803. # in order to reproduce the former behavior
  804. if not isinstance(value, BaseModel):
  805. current[i] = field.convert_to_export(value, record)
  806. else:
  807. primary_done.append(name)
  808. # recursively export the fields that follow name; use
  809. # 'display_name' where no subfield is exported
  810. fields2 = [(p[1:] or ['display_name'] if p and p[0] == name else [])
  811. for p in fields]
  812. # in import_compat mode, m2m should always be exported as
  813. # a comma-separated list of xids or names in a single cell
  814. if import_compatible and field.type == 'many2many':
  815. index = None
  816. # find out which subfield the user wants & its
  817. # location as we might not get it as the first
  818. # column we encounter
  819. for name in ['id', 'name', 'display_name']:
  820. with contextlib.suppress(ValueError):
  821. index = fields2.index([name])
  822. break
  823. if index is None:
  824. # not found anything, assume we just want the
  825. # name_get in the first column
  826. name = None
  827. index = i
  828. if name == 'id':
  829. xml_ids = [xid for _, xid in value.__ensure_xml_id()]
  830. current[index] = ','.join(xml_ids)
  831. else:
  832. current[index] = field.convert_to_export(value, record)
  833. continue
  834. lines2 = value._export_rows(fields2, _is_toplevel_call=False)
  835. if lines2:
  836. # merge first line with record's main line
  837. for j, val in enumerate(lines2[0]):
  838. if val or isinstance(val, (int, float)):
  839. current[j] = val
  840. # append the other lines at the end
  841. lines += lines2[1:]
  842. else:
  843. current[i] = ''
  844. # if any xid should be exported, only do so at toplevel
  845. if _is_toplevel_call and any(f[-1] == 'id' for f in fields):
  846. bymodels = collections.defaultdict(set)
  847. xidmap = collections.defaultdict(list)
  848. # collect all the tuples in "lines" (along with their coordinates)
  849. for i, line in enumerate(lines):
  850. for j, cell in enumerate(line):
  851. if type(cell) is tuple:
  852. bymodels[cell[0]].add(cell[1])
  853. xidmap[cell].append((i, j))
  854. # for each model, xid-export everything and inject in matrix
  855. for model, ids in bymodels.items():
  856. for record, xid in self.env[model].browse(ids).__ensure_xml_id():
  857. for i, j in xidmap.pop((record._name, record.id)):
  858. lines[i][j] = xid
  859. assert not xidmap, "failed to export xids for %s" % ', '.join('{}:{}' % it for it in xidmap.items())
  860. return lines
  861. def export_data(self, fields_to_export):
  862. """ Export fields for selected objects
  863. This method is used when exporting data via client menu
  864. :param list fields_to_export: list of fields
  865. :returns: dictionary with a *datas* matrix
  866. :rtype: dict
  867. """
  868. if not (self.env.is_admin() or self.env.user.has_group('base.group_allow_export')):
  869. raise UserError(_("You don't have the rights to export data. Please contact an Administrator."))
  870. fields_to_export = [fix_import_export_id_paths(f) for f in fields_to_export]
  871. return {'datas': self._export_rows(fields_to_export)}
  872. @api.model
  873. def load(self, fields, data):
  874. """
  875. Attempts to load the data matrix, and returns a list of ids (or
  876. ``False`` if there was an error and no id could be generated) and a
  877. list of messages.
  878. The ids are those of the records created and saved (in database), in
  879. the same order they were extracted from the file. They can be passed
  880. directly to :meth:`~read`
  881. :param fields: list of fields to import, at the same index as the corresponding data
  882. :type fields: list(str)
  883. :param data: row-major matrix of data to import
  884. :type data: list(list(str))
  885. :returns: {ids: list(int)|False, messages: [Message][, lastrow: int]}
  886. """
  887. self.env.flush_all()
  888. # determine values of mode, current_module and noupdate
  889. mode = self._context.get('mode', 'init')
  890. current_module = self._context.get('module', '__import__')
  891. noupdate = self._context.get('noupdate', False)
  892. # add current module in context for the conversion of xml ids
  893. self = self.with_context(_import_current_module=current_module)
  894. cr = self._cr
  895. sp = cr.savepoint(flush=False)
  896. fields = [fix_import_export_id_paths(f) for f in fields]
  897. fg = self.fields_get()
  898. ids = []
  899. messages = []
  900. # list of (xid, vals, info) for records to be created in batch
  901. batch = []
  902. batch_xml_ids = set()
  903. # models in which we may have created / modified data, therefore might
  904. # require flushing in order to name_search: the root model and any
  905. # o2m
  906. creatable_models = {self._name}
  907. for field_path in fields:
  908. if field_path[0] in (None, 'id', '.id'):
  909. continue
  910. model_fields = self._fields
  911. if isinstance(model_fields[field_path[0]], odoo.fields.Many2one):
  912. # this only applies for toplevel m2o (?) fields
  913. if field_path[0] in (self.env.context.get('name_create_enabled_fieds') or {}):
  914. creatable_models.add(model_fields[field_path[0]].comodel_name)
  915. for field_name in field_path:
  916. if field_name in (None, 'id', '.id'):
  917. break
  918. if isinstance(model_fields[field_name], odoo.fields.One2many):
  919. comodel = model_fields[field_name].comodel_name
  920. creatable_models.add(comodel)
  921. model_fields = self.env[comodel]._fields
  922. def flush(*, xml_id=None, model=None):
  923. if not batch:
  924. return
  925. assert not (xml_id and model), \
  926. "flush can specify *either* an external id or a model, not both"
  927. if xml_id and xml_id not in batch_xml_ids:
  928. if xml_id not in self.env:
  929. return
  930. if model and model not in creatable_models:
  931. return
  932. data_list = [
  933. dict(xml_id=xid, values=vals, info=info, noupdate=noupdate)
  934. for xid, vals, info in batch
  935. ]
  936. batch.clear()
  937. batch_xml_ids.clear()
  938. # try to create in batch
  939. try:
  940. with cr.savepoint():
  941. recs = self._load_records(data_list, mode == 'update')
  942. ids.extend(recs.ids)
  943. return
  944. except psycopg2.InternalError as e:
  945. # broken transaction, exit and hope the source error was already logged
  946. if not any(message['type'] == 'error' for message in messages):
  947. info = data_list[0]['info']
  948. messages.append(dict(info, type='error', message=_(u"Unknown database error: '%s'", e)))
  949. return
  950. except Exception:
  951. pass
  952. errors = 0
  953. # try again, this time record by record
  954. for i, rec_data in enumerate(data_list, 1):
  955. try:
  956. with cr.savepoint():
  957. rec = self._load_records([rec_data], mode == 'update')
  958. ids.append(rec.id)
  959. except psycopg2.Warning as e:
  960. info = rec_data['info']
  961. messages.append(dict(info, type='warning', message=str(e)))
  962. except psycopg2.Error as e:
  963. info = rec_data['info']
  964. messages.append(dict(info, type='error', **PGERROR_TO_OE[e.pgcode](self, fg, info, e)))
  965. # Failed to write, log to messages, rollback savepoint (to
  966. # avoid broken transaction) and keep going
  967. errors += 1
  968. except UserError as e:
  969. info = rec_data['info']
  970. messages.append(dict(info, type='error', message=str(e)))
  971. errors += 1
  972. except Exception as e:
  973. _logger.debug("Error while loading record", exc_info=True)
  974. info = rec_data['info']
  975. message = (_(u'Unknown error during import:') + u' %s: %s' % (type(e), e))
  976. moreinfo = _('Resolve other errors first')
  977. messages.append(dict(info, type='error', message=message, moreinfo=moreinfo))
  978. # Failed for some reason, perhaps due to invalid data supplied,
  979. # rollback savepoint and keep going
  980. errors += 1
  981. if errors >= 10 and (errors >= i / 10):
  982. messages.append({
  983. 'type': 'warning',
  984. 'message': _(u"Found more than 10 errors and more than one error per 10 records, interrupted to avoid showing too many errors.")
  985. })
  986. break
  987. # make 'flush' available to the methods below, in the case where XMLID
  988. # resolution fails, for instance
  989. flush_recordset = self.with_context(import_flush=flush, import_cache=LRU(1024))
  990. # TODO: break load's API instead of smuggling via context?
  991. limit = self._context.get('_import_limit')
  992. if limit is None:
  993. limit = float('inf')
  994. extracted = flush_recordset._extract_records(fields, data, log=messages.append, limit=limit)
  995. converted = flush_recordset._convert_records(extracted, log=messages.append)
  996. info = {'rows': {'to': -1}}
  997. for id, xid, record, info in converted:
  998. if self.env.context.get('import_file') and self.env.context.get('import_skip_records'):
  999. if any([record.get(field) is None for field in self.env.context['import_skip_records']]):
  1000. continue
  1001. if xid:
  1002. xid = xid if '.' in xid else "%s.%s" % (current_module, xid)
  1003. batch_xml_ids.add(xid)
  1004. elif id:
  1005. record['id'] = id
  1006. batch.append((xid, record, info))
  1007. flush()
  1008. if any(message['type'] == 'error' for message in messages):
  1009. sp.rollback()
  1010. ids = False
  1011. # cancel all changes done to the registry/ormcache
  1012. self.pool.reset_changes()
  1013. sp.close(rollback=False)
  1014. nextrow = info['rows']['to'] + 1
  1015. if nextrow < limit:
  1016. nextrow = 0
  1017. return {
  1018. 'ids': ids,
  1019. 'messages': messages,
  1020. 'nextrow': nextrow,
  1021. }
  1022. def _add_fake_fields(self, fields):
  1023. from odoo.fields import Char, Integer
  1024. fields[None] = Char('rec_name')
  1025. fields['id'] = Char('External ID')
  1026. fields['.id'] = Integer('Database ID')
  1027. return fields
  1028. def _extract_records(self, fields_, data, log=lambda a: None, limit=float('inf')):
  1029. """ Generates record dicts from the data sequence.
  1030. The result is a generator of dicts mapping field names to raw
  1031. (unconverted, unvalidated) values.
  1032. For relational fields, if sub-fields were provided the value will be
  1033. a list of sub-records
  1034. The following sub-fields may be set on the record (by key):
  1035. * None is the name_get for the record (to use with name_create/name_search)
  1036. * "id" is the External ID for the record
  1037. * ".id" is the Database ID for the record
  1038. """
  1039. fields = dict(self._fields)
  1040. # Fake fields to avoid special cases in extractor
  1041. fields = self._add_fake_fields(fields)
  1042. # m2o fields can't be on multiple lines so exclude them from the
  1043. # is_relational field rows filter, but special-case it later on to
  1044. # be handled with relational fields (as it can have subfields)
  1045. is_relational = lambda field: fields[field].relational
  1046. get_o2m_values = itemgetter_tuple([
  1047. index
  1048. for index, fnames in enumerate(fields_)
  1049. if fields[fnames[0]].type == 'one2many'
  1050. ])
  1051. get_nono2m_values = itemgetter_tuple([
  1052. index
  1053. for index, fnames in enumerate(fields_)
  1054. if fields[fnames[0]].type != 'one2many'
  1055. ])
  1056. # Checks if the provided row has any non-empty one2many fields
  1057. def only_o2m_values(row):
  1058. return any(get_o2m_values(row)) and not any(get_nono2m_values(row))
  1059. index = 0
  1060. while index < len(data) and index < limit:
  1061. row = data[index]
  1062. # copy non-relational fields to record dict
  1063. record = {fnames[0]: value
  1064. for fnames, value in zip(fields_, row)
  1065. if not is_relational(fnames[0])}
  1066. # Get all following rows which have relational values attached to
  1067. # the current record (no non-relational values)
  1068. record_span = itertools.takewhile(
  1069. only_o2m_values, itertools.islice(data, index + 1, None))
  1070. # stitch record row back on for relational fields
  1071. record_span = list(itertools.chain([row], record_span))
  1072. for relfield in set(fnames[0] for fnames in fields_ if is_relational(fnames[0])):
  1073. comodel = self.env[fields[relfield].comodel_name]
  1074. # get only cells for this sub-field, should be strictly
  1075. # non-empty, field path [None] is for name_get field
  1076. indices, subfields = zip(*((index, fnames[1:] or [None])
  1077. for index, fnames in enumerate(fields_)
  1078. if fnames[0] == relfield))
  1079. # return all rows which have at least one value for the
  1080. # subfields of relfield
  1081. relfield_data = [it for it in map(itemgetter_tuple(indices), record_span) if any(it)]
  1082. record[relfield] = [
  1083. subrecord
  1084. for subrecord, _subinfo in comodel._extract_records(subfields, relfield_data, log=log)
  1085. ]
  1086. yield record, {'rows': {
  1087. 'from': index,
  1088. 'to': index + len(record_span) - 1,
  1089. }}
  1090. index += len(record_span)
  1091. @api.model
  1092. def _convert_records(self, records, log=lambda a: None):
  1093. """ Converts records from the source iterable (recursive dicts of
  1094. strings) into forms which can be written to the database (via
  1095. ``self.create`` or ``(ir.model.data)._update``)
  1096. :returns: a list of triplets of (id, xid, record)
  1097. :rtype: list[(int|None, str|None, dict)]
  1098. """
  1099. field_names = {name: field.string for name, field in self._fields.items()}
  1100. if self.env.lang:
  1101. field_names.update(self.env['ir.model.fields'].get_field_string(self._name))
  1102. convert = self.env['ir.fields.converter'].for_model(self)
  1103. def _log(base, record, field, exception):
  1104. type = 'warning' if isinstance(exception, Warning) else 'error'
  1105. # logs the logical (not human-readable) field name for automated
  1106. # processing of response, but injects human readable in message
  1107. field_name = field_names[field]
  1108. exc_vals = dict(base, record=record, field=field_name)
  1109. record = dict(base, type=type, record=record, field=field,
  1110. message=str(exception.args[0]) % exc_vals)
  1111. if len(exception.args) > 1:
  1112. info = {}
  1113. if exception.args[1] and isinstance(exception.args[1], dict):
  1114. info = exception.args[1]
  1115. # ensure field_name is added to the exception. Used in import to
  1116. # concatenate multiple errors in the same block
  1117. info['field_name'] = field_name
  1118. record.update(info)
  1119. log(record)
  1120. stream = CountingStream(records)
  1121. for record, extras in stream:
  1122. # xid
  1123. xid = record.get('id', False)
  1124. # dbid
  1125. dbid = False
  1126. if '.id' in record:
  1127. try:
  1128. dbid = int(record['.id'])
  1129. except ValueError:
  1130. # in case of overridden id column
  1131. dbid = record['.id']
  1132. if not self.search([('id', '=', dbid)]):
  1133. log(dict(extras,
  1134. type='error',
  1135. record=stream.index,
  1136. field='.id',
  1137. message=_(u"Unknown database identifier '%s'", dbid)))
  1138. dbid = False
  1139. converted = convert(record, functools.partial(_log, extras, stream.index))
  1140. yield dbid, xid, converted, dict(extras, record=stream.index)
  1141. def _validate_fields(self, field_names, excluded_names=()):
  1142. """ Invoke the constraint methods for which at least one field name is
  1143. in ``field_names`` and none is in ``excluded_names``.
  1144. """
  1145. field_names = set(field_names)
  1146. excluded_names = set(excluded_names)
  1147. for check in self._constraint_methods:
  1148. if (not field_names.isdisjoint(check._constrains)
  1149. and excluded_names.isdisjoint(check._constrains)):
  1150. check(self)
  1151. @api.model
  1152. def default_get(self, fields_list):
  1153. """ default_get(fields_list) -> default_values
  1154. Return default values for the fields in ``fields_list``. Default
  1155. values are determined by the context, user defaults, and the model
  1156. itself.
  1157. :param list fields_list: names of field whose default is requested
  1158. :return: a dictionary mapping field names to their corresponding default values,
  1159. if they have a default value.
  1160. :rtype: dict
  1161. .. note::
  1162. Unrequested defaults won't be considered, there is no need to return a
  1163. value for fields whose names are not in `fields_list`.
  1164. """
  1165. defaults = {}
  1166. parent_fields = defaultdict(list)
  1167. ir_defaults = self.env['ir.default'].get_model_defaults(self._name)
  1168. for name in fields_list:
  1169. # 1. look up context
  1170. key = 'default_' + name
  1171. if key in self._context:
  1172. defaults[name] = self._context[key]
  1173. continue
  1174. # 2. look up ir.default
  1175. if name in ir_defaults:
  1176. defaults[name] = ir_defaults[name]
  1177. continue
  1178. field = self._fields.get(name)
  1179. # 3. look up field.default
  1180. if field and field.default:
  1181. defaults[name] = field.default(self)
  1182. continue
  1183. # 4. delegate to parent model
  1184. if field and field.inherited:
  1185. field = field.related_field
  1186. parent_fields[field.model_name].append(field.name)
  1187. # convert default values to the right format
  1188. #
  1189. # we explicitly avoid using _convert_to_write() for x2many fields,
  1190. # because the latter leaves values like [(Command.LINK, 2),
  1191. # (Command.LINK, 3)], which are not supported by the web client as
  1192. # default values; stepping through the cache allows to normalize
  1193. # such a list to [(Command.SET, 0, [2, 3])], which is properly
  1194. # supported by the web client
  1195. for fname, value in defaults.items():
  1196. if fname in self._fields:
  1197. field = self._fields[fname]
  1198. value = field.convert_to_cache(value, self, validate=False)
  1199. defaults[fname] = field.convert_to_write(value, self)
  1200. # add default values for inherited fields
  1201. for model, names in parent_fields.items():
  1202. defaults.update(self.env[model].default_get(names))
  1203. return defaults
  1204. @api.model
  1205. def fields_get_keys(self):
  1206. warnings.warn(
  1207. 'fields_get_keys() method is deprecated, use `_fields` or `get_views` instead',
  1208. DeprecationWarning, stacklevel=2,
  1209. )
  1210. return list(self._fields)
  1211. @api.model
  1212. def _rec_name_fallback(self):
  1213. # if self._rec_name is set, it belongs to self._fields
  1214. return self._rec_name or 'id'
  1215. @api.model
  1216. def user_has_groups(self, groups):
  1217. """Return true if the user is member of at least one of the groups in
  1218. ``groups``, and is not a member of any of the groups in ``groups``
  1219. preceded by ``!``. Typically used to resolve ``groups`` attribute in
  1220. view and model definitions.
  1221. :param str groups: comma-separated list of fully-qualified group
  1222. external IDs, e.g., ``base.group_user,base.group_system``,
  1223. optionally preceded by ``!``
  1224. :return: True if the current user is a member of one of the given groups
  1225. not preceded by ``!`` and is not member of any of the groups
  1226. preceded by ``!``
  1227. """
  1228. from odoo.http import request
  1229. user = self.env.user
  1230. has_groups = []
  1231. not_has_groups = []
  1232. for group_ext_id in groups.split(','):
  1233. group_ext_id = group_ext_id.strip()
  1234. if group_ext_id[0] == '!':
  1235. not_has_groups.append(group_ext_id[1:])
  1236. else:
  1237. has_groups.append(group_ext_id)
  1238. for group_ext_id in not_has_groups:
  1239. if group_ext_id == 'base.group_no_one':
  1240. # check: the group_no_one is effective in debug mode only
  1241. if user.has_group(group_ext_id) and request and request.session.debug:
  1242. return False
  1243. else:
  1244. if user.has_group(group_ext_id):
  1245. return False
  1246. for group_ext_id in has_groups:
  1247. if group_ext_id == 'base.group_no_one':
  1248. # check: the group_no_one is effective in debug mode only
  1249. if user.has_group(group_ext_id) and request and request.session.debug:
  1250. return True
  1251. else:
  1252. if user.has_group(group_ext_id):
  1253. return True
  1254. return not has_groups
  1255. @api.model
  1256. def search_count(self, domain, limit=None):
  1257. """ search_count(domain) -> int
  1258. Returns the number of records in the current model matching :ref:`the
  1259. provided domain <reference/orm/domains>`.
  1260. :param domain: :ref:`A search domain <reference/orm/domains>`. Use an empty
  1261. list to match all records.
  1262. :param limit: maximum number of record to count (upperbound) (default: all)
  1263. """
  1264. res = self.search(domain, limit=limit, count=True)
  1265. return res if isinstance(res, int) else len(res)
  1266. @api.model
  1267. @api.returns('self',
  1268. upgrade=lambda self, value, domain, offset=0, limit=None, order=None, count=False: value if count else self.browse(value),
  1269. downgrade=lambda self, value, domain, offset=0, limit=None, order=None, count=False: value if count else value.ids)
  1270. def search(self, domain, offset=0, limit=None, order=None, count=False):
  1271. """ search(domain[, offset=0][, limit=None][, order=None][, count=False])
  1272. Searches for records based on the ``domain``
  1273. :ref:`search domain <reference/orm/domains>`.
  1274. :param domain: :ref:`A search domain <reference/orm/domains>`. Use an empty
  1275. list to match all records.
  1276. :param int offset: number of results to ignore (default: none)
  1277. :param int limit: maximum number of records to return (default: all)
  1278. :param str order: sort string
  1279. :param bool count: if True, only counts and returns the number of matching records (default: False)
  1280. :returns: at most ``limit`` records matching the search criteria
  1281. :raise AccessError: if user is not allowed to access requested information
  1282. """
  1283. res = self._search(domain, offset=offset, limit=limit, order=order, count=count)
  1284. return res if count else self.browse(res)
  1285. #
  1286. # display_name, name_get, name_create, name_search
  1287. #
  1288. @api.depends(lambda self: (self._rec_name,) if self._rec_name else ())
  1289. def _compute_display_name(self):
  1290. """Compute the value of the `display_name` field.
  1291. In general `display_name` is equal to calling `name_get()[0][1]`.
  1292. In that case, it is recommended to use `display_name` to uniformize the
  1293. code and to potentially take advantage of prefetch when applicable.
  1294. However some models might override this method. For them, the behavior
  1295. might differ, and it is important to select which of `display_name` or
  1296. `name_get()[0][1]` to call depending on the desired result.
  1297. """
  1298. names = dict(self.name_get())
  1299. for record in self:
  1300. record.display_name = names.get(record.id)
  1301. def name_get(self):
  1302. """Returns a textual representation for the records in ``self``, with
  1303. one item output per input record, in the same order.
  1304. .. warning::
  1305. Although :meth:`~.name_get` can use context data for richer
  1306. contextual formatting, as it is the default implementation for
  1307. :attr:`~.display_name` it is important that it resets to the
  1308. "default" behaviour if the context keys are empty / missing.
  1309. :return: list of pairs ``(id, text_repr)`` for each record
  1310. :rtype: list[(int, str)]
  1311. """
  1312. result = []
  1313. name = self._rec_name
  1314. if name in self._fields:
  1315. convert = self._fields[name].convert_to_display_name
  1316. for record in self:
  1317. result.append((record.id, convert(record[name], record) or ""))
  1318. else:
  1319. for record in self:
  1320. result.append((record.id, "%s,%s" % (record._name, record.id)))
  1321. return result
  1322. @api.model
  1323. def name_create(self, name):
  1324. """ name_create(name) -> record
  1325. Create a new record by calling :meth:`~.create` with only one value
  1326. provided: the display name of the new record.
  1327. The new record will be initialized with any default values
  1328. applicable to this model, or provided through the context. The usual
  1329. behavior of :meth:`~.create` applies.
  1330. :param name: display name of the record to create
  1331. :rtype: tuple
  1332. :return: the :meth:`~.name_get` pair value of the created record
  1333. """
  1334. if self._rec_name:
  1335. record = self.create({self._rec_name: name})
  1336. return record.name_get()[0]
  1337. else:
  1338. _logger.warning("Cannot execute name_create, no _rec_name defined on %s", self._name)
  1339. return False
  1340. @api.model
  1341. def name_search(self, name='', args=None, operator='ilike', limit=100):
  1342. """ name_search(name='', args=None, operator='ilike', limit=100) -> records
  1343. Search for records that have a display name matching the given
  1344. ``name`` pattern when compared with the given ``operator``, while also
  1345. matching the optional search domain (``args``).
  1346. This is used for example to provide suggestions based on a partial
  1347. value for a relational field. Should usually behave as the reverse of
  1348. :meth:`~.name_get`, but that is ont guaranteed.
  1349. This method is equivalent to calling :meth:`~.search` with a search
  1350. domain based on ``display_name`` and then :meth:`~.name_get` on the
  1351. result of the search.
  1352. :param str name: the name pattern to match
  1353. :param list args: optional search domain (see :meth:`~.search` for
  1354. syntax), specifying further restrictions
  1355. :param str operator: domain operator for matching ``name``, such as
  1356. ``'like'`` or ``'='``.
  1357. :param int limit: optional max number of records to return
  1358. :rtype: list
  1359. :return: list of pairs ``(id, text_repr)`` for all matching records.
  1360. """
  1361. ids = self._name_search(name, args, operator, limit=limit)
  1362. return self.browse(ids).sudo().name_get()
  1363. @api.model
  1364. def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None):
  1365. """ _name_search(name='', args=None, operator='ilike', limit=100, name_get_uid=None) -> ids
  1366. Private implementation of name_search, allows passing a dedicated user
  1367. for the name_get part to solve some access rights issues.
  1368. """
  1369. args = list(args or [])
  1370. search_fnames = self._rec_names_search or ([self._rec_name] if self._rec_name else [])
  1371. if not search_fnames:
  1372. _logger.warning("Cannot execute name_search, no _rec_name or _rec_names_search defined on %s", self._name)
  1373. # optimize out the default criterion of ``like ''`` that matches everything
  1374. elif not (name == '' and operator in ('like', 'ilike')):
  1375. aggregator = expression.AND if operator in expression.NEGATIVE_TERM_OPERATORS else expression.OR
  1376. domain = aggregator([[(field_name, operator, name)] for field_name in search_fnames])
  1377. args += domain
  1378. return self._search(args, limit=limit, access_rights_uid=name_get_uid)
  1379. @api.model
  1380. def _add_missing_default_values(self, values):
  1381. # avoid overriding inherited values when parent is set
  1382. avoid_models = set()
  1383. def collect_models_to_avoid(model):
  1384. for parent_mname, parent_fname in model._inherits.items():
  1385. if parent_fname in values:
  1386. avoid_models.add(parent_mname)
  1387. else:
  1388. # manage the case where an ancestor parent field is set
  1389. collect_models_to_avoid(self.env[parent_mname])
  1390. collect_models_to_avoid(self)
  1391. def avoid(field):
  1392. # check whether the field is inherited from one of avoid_models
  1393. if avoid_models:
  1394. while field.inherited:
  1395. field = field.related_field
  1396. if field.model_name in avoid_models:
  1397. return True
  1398. return False
  1399. # compute missing fields
  1400. missing_defaults = {
  1401. name
  1402. for name, field in self._fields.items()
  1403. if name not in values
  1404. if not avoid(field)
  1405. }
  1406. if missing_defaults:
  1407. # override defaults with the provided values, never allow the other way around
  1408. defaults = self.default_get(list(missing_defaults))
  1409. for name, value in defaults.items():
  1410. if self._fields[name].type == 'many2many' and value and isinstance(value[0], int):
  1411. # convert a list of ids into a list of commands
  1412. defaults[name] = [Command.set(value)]
  1413. elif self._fields[name].type == 'one2many' and value and isinstance(value[0], dict):
  1414. # convert a list of dicts into a list of commands
  1415. defaults[name] = [Command.create(x) for x in value]
  1416. defaults.update(values)
  1417. else:
  1418. defaults = values
  1419. # delegate the default properties to the properties field
  1420. for field in self._fields.values():
  1421. if field.type == 'properties':
  1422. defaults[field.name] = field._add_default_values(self.env, defaults)
  1423. return defaults
  1424. @classmethod
  1425. def clear_caches(cls):
  1426. """ Clear the caches
  1427. This clears the caches associated to methods decorated with
  1428. ``tools.ormcache`` or ``tools.ormcache_multi``.
  1429. """
  1430. cls.pool._clear_cache()
  1431. @api.model
  1432. def _read_group_expand_full(self, groups, domain, order):
  1433. """Extend the group to include all target records by default."""
  1434. return groups.search([], order=order)
  1435. @api.model
  1436. def _read_group_fill_results(self, domain, groupby, remaining_groupbys,
  1437. aggregated_fields, count_field,
  1438. read_group_result, read_group_order=None):
  1439. """Helper method for filling in empty groups for all possible values of
  1440. the field being grouped by"""
  1441. field = self._fields[groupby]
  1442. if not field.group_expand:
  1443. return read_group_result
  1444. # field.group_expand is a callable or the name of a method, that returns
  1445. # the groups that we want to display for this field, in the form of a
  1446. # recordset or a list of values (depending on the type of the field).
  1447. # This is useful to implement kanban views for instance, where some
  1448. # columns should be displayed even if they don't contain any record.
  1449. group_expand = field.group_expand
  1450. if isinstance(group_expand, str):
  1451. group_expand = getattr(type(self), group_expand)
  1452. assert callable(group_expand)
  1453. # determine all groups that should be returned
  1454. values = [line[groupby] for line in read_group_result if line[groupby]]
  1455. if field.relational:
  1456. # groups is a recordset; determine order on groups's model
  1457. groups = self.env[field.comodel_name].browse([value[0] for value in values])
  1458. order = groups._order
  1459. if read_group_order == groupby + ' desc':
  1460. order = tools.reverse_order(order)
  1461. groups = group_expand(self, groups, domain, order)
  1462. groups = groups.sudo()
  1463. values = lazy_name_get(groups)
  1464. value2key = lambda value: value and value[0]
  1465. else:
  1466. # groups is a list of values
  1467. values = group_expand(self, values, domain, None)
  1468. if read_group_order == groupby + ' desc':
  1469. values.reverse()
  1470. value2key = lambda value: value
  1471. # Merge the current results (list of dicts) with all groups. Determine
  1472. # the global order of results groups, which is supposed to be in the
  1473. # same order as read_group_result (in the case of a many2one field).
  1474. result = OrderedDict((value2key(value), {}) for value in values)
  1475. # fill in results from read_group_result
  1476. for line in read_group_result:
  1477. key = value2key(line[groupby])
  1478. if not result.get(key):
  1479. result[key] = line
  1480. else:
  1481. result[key][count_field] = line[count_field]
  1482. # fill in missing results from all groups
  1483. for value in values:
  1484. key = value2key(value)
  1485. if not result[key]:
  1486. line = dict.fromkeys(aggregated_fields, False)
  1487. line[groupby] = value
  1488. line[groupby + '_count'] = 0
  1489. line['__domain'] = [(groupby, '=', key)] + domain
  1490. if remaining_groupbys:
  1491. line['__context'] = {'group_by': remaining_groupbys}
  1492. result[key] = line
  1493. # add folding information if present
  1494. if field.relational and groups._fold_name in groups._fields:
  1495. fold = {group.id: group[groups._fold_name]
  1496. for group in groups.browse([key for key in result if key])}
  1497. for key, line in result.items():
  1498. line['__fold'] = fold.get(key, False)
  1499. return list(result.values())
  1500. @api.model
  1501. def _read_group_fill_temporal(self, data, groupby, aggregated_fields, annotated_groupbys,
  1502. fill_from=False, fill_to=False, min_groups=False):
  1503. """Helper method for filling date/datetime 'holes' in a result set.
  1504. We are in a use case where data are grouped by a date field (typically
  1505. months but it could be any other interval) and displayed in a chart.
  1506. Assume we group records by month, and we only have data for June,
  1507. September and December. By default, plotting the result gives something
  1508. like::
  1509. ___
  1510. ___ | |
  1511. | | ___ | |
  1512. |___||___||___|
  1513. Jun Sep Dec
  1514. The problem is that December data immediately follow September data,
  1515. which is misleading for the user. Adding explicit zeroes for missing
  1516. data gives something like::
  1517. ___
  1518. ___ | |
  1519. | | ___ | |
  1520. |___| ___ ___ |___| ___ ___ |___|
  1521. Jun Jul Aug Sep Oct Nov Dec
  1522. To customize this output, the context key "fill_temporal" can be used
  1523. under its dictionary format, which has 3 attributes : fill_from,
  1524. fill_to, min_groups (see params of this function)
  1525. Fill between bounds:
  1526. Using either `fill_from` and/or `fill_to` attributes, we can further
  1527. specify that at least a certain date range should be returned as
  1528. contiguous groups. Any group outside those bounds will not be removed,
  1529. but the filling will only occur between the specified bounds. When not
  1530. specified, existing groups will be used as bounds, if applicable.
  1531. By specifying such bounds, we can get empty groups before/after any
  1532. group with data.
  1533. If we want to fill groups only between August (fill_from)
  1534. and October (fill_to)::
  1535. ___
  1536. ___ | |
  1537. | | ___ | |
  1538. |___| ___ |___| ___ |___|
  1539. Jun Aug Sep Oct Dec
  1540. We still get June and December. To filter them out, we should match
  1541. `fill_from` and `fill_to` with the domain e.g. ``['&',
  1542. ('date_field', '>=', 'YYYY-08-01'), ('date_field', '<', 'YYYY-11-01')]``::
  1543. ___
  1544. ___ |___| ___
  1545. Aug Sep Oct
  1546. Minimal filling amount:
  1547. Using `min_groups`, we can specify that we want at least that amount of
  1548. contiguous groups. This amount is guaranteed to be provided from
  1549. `fill_from` if specified, or from the lowest existing group otherwise.
  1550. This amount is not restricted by `fill_to`. If there is an existing
  1551. group before `fill_from`, `fill_from` is still used as the starting
  1552. group for min_groups, because the filling does not apply on that
  1553. existing group. If neither `fill_from` nor `fill_to` is specified, and
  1554. there is no existing group, no group will be returned.
  1555. If we set min_groups = 4::
  1556. ___
  1557. ___ |___| ___ ___
  1558. Aug Sep Oct Nov
  1559. :param list data: the data containing groups
  1560. :param list groupby: name of the first group by
  1561. :param list aggregated_fields: list of aggregated fields in the query
  1562. :param str fill_from: (inclusive) string representation of a
  1563. date/datetime, start bound of the fill_temporal range
  1564. formats: date -> %Y-%m-%d, datetime -> %Y-%m-%d %H:%M:%S
  1565. :param str fill_to: (inclusive) string representation of a
  1566. date/datetime, end bound of the fill_temporal range
  1567. formats: date -> %Y-%m-%d, datetime -> %Y-%m-%d %H:%M:%S
  1568. :param int min_groups: minimal amount of required groups for the
  1569. fill_temporal range (should be >= 1)
  1570. :rtype: list
  1571. :return: list
  1572. """
  1573. first_a_gby = annotated_groupbys[0]
  1574. if first_a_gby['type'] not in ('date', 'datetime'):
  1575. return data
  1576. interval = first_a_gby['interval']
  1577. granularity = first_a_gby['granularity']
  1578. tz = pytz.timezone(self._context['tz']) if first_a_gby["tz_convert"] else False
  1579. groupby_name = groupby[0]
  1580. # existing non null datetimes
  1581. existing = [d[groupby_name] for d in data if d[groupby_name]] or [None]
  1582. # assumption: existing data is sorted by field 'groupby_name'
  1583. existing_from, existing_to = existing[0], existing[-1]
  1584. if fill_from:
  1585. fill_from = date_utils.start_of(odoo.fields.Datetime.to_datetime(fill_from), granularity)
  1586. if tz:
  1587. fill_from = tz.localize(fill_from)
  1588. elif existing_from:
  1589. fill_from = existing_from
  1590. if fill_to:
  1591. fill_to = date_utils.start_of(odoo.fields.Datetime.to_datetime(fill_to), granularity)
  1592. if tz:
  1593. fill_to = tz.localize(fill_to)
  1594. elif existing_to:
  1595. fill_to = existing_to
  1596. if not fill_to and fill_from:
  1597. fill_to = fill_from
  1598. if not fill_from and fill_to:
  1599. fill_from = fill_to
  1600. if not fill_from and not fill_to:
  1601. return data
  1602. if min_groups > 0:
  1603. fill_to = max(fill_to, fill_from + (min_groups - 1) * interval)
  1604. if fill_to < fill_from:
  1605. return data
  1606. required_dates = date_utils.date_range(fill_from, fill_to, interval)
  1607. if existing[0] is None:
  1608. existing = list(required_dates)
  1609. else:
  1610. existing = sorted(set().union(existing, required_dates))
  1611. empty_item = {'id': False, (groupby_name.split(':')[0] + '_count'): 0}
  1612. empty_item.update({key: False for key in aggregated_fields})
  1613. empty_item.update({key: False for key in [group['groupby'] for group in annotated_groupbys[1:]]})
  1614. grouped_data = collections.defaultdict(list)
  1615. for d in data:
  1616. grouped_data[d[groupby_name]].append(d)
  1617. result = []
  1618. for dt in existing:
  1619. result.extend(grouped_data[dt] or [dict(empty_item, **{groupby_name: dt})])
  1620. if False in grouped_data:
  1621. result.extend(grouped_data[False])
  1622. return result
  1623. @api.model
  1624. def _read_group_prepare(self, orderby, aggregated_fields, annotated_groupbys, query):
  1625. """
  1626. Prepares the GROUP BY and ORDER BY terms for the read_group method. Adds the missing JOIN clause
  1627. to the query if order should be computed against m2o field.
  1628. :param orderby: the orderby definition in the form "%(field)s %(order)s"
  1629. :param aggregated_fields: list of aggregated fields in the query
  1630. :param annotated_groupbys: list of dictionaries returned by
  1631. :meth:`_read_group_process_groupby`
  1632. These dictionaries contain the qualified name of each groupby
  1633. (fully qualified SQL name for the corresponding field),
  1634. and the (non raw) field name.
  1635. :param Query query: the query under construction
  1636. :return: (groupby_terms, orderby_terms)
  1637. """
  1638. orderby_terms = []
  1639. groupby_terms = [gb['qualified_field'] for gb in annotated_groupbys]
  1640. if not orderby:
  1641. return groupby_terms, orderby_terms
  1642. self._check_qorder(orderby)
  1643. # when a field is grouped as 'foo:bar', both orderby='foo' and
  1644. # orderby='foo:bar' generate the clause 'ORDER BY "foo:bar"'
  1645. groupby_fields = {
  1646. gb[key]: gb['groupby']
  1647. for gb in annotated_groupbys
  1648. for key in ('field', 'groupby')
  1649. }
  1650. for order_part in orderby.split(','):
  1651. order_split = order_part.split() # potentially ["field:group_func", "desc"]
  1652. order_field = order_split[0]
  1653. is_many2one_id = order_field.endswith(".id")
  1654. if is_many2one_id:
  1655. order_field = order_field[:-3]
  1656. if order_field == 'id' or order_field in groupby_fields:
  1657. order_field_name = order_field.split(':')[0]
  1658. if self._fields[order_field_name].type == 'many2one' and not is_many2one_id:
  1659. order_clause = self._generate_order_by(order_part, query)
  1660. order_clause = order_clause.replace('ORDER BY ', '')
  1661. if order_clause:
  1662. orderby_terms.append(order_clause)
  1663. groupby_terms += [order_term.split()[0] for order_term in order_clause.split(',')]
  1664. else:
  1665. order_split[0] = '"%s"' % groupby_fields.get(order_field, order_field)
  1666. orderby_terms.append(' '.join(order_split))
  1667. elif order_field in aggregated_fields:
  1668. order_split[0] = '"%s"' % order_field
  1669. orderby_terms.append(' '.join(order_split))
  1670. elif order_field not in self._fields:
  1671. raise ValueError("Invalid field %r on model %r" % (order_field, self._name))
  1672. elif order_field == 'sequence':
  1673. pass
  1674. else:
  1675. # Cannot order by a field that will not appear in the results (needs to be grouped or aggregated)
  1676. _logger.warning('%s: read_group order by `%s` ignored, cannot sort on empty columns (not grouped/aggregated)',
  1677. self._name, order_part)
  1678. return groupby_terms, orderby_terms
  1679. @api.model
  1680. def _read_group_process_groupby(self, gb, query):
  1681. """
  1682. Helper method to collect important information about groupbys: raw
  1683. field name, type, time information, qualified name, ...
  1684. """
  1685. split = gb.split(':')
  1686. field = self._fields.get(split[0])
  1687. if not field:
  1688. raise ValueError("Invalid field %r on model %r" % (split[0], self._name))
  1689. field_type = field.type
  1690. gb_function = split[1] if len(split) == 2 else None
  1691. temporal = field_type in ('date', 'datetime')
  1692. tz_convert = field_type == 'datetime' and self._context.get('tz') in pytz.all_timezones
  1693. qualified_field = self._inherits_join_calc(self._table, split[0], query)
  1694. if temporal:
  1695. display_formats = {
  1696. # Careful with week/year formats:
  1697. # - yyyy (lower) must always be used, *except* for week+year formats
  1698. # - YYYY (upper) must always be used for week+year format
  1699. # e.g. 2006-01-01 is W52 2005 in some locales (de_DE),
  1700. # and W1 2006 for others
  1701. #
  1702. # Mixing both formats, e.g. 'MMM YYYY' would yield wrong results,
  1703. # such as 2006-01-01 being formatted as "January 2005" in some locales.
  1704. # Cfr: http://babel.pocoo.org/en/latest/dates.html#date-fields
  1705. 'hour': 'hh:00 dd MMM',
  1706. 'day': 'dd MMM yyyy', # yyyy = normal year
  1707. 'week': "'W'w YYYY", # w YYYY = ISO week-year
  1708. 'month': 'MMMM yyyy',
  1709. 'quarter': 'QQQ yyyy',
  1710. 'year': 'yyyy',
  1711. }
  1712. time_intervals = {
  1713. 'hour': dateutil.relativedelta.relativedelta(hours=1),
  1714. 'day': dateutil.relativedelta.relativedelta(days=1),
  1715. 'week': datetime.timedelta(days=7),
  1716. 'month': dateutil.relativedelta.relativedelta(months=1),
  1717. 'quarter': dateutil.relativedelta.relativedelta(months=3),
  1718. 'year': dateutil.relativedelta.relativedelta(years=1)
  1719. }
  1720. if tz_convert:
  1721. qualified_field = "timezone('%s', timezone('UTC',%s))" % (self._context.get('tz', 'UTC'), qualified_field)
  1722. qualified_field = "date_trunc('%s', %s::timestamp)" % (gb_function or 'month', qualified_field)
  1723. if field_type == 'boolean':
  1724. qualified_field = "coalesce(%s,false)" % qualified_field
  1725. return {
  1726. 'field': split[0],
  1727. 'groupby': gb,
  1728. 'type': field_type,
  1729. 'display_format': display_formats[gb_function or 'month'] if temporal else None,
  1730. 'interval': time_intervals[gb_function or 'month'] if temporal else None,
  1731. 'granularity': gb_function or 'month' if temporal else None,
  1732. 'tz_convert': tz_convert,
  1733. 'qualified_field': qualified_field,
  1734. }
  1735. @api.model
  1736. def _read_group_prepare_data(self, key, value, groupby_dict):
  1737. """
  1738. Helper method to sanitize the data received by read_group. The None
  1739. values are converted to False, and the date/datetime are formatted,
  1740. and corrected according to the timezones.
  1741. """
  1742. value = False if value is None else value
  1743. gb = groupby_dict.get(key)
  1744. if gb and gb['type'] in ('date', 'datetime') and value:
  1745. if isinstance(value, str):
  1746. dt_format = DEFAULT_SERVER_DATETIME_FORMAT if gb['type'] == 'datetime' else DEFAULT_SERVER_DATE_FORMAT
  1747. value = datetime.datetime.strptime(value, dt_format)
  1748. if gb['tz_convert']:
  1749. value = pytz.timezone(self._context['tz']).localize(value)
  1750. return value
  1751. @api.model
  1752. def _read_group_format_result(self, data, annotated_groupbys, groupby, domain):
  1753. """
  1754. Helper method to format the data contained in the dictionary data by
  1755. adding the domain corresponding to its values, the groupbys in the
  1756. context and by properly formatting the date/datetime values.
  1757. :param data: a single group
  1758. :param annotated_groupbys: expanded grouping metainformation
  1759. :param groupby: original grouping metainformation
  1760. :param domain: original domain for read_group
  1761. """
  1762. sections = []
  1763. for gb in annotated_groupbys:
  1764. ftype = gb['type']
  1765. value = data[gb['groupby']]
  1766. # full domain for this groupby spec
  1767. d = None
  1768. if value:
  1769. if ftype in ['many2one', 'many2many']:
  1770. value = value[0]
  1771. elif ftype in ('date', 'datetime'):
  1772. locale = get_lang(self.env).code
  1773. fmt = DEFAULT_SERVER_DATETIME_FORMAT if ftype == 'datetime' else DEFAULT_SERVER_DATE_FORMAT
  1774. tzinfo = None
  1775. range_start = value
  1776. range_end = value + gb['interval']
  1777. # value from postgres is in local tz (so range is
  1778. # considered in local tz e.g. "day" is [00:00, 00:00[
  1779. # local rather than UTC which could be [11:00, 11:00]
  1780. # local) but domain and raw value should be in UTC
  1781. if gb['tz_convert']:
  1782. tzinfo = range_start.tzinfo
  1783. range_start = range_start.astimezone(pytz.utc)
  1784. # take into account possible hour change between start and end
  1785. range_end = tzinfo.localize(range_end.replace(tzinfo=None))
  1786. range_end = range_end.astimezone(pytz.utc)
  1787. range_start = range_start.strftime(fmt)
  1788. range_end = range_end.strftime(fmt)
  1789. if ftype == 'datetime':
  1790. label = babel.dates.format_datetime(
  1791. value, format=gb['display_format'],
  1792. tzinfo=tzinfo, locale=locale
  1793. )
  1794. else:
  1795. label = babel.dates.format_date(
  1796. value, format=gb['display_format'],
  1797. locale=locale
  1798. )
  1799. data[gb['groupby']] = ('%s/%s' % (range_start, range_end), label)
  1800. data.setdefault('__range', {})[gb['groupby']] = {'from': range_start, 'to': range_end}
  1801. d = [
  1802. '&',
  1803. (gb['field'], '>=', range_start),
  1804. (gb['field'], '<', range_end),
  1805. ]
  1806. elif ftype in ('date', 'datetime'):
  1807. # Set the __range of the group containing records with an unset
  1808. # date/datetime field value to False.
  1809. data.setdefault('__range', {})[gb['groupby']] = False
  1810. if d is None:
  1811. d = [(gb['field'], '=', value)]
  1812. sections.append(d)
  1813. sections.append(domain)
  1814. data['__domain'] = expression.AND(sections)
  1815. if len(groupby) - len(annotated_groupbys) >= 1:
  1816. data['__context'] = { 'group_by': groupby[len(annotated_groupbys):]}
  1817. del data['id']
  1818. return data
  1819. @api.model
  1820. def _read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  1821. """
  1822. Executes exactly what the public read_group() does, except it doesn't
  1823. order many2one fields on their comodel's order but on their ID instead.
  1824. """
  1825. if not orderby:
  1826. if isinstance(groupby, str):
  1827. groupby = [groupby]
  1828. groupby_list = groupby[:1] if lazy else groupby
  1829. order_list = []
  1830. for order_spec in groupby_list:
  1831. field_name = order_spec.split(":")[0] # field name could be formatted like "field:group_func"
  1832. if self._fields[field_name].type == 'many2one':
  1833. order_spec = f"{field_name}.id" # do not order by comodel's order
  1834. order_list.append(order_spec)
  1835. orderby = ','.join(order_list)
  1836. return self.read_group(domain, fields, groupby, offset, limit, orderby, lazy)
  1837. @api.model
  1838. def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  1839. """Get the list of records in list view grouped by the given ``groupby`` fields.
  1840. :param list domain: :ref:`A search domain <reference/orm/domains>`. Use an empty
  1841. list to match all records.
  1842. :param list fields: list of fields present in the list view specified on the object.
  1843. Each element is either 'field' (field name, using the default aggregation),
  1844. or 'field:agg' (aggregate field with aggregation function 'agg'),
  1845. or 'name:agg(field)' (aggregate field with 'agg' and return it as 'name').
  1846. The possible aggregation functions are the ones provided by
  1847. `PostgreSQL <https://www.postgresql.org/docs/current/static/functions-aggregate.html>`_
  1848. and 'count_distinct', with the expected meaning.
  1849. :param list groupby: list of groupby descriptions by which the records will be grouped.
  1850. A groupby description is either a field (then it will be grouped by that field)
  1851. or a string 'field:granularity'. Right now, the only supported granularities
  1852. are 'day', 'week', 'month', 'quarter' or 'year', and they only make sense for
  1853. date/datetime fields.
  1854. :param int offset: optional number of records to skip
  1855. :param int limit: optional max number of records to return
  1856. :param str orderby: optional ``order by`` specification, for
  1857. overriding the natural sort ordering of the
  1858. groups, see also :py:meth:`~osv.osv.osv.search`
  1859. (supported only for many2one fields currently)
  1860. :param bool lazy: if true, the results are only grouped by the first groupby and the
  1861. remaining groupbys are put in the __context key. If false, all the groupbys are
  1862. done in one call.
  1863. :return: list of dictionaries(one dictionary for each record) containing:
  1864. * the values of fields grouped by the fields in ``groupby`` argument
  1865. * __domain: list of tuples specifying the search criteria
  1866. * __context: dictionary with argument like ``groupby``
  1867. * __range: (date/datetime only) dictionary with field_name:granularity as keys
  1868. mapping to a dictionary with keys: "from" (inclusive) and "to" (exclusive)
  1869. mapping to a string representation of the temporal bounds of the group
  1870. :rtype: [{'field_name_1': value, ...}, ...]
  1871. :raise AccessError: if user is not allowed to access requested information
  1872. """
  1873. result = self._read_group_raw(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
  1874. groupby = [groupby] if isinstance(groupby, str) else groupby[:1] if lazy else OrderedSet(groupby)
  1875. groupby_dates = [
  1876. groupby_description for groupby_description in groupby
  1877. if self._fields[groupby_description.split(':')[0]].type in ('date', 'datetime') # e.g. 'date:month'
  1878. ]
  1879. if not groupby_dates:
  1880. return result
  1881. # iterate on all results and replace the "full" date/datetime value (<=> group[df])
  1882. # which is a tuple (range, label) by just the formatted label, in-place.
  1883. for group in result:
  1884. for groupby_date in groupby_dates:
  1885. # could group on a date(time) field which is empty in some
  1886. # records, in which case as with m2o the _raw value will be
  1887. # `False` instead of a (value, label) pair. In that case,
  1888. # leave the `False` value alone
  1889. if group.get(groupby_date):
  1890. group[groupby_date] = group[groupby_date][1]
  1891. return result
  1892. @api.model
  1893. def _read_group_raw(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
  1894. self.check_access_rights('read')
  1895. query = self._where_calc(domain)
  1896. fields = fields or [f.name for f in self._fields.values() if f.store]
  1897. groupby = [groupby] if isinstance(groupby, str) else list(OrderedSet(groupby))
  1898. groupby_list = groupby[:1] if lazy else groupby
  1899. annotated_groupbys = [self._read_group_process_groupby(gb, query) for gb in groupby_list]
  1900. groupby_fields = [g['field'] for g in annotated_groupbys]
  1901. order = orderby or ','.join([g for g in groupby_list])
  1902. groupby_dict = {gb['groupby']: gb for gb in annotated_groupbys}
  1903. self._apply_ir_rules(query, 'read')
  1904. for gb in groupby_fields:
  1905. if gb not in self._fields:
  1906. raise UserError(_("Unknown field %r in 'groupby'", gb))
  1907. if not self._fields[gb].base_field.groupable:
  1908. raise UserError(_(
  1909. "Field %s is not a stored field, only stored fields (regular or "
  1910. "many2many) are valid for the 'groupby' parameter", self._fields[gb],
  1911. ))
  1912. aggregated_fields = []
  1913. select_terms = []
  1914. fnames = [] # list of fields to flush
  1915. for fspec in fields:
  1916. if fspec == 'sequence':
  1917. continue
  1918. if fspec == '__count':
  1919. # the web client sometimes adds this pseudo-field in the list
  1920. continue
  1921. match = regex_field_agg.match(fspec)
  1922. if not match:
  1923. raise UserError(_("Invalid field specification %r.", fspec))
  1924. name, func, fname = match.groups()
  1925. if func:
  1926. # we have either 'name:func' or 'name:func(fname)'
  1927. fname = fname or name
  1928. field = self._fields.get(fname)
  1929. if not field:
  1930. raise ValueError("Invalid field %r on model %r" % (fname, self._name))
  1931. if not (field.base_field.store and field.base_field.column_type):
  1932. raise UserError(_("Cannot aggregate field %r.", fname))
  1933. if func not in VALID_AGGREGATE_FUNCTIONS:
  1934. raise UserError(_("Invalid aggregation function %r.", func))
  1935. else:
  1936. # we have 'name', retrieve the aggregator on the field
  1937. field = self._fields.get(name)
  1938. if not field:
  1939. raise ValueError("Invalid field %r on model %r" % (name, self._name))
  1940. if not (field.base_field.store and
  1941. field.base_field.column_type and field.group_operator):
  1942. continue
  1943. func, fname = field.group_operator, name
  1944. fnames.append(fname)
  1945. if fname in groupby_fields:
  1946. continue
  1947. if name in aggregated_fields:
  1948. raise UserError(_("Output name %r is used twice.", name))
  1949. aggregated_fields.append(name)
  1950. expr = self._inherits_join_calc(self._table, fname, query)
  1951. if func.lower() == 'count_distinct':
  1952. term = 'COUNT(DISTINCT %s) AS "%s"' % (expr, name)
  1953. else:
  1954. term = '%s(%s) AS "%s"' % (func, expr, name)
  1955. select_terms.append(term)
  1956. for gb in annotated_groupbys:
  1957. select_terms.append('%s as "%s" ' % (gb['qualified_field'], gb['groupby']))
  1958. self._flush_search(domain, fields=fnames + groupby_fields)
  1959. groupby_terms, orderby_terms = self._read_group_prepare(order, aggregated_fields, annotated_groupbys, query)
  1960. from_clause, where_clause, where_clause_params = query.get_sql()
  1961. if lazy and (len(groupby_fields) >= 2 or not self._context.get('group_by_no_leaf')):
  1962. count_field = groupby_fields[0] if len(groupby_fields) >= 1 else '_'
  1963. else:
  1964. count_field = '_'
  1965. count_field += '_count'
  1966. prefix_terms = lambda prefix, terms: (prefix + " " + ",".join(terms)) if terms else ''
  1967. prefix_term = lambda prefix, term: ('%s %s' % (prefix, term)) if term else ''
  1968. query = """
  1969. SELECT min("%(table)s".id) AS id, count("%(table)s".id) AS "%(count_field)s" %(extra_fields)s
  1970. FROM %(from)s
  1971. %(where)s
  1972. %(groupby)s
  1973. %(orderby)s
  1974. %(limit)s
  1975. %(offset)s
  1976. """ % {
  1977. 'table': self._table,
  1978. 'count_field': count_field,
  1979. 'extra_fields': prefix_terms(',', select_terms),
  1980. 'from': from_clause,
  1981. 'where': prefix_term('WHERE', where_clause),
  1982. 'groupby': prefix_terms('GROUP BY', groupby_terms),
  1983. 'orderby': prefix_terms('ORDER BY', orderby_terms),
  1984. 'limit': prefix_term('LIMIT', int(limit) if limit else None),
  1985. 'offset': prefix_term('OFFSET', int(offset) if limit else None),
  1986. }
  1987. self._cr.execute(query, where_clause_params)
  1988. fetched_data = self._cr.dictfetchall()
  1989. if not groupby_fields:
  1990. return fetched_data
  1991. self._read_group_resolve_many2x_fields(fetched_data, annotated_groupbys)
  1992. data = [{k: self._read_group_prepare_data(k, v, groupby_dict) for k, v in r.items()} for r in fetched_data]
  1993. fill_temporal = self.env.context.get('fill_temporal')
  1994. if (data and fill_temporal) or isinstance(fill_temporal, dict):
  1995. # fill_temporal = {} is equivalent to fill_temporal = True
  1996. # if fill_temporal is a dictionary and there is no data, there is a chance that we
  1997. # want to display empty columns anyway, so we should apply the fill_temporal logic
  1998. if not isinstance(fill_temporal, dict):
  1999. fill_temporal = {}
  2000. data = self._read_group_fill_temporal(data, groupby, aggregated_fields,
  2001. annotated_groupbys, **fill_temporal)
  2002. result = [self._read_group_format_result(d, annotated_groupbys, groupby, domain) for d in data]
  2003. if lazy:
  2004. # Right now, read_group only fill results in lazy mode (by default).
  2005. # If you need to have the empty groups in 'eager' mode, then the
  2006. # method _read_group_fill_results need to be completely reimplemented
  2007. # in a sane way
  2008. result = self._read_group_fill_results(
  2009. domain, groupby_fields[0], groupby[len(annotated_groupbys):],
  2010. aggregated_fields, count_field, result, read_group_order=order,
  2011. )
  2012. return result
  2013. def _read_group_resolve_many2x_fields(self, data, fields):
  2014. many2xfields = {field['field'] for field in fields if field['type'] in ['many2one', 'many2many']}
  2015. for field in many2xfields:
  2016. ids_set = {d[field] for d in data if d[field]}
  2017. m2x_records = self.env[self._fields[field].comodel_name].browse(ids_set)
  2018. data_dict = dict(lazy_name_get(m2x_records.sudo()))
  2019. for d in data:
  2020. d[field] = (d[field], data_dict[d[field]]) if d[field] else False
  2021. def _inherits_join_add(self, current_model, parent_model_name, query):
  2022. """
  2023. Add missing table SELECT and JOIN clause to ``query`` for reaching the parent table (no duplicates)
  2024. :param current_model: current model object
  2025. :param parent_model_name: name of the parent model for which the clauses should be added
  2026. :param query: query object on which the JOIN should be added
  2027. """
  2028. inherits_field = current_model._inherits[parent_model_name]
  2029. parent_model = self.env[parent_model_name]
  2030. parent_alias = query.left_join(
  2031. current_model._table, inherits_field, parent_model._table, 'id', inherits_field,
  2032. )
  2033. return parent_alias
  2034. @api.model
  2035. def _inherits_join_calc(self, alias, fname, query):
  2036. """
  2037. Adds missing table select and join clause(s) to ``query`` for reaching
  2038. the field coming from an '_inherits' parent table (no duplicates).
  2039. :param alias: name of the initial SQL alias
  2040. :param fname: name of inherited field to reach
  2041. :param query: query object on which the JOIN should be added
  2042. :return: qualified name of field, to be used in SELECT clause
  2043. """
  2044. # INVARIANT: alias is the SQL alias of model._table in query
  2045. model, field = self, self._fields[fname]
  2046. while field.inherited:
  2047. # retrieve the parent model where field is inherited from
  2048. parent_model = self.env[field.related_field.model_name]
  2049. parent_fname = field.related.split('.')[0]
  2050. # JOIN parent_model._table AS parent_alias ON alias.parent_fname = parent_alias.id
  2051. parent_alias = query.left_join(
  2052. alias, parent_fname, parent_model._table, 'id', parent_fname,
  2053. )
  2054. model, alias, field = parent_model, parent_alias, field.related_field
  2055. if field.type == 'many2many':
  2056. # special case for many2many fields: prepare a query on the comodel
  2057. # in order to reuse the mechanism _apply_ir_rules, then inject the
  2058. # query as an extra condition of the left join
  2059. comodel = self.env[field.comodel_name]
  2060. subquery = Query(self.env.cr, comodel._table)
  2061. comodel._apply_ir_rules(subquery)
  2062. # add the extra join condition only if there is an actual subquery
  2063. extra, extra_params = None, ()
  2064. if subquery.where_clause:
  2065. subquery_str, extra_params = subquery.select()
  2066. extra = '"{rhs}"."%s" IN (%s)' % (field.column2, subquery_str)
  2067. # LEFT JOIN field_relation ON
  2068. # alias.id = field_relation.field_column1
  2069. # AND field_relation.field_column2 IN (subquery)
  2070. rel_alias = query.left_join(
  2071. alias, 'id', field.relation, field.column1, field.name,
  2072. extra=extra, extra_params=extra_params,
  2073. )
  2074. return '"%s"."%s"' % (rel_alias, field.column2)
  2075. elif field.translate:
  2076. lang = self.env.lang or 'en_US'
  2077. if lang == 'en_US':
  2078. return f'"{alias}"."{fname}"->>\'en_US\''
  2079. return f'COALESCE("{alias}"."{fname}"->>\'{lang}\', "{alias}"."{fname}"->>\'en_US\')'
  2080. else:
  2081. return '"%s"."%s"' % (alias, fname)
  2082. def _parent_store_compute(self):
  2083. """ Compute parent_path field from scratch. """
  2084. if not self._parent_store:
  2085. return
  2086. # Each record is associated to a string 'parent_path', that represents
  2087. # the path from the record's root node to the record. The path is made
  2088. # of the node ids suffixed with a slash (see example below). The nodes
  2089. # in the subtree of record are the ones where 'parent_path' starts with
  2090. # the 'parent_path' of record.
  2091. #
  2092. # a node | id | parent_path
  2093. # / \ a | 42 | 42/
  2094. # ... b b | 63 | 42/63/
  2095. # / \ c | 84 | 42/63/84/
  2096. # c d d | 85 | 42/63/85/
  2097. #
  2098. # Note: the final '/' is necessary to match subtrees correctly: '42/63'
  2099. # is a prefix of '42/630', but '42/63/' is not a prefix of '42/630/'.
  2100. _logger.info('Computing parent_path for table %s...', self._table)
  2101. query = """
  2102. WITH RECURSIVE __parent_store_compute(id, parent_path) AS (
  2103. SELECT row.id, concat(row.id, '/')
  2104. FROM {table} row
  2105. WHERE row.{parent} IS NULL
  2106. UNION
  2107. SELECT row.id, concat(comp.parent_path, row.id, '/')
  2108. FROM {table} row, __parent_store_compute comp
  2109. WHERE row.{parent} = comp.id
  2110. )
  2111. UPDATE {table} row SET parent_path = comp.parent_path
  2112. FROM __parent_store_compute comp
  2113. WHERE row.id = comp.id
  2114. """.format(table=self._table, parent=self._parent_name)
  2115. self.env.cr.execute(query)
  2116. self.invalidate_model(['parent_path'])
  2117. return True
  2118. def _check_removed_columns(self, log=False):
  2119. if self._abstract:
  2120. return
  2121. # iterate on the database columns to drop the NOT NULL constraints of
  2122. # fields which were required but have been removed (or will be added by
  2123. # another module)
  2124. cr = self._cr
  2125. cols = [name for name, field in self._fields.items()
  2126. if field.store and field.column_type]
  2127. cr.execute("SELECT a.attname, a.attnotnull"
  2128. " FROM pg_class c, pg_attribute a"
  2129. " WHERE c.relname=%s"
  2130. " AND c.oid=a.attrelid"
  2131. " AND a.attisdropped=%s"
  2132. " AND pg_catalog.format_type(a.atttypid, a.atttypmod) NOT IN ('cid', 'tid', 'oid', 'xid')"
  2133. " AND a.attname NOT IN %s", (self._table, False, tuple(cols))),
  2134. for row in cr.dictfetchall():
  2135. if log:
  2136. _logger.debug("column %s is in the table %s but not in the corresponding object %s",
  2137. row['attname'], self._table, self._name)
  2138. if row['attnotnull']:
  2139. tools.drop_not_null(cr, self._table, row['attname'])
  2140. def _init_column(self, column_name):
  2141. """ Initialize the value of the given column for existing rows. """
  2142. # get the default value; ideally, we should use default_get(), but it
  2143. # fails due to ir.default not being ready
  2144. field = self._fields[column_name]
  2145. if field.default:
  2146. value = field.default(self)
  2147. value = field.convert_to_write(value, self)
  2148. value = field.convert_to_column(value, self)
  2149. else:
  2150. value = None
  2151. # Write value if non-NULL, except for booleans for which False means
  2152. # the same as NULL - this saves us an expensive query on large tables.
  2153. necessary = (value is not None) if field.type != 'boolean' else value
  2154. if necessary:
  2155. _logger.debug("Table '%s': setting default value of new column %s to %r",
  2156. self._table, column_name, value)
  2157. query = f'UPDATE "{self._table}" SET "{column_name}" = %s WHERE "{column_name}" IS NULL'
  2158. self._cr.execute(query, (value,))
  2159. @ormcache()
  2160. def _table_has_rows(self):
  2161. """ Return whether the model's table has rows. This method should only
  2162. be used when updating the database schema (:meth:`~._auto_init`).
  2163. """
  2164. self.env.cr.execute('SELECT 1 FROM "%s" LIMIT 1' % self._table)
  2165. return self.env.cr.rowcount
  2166. def _auto_init(self):
  2167. """ Initialize the database schema of ``self``:
  2168. - create the corresponding table,
  2169. - create/update the necessary columns/tables for fields,
  2170. - initialize new columns on existing rows,
  2171. - add the SQL constraints given on the model,
  2172. - add the indexes on indexed fields,
  2173. Also prepare post-init stuff to:
  2174. - add foreign key constraints,
  2175. - reflect models, fields, relations and constraints,
  2176. - mark fields to recompute on existing records.
  2177. Note: you should not override this method. Instead, you can modify
  2178. the model's database schema by overriding method :meth:`~.init`,
  2179. which is called right after this one.
  2180. """
  2181. raise_on_invalid_object_name(self._name)
  2182. # This prevents anything called by this method (in particular default
  2183. # values) from prefetching a field for which the corresponding column
  2184. # has not been added in database yet!
  2185. self = self.with_context(prefetch_fields=False)
  2186. cr = self._cr
  2187. update_custom_fields = self._context.get('update_custom_fields', False)
  2188. must_create_table = not tools.table_exists(cr, self._table)
  2189. parent_path_compute = False
  2190. if self._auto:
  2191. if must_create_table:
  2192. def make_type(field):
  2193. return field.column_type[1] + (" NOT NULL" if field.required else "")
  2194. tools.create_model_table(cr, self._table, self._description, [
  2195. (field.name, make_type(field), field.string)
  2196. for field in sorted(self._fields.values(), key=lambda f: f.column_order)
  2197. if field.name != 'id' and field.store and field.column_type
  2198. ])
  2199. if self._parent_store:
  2200. if not tools.column_exists(cr, self._table, 'parent_path'):
  2201. tools.create_column(self._cr, self._table, 'parent_path', 'VARCHAR')
  2202. parent_path_compute = True
  2203. self._check_parent_path()
  2204. if not must_create_table:
  2205. self._check_removed_columns(log=False)
  2206. # update the database schema for fields
  2207. columns = tools.table_columns(cr, self._table)
  2208. fields_to_compute = []
  2209. for field in sorted(self._fields.values(), key=lambda f: f.column_order):
  2210. if not field.store:
  2211. continue
  2212. if field.manual and not update_custom_fields:
  2213. continue # don't update custom fields
  2214. new = field.update_db(self, columns)
  2215. if new and field.compute:
  2216. fields_to_compute.append(field)
  2217. if fields_to_compute:
  2218. # mark existing records for computation now, so that computed
  2219. # required fields are flushed before the NOT NULL constraint is
  2220. # added to the database
  2221. cr.execute('SELECT id FROM "{}"'.format(self._table))
  2222. records = self.browse(row[0] for row in cr.fetchall())
  2223. if records:
  2224. for field in fields_to_compute:
  2225. _logger.info("Prepare computation of %s", field)
  2226. self.env.add_to_compute(field, records)
  2227. if self._auto:
  2228. self._add_sql_constraints()
  2229. if parent_path_compute:
  2230. self._parent_store_compute()
  2231. def init(self):
  2232. """ This method is called after :meth:`~._auto_init`, and may be
  2233. overridden to create or modify a model's database schema.
  2234. """
  2235. def _check_parent_path(self):
  2236. field = self._fields.get('parent_path')
  2237. if field is None:
  2238. _logger.error("add a field parent_path on model %r: `parent_path = fields.Char(index=True, unaccent=False)`.", self._name)
  2239. elif not field.index:
  2240. _logger.error('parent_path field on model %r should be indexed! Add index=True to the field definition.', self._name)
  2241. elif field.unaccent:
  2242. _logger.warning("parent_path field on model %r should have unaccent disabled. Add `unaccent=False` to the field definition.", self._name)
  2243. def _add_sql_constraints(self):
  2244. """ Modify this model's database table constraints so they match the one
  2245. in _sql_constraints.
  2246. """
  2247. cr = self._cr
  2248. foreign_key_re = re.compile(r'\s*foreign\s+key\b.*', re.I)
  2249. for (key, definition, message) in self._sql_constraints:
  2250. conname = '%s_%s' % (self._table, key)
  2251. current_definition = tools.constraint_definition(cr, self._table, conname)
  2252. if len(conname) > 63 and not current_definition:
  2253. _logger.info("Constraint name %r has more than 63 characters", conname)
  2254. if current_definition == definition:
  2255. continue
  2256. if current_definition:
  2257. # constraint exists but its definition may have changed
  2258. tools.drop_constraint(cr, self._table, conname)
  2259. if not definition:
  2260. # virtual constraint (e.g. implemented by a custom index)
  2261. self.pool.post_init(tools.check_index_exist, cr, conname)
  2262. elif foreign_key_re.match(definition):
  2263. self.pool.post_init(tools.add_constraint, cr, self._table, conname, definition)
  2264. else:
  2265. self.pool.post_constraint(tools.add_constraint, cr, self._table, conname, definition)
  2266. #
  2267. # Update objects that use this one to update their _inherits fields
  2268. #
  2269. @api.model
  2270. def _add_inherited_fields(self):
  2271. """ Determine inherited fields. """
  2272. if self._abstract or not self._inherits:
  2273. return
  2274. # determine which fields can be inherited
  2275. to_inherit = {
  2276. name: (parent_fname, field)
  2277. for parent_model_name, parent_fname in self._inherits.items()
  2278. for name, field in self.env[parent_model_name]._fields.items()
  2279. }
  2280. # add inherited fields that are not redefined locally
  2281. for name, (parent_fname, field) in to_inherit.items():
  2282. if name not in self._fields:
  2283. # inherited fields are implemented as related fields, with the
  2284. # following specific properties:
  2285. # - reading inherited fields should not bypass access rights
  2286. # - copy inherited fields iff their original field is copied
  2287. Field = type(field)
  2288. self._add_field(name, Field(
  2289. inherited=True,
  2290. inherited_field=field,
  2291. related=f"{parent_fname}.{name}",
  2292. related_sudo=False,
  2293. copy=field.copy,
  2294. readonly=field.readonly,
  2295. ))
  2296. @api.model
  2297. def _inherits_check(self):
  2298. for table, field_name in self._inherits.items():
  2299. field = self._fields.get(field_name)
  2300. if not field:
  2301. _logger.info('Missing many2one field definition for _inherits reference "%s" in "%s", using default one.', field_name, self._name)
  2302. from .fields import Many2one
  2303. field = Many2one(table, string="Automatically created field to link to parent %s" % table, required=True, ondelete="cascade")
  2304. self._add_field(field_name, field)
  2305. elif not (field.required and (field.ondelete or "").lower() in ("cascade", "restrict")):
  2306. _logger.warning('Field definition for _inherits reference "%s" in "%s" must be marked as "required" with ondelete="cascade" or "restrict", forcing it to required + cascade.', field_name, self._name)
  2307. field.required = True
  2308. field.ondelete = "cascade"
  2309. field.delegate = True
  2310. # reflect fields with delegate=True in dictionary self._inherits
  2311. for field in self._fields.values():
  2312. if field.type == 'many2one' and not field.related and field.delegate:
  2313. if not field.required:
  2314. _logger.warning("Field %s with delegate=True must be required.", field)
  2315. field.required = True
  2316. if field.ondelete.lower() not in ('cascade', 'restrict'):
  2317. field.ondelete = 'cascade'
  2318. type(self)._inherits = {**self._inherits, field.comodel_name: field.name}
  2319. self.pool[field.comodel_name]._inherits_children.add(self._name)
  2320. @api.model
  2321. def _prepare_setup(self):
  2322. """ Prepare the setup of the model. """
  2323. cls = type(self)
  2324. cls._setup_done = False
  2325. # changing base classes is costly, do it only when necessary
  2326. if cls.__bases__ != cls.__base_classes:
  2327. cls.__bases__ = cls.__base_classes
  2328. # reset those attributes on the model's class for _setup_fields() below
  2329. for attr in ('_rec_name', '_active_name'):
  2330. discardattr(cls, attr)
  2331. @api.model
  2332. def _setup_base(self):
  2333. """ Determine the inherited and custom fields of the model. """
  2334. cls = type(self)
  2335. if cls._setup_done:
  2336. return
  2337. # the classes that define this model, i.e., the ones that are not
  2338. # registry classes; the purpose of this attribute is to behave as a
  2339. # cache of [c for c in cls.mro() if not is_registry_class(c))], which
  2340. # is heavily used in function fields.resolve_mro()
  2341. cls._model_classes = tuple(c for c in cls.mro() if getattr(c, 'pool', None) is None)
  2342. # 1. determine the proper fields of the model: the fields defined on the
  2343. # class and magic fields, not the inherited or custom ones
  2344. # retrieve fields from parent classes, and duplicate them on cls to
  2345. # avoid clashes with inheritance between different models
  2346. for name in cls._fields:
  2347. discardattr(cls, name)
  2348. cls._fields.clear()
  2349. # collect the definitions of each field (base definition + overrides)
  2350. definitions = defaultdict(list)
  2351. for klass in reversed(cls._model_classes):
  2352. # this condition is an optimization of is_definition_class(klass)
  2353. if isinstance(klass, MetaModel):
  2354. for field in klass._field_definitions:
  2355. definitions[field.name].append(field)
  2356. for name, fields_ in definitions.items():
  2357. if f'{cls._name}.{name}' in cls.pool._database_translated_fields:
  2358. # the field is currently translated in the database; ensure the
  2359. # field is translated to avoid converting its column to varchar
  2360. # and losing data
  2361. translate = next((
  2362. field.args['translate'] for field in reversed(fields_) if 'translate' in field.args
  2363. ), False)
  2364. if not translate:
  2365. # patch the field definition by adding an override
  2366. _logger.debug("Patching %s.%s with translate=True", cls._name, name)
  2367. fields_.append(type(fields_[0])(translate=True))
  2368. if len(fields_) == 1 and fields_[0]._direct and fields_[0].model_name == cls._name:
  2369. cls._fields[name] = fields_[0]
  2370. else:
  2371. Field = type(fields_[-1])
  2372. self._add_field(name, Field(_base_fields=fields_))
  2373. # 2. add manual fields
  2374. if self.pool._init_modules:
  2375. self.env['ir.model.fields']._add_manual_fields(self)
  2376. # 3. make sure that parent models determine their own fields, then add
  2377. # inherited fields to cls
  2378. self._inherits_check()
  2379. for parent in self._inherits:
  2380. self.env[parent]._setup_base()
  2381. self._add_inherited_fields()
  2382. # 4. initialize more field metadata
  2383. cls._setup_done = True
  2384. for field in cls._fields.values():
  2385. field.prepare_setup()
  2386. # 5. determine and validate rec_name
  2387. if cls._rec_name:
  2388. assert cls._rec_name in cls._fields, \
  2389. "Invalid _rec_name=%r for model %r" % (cls._rec_name, cls._name)
  2390. elif 'name' in cls._fields:
  2391. cls._rec_name = 'name'
  2392. elif cls._custom and 'x_name' in cls._fields:
  2393. cls._rec_name = 'x_name'
  2394. # 6. determine and validate active_name
  2395. if cls._active_name:
  2396. assert (cls._active_name in cls._fields
  2397. and cls._active_name in ('active', 'x_active')), \
  2398. ("Invalid _active_name=%r for model %r; only 'active' and "
  2399. "'x_active' are supported and the field must be present on "
  2400. "the model") % (cls._active_name, cls._name)
  2401. elif 'active' in cls._fields:
  2402. cls._active_name = 'active'
  2403. elif 'x_active' in cls._fields:
  2404. cls._active_name = 'x_active'
  2405. @api.model
  2406. def _setup_fields(self):
  2407. """ Setup the fields, except for recomputation triggers. """
  2408. cls = type(self)
  2409. # set up fields
  2410. bad_fields = []
  2411. for name, field in cls._fields.items():
  2412. try:
  2413. field.setup(self)
  2414. except Exception:
  2415. if field.base_field.manual:
  2416. # Something goes wrong when setup a manual field.
  2417. # This can happen with related fields using another manual many2one field
  2418. # that hasn't been loaded because the comodel does not exist yet.
  2419. # This can also be a manual function field depending on not loaded fields yet.
  2420. bad_fields.append(name)
  2421. continue
  2422. raise
  2423. for name in bad_fields:
  2424. self._pop_field(name)
  2425. @api.model
  2426. def _setup_complete(self):
  2427. """ Setup recomputation triggers, and complete the model setup. """
  2428. cls = type(self)
  2429. # register constraints and onchange methods
  2430. cls._init_constraints_onchanges()
  2431. @api.model
  2432. def fields_get(self, allfields=None, attributes=None):
  2433. """ fields_get([allfields][, attributes])
  2434. Return the definition of each field.
  2435. The returned value is a dictionary (indexed by field name) of
  2436. dictionaries. The _inherits'd fields are included. The string, help,
  2437. and selection (if present) attributes are translated.
  2438. :param list allfields: fields to document, all if empty or not provided
  2439. :param list attributes: attributes to return for each field, all if empty or not provided
  2440. :return: dictionary mapping field names to a dictionary mapping attributes to values.
  2441. :rtype: dict
  2442. """
  2443. res = {}
  2444. for fname, field in self._fields.items():
  2445. if allfields and fname not in allfields:
  2446. continue
  2447. if field.groups and not self.env.su and not self.user_has_groups(field.groups):
  2448. continue
  2449. description = field.get_description(self.env, attributes=attributes)
  2450. res[fname] = description
  2451. return res
  2452. @api.model
  2453. def check_field_access_rights(self, operation, fields):
  2454. """Check the user access rights on the given fields.
  2455. :param str operation: one of ``create``, ``read``, ``write``, ``unlink``
  2456. :param fields: names of the fields
  2457. :type fields: list or None
  2458. :return: provided fields if fields is truthy (or the fields
  2459. readable by the current user).
  2460. :rtype: list
  2461. :raise AccessError: if the user is not allowed to access
  2462. the provided fields.
  2463. """
  2464. if self.env.su:
  2465. return fields or list(self._fields)
  2466. def valid(fname):
  2467. """ determine whether user has access to field ``fname`` """
  2468. field = self._fields.get(fname)
  2469. if field and field.groups:
  2470. return self.user_has_groups(field.groups)
  2471. else:
  2472. return True
  2473. if not fields:
  2474. fields = [name for name in self._fields if valid(name)]
  2475. else:
  2476. invalid_fields = {name for name in fields if not valid(name)}
  2477. if invalid_fields:
  2478. _logger.info('Access Denied by ACLs for operation: %s, uid: %s, model: %s, fields: %s',
  2479. operation, self._uid, self._name, ', '.join(invalid_fields))
  2480. description = self.env['ir.model']._get(self._name).name
  2481. if not self.env.user.has_group('base.group_no_one'):
  2482. raise AccessError(_(
  2483. "You do not have enough rights to access the fields \"%(fields)s\""
  2484. " on %(document_kind)s (%(document_model)s). "
  2485. "Please contact your system administrator."
  2486. "\n\n(Operation: %(operation)s)",
  2487. fields=','.join(list(invalid_fields)),
  2488. document_kind=description,
  2489. document_model=self._name,
  2490. operation=operation,
  2491. ))
  2492. def format_groups(field):
  2493. if field.groups == '.':
  2494. return _("always forbidden")
  2495. anyof = self.env['res.groups']
  2496. noneof = self.env['res.groups']
  2497. for g in field.groups.split(','):
  2498. if g.startswith('!'):
  2499. noneof |= self.env.ref(g[1:])
  2500. else:
  2501. anyof |= self.env.ref(g)
  2502. strs = []
  2503. if anyof:
  2504. strs.append(_(
  2505. "allowed for groups %s",
  2506. ', '.join(
  2507. anyof.sorted(lambda g: g.id)
  2508. .mapped(lambda g: repr(g.display_name))
  2509. ),
  2510. ))
  2511. if noneof:
  2512. strs.append(_(
  2513. "forbidden for groups %s",
  2514. ', '.join(
  2515. noneof.sorted(lambda g: g.id)
  2516. .mapped(lambda g: repr(g.display_name))
  2517. ),
  2518. ))
  2519. return '; '.join(strs)
  2520. raise AccessError(_(
  2521. "The requested operation can not be completed due to security restrictions."
  2522. "\n\nDocument type: %(document_kind)s (%(document_model)s)"
  2523. "\nOperation: %(operation)s"
  2524. "\nUser: %(user)s"
  2525. "\nFields:"
  2526. "\n%(fields_list)s",
  2527. document_model=self._name,
  2528. document_kind=description or self._name,
  2529. operation=operation,
  2530. user=self._uid,
  2531. fields_list='\n'.join(
  2532. '- %s (%s)' % (f, format_groups(self._fields[f]))
  2533. for f in sorted(invalid_fields)
  2534. ),
  2535. ))
  2536. return fields
  2537. def read(self, fields=None, load='_classic_read'):
  2538. """ read([fields])
  2539. Reads the requested fields for the records in ``self``, low-level/RPC
  2540. method.
  2541. :param list fields: field names to return (default is all fields)
  2542. :param str load: loading mode, currently the only option is to set to
  2543. ``None`` to avoid loading the ``name_get`` of m2o fields
  2544. :return: a list of dictionaries mapping field names to their values,
  2545. with one dictionary per record
  2546. :rtype: list
  2547. :raise AccessError: if user is not allowed to access requested information
  2548. :raise ValueError: if a requested field does not exist
  2549. """
  2550. fields = self.check_field_access_rights('read', fields)
  2551. # fetch stored fields from the database to the cache
  2552. stored_fields = OrderedSet()
  2553. for name in fields:
  2554. field = self._fields.get(name)
  2555. if not field:
  2556. raise ValueError("Invalid field %r on model %r" % (name, self._name))
  2557. if field.store:
  2558. stored_fields.add(name)
  2559. elif field.compute:
  2560. # optimization: prefetch direct field dependencies
  2561. for dotname in self.pool.field_depends[field]:
  2562. f = self._fields[dotname.split('.')[0]]
  2563. if f.prefetch is True and (not f.groups or self.user_has_groups(f.groups)):
  2564. stored_fields.add(f.name)
  2565. self._read(stored_fields)
  2566. return self._read_format(fnames=fields, load=load)
  2567. def update_field_translations(self, field_name, translations):
  2568. """ Update the values of a translated field.
  2569. :param str field_name: field name
  2570. :param dict translations: if the field has ``translate=True``, it should be a dictionary
  2571. like ``{lang: new_value}``; if ``translate`` is a callable, it should be like
  2572. ``{lang: {old_term: new_term}}``
  2573. """
  2574. return self._update_field_translations(field_name, translations)
  2575. def _update_field_translations(self, field_name, translations, digest=None):
  2576. """ Private implementation of :meth:`~update_field_translations`.
  2577. The main difference comes from the extra function ``digest``, which may
  2578. be used to make identifiers for old terms.
  2579. :param dict translations:
  2580. if the field has ``translate=True``, it should be a dictionary like ``{lang: new_value}``
  2581. new_value: str: the new translation for lang
  2582. new_value: False: void the current translation for lang and fallback to current en_US value
  2583. if ``translate`` is a callable, it should be like
  2584. ``{lang: {old_term: new_term}}``, or ``{lang: {digest(old_term): new_term}}`` when ``digest`` is callable
  2585. new_value: str: the new translation of old_term for lang
  2586. :param digest: an optional digest function for the old_term
  2587. """
  2588. self.ensure_one()
  2589. self.check_access_rights('write')
  2590. self.check_field_access_rights('write', [field_name])
  2591. self.check_access_rule('write')
  2592. valid_langs = set(code for code, _ in self.env['res.lang'].get_installed()) | {'en_US'}
  2593. missing_langs = set(translations) - valid_langs
  2594. if missing_langs:
  2595. raise UserError(
  2596. _("The following languages are not activated: %(missing_names)s",
  2597. missing_names=', '.join(missing_langs))
  2598. )
  2599. field = self._fields[field_name]
  2600. if not field.translate:
  2601. return False # or raise error
  2602. if not field.store and not field.related and field.compute:
  2603. # a non-related non-stored computed field cannot be translated, even if it has inverse function
  2604. return False
  2605. # Strictly speaking, a translated related/computed field cannot be stored
  2606. # because the compute function only support one language
  2607. # `not field.store` is a redundant logic.
  2608. # But some developers store translated related fields.
  2609. # In these cases, only all translations of the first stored translation field will be updated
  2610. # For other stored related translated field, the translation for the flush language will be updated
  2611. if field.related and not field.store:
  2612. related_path, field_name = field.related.rsplit(".", 1)
  2613. return self.mapped(related_path)._update_field_translations(field_name, translations, digest)
  2614. if field.translate is True:
  2615. # falsy values (except emtpy str) are used to void the corresponding translation
  2616. if any(translation and not isinstance(translation, str) for translation in translations.values()):
  2617. raise UserError(_("Translations for model translated fields only accept falsy values and str"))
  2618. value_en = translations.get('en_US', True)
  2619. if not value_en and value_en != '':
  2620. translations.pop('en_US')
  2621. translations = {
  2622. lang: translation if isinstance(translation, str) else None
  2623. for lang, translation in translations.items()
  2624. }
  2625. if not translations:
  2626. return False
  2627. translation_fallback = translations['en_US'] if translations.get('en_US') is not None \
  2628. else translations[self.env.lang] if translations.get(self.env.lang) is not None \
  2629. else next((v for v in translations.values() if v is not None), None)
  2630. self.invalidate_recordset([field_name])
  2631. self._cr.execute(f'''
  2632. UPDATE "{self._table}"
  2633. SET "{field_name}" = NULLIF(
  2634. jsonb_strip_nulls(%s || COALESCE("{field_name}", '{{}}'::jsonb) || %s),
  2635. '{{}}'::jsonb)
  2636. WHERE id = %s
  2637. ''', (Json({'en_US': translation_fallback}), Json(translations), self.id))
  2638. else:
  2639. # Note:
  2640. # update terms in 'en_US' will not change its value other translated values
  2641. # record_en = Model_en.create({'html': '<div>English 1</div><div>English 2<div/>'
  2642. # record_en.update_field_translations('html', {'fr_FR': {'English 2': 'French 2'}}
  2643. # record_en.update_field_translations('html', {'en_US': {'English 1': 'English 3'}}
  2644. # assert record_en == '<div>English 3</div><div>English 2<div/>'
  2645. # assert record_fr.with_context(lang='fr_FR') == '<div>English 1</div><div>French 2<div/>'
  2646. # assert record_nl.with_context(lang='nl_NL') == '<div>English 3</div><div>English 2<div/>'
  2647. old_translations = field._get_stored_translations(self)
  2648. if not old_translations:
  2649. return False
  2650. new_translations = old_translations
  2651. old_value_en = old_translations.get('en_US')
  2652. for lang, translation in translations.items():
  2653. old_value = new_translations.get(lang, old_value_en)
  2654. if digest:
  2655. old_terms = field.get_trans_terms(old_value)
  2656. old_terms_digested2value = {digest(old_term): old_term for old_term in old_terms}
  2657. translation = {
  2658. old_terms_digested2value[key]: value
  2659. for key, value in translation.items()
  2660. if key in old_terms_digested2value
  2661. }
  2662. new_translations[lang] = field.translate(translation.get, old_value)
  2663. self.env.cache.update_raw(self, field, [new_translations], dirty=True)
  2664. # the following write is incharge of
  2665. # 1. mark field as modified
  2666. # 2. execute logics in the override `write` method
  2667. # 3. update write_date of the record if exists to support 't-cache'
  2668. # even if the value in cache is the same as the value written
  2669. self[field_name] = self[field_name]
  2670. return True
  2671. def get_field_translations(self, field_name, langs=None):
  2672. """ get model/model_term translations for records
  2673. :param str field_name: field name
  2674. :param list langs: languages
  2675. :return: (translations, context) where
  2676. translations: list of dicts like [{"lang": lang, "source": source_term, "value": value_term}]
  2677. context: {"translation_type": "text"/"char", "translation_show_source": True/False}
  2678. """
  2679. self.ensure_one()
  2680. field = self._fields[field_name]
  2681. # We don't forbid reading inactive/non-existing languages,
  2682. langs = set(langs or [l[0] for l in self.env['res.lang'].get_installed()])
  2683. val_en = self.with_context(lang='en_US')[field_name]
  2684. if not callable(field.translate):
  2685. translations = [{
  2686. 'lang': lang,
  2687. 'source': val_en,
  2688. 'value': self.with_context(lang=lang)[field_name]
  2689. } for lang in langs]
  2690. else:
  2691. translation_dictionary = field.get_translation_dictionary(
  2692. val_en, {lang: self.with_context(lang=lang)[field_name] for lang in langs}
  2693. )
  2694. translations = [{
  2695. 'lang': lang,
  2696. 'source': term_en,
  2697. 'value': term_lang if term_lang != term_en else ''
  2698. } for term_en, translations in translation_dictionary.items()
  2699. for lang, term_lang in translations.items()]
  2700. context = {}
  2701. context['translation_type'] = 'text' if field.type in ['text', 'html'] else 'char'
  2702. context['translation_show_source'] = callable(field.translate)
  2703. return translations, context
  2704. def _get_base_lang(self):
  2705. """ Returns the base language of the record. """
  2706. self.ensure_one()
  2707. return 'en_US'
  2708. def _read_format(self, fnames, load='_classic_read'):
  2709. """Returns a list of dictionaries mapping field names to their values,
  2710. with one dictionary per record that exists.
  2711. The output format is similar to the one expected from the `read` method.
  2712. The current method is different from `read` because it retrieves its
  2713. values from the cache without doing a query when it is avoidable.
  2714. """
  2715. data = [(record, {'id': record._ids[0]}) for record in self]
  2716. use_name_get = (load == '_classic_read')
  2717. for name in fnames:
  2718. convert = self._fields[name].convert_to_read
  2719. for record, vals in data:
  2720. # missing records have their vals empty
  2721. if not vals:
  2722. continue
  2723. try:
  2724. vals[name] = convert(record[name], record, use_name_get)
  2725. except MissingError:
  2726. vals.clear()
  2727. result = [vals for record, vals in data if vals]
  2728. return result
  2729. def _fetch_field(self, field):
  2730. """ Read from the database in order to fetch ``field`` (:class:`Field`
  2731. instance) for ``self`` in cache.
  2732. """
  2733. self.check_field_access_rights('read', [field.name])
  2734. # determine which fields can be prefetched
  2735. if self._context.get('prefetch_fields', True) and field.prefetch:
  2736. fnames = [
  2737. name
  2738. for name, f in self._fields.items()
  2739. # select fields with the same prefetch group
  2740. if f.prefetch == field.prefetch
  2741. # discard fields with groups that the user may not access
  2742. if not (f.groups and not self.user_has_groups(f.groups))
  2743. ]
  2744. if field.name not in fnames:
  2745. fnames.append(field.name)
  2746. else:
  2747. fnames = [field.name]
  2748. self._read(fnames)
  2749. def _read(self, field_names):
  2750. """ Read the given fields of the records in ``self`` from the database,
  2751. and store them in cache. Skip fields that are not stored.
  2752. :param field_names: list of field names to read
  2753. """
  2754. if not self:
  2755. return
  2756. self.check_access_rights('read')
  2757. # determine columns fields and those with their own read() method
  2758. column_fields = []
  2759. other_fields = []
  2760. translated_field_names = []
  2761. for name in field_names:
  2762. if name == 'id':
  2763. continue
  2764. field = self._fields.get(name)
  2765. if not field:
  2766. _logger.warning("%s._read() with unknown field %r", self._name, name)
  2767. continue
  2768. if field.base_field.store and field.base_field.column_type:
  2769. column_fields.append(field)
  2770. elif field.store and not field.column_type:
  2771. # non-column fields: for the sake of simplicity, we ignore inherited fields
  2772. other_fields.append(field)
  2773. if field.store and field.translate:
  2774. translated_field_names.append(field.name)
  2775. if field.type == 'properties':
  2776. # force calling fields.read for properties field because
  2777. # we want to read all relational properties in batch
  2778. # (and check their existence in batch as well)
  2779. other_fields.append(field)
  2780. if column_fields:
  2781. cr, context = self.env.cr, self.env.context
  2782. # If a read() follows a write(), we must flush the updates that have
  2783. # an impact on checking security rules, as they are injected into
  2784. # the query. However, we don't need to flush the fields to fetch,
  2785. # as explained below when putting values in cache.
  2786. # Since only one language translation is fetched from database,
  2787. # we must flush these translated fields before read
  2788. # E.g. in database, the {'en_US': 'English'},
  2789. # write record.with_context(lang='en_US').name = 'English2'
  2790. # then record.with_context(lang='fr_FR').name => cache miss => _read
  2791. # 'English2'should is flushed before query as it is the fallback of empty 'fr_FR'
  2792. if translated_field_names:
  2793. self.flush_recordset(translated_field_names)
  2794. self._flush_search([], order='id')
  2795. # make a query object for selecting ids, and apply security rules to it
  2796. query = Query(cr, self._table, self._table_query)
  2797. self._apply_ir_rules(query, 'read')
  2798. # the query may involve several tables: we need fully-qualified names
  2799. def qualify(field):
  2800. qname = self._inherits_join_calc(self._table, field.name, query)
  2801. if field.type == 'binary' and (
  2802. context.get('bin_size') or context.get('bin_size_' + field.name)):
  2803. # PG 9.2 introduces conflicting pg_size_pretty(numeric) -> need ::cast
  2804. qname = f'pg_size_pretty(length({qname})::bigint)'
  2805. return f'{qname} AS "{field.name}"'
  2806. # selected fields are: 'id' followed by column_fields
  2807. qual_names = [qualify(field) for field in [self._fields['id']] + column_fields]
  2808. # determine the actual query to execute (last parameter is added below)
  2809. query.add_where(f'"{self._table}".id IN %s')
  2810. query_str, params = query.select(*qual_names)
  2811. result = []
  2812. for sub_ids in cr.split_for_in_conditions(self.ids):
  2813. cr.execute(query_str, params + [sub_ids])
  2814. result += cr.fetchall()
  2815. else:
  2816. try:
  2817. self.check_access_rule('read')
  2818. except MissingError:
  2819. # Method _read() should never raise a MissingError, but method
  2820. # check_access_rule() can, because it must read fields on self.
  2821. # So we restrict 'self' to existing records (to avoid an extra
  2822. # exists() at the end of the method).
  2823. self = self.exists()
  2824. self.check_access_rule('read')
  2825. result = [(id_,) for id_ in self.ids]
  2826. fetched = self.browse()
  2827. if result:
  2828. # result = [(id1, a1, b1), (id2, a2, b2), ...]
  2829. # column_values = [(id1, id2, ...), (a1, a2, ...), (b1, b2, ...)]
  2830. column_values = zip(*result)
  2831. ids = next(column_values)
  2832. fetched = self.browse(ids)
  2833. # If we assume that the value of a pending update is in cache, we
  2834. # can avoid flushing pending updates if the fetched values do not
  2835. # overwrite values in cache.
  2836. for field in column_fields:
  2837. values = next(column_values)
  2838. # store values in cache, but without overwriting
  2839. self.env.cache.insert_missing(fetched, field, values)
  2840. # process non-column fields
  2841. for field in other_fields:
  2842. field.read(fetched)
  2843. # possibly raise exception for the records that could not be read
  2844. missing = self - fetched
  2845. if missing:
  2846. extras = fetched - self
  2847. if extras:
  2848. raise AccessError(_(
  2849. "Database fetch misses ids (%(missing)s) and has extra ids (%(extra)s),"
  2850. " may be caused by a type incoherence in a previous request",
  2851. missing=missing._ids,
  2852. extra=extras._ids,
  2853. ))
  2854. # mark non-existing records in missing
  2855. forbidden = missing.exists()
  2856. if forbidden:
  2857. raise self.env['ir.rule']._make_access_error('read', forbidden)
  2858. def get_metadata(self):
  2859. """Return some metadata about the given records.
  2860. :return: list of ownership dictionaries for each requested record
  2861. :rtype: list of dictionaries with the following keys:
  2862. * id: object id
  2863. * create_uid: user who created the record
  2864. * create_date: date when the record was created
  2865. * write_uid: last user who changed the record
  2866. * write_date: date of the last change to the record
  2867. * xmlid: XML ID to use to refer to this record (if there is one), in format ``module.name``
  2868. * xmlids: list of dict with xmlid in format ``module.name``, and noupdate as boolean
  2869. * noupdate: A boolean telling if the record will be updated or not
  2870. """
  2871. IrModelData = self.env['ir.model.data'].sudo()
  2872. if self._log_access:
  2873. res = self.read(LOG_ACCESS_COLUMNS)
  2874. else:
  2875. res = [{'id': x} for x in self.ids]
  2876. xml_data = defaultdict(list)
  2877. imds = IrModelData.search_read(
  2878. [('model', '=', self._name), ('res_id', 'in', self.ids)],
  2879. ['res_id', 'noupdate', 'module', 'name'],
  2880. order='id DESC'
  2881. )
  2882. for imd in imds:
  2883. xml_data[imd['res_id']].append({
  2884. 'xmlid': "%s.%s" % (imd['module'], imd['name']),
  2885. 'noupdate': imd['noupdate'],
  2886. })
  2887. for r in res:
  2888. main = xml_data.get(r['id'], [{}])[-1]
  2889. r['xmlid'] = main.get('xmlid', False)
  2890. r['noupdate'] = main.get('noupdate', False)
  2891. r['xmlids'] = xml_data.get(r['id'], [])[::-1]
  2892. return res
  2893. def get_base_url(self):
  2894. """ Return rooturl for a specific record.
  2895. By default, it returns the ir.config.parameter of base_url
  2896. but it can be overridden by model.
  2897. :return: the base url for this record
  2898. :rtype: str
  2899. """
  2900. if len(self) > 1:
  2901. raise ValueError("Expected singleton or no record: %s" % self)
  2902. return self.env['ir.config_parameter'].sudo().get_param('web.base.url')
  2903. def _check_company(self, fnames=None):
  2904. """ Check the companies of the values of the given field names.
  2905. :param list fnames: names of relational fields to check
  2906. :raises UserError: if the `company_id` of the value of any field is not
  2907. in `[False, self.company_id]` (or `self` if
  2908. :class:`~odoo.addons.base.models.res_company`).
  2909. For :class:`~odoo.addons.base.models.res_users` relational fields,
  2910. verifies record company is in `company_ids` fields.
  2911. User with main company A, having access to company A and B, could be
  2912. assigned or linked to records in company B.
  2913. """
  2914. if fnames is None:
  2915. fnames = self._fields
  2916. regular_fields = []
  2917. property_fields = []
  2918. for name in fnames:
  2919. field = self._fields[name]
  2920. if field.relational and field.check_company and \
  2921. 'company_id' in self.env[field.comodel_name]:
  2922. if not field.company_dependent:
  2923. regular_fields.append(name)
  2924. else:
  2925. property_fields.append(name)
  2926. if not (regular_fields or property_fields):
  2927. return
  2928. inconsistencies = []
  2929. for record in self:
  2930. company = record.company_id if record._name != 'res.company' else record
  2931. # The first part of the check verifies that all records linked via relation fields are compatible
  2932. # with the company of the origin document, i.e. `self.account_id.company_id == self.company_id`
  2933. for name in regular_fields:
  2934. corecord = record.sudo()[name]
  2935. # Special case with `res.users` since an user can belong to multiple companies.
  2936. if corecord._name == 'res.users' and corecord.company_ids:
  2937. if not (company <= corecord.company_ids):
  2938. inconsistencies.append((record, name, corecord))
  2939. elif not (corecord.company_id <= company):
  2940. inconsistencies.append((record, name, corecord))
  2941. # The second part of the check (for property / company-dependent fields) verifies that the records
  2942. # linked via those relation fields are compatible with the company that owns the property value, i.e.
  2943. # the company for which the value is being assigned, i.e:
  2944. # `self.property_account_payable_id.company_id == self.env.company
  2945. company = self.env.company
  2946. for name in property_fields:
  2947. # Special case with `res.users` since an user can belong to multiple companies.
  2948. corecord = record.sudo()[name]
  2949. if corecord._name == 'res.users' and corecord.company_ids:
  2950. if not (company <= corecord.company_ids):
  2951. inconsistencies.append((record, name, corecord))
  2952. elif not (corecord.company_id <= company):
  2953. inconsistencies.append((record, name, corecord))
  2954. if inconsistencies:
  2955. lines = [_("Incompatible companies on records:")]
  2956. company_msg = _lt("- Record is company %(company)r and %(field)r (%(fname)s: %(values)s) belongs to another company.")
  2957. record_msg = _lt("- %(record)r belongs to company %(company)r and %(field)r (%(fname)s: %(values)s) belongs to another company.")
  2958. for record, name, corecords in inconsistencies[:5]:
  2959. if record._name == 'res.company':
  2960. msg, company = company_msg, record
  2961. else:
  2962. msg, company = record_msg, record.company_id
  2963. field = self.env['ir.model.fields']._get(self._name, name)
  2964. lines.append(str(msg) % {
  2965. 'record': record.display_name,
  2966. 'company': company.display_name,
  2967. 'field': field.field_description,
  2968. 'fname': field.name,
  2969. 'values': ", ".join(repr(rec.display_name) for rec in corecords),
  2970. })
  2971. raise UserError("\n".join(lines))
  2972. @api.model
  2973. def check_access_rights(self, operation, raise_exception=True):
  2974. """ Verify that the given operation is allowed for the current user accord to ir.model.access.
  2975. :param str operation: one of ``create``, ``read``, ``write``, ``unlink``
  2976. :param bool raise_exception: whether an exception should be raise if operation is forbidden
  2977. :return: whether the operation is allowed
  2978. :rtype: bool
  2979. :raise AccessError: if the operation is forbidden and raise_exception is True
  2980. """
  2981. return self.env['ir.model.access'].check(self._name, operation, raise_exception)
  2982. def check_access_rule(self, operation):
  2983. """ Verify that the given operation is allowed for the current user according to ir.rules.
  2984. :param str operation: one of ``create``, ``read``, ``write``, ``unlink``
  2985. :return: None if the operation is allowed
  2986. :raise UserError: if current ``ir.rules`` do not permit this operation.
  2987. """
  2988. if self.env.su:
  2989. return
  2990. # SQL Alternative if computing in-memory is too slow for large dataset
  2991. # invalid = self - self._filter_access_rules(operation)
  2992. invalid = self - self._filter_access_rules_python(operation)
  2993. if not invalid:
  2994. return
  2995. forbidden = invalid.exists()
  2996. if forbidden:
  2997. # the invalid records are (partially) hidden by access rules
  2998. raise self.env['ir.rule']._make_access_error(operation, forbidden)
  2999. # If we get here, the invalid records are not in the database.
  3000. if operation in ('read', 'unlink'):
  3001. # No need to warn about deleting an already deleted record.
  3002. # And no error when reading a record that was deleted, to prevent spurious
  3003. # errors for non-transactional search/read sequences coming from clients.
  3004. return
  3005. _logger.info('Failed operation on deleted record(s): %s, uid: %s, model: %s', operation, self._uid, self._name)
  3006. raise MissingError(
  3007. _('One of the documents you are trying to access has been deleted, please try again after refreshing.')
  3008. + '\n\n({} {}, {} {}, {} {}, {} {})'.format(
  3009. _('Document type:'), self._name, _('Operation:'), operation,
  3010. _('Records:'), invalid.ids[:6], _('User:'), self._uid,
  3011. )
  3012. )
  3013. def _filter_access_rules(self, operation):
  3014. """ Return the subset of ``self`` for which ``operation`` is allowed. """
  3015. if self.env.su:
  3016. return self
  3017. if not self._ids:
  3018. return self
  3019. query = Query(self.env.cr, self._table, self._table_query)
  3020. self._apply_ir_rules(query, operation)
  3021. if not query.where_clause:
  3022. return self
  3023. # determine ids in database that satisfy ir.rules
  3024. valid_ids = set()
  3025. query.add_where(f'"{self._table}".id IN %s')
  3026. query_str, params = query.select()
  3027. self._flush_search([])
  3028. for sub_ids in self._cr.split_for_in_conditions(self.ids):
  3029. self._cr.execute(query_str, params + [sub_ids])
  3030. valid_ids.update(row[0] for row in self._cr.fetchall())
  3031. # return new ids without origin and ids with origin in valid_ids
  3032. return self.browse([
  3033. it
  3034. for it in self._ids
  3035. if not (it or it.origin) or (it or it.origin) in valid_ids
  3036. ])
  3037. def _filter_access_rules_python(self, operation):
  3038. dom = self.env['ir.rule']._compute_domain(self._name, operation)
  3039. return self.sudo().filtered_domain(dom or [])
  3040. def unlink(self):
  3041. """ unlink()
  3042. Deletes the records in ``self``.
  3043. :raise AccessError: if the user is not allowed to delete all the given records
  3044. :raise UserError: if the record is default property for other records
  3045. """
  3046. if not self:
  3047. return True
  3048. self.check_access_rights('unlink')
  3049. self.check_access_rule('unlink')
  3050. from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
  3051. for func in self._ondelete_methods:
  3052. # func._ondelete is True if it should be called during uninstallation
  3053. if func._ondelete or not self._context.get(MODULE_UNINSTALL_FLAG):
  3054. func(self)
  3055. # TOFIX: this avoids an infinite loop when trying to recompute a
  3056. # field, which triggers the recomputation of another field using the
  3057. # same compute function, which then triggers again the computation
  3058. # of those two fields
  3059. for field in self._fields.values():
  3060. self.env.remove_to_compute(field, self)
  3061. self.env.flush_all()
  3062. cr = self._cr
  3063. Data = self.env['ir.model.data'].sudo().with_context({})
  3064. Defaults = self.env['ir.default'].sudo()
  3065. Property = self.env['ir.property'].sudo()
  3066. Attachment = self.env['ir.attachment'].sudo()
  3067. ir_property_unlink = Property
  3068. ir_model_data_unlink = Data
  3069. ir_attachment_unlink = Attachment
  3070. # mark fields that depend on 'self' to recompute them after 'self' has
  3071. # been deleted (like updating a sum of lines after deleting one line)
  3072. with self.env.protecting(self._fields.values(), self):
  3073. self.modified(self._fields, before=True)
  3074. for sub_ids in cr.split_for_in_conditions(self.ids):
  3075. records = self.browse(sub_ids)
  3076. # Check if the records are used as default properties.
  3077. refs = [f'{self._name},{id_}' for id_ in sub_ids]
  3078. if Property.search([('res_id', '=', False), ('value_reference', 'in', refs)], limit=1):
  3079. raise UserError(_('Unable to delete this document because it is used as a default property'))
  3080. # Delete the records' properties.
  3081. ir_property_unlink |= Property.search([('res_id', 'in', refs)])
  3082. query = f'DELETE FROM "{self._table}" WHERE id IN %s'
  3083. cr.execute(query, (sub_ids,))
  3084. # Removing the ir_model_data reference if the record being deleted
  3085. # is a record created by xml/csv file, as these are not connected
  3086. # with real database foreign keys, and would be dangling references.
  3087. #
  3088. # Note: the following steps are performed as superuser to avoid
  3089. # access rights restrictions, and with no context to avoid possible
  3090. # side-effects during admin calls.
  3091. data = Data.search([('model', '=', self._name), ('res_id', 'in', sub_ids)])
  3092. ir_model_data_unlink |= data
  3093. # For the same reason, remove the defaults having some of the
  3094. # records as value
  3095. Defaults.discard_records(records)
  3096. # For the same reason, remove the relevant records in ir_attachment
  3097. # (the search is performed with sql as the search method of
  3098. # ir_attachment is overridden to hide attachments of deleted
  3099. # records)
  3100. query = 'SELECT id FROM ir_attachment WHERE res_model=%s AND res_id IN %s'
  3101. cr.execute(query, (self._name, sub_ids))
  3102. ir_attachment_unlink |= Attachment.browse(row[0] for row in cr.fetchall())
  3103. # invalidate the *whole* cache, since the orm does not handle all
  3104. # changes made in the database, like cascading delete!
  3105. self.env.invalidate_all(flush=False)
  3106. if ir_property_unlink:
  3107. ir_property_unlink.unlink()
  3108. if ir_model_data_unlink:
  3109. ir_model_data_unlink.unlink()
  3110. if ir_attachment_unlink:
  3111. ir_attachment_unlink.unlink()
  3112. # DLE P93: flush after the unlink, for recompute fields depending on
  3113. # the modified of the unlink
  3114. self.env.flush_all()
  3115. # auditing: deletions are infrequent and leave no trace in the database
  3116. _unlink.info('User #%s deleted %s records with IDs: %r', self._uid, self._name, self.ids)
  3117. return True
  3118. def write(self, vals):
  3119. """ write(vals)
  3120. Updates all records in ``self`` with the provided values.
  3121. :param dict vals: fields to update and the value to set on them
  3122. :raise AccessError: if user is not allowed to modify the specified records/fields
  3123. :raise ValidationError: if invalid values are specified for selection fields
  3124. :raise UserError: if a loop would be created in a hierarchy of objects a result of the operation (such as setting an object as its own parent)
  3125. * For numeric fields (:class:`~odoo.fields.Integer`,
  3126. :class:`~odoo.fields.Float`) the value should be of the
  3127. corresponding type
  3128. * For :class:`~odoo.fields.Boolean`, the value should be a
  3129. :class:`python:bool`
  3130. * For :class:`~odoo.fields.Selection`, the value should match the
  3131. selection values (generally :class:`python:str`, sometimes
  3132. :class:`python:int`)
  3133. * For :class:`~odoo.fields.Many2one`, the value should be the
  3134. database identifier of the record to set
  3135. * The expected value of a :class:`~odoo.fields.One2many` or
  3136. :class:`~odoo.fields.Many2many` relational field is a list of
  3137. :class:`~odoo.fields.Command` that manipulate the relation the
  3138. implement. There are a total of 7 commands:
  3139. :meth:`~odoo.fields.Command.create`,
  3140. :meth:`~odoo.fields.Command.update`,
  3141. :meth:`~odoo.fields.Command.delete`,
  3142. :meth:`~odoo.fields.Command.unlink`,
  3143. :meth:`~odoo.fields.Command.link`,
  3144. :meth:`~odoo.fields.Command.clear`, and
  3145. :meth:`~odoo.fields.Command.set`.
  3146. * For :class:`~odoo.fields.Date` and `~odoo.fields.Datetime`,
  3147. the value should be either a date(time), or a string.
  3148. .. warning::
  3149. If a string is provided for Date(time) fields,
  3150. it must be UTC-only and formatted according to
  3151. :const:`odoo.tools.misc.DEFAULT_SERVER_DATE_FORMAT` and
  3152. :const:`odoo.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT`
  3153. * Other non-relational fields use a string for value
  3154. """
  3155. if not self:
  3156. return True
  3157. self.check_access_rights('write')
  3158. self.check_field_access_rights('write', vals.keys())
  3159. self.check_access_rule('write')
  3160. env = self.env
  3161. bad_names = {'id', 'parent_path'}
  3162. if self._log_access:
  3163. # the superuser can set log_access fields while loading registry
  3164. if not(self.env.uid == SUPERUSER_ID and not self.pool.ready):
  3165. bad_names.update(LOG_ACCESS_COLUMNS)
  3166. # set magic fields
  3167. vals = {key: val for key, val in vals.items() if key not in bad_names}
  3168. if self._log_access:
  3169. vals.setdefault('write_uid', self.env.uid)
  3170. vals.setdefault('write_date', self.env.cr.now())
  3171. field_values = [] # [(field, value)]
  3172. determine_inverses = defaultdict(list) # {inverse: fields}
  3173. fnames_modifying_relations = []
  3174. protected = set()
  3175. check_company = False
  3176. for fname, value in vals.items():
  3177. field = self._fields.get(fname)
  3178. if not field:
  3179. raise ValueError("Invalid field %r on model %r" % (fname, self._name))
  3180. field_values.append((field, value))
  3181. if field.inverse:
  3182. if field.type in ('one2many', 'many2many'):
  3183. # The written value is a list of commands that must applied
  3184. # on the field's current value. Because the field is
  3185. # protected while being written, the field's current value
  3186. # will not be computed and default to an empty recordset. So
  3187. # make sure the field's value is in cache before writing, in
  3188. # order to avoid an inconsistent update.
  3189. self[fname]
  3190. determine_inverses[field.inverse].append(field)
  3191. if self.pool.is_modifying_relations(field):
  3192. fnames_modifying_relations.append(fname)
  3193. if field.inverse or (field.compute and not field.readonly):
  3194. if field.store or field.type not in ('one2many', 'many2many'):
  3195. # Protect the field from being recomputed while being
  3196. # inversed. In the case of non-stored x2many fields, the
  3197. # field's value may contain unexpeced new records (created
  3198. # by command 0). Those new records are necessary for
  3199. # inversing the field, but should no longer appear if the
  3200. # field is recomputed afterwards. Not protecting the field
  3201. # will automatically invalidate the field from the cache,
  3202. # forcing its value to be recomputed once dependencies are
  3203. # up-to-date.
  3204. protected.update(self.pool.field_computed.get(field, [field]))
  3205. if fname == 'company_id' or (field.relational and field.check_company):
  3206. check_company = True
  3207. # force the computation of fields that are computed with some assigned
  3208. # fields, but are not assigned themselves
  3209. to_compute = [field.name
  3210. for field in protected
  3211. if field.compute and field.name not in vals]
  3212. if to_compute:
  3213. self._recompute_recordset(to_compute)
  3214. # protect fields being written against recomputation
  3215. with env.protecting(protected, self):
  3216. # Determine records depending on values. When modifying a relational
  3217. # field, you have to recompute what depends on the field's values
  3218. # before and after modification. This is because the modification
  3219. # has an impact on the "data path" between a computed field and its
  3220. # dependency. Note that this double call to modified() is only
  3221. # necessary for relational fields.
  3222. #
  3223. # It is best explained with a simple example: consider two sales
  3224. # orders SO1 and SO2. The computed total amount on sales orders
  3225. # indirectly depends on the many2one field 'order_id' linking lines
  3226. # to their sales order. Now consider the following code:
  3227. #
  3228. # line = so1.line_ids[0] # pick a line from SO1
  3229. # line.order_id = so2 # move the line to SO2
  3230. #
  3231. # In this situation, the total amount must be recomputed on *both*
  3232. # sales order: the line's order before the modification, and the
  3233. # line's order after the modification.
  3234. self.modified(fnames_modifying_relations, before=True)
  3235. real_recs = self.filtered('id')
  3236. # field.write_sequence determines a priority for writing on fields.
  3237. # Monetary fields need their corresponding currency field in cache
  3238. # for rounding values. X2many fields must be written last, because
  3239. # they flush other fields when deleting lines.
  3240. for field, value in sorted(field_values, key=lambda item: item[0].write_sequence):
  3241. field.write(self, value)
  3242. # determine records depending on new values
  3243. #
  3244. # Call modified after write, because the modified can trigger a
  3245. # search which can trigger a flush which can trigger a recompute
  3246. # which remove the field from the recompute list while all the
  3247. # values required for the computation could not be yet in cache.
  3248. # e.g. Write on `name` of `res.partner` trigger the recompute of
  3249. # `display_name`, which triggers a search on child_ids to find the
  3250. # childs to which the display_name must be recomputed, which
  3251. # triggers the flush of `display_name` because the _order of
  3252. # res.partner includes display_name. The computation of display_name
  3253. # is then done too soon because the parent_id was not yet written.
  3254. # (`test_01_website_reset_password_tour`)
  3255. self.modified(vals)
  3256. if self._parent_store and self._parent_name in vals:
  3257. self.flush_model([self._parent_name])
  3258. # validate non-inversed fields first
  3259. inverse_fields = [f.name for fs in determine_inverses.values() for f in fs]
  3260. real_recs._validate_fields(vals, inverse_fields)
  3261. for fields in determine_inverses.values():
  3262. # write again on non-stored fields that have been invalidated from cache
  3263. for field in fields:
  3264. if not field.store and any(self.env.cache.get_missing_ids(real_recs, field)):
  3265. field.write(real_recs, vals[field.name])
  3266. # inverse records that are not being computed
  3267. try:
  3268. fields[0].determine_inverse(real_recs)
  3269. except AccessError as e:
  3270. if fields[0].inherited:
  3271. description = self.env['ir.model']._get(self._name).name
  3272. raise AccessError(_(
  3273. "%(previous_message)s\n\nImplicitly accessed through '%(document_kind)s' (%(document_model)s).",
  3274. previous_message=e.args[0],
  3275. document_kind=description,
  3276. document_model=self._name,
  3277. ))
  3278. raise
  3279. # validate inversed fields
  3280. real_recs._validate_fields(inverse_fields)
  3281. if check_company and self._check_company_auto:
  3282. self._check_company()
  3283. return True
  3284. def _write(self, vals):
  3285. """ Low-level implementation of write()
  3286. The ids of self should be a database id and unique.
  3287. Ignore non-existent record.
  3288. """
  3289. if not self:
  3290. return
  3291. cr = self._cr
  3292. # determine records that require updating parent_path
  3293. parent_records = self._parent_store_update_prepare(vals)
  3294. if self._log_access:
  3295. # set magic fields (already done by write(), but not for computed fields)
  3296. vals = dict(vals)
  3297. vals.setdefault('write_uid', self.env.uid)
  3298. vals.setdefault('write_date', self.env.cr.now())
  3299. # determine SQL values
  3300. columns = []
  3301. params = []
  3302. for name, val in sorted(vals.items()):
  3303. if self._log_access and name in LOG_ACCESS_COLUMNS and not val:
  3304. continue
  3305. field = self._fields[name]
  3306. assert field.store
  3307. assert field.column_type
  3308. if field.translate is True and val:
  3309. # The first param is for the fallback value {'en_US': 'first_written_value'}
  3310. # which fills the 'en_US' key of jsonb only when the old column value is NULL.
  3311. # The second param is for the real value {'fr_FR': 'French', 'nl_NL': 'Dutch'}
  3312. columns.append(f'''"{name}" = %s || COALESCE("{name}", '{{}}'::jsonb) || %s''')
  3313. params.append(Json({} if 'en_US' in val.adapted else {'en_US': next(iter(val.adapted.values()))}))
  3314. params.append(val)
  3315. else:
  3316. columns.append(f'"{name}" = %s')
  3317. params.append(val)
  3318. # update columns
  3319. if columns:
  3320. template = ', '.join(columns)
  3321. query = f'UPDATE "{self._table}" SET {template} WHERE id IN %s'
  3322. for sub_ids in cr.split_for_in_conditions(self._ids):
  3323. cr.execute(query, params + [sub_ids])
  3324. # update parent_path
  3325. if parent_records:
  3326. parent_records._parent_store_update()
  3327. @api.model_create_multi
  3328. @api.returns('self', lambda value: value.id)
  3329. def create(self, vals_list):
  3330. """ create(vals_list) -> records
  3331. Creates new records for the model.
  3332. The new records are initialized using the values from the list of dicts
  3333. ``vals_list``, and if necessary those from :meth:`~.default_get`.
  3334. :param Union[list[dict], dict] vals_list:
  3335. values for the model's fields, as a list of dictionaries::
  3336. [{'field_name': field_value, ...}, ...]
  3337. For backward compatibility, ``vals_list`` may be a dictionary.
  3338. It is treated as a singleton list ``[vals]``, and a single record
  3339. is returned.
  3340. see :meth:`~.write` for details
  3341. :return: the created records
  3342. :raise AccessError: if the current user is not allowed to create records of the specified model
  3343. :raise ValidationError: if user tries to enter invalid value for a selection field
  3344. :raise ValueError: if a field name specified in the create values does not exist.
  3345. :raise UserError: if a loop would be created in a hierarchy of objects a result of the operation
  3346. (such as setting an object as its own parent)
  3347. """
  3348. if not vals_list:
  3349. return self.browse()
  3350. self = self.browse()
  3351. self.check_access_rights('create')
  3352. vals_list = self._prepare_create_values(vals_list)
  3353. # classify fields for each record
  3354. data_list = []
  3355. determine_inverses = defaultdict(set) # {inverse: fields}
  3356. for vals in vals_list:
  3357. precomputed = vals.pop('__precomputed__', ())
  3358. # distribute fields into sets for various purposes
  3359. data = {}
  3360. data['stored'] = stored = {}
  3361. data['inversed'] = inversed = {}
  3362. data['inherited'] = inherited = defaultdict(dict)
  3363. data['protected'] = protected = set()
  3364. for key, val in vals.items():
  3365. field = self._fields.get(key)
  3366. if not field:
  3367. raise ValueError("Invalid field %r on model %r" % (key, self._name))
  3368. if field.company_dependent:
  3369. irprop_def = self.env['ir.property']._get(key, self._name)
  3370. cached_def = field.convert_to_cache(irprop_def, self)
  3371. cached_val = field.convert_to_cache(val, self)
  3372. if cached_val == cached_def:
  3373. # val is the same as the default value defined in
  3374. # 'ir.property'; by design, 'ir.property' will not
  3375. # create entries specific to these records; skipping the
  3376. # field inverse saves 4 SQL queries
  3377. continue
  3378. if field.store:
  3379. stored[key] = val
  3380. if field.inherited:
  3381. inherited[field.related_field.model_name][key] = val
  3382. elif field.inverse and field not in precomputed:
  3383. inversed[key] = val
  3384. determine_inverses[field.inverse].add(field)
  3385. # protect editable computed fields and precomputed fields
  3386. # against (re)computation
  3387. if field.compute and (not field.readonly or field.precompute):
  3388. protected.update(self.pool.field_computed.get(field, [field]))
  3389. data_list.append(data)
  3390. # create or update parent records
  3391. for model_name, parent_name in self._inherits.items():
  3392. parent_data_list = []
  3393. for data in data_list:
  3394. if not data['stored'].get(parent_name):
  3395. parent_data_list.append(data)
  3396. elif data['inherited'][model_name]:
  3397. parent = self.env[model_name].browse(data['stored'][parent_name])
  3398. parent.write(data['inherited'][model_name])
  3399. if parent_data_list:
  3400. parents = self.env[model_name].create([
  3401. data['inherited'][model_name]
  3402. for data in parent_data_list
  3403. ])
  3404. for parent, data in zip(parents, parent_data_list):
  3405. data['stored'][parent_name] = parent.id
  3406. # create records with stored fields
  3407. records = self._create(data_list)
  3408. # protect fields being written against recomputation
  3409. protected = [(data['protected'], data['record']) for data in data_list]
  3410. with self.env.protecting(protected):
  3411. # call inverse method for each group of fields
  3412. for fields in determine_inverses.values():
  3413. # determine which records to inverse for those fields
  3414. inv_names = {field.name for field in fields}
  3415. rec_vals = [
  3416. (data['record'], {
  3417. name: data['inversed'][name]
  3418. for name in inv_names
  3419. if name in data['inversed']
  3420. })
  3421. for data in data_list
  3422. if not inv_names.isdisjoint(data['inversed'])
  3423. ]
  3424. # If a field is not stored, its inverse method will probably
  3425. # write on its dependencies, which will invalidate the field on
  3426. # all records. We therefore inverse the field record by record.
  3427. if all(field.store or field.company_dependent for field in fields):
  3428. batches = [rec_vals]
  3429. else:
  3430. batches = [[rec_data] for rec_data in rec_vals]
  3431. for batch in batches:
  3432. for record, vals in batch:
  3433. record._update_cache(vals)
  3434. batch_recs = self.concat(*(record for record, vals in batch))
  3435. next(iter(fields)).determine_inverse(batch_recs)
  3436. # check Python constraints for non-stored inversed fields
  3437. for data in data_list:
  3438. data['record']._validate_fields(data['inversed'], data['stored'])
  3439. if self._check_company_auto:
  3440. records._check_company()
  3441. return records
  3442. def _prepare_create_values(self, vals_list):
  3443. """ Clean up and complete the given create values, and return a list of
  3444. new vals containing:
  3445. * default values,
  3446. * discarded forbidden values (magic fields),
  3447. * precomputed fields.
  3448. :param list vals_list: List of create values
  3449. :returns: new list of completed create values
  3450. :rtype: dict
  3451. """
  3452. bad_names = ['id', 'parent_path']
  3453. if self._log_access:
  3454. # the superuser can set log_access fields while loading registry
  3455. if not(self.env.uid == SUPERUSER_ID and not self.pool.ready):
  3456. bad_names.extend(LOG_ACCESS_COLUMNS)
  3457. # also discard precomputed readonly fields (to force their computation)
  3458. bad_names.extend(
  3459. fname
  3460. for fname, field in self._fields.items()
  3461. if field.precompute and field.readonly
  3462. # ignore `readonly=True` when it's combined with the `states` attribute,
  3463. # making the field readonly according to the record state.
  3464. # e.g.
  3465. # product_uom = fields.Many2one(
  3466. # 'uom.uom', 'Product Unit of Measure',
  3467. # compute='_compute_product_uom', store=True, precompute=True,
  3468. # readonly=True, required=True, states={'draft': [('readonly', False)]},
  3469. # )
  3470. and (not field.states or not any(
  3471. modifier == 'readonly'
  3472. for modifiers in field.states.values()
  3473. for modifier, _value in modifiers
  3474. ))
  3475. )
  3476. result_vals_list = []
  3477. for vals in vals_list:
  3478. # add default values
  3479. vals = self._add_missing_default_values(vals)
  3480. # add magic fields
  3481. for fname in bad_names:
  3482. vals.pop(fname, None)
  3483. if self._log_access:
  3484. vals.setdefault('create_uid', self.env.uid)
  3485. vals.setdefault('create_date', self.env.cr.now())
  3486. vals.setdefault('write_uid', self.env.uid)
  3487. vals.setdefault('write_date', self.env.cr.now())
  3488. result_vals_list.append(vals)
  3489. # add precomputed fields
  3490. self._add_precomputed_values(result_vals_list)
  3491. return result_vals_list
  3492. def _add_precomputed_values(self, vals_list):
  3493. """ Add missing precomputed fields to ``vals_list`` values.
  3494. Only applies for precompute=True fields.
  3495. :param dict vals_list: list(dict) of create values
  3496. """
  3497. precomputable = {
  3498. fname: field
  3499. for fname, field in self._fields.items()
  3500. if field.precompute
  3501. }
  3502. if not precomputable:
  3503. return
  3504. # determine which vals must be completed
  3505. vals_list_todo = [
  3506. vals
  3507. for vals in vals_list
  3508. if any(fname not in vals for fname in precomputable)
  3509. ]
  3510. if not vals_list_todo:
  3511. return
  3512. # create new records for the vals that must be completed
  3513. records = self.browse().concat(*(self.new(vals) for vals in vals_list_todo))
  3514. for record, vals in zip(records, vals_list_todo):
  3515. vals['__precomputed__'] = precomputed = set()
  3516. for fname, field in precomputable.items():
  3517. if fname not in vals:
  3518. # computed stored fields with a column
  3519. # have to be computed before create
  3520. # s.t. required and constraints can be applied on those fields.
  3521. vals[fname] = field.convert_to_write(record[fname], self)
  3522. precomputed.add(field)
  3523. @api.model
  3524. def _create(self, data_list):
  3525. """ Create records from the stored field values in ``data_list``. """
  3526. assert data_list
  3527. cr = self.env.cr
  3528. # insert rows in batches of maximum INSERT_BATCH_SIZE
  3529. ids = [] # ids of created records
  3530. other_fields = OrderedSet() # non-column fields
  3531. for data_sublist in split_every(INSERT_BATCH_SIZE, data_list):
  3532. stored_list = [data['stored'] for data in data_sublist]
  3533. fnames = sorted({name for stored in stored_list for name in stored})
  3534. columns = []
  3535. rows = [[] for _ in stored_list]
  3536. for fname in fnames:
  3537. field = self._fields[fname]
  3538. if field.column_type:
  3539. columns.append(fname)
  3540. for stored, row in zip(stored_list, rows):
  3541. if fname in stored:
  3542. colval = field.convert_to_column(stored[fname], self, stored)
  3543. if field.translate is True and colval:
  3544. if 'en_US' not in colval.adapted:
  3545. colval.adapted['en_US'] = next(iter(colval.adapted.values()))
  3546. row.append(colval)
  3547. else:
  3548. row.append(SQL_DEFAULT)
  3549. else:
  3550. other_fields.add(field)
  3551. if field.type == 'properties':
  3552. # force calling fields.create for properties field because
  3553. # we might want to update the parent definition
  3554. other_fields.add(field)
  3555. if not columns:
  3556. # manage the case where we create empty records
  3557. columns = ['id']
  3558. for row in rows:
  3559. row.append(SQL_DEFAULT)
  3560. header = ", ".join(f'"{column}"' for column in columns)
  3561. template = ", ".join("%s" for _ in rows)
  3562. cr.execute(
  3563. f'INSERT INTO "{self._table}" ({header}) VALUES {template} RETURNING "id"',
  3564. [tuple(row) for row in rows],
  3565. )
  3566. ids.extend(id_ for id_, in cr.fetchall())
  3567. # put the new records in cache, and update inverse fields, for many2one
  3568. #
  3569. # cachetoclear is an optimization to avoid modified()'s cost until other_fields are processed
  3570. cachetoclear = []
  3571. records = self.browse(ids)
  3572. inverses_update = defaultdict(list) # {(field, value): ids}
  3573. common_set_vals = set(LOG_ACCESS_COLUMNS + [self.CONCURRENCY_CHECK_FIELD, 'id', 'parent_path'])
  3574. for data, record in zip(data_list, records):
  3575. data['record'] = record
  3576. # DLE P104: test_inherit.py, test_50_search_one2many
  3577. vals = dict({k: v for d in data['inherited'].values() for k, v in d.items()}, **data['stored'])
  3578. set_vals = common_set_vals.union(vals)
  3579. for field in self._fields.values():
  3580. if field.type in ('one2many', 'many2many'):
  3581. self.env.cache.set(record, field, ())
  3582. elif field.related and not field.column_type:
  3583. self.env.cache.set(record, field, field.convert_to_cache(None, record))
  3584. # DLE P123: `test_adv_activity`, `test_message_assignation_inbox`, `test_message_log`, `test_create_mail_simple`, ...
  3585. # Set `mail.message.parent_id` to False in cache so it doesn't do the useless SELECT when computing the modified of `child_ids`
  3586. # in other words, if `parent_id` is not set, no other message `child_ids` are impacted.
  3587. # + avoid the fetch of fields which are False. e.g. if a boolean field is not passed in vals and as no default set in the field attributes,
  3588. # then we know it can be set to False in the cache in the case of a create.
  3589. elif field.name not in set_vals and not field.compute:
  3590. self.env.cache.set(record, field, field.convert_to_cache(None, record))
  3591. for fname, value in vals.items():
  3592. field = self._fields[fname]
  3593. if field.type in ('one2many', 'many2many'):
  3594. cachetoclear.append((record, field))
  3595. else:
  3596. cache_value = field.convert_to_cache(value, record)
  3597. self.env.cache.set(record, field, cache_value)
  3598. if field.type in ('many2one', 'many2one_reference') and self.pool.field_inverses[field]:
  3599. inverses_update[(field, cache_value)].append(record.id)
  3600. for (field, value), record_ids in inverses_update.items():
  3601. field._update_inverses(self.browse(record_ids), value)
  3602. # update parent_path
  3603. records._parent_store_create()
  3604. # protect fields being written against recomputation
  3605. protected = [(data['protected'], data['record']) for data in data_list]
  3606. with self.env.protecting(protected):
  3607. # mark computed fields as todo
  3608. records.modified(self._fields, create=True)
  3609. if other_fields:
  3610. # discard default values from context for other fields
  3611. others = records.with_context(clean_context(self._context))
  3612. for field in sorted(other_fields, key=attrgetter('_sequence')):
  3613. field.create([
  3614. (other, data['stored'][field.name])
  3615. for other, data in zip(others, data_list)
  3616. if field.name in data['stored']
  3617. ])
  3618. # mark fields to recompute
  3619. records.modified([field.name for field in other_fields], create=True)
  3620. # if value in cache has not been updated by other_fields, remove it
  3621. for record, field in cachetoclear:
  3622. if self.env.cache.contains(record, field) and not self.env.cache.get(record, field):
  3623. self.env.cache.remove(record, field)
  3624. # check Python constraints for stored fields
  3625. records._validate_fields(name for data in data_list for name in data['stored'])
  3626. records.check_access_rule('create')
  3627. return records
  3628. def _compute_field_value(self, field):
  3629. fields.determine(field.compute, self)
  3630. if field.store and any(self._ids):
  3631. # check constraints of the fields that have been computed
  3632. fnames = [f.name for f in self.pool.field_computed[field]]
  3633. self.filtered('id')._validate_fields(fnames)
  3634. def _parent_store_create(self):
  3635. """ Set the parent_path field on ``self`` after its creation. """
  3636. if not self._parent_store:
  3637. return
  3638. query = """
  3639. UPDATE {0} node
  3640. SET parent_path=concat((SELECT parent.parent_path FROM {0} parent
  3641. WHERE parent.id=node.{1}), node.id, '/')
  3642. WHERE node.id IN %s
  3643. RETURNING node.id, node.parent_path
  3644. """.format(self._table, self._parent_name)
  3645. self._cr.execute(query, [tuple(self.ids)])
  3646. # update the cache of updated nodes, and determine what to recompute
  3647. updated = dict(self._cr.fetchall())
  3648. records = self.browse(updated)
  3649. self.env.cache.update(records, self._fields['parent_path'], updated.values())
  3650. def _parent_store_update_prepare(self, vals):
  3651. """ Return the records in ``self`` that must update their parent_path
  3652. field. This must be called before updating the parent field.
  3653. """
  3654. if not self._parent_store or self._parent_name not in vals:
  3655. return self.browse()
  3656. # No need to recompute the values if the parent is the same.
  3657. parent_val = vals[self._parent_name]
  3658. if parent_val:
  3659. query = """ SELECT id FROM {0}
  3660. WHERE id IN %s AND ({1} != %s OR {1} IS NULL) """
  3661. params = [tuple(self.ids), parent_val]
  3662. else:
  3663. query = """ SELECT id FROM {0}
  3664. WHERE id IN %s AND {1} IS NOT NULL """
  3665. params = [tuple(self.ids)]
  3666. query = query.format(self._table, self._parent_name)
  3667. self._cr.execute(query, params)
  3668. return self.browse([row[0] for row in self._cr.fetchall()])
  3669. def _parent_store_update(self):
  3670. """ Update the parent_path field of ``self``. """
  3671. cr = self.env.cr
  3672. # determine new prefix of parent_path
  3673. query = """
  3674. SELECT parent.parent_path FROM {0} node, {0} parent
  3675. WHERE node.id = %s AND parent.id = node.{1}
  3676. """
  3677. cr.execute(query.format(self._table, self._parent_name), [self.ids[0]])
  3678. prefix = cr.fetchone()[0] if cr.rowcount else ''
  3679. # check for recursion
  3680. if prefix:
  3681. parent_ids = {int(label) for label in prefix.split('/')[:-1]}
  3682. if not parent_ids.isdisjoint(self._ids):
  3683. raise UserError(_("Recursion Detected."))
  3684. # update parent_path of all records and their descendants
  3685. query = """
  3686. UPDATE {0} child
  3687. SET parent_path = concat(%s, substr(child.parent_path,
  3688. length(node.parent_path) - length(node.id || '/') + 1))
  3689. FROM {0} node
  3690. WHERE node.id IN %s
  3691. AND child.parent_path LIKE concat(node.parent_path, '%%')
  3692. RETURNING child.id, child.parent_path
  3693. """
  3694. cr.execute(query.format(self._table), [prefix, tuple(self.ids)])
  3695. # update the cache of updated nodes, and determine what to recompute
  3696. updated = dict(cr.fetchall())
  3697. records = self.browse(updated)
  3698. self.env.cache.update(records, self._fields['parent_path'], updated.values())
  3699. records.modified(['parent_path'])
  3700. def _load_records_write(self, values):
  3701. self.write(values)
  3702. def _load_records_create(self, values):
  3703. return self.create(values)
  3704. def _load_records(self, data_list, update=False):
  3705. """ Create or update records of this model, and assign XMLIDs.
  3706. :param data_list: list of dicts with keys `xml_id` (XMLID to
  3707. assign), `noupdate` (flag on XMLID), `values` (field values)
  3708. :param update: should be ``True`` when upgrading a module
  3709. :return: the records corresponding to ``data_list``
  3710. """
  3711. original_self = self.browse()
  3712. # records created during installation should not display messages
  3713. self = self.with_context(install_mode=True)
  3714. imd = self.env['ir.model.data'].sudo()
  3715. # The algorithm below partitions 'data_list' into three sets: the ones
  3716. # to create, the ones to update, and the others. For each set, we assign
  3717. # data['record'] for each data. All those records are then retrieved for
  3718. # the result.
  3719. # determine existing xml_ids
  3720. xml_ids = [data['xml_id'] for data in data_list if data.get('xml_id')]
  3721. existing = {
  3722. ("%s.%s" % row[1:3]): row
  3723. for row in imd._lookup_xmlids(xml_ids, self)
  3724. }
  3725. # determine which records to create and update
  3726. to_create = [] # list of data
  3727. to_update = [] # list of data
  3728. imd_data_list = [] # list of data for _update_xmlids()
  3729. for data in data_list:
  3730. xml_id = data.get('xml_id')
  3731. if not xml_id:
  3732. vals = data['values']
  3733. if vals.get('id'):
  3734. data['record'] = self.browse(vals['id'])
  3735. to_update.append(data)
  3736. elif not update:
  3737. to_create.append(data)
  3738. continue
  3739. row = existing.get(xml_id)
  3740. if not row:
  3741. to_create.append(data)
  3742. continue
  3743. d_id, d_module, d_name, d_model, d_res_id, d_noupdate, r_id = row
  3744. if self._name != d_model:
  3745. raise ValidationError(
  3746. f"For external id {xml_id} "
  3747. f"when trying to create/update a record of model {self._name} "
  3748. f"found record of different model {d_model} ({d_id})"
  3749. )
  3750. record = self.browse(d_res_id)
  3751. if r_id:
  3752. data['record'] = record
  3753. imd_data_list.append(data)
  3754. if not (update and d_noupdate):
  3755. to_update.append(data)
  3756. else:
  3757. imd.browse(d_id).unlink()
  3758. to_create.append(data)
  3759. # update existing records
  3760. for data in to_update:
  3761. data['record']._load_records_write(data['values'])
  3762. # check for records to create with an XMLID from another module
  3763. module = self.env.context.get('install_module')
  3764. if module:
  3765. prefix = module + "."
  3766. for data in to_create:
  3767. if data.get('xml_id') and not data['xml_id'].startswith(prefix):
  3768. _logger.warning("Creating record %s in module %s.", data['xml_id'], module)
  3769. if self.env.context.get('import_file'):
  3770. existing_modules = self.env['ir.module.module'].sudo().search([]).mapped('name')
  3771. for data in to_create:
  3772. xml_id = data.get('xml_id')
  3773. if xml_id:
  3774. module_name, sep, record_id = xml_id.partition('.')
  3775. if sep and module_name in existing_modules:
  3776. raise UserError(
  3777. _("The record %(xml_id)s has the module prefix %(module_name)s. This is the part before the '.' in the external id. Because the prefix refers to an existing module, the record would be deleted when the module is upgraded. Use either no prefix and no dot or a prefix that isn't an existing module. For example, __import__, resulting in the external id __import__.%(record_id)s.",
  3778. xml_id=xml_id, module_name=module_name, record_id=record_id))
  3779. # create records
  3780. if to_create:
  3781. records = self._load_records_create([data['values'] for data in to_create])
  3782. for data, record in zip(to_create, records):
  3783. data['record'] = record
  3784. if data.get('xml_id'):
  3785. # add XML ids for parent records that have just been created
  3786. for parent_model, parent_field in self._inherits.items():
  3787. if not data['values'].get(parent_field):
  3788. imd_data_list.append({
  3789. 'xml_id': f"{data['xml_id']}_{parent_model.replace('.', '_')}",
  3790. 'record': record[parent_field],
  3791. 'noupdate': data.get('noupdate', False),
  3792. })
  3793. imd_data_list.append(data)
  3794. # create or update XMLIDs
  3795. imd._update_xmlids(imd_data_list, update)
  3796. return original_self.concat(*(data['record'] for data in data_list))
  3797. # TODO: ameliorer avec NULL
  3798. @api.model
  3799. def _where_calc(self, domain, active_test=True):
  3800. """Computes the WHERE clause needed to implement an OpenERP domain.
  3801. :param list domain: the domain to compute
  3802. :param bool active_test: whether the default filtering of records with
  3803. ``active`` field set to ``False`` should be applied.
  3804. :return: the query expressing the given domain as provided in domain
  3805. :rtype: Query
  3806. """
  3807. # if the object has an active field ('active', 'x_active'), filter out all
  3808. # inactive records unless they were explicitly asked for
  3809. if self._active_name and active_test and self._context.get('active_test', True):
  3810. # the item[0] trick below works for domain items and '&'/'|'/'!'
  3811. # operators too
  3812. if not any(item[0] == self._active_name for item in domain):
  3813. domain = [(self._active_name, '=', 1)] + domain
  3814. if domain:
  3815. return expression.expression(domain, self).query
  3816. else:
  3817. return Query(self.env.cr, self._table, self._table_query)
  3818. def _check_qorder(self, word):
  3819. if not regex_order.match(word):
  3820. raise UserError(_(
  3821. "Invalid \"order\" specified (%s)."
  3822. " A valid \"order\" specification is a comma-separated list of valid field names"
  3823. " (optionally followed by asc/desc for the direction)",
  3824. word,
  3825. ))
  3826. return True
  3827. @api.model
  3828. def _apply_ir_rules(self, query, mode='read'):
  3829. """Add what's missing in ``query`` to implement all appropriate ir.rules
  3830. (using the ``model_name``'s rules or the current model's rules if ``model_name`` is None)
  3831. :param query: the current query object
  3832. """
  3833. if self.env.su:
  3834. return
  3835. # apply main rules on the object
  3836. Rule = self.env['ir.rule']
  3837. domain = Rule._compute_domain(self._name, mode)
  3838. if domain:
  3839. expression.expression(domain, self.sudo(), self._table, query)
  3840. # apply ir.rules from the parents (through _inherits)
  3841. for parent_model_name in self._inherits:
  3842. domain = Rule._compute_domain(parent_model_name, mode)
  3843. if domain:
  3844. parent_model = self.env[parent_model_name]
  3845. parent_alias = self._inherits_join_add(self, parent_model_name, query)
  3846. expression.expression(domain, parent_model.sudo(), parent_alias, query)
  3847. @api.model
  3848. def _generate_m2o_order_by(self, alias, order_field, query, reverse_direction, seen):
  3849. """
  3850. Add possibly missing JOIN to ``query`` and generate the ORDER BY clause for m2o fields,
  3851. either native m2o fields or function/related fields that are stored, including
  3852. intermediate JOINs for inheritance if required.
  3853. :return: the qualified field name to use in an ORDER BY clause to sort by ``order_field``
  3854. """
  3855. field = self._fields[order_field]
  3856. if field.inherited:
  3857. # also add missing joins for reaching the table containing the m2o field
  3858. qualified_field = self._inherits_join_calc(alias, order_field, query)
  3859. alias, order_field = qualified_field.replace('"', '').split('.', 1)
  3860. field = field.base_field
  3861. assert field.type == 'many2one', 'Invalid field passed to _generate_m2o_order_by()'
  3862. if not field.store:
  3863. _logger.debug("Many2one function/related fields must be stored "
  3864. "to be used as ordering fields! Ignoring sorting for %s.%s",
  3865. self._name, order_field)
  3866. return []
  3867. # figure out the applicable order_by for the m2o
  3868. dest_model = self.env[field.comodel_name]
  3869. m2o_order = dest_model._order
  3870. if not regex_order.match(m2o_order):
  3871. # _order is complex, can't use it here, so we default to _rec_name
  3872. m2o_order = dest_model._rec_name
  3873. # Join the dest m2o table if it's not joined yet. We use [LEFT] OUTER join here
  3874. # as we don't want to exclude results that have NULL values for the m2o
  3875. dest_alias = query.left_join(alias, order_field, dest_model._table, 'id', order_field)
  3876. return dest_model._generate_order_by_inner(dest_alias, m2o_order, query,
  3877. reverse_direction, seen)
  3878. @api.model
  3879. def _generate_order_by_inner(self, alias, order_spec, query, reverse_direction=False, seen=None):
  3880. if seen is None:
  3881. seen = set()
  3882. self._check_qorder(order_spec)
  3883. order_by_elements = []
  3884. for order_part in order_spec.split(','):
  3885. order_split = order_part.strip().split(' ')
  3886. order_field = order_split[0].strip()
  3887. order_direction = order_split[1].strip().upper() if len(order_split) == 2 else ''
  3888. if reverse_direction:
  3889. order_direction = 'ASC' if order_direction == 'DESC' else 'DESC'
  3890. do_reverse = order_direction == 'DESC'
  3891. field = self._fields.get(order_field)
  3892. if not field:
  3893. raise ValueError("Invalid field %r on model %r" % (order_field, self._name))
  3894. if order_field == 'id':
  3895. order_by_elements.append('"%s"."%s" %s' % (alias, order_field, order_direction))
  3896. else:
  3897. if field.inherited:
  3898. field = field.base_field
  3899. if field.store and field.type == 'many2one':
  3900. key = (field.model_name, field.comodel_name, order_field)
  3901. if key not in seen:
  3902. seen.add(key)
  3903. order_by_elements += self._generate_m2o_order_by(alias, order_field, query, do_reverse, seen)
  3904. elif field.store and field.column_type:
  3905. qualifield_name = self._inherits_join_calc(alias, order_field, query)
  3906. if field.type == 'boolean':
  3907. qualifield_name = "COALESCE(%s, false)" % qualifield_name
  3908. order_by_elements.append("%s %s" % (qualifield_name, order_direction))
  3909. else:
  3910. _logger.warning("Model %r cannot be sorted on field %r (not a column)", self._name, order_field)
  3911. continue # ignore non-readable or "non-joinable" fields
  3912. return order_by_elements
  3913. @api.model
  3914. def _generate_order_by(self, order_spec, query):
  3915. """
  3916. Attempt to construct an appropriate ORDER BY clause based on order_spec, which must be
  3917. a comma-separated list of valid field names, optionally followed by an ASC or DESC direction.
  3918. :raise ValueError in case order_spec is malformed
  3919. """
  3920. order_by_clause = ''
  3921. order_spec = order_spec or self._order
  3922. if order_spec:
  3923. order_by_elements = self._generate_order_by_inner(self._table, order_spec, query)
  3924. if order_by_elements:
  3925. order_by_clause = ",".join(order_by_elements)
  3926. return order_by_clause and (' ORDER BY %s ' % order_by_clause) or ''
  3927. @api.model
  3928. def _flush_search(self, domain, fields=None, order=None, seen=None):
  3929. """ Flush all the fields appearing in `domain`, `fields` and `order`. """
  3930. if seen is None:
  3931. seen = set()
  3932. elif self._name in seen:
  3933. return
  3934. seen.add(self._name)
  3935. to_flush = defaultdict(set) # {model_name: field_names}
  3936. if fields:
  3937. to_flush[self._name].update(fields)
  3938. def collect_from_domain(model, domain):
  3939. for arg in domain:
  3940. if isinstance(arg, str):
  3941. continue
  3942. if not isinstance(arg[0], str):
  3943. continue
  3944. comodel = collect_from_path(model, arg[0])
  3945. if arg[1] in ('child_of', 'parent_of') and comodel._parent_store:
  3946. # hierarchy operators need the parent field
  3947. collect_from_path(comodel, comodel._parent_name)
  3948. def collect_from_path(model, path):
  3949. # path is a dot-separated sequence of field names
  3950. for fname in path.split('.'):
  3951. field = model._fields.get(fname)
  3952. if not field:
  3953. break
  3954. to_flush[model._name].add(fname)
  3955. if field.type == 'one2many' and field.inverse_name:
  3956. to_flush[field.comodel_name].add(field.inverse_name)
  3957. field_domain = field.get_domain_list(model)
  3958. if field_domain:
  3959. collect_from_domain(self.env[field.comodel_name], field_domain)
  3960. # DLE P111: `test_message_process_email_partner_find`
  3961. # Search on res.users with email_normalized in domain
  3962. # must trigger the recompute and flush of res.partner.email_normalized
  3963. if field.related:
  3964. # DLE P129: `test_transit_multi_companies`
  3965. # `self.env['stock.picking'].search([('product_id', '=', product.id)])`
  3966. # Should flush `stock.move.picking_ids` as `product_id` on `stock.picking` is defined as:
  3967. # `product_id = fields.Many2one('product.product', 'Product', related='move_lines.product_id', readonly=False)`
  3968. collect_from_path(model, field.related)
  3969. if field.relational:
  3970. model = self.env[field.comodel_name]
  3971. # return the model found by traversing all fields (used in collect_from_domain)
  3972. return model
  3973. # also take into account the fields in the record rules
  3974. domain = list(domain) + (self.env['ir.rule']._compute_domain(self._name, 'read') or [])
  3975. collect_from_domain(self, domain)
  3976. # flush the order fields
  3977. order_spec = order or self._order
  3978. for order_part in order_spec.split(','):
  3979. order_field = order_part.split()[0]
  3980. field = self._fields.get(order_field)
  3981. if field is not None:
  3982. to_flush[self._name].add(order_field)
  3983. if field.relational:
  3984. self.env[field.comodel_name]._flush_search([], seen=seen)
  3985. if self._active_name:
  3986. to_flush[self._name].add(self._active_name)
  3987. # flush model dependencies (recursively)
  3988. if self._depends:
  3989. models = [self]
  3990. while models:
  3991. model = models.pop()
  3992. for model_name, field_names in model._depends.items():
  3993. to_flush[model_name].update(field_names)
  3994. models.append(self.env[model_name])
  3995. for model_name, field_names in to_flush.items():
  3996. self.env[model_name].flush_model(field_names)
  3997. @api.model
  3998. def _search(self, domain, offset=0, limit=None, order=None, count=False, access_rights_uid=None):
  3999. """
  4000. Private implementation of search() method, allowing specifying the uid to use for the access right check.
  4001. This is useful for example when filling in the selection list for a drop-down and avoiding access rights errors,
  4002. by specifying ``access_rights_uid=1`` to bypass access rights check, but not ir.rules!
  4003. This is ok at the security level because this method is private and not callable through XML-RPC.
  4004. :param access_rights_uid: optional user ID to use when checking access rights
  4005. (not for ir.rules, this is only for ir.model.access)
  4006. :return: a list of record ids or an integer (if count is True)
  4007. """
  4008. model = self.with_user(access_rights_uid) if access_rights_uid else self
  4009. model.check_access_rights('read')
  4010. if expression.is_false(self, domain):
  4011. # optimization: no need to query, as no record satisfies the domain
  4012. return 0 if count else []
  4013. # the flush must be done before the _where_calc(), as the latter can do some selects
  4014. self._flush_search(domain, order=order)
  4015. query = self._where_calc(domain)
  4016. self._apply_ir_rules(query, 'read')
  4017. query.limit = limit
  4018. if count:
  4019. # Ignore order and offset when just counting, they don't make sense and could
  4020. # hurt performance
  4021. if limit:
  4022. # Special case to avoid counting every record in DB (which can be really slow).
  4023. # The result will be between 0 and limit.
  4024. query_str, params = query.select("") # generates a `SELECT FROM` (faster)
  4025. query_str = f"SELECT COUNT(*) FROM ({query_str}) t"
  4026. else:
  4027. query_str, params = query.select("COUNT(*)")
  4028. self._cr.execute(query_str, params)
  4029. return self._cr.fetchone()[0]
  4030. query.order = self._generate_order_by(order, query).replace('ORDER BY ', '')
  4031. query.offset = offset
  4032. return query
  4033. @api.returns(None, lambda value: value[0])
  4034. def copy_data(self, default=None):
  4035. """
  4036. Copy given record's data with all its fields values
  4037. :param default: field values to override in the original values of the copied record
  4038. :return: list with a dictionary containing all the field values
  4039. """
  4040. # In the old API, this method took a single id and return a dict. When
  4041. # invoked with the new API, it returned a list of dicts.
  4042. self.ensure_one()
  4043. # avoid recursion through already copied records in case of circular relationship
  4044. if '__copy_data_seen' not in self._context:
  4045. self = self.with_context(__copy_data_seen=defaultdict(set))
  4046. seen_map = self._context['__copy_data_seen']
  4047. if self.id in seen_map[self._name]:
  4048. return
  4049. seen_map[self._name].add(self.id)
  4050. default = dict(default or [])
  4051. # build a black list of fields that should not be copied
  4052. blacklist = set(MAGIC_COLUMNS + ['parent_path'])
  4053. whitelist = set(name for name, field in self._fields.items() if not field.inherited)
  4054. def blacklist_given_fields(model):
  4055. # blacklist the fields that are given by inheritance
  4056. for parent_model, parent_field in model._inherits.items():
  4057. blacklist.add(parent_field)
  4058. if parent_field in default:
  4059. # all the fields of 'parent_model' are given by the record:
  4060. # default[parent_field], except the ones redefined in self
  4061. blacklist.update(set(self.env[parent_model]._fields) - whitelist)
  4062. else:
  4063. blacklist_given_fields(self.env[parent_model])
  4064. blacklist_given_fields(self)
  4065. fields_to_copy = {name: field
  4066. for name, field in self._fields.items()
  4067. if field.copy and name not in default and name not in blacklist}
  4068. for name, field in fields_to_copy.items():
  4069. if field.type == 'one2many':
  4070. # duplicate following the order of the ids because we'll rely on
  4071. # it later for copying translations in copy_translation()!
  4072. lines = [rec.copy_data()[0] for rec in self[name].sorted(key='id')]
  4073. # the lines are duplicated using the wrong (old) parent, but then are
  4074. # reassigned to the correct one thanks to the (Command.CREATE, 0, ...)
  4075. default[name] = [Command.create(line) for line in lines if line]
  4076. elif field.type == 'many2many':
  4077. default[name] = [Command.set(self[name].ids)]
  4078. else:
  4079. default[name] = field.convert_to_write(self[name], self)
  4080. return [default]
  4081. def copy_translations(self, new, excluded=()):
  4082. """ Recursively copy the translations from original to new record
  4083. :param self: the original record
  4084. :param new: the new record (copy of the original one)
  4085. :param excluded: a container of user-provided field names
  4086. """
  4087. old = self
  4088. # avoid recursion through already copied records in case of circular relationship
  4089. if '__copy_translations_seen' not in old._context:
  4090. old = old.with_context(__copy_translations_seen=defaultdict(set))
  4091. seen_map = old._context['__copy_translations_seen']
  4092. if old.id in seen_map[old._name]:
  4093. return
  4094. seen_map[old._name].add(old.id)
  4095. valid_langs = set(code for code, _ in self.env['res.lang'].get_installed()) | {'en_US'}
  4096. for name, field in old._fields.items():
  4097. if not field.copy:
  4098. continue
  4099. if field.inherited and field.related.split('.')[0] in excluded:
  4100. # inherited fields that come from a user-provided parent record
  4101. # must not copy translations, as the parent record is not a copy
  4102. # of the old parent record
  4103. continue
  4104. if field.type == 'one2many' and field.name not in excluded:
  4105. # we must recursively copy the translations for o2m; here we
  4106. # rely on the order of the ids to match the translations as
  4107. # foreseen in copy_data()
  4108. old_lines = old[name].sorted(key='id')
  4109. new_lines = new[name].sorted(key='id')
  4110. for (old_line, new_line) in zip(old_lines, new_lines):
  4111. # don't pass excluded as it is not about those lines
  4112. old_line.copy_translations(new_line)
  4113. elif field.translate and field.store and name not in excluded and old[name]:
  4114. # for translatable fields we copy their translations
  4115. old_translations = field._get_stored_translations(old)
  4116. if not old_translations:
  4117. continue
  4118. lang = self.env.lang or 'en_US'
  4119. old_value_lang = old_translations.pop(lang, old_translations['en_US'])
  4120. old_translations = {
  4121. lang: value
  4122. for lang, value in old_translations.items()
  4123. if lang in valid_langs
  4124. }
  4125. if not old_translations:
  4126. continue
  4127. if not callable(field.translate):
  4128. new.update_field_translations(name, old_translations)
  4129. else:
  4130. # {lang: {old_term: new_term}}
  4131. translations = defaultdict(dict)
  4132. # {from_lang_term: {lang: to_lang_term}
  4133. translation_dictionary = field.get_translation_dictionary(old_value_lang, old_translations)
  4134. for from_lang_term, to_lang_terms in translation_dictionary.items():
  4135. for lang, to_lang_term in to_lang_terms.items():
  4136. translations[lang][from_lang_term] = to_lang_term
  4137. new.update_field_translations(name, translations)
  4138. @api.returns('self', lambda value: value.id)
  4139. def copy(self, default=None):
  4140. """ copy(default=None)
  4141. Duplicate record ``self`` updating it with default values
  4142. :param dict default: dictionary of field values to override in the
  4143. original values of the copied record, e.g: ``{'field_name': overridden_value, ...}``
  4144. :returns: new record
  4145. """
  4146. self.ensure_one()
  4147. vals = self.with_context(active_test=False).copy_data(default)[0]
  4148. record_copy = self.create(vals)
  4149. self.with_context(from_copy_translation=True).copy_translations(record_copy, excluded=default or ())
  4150. return record_copy
  4151. @api.returns('self')
  4152. def exists(self):
  4153. """ exists() -> records
  4154. Returns the subset of records in ``self`` that exist.
  4155. It can be used as a test on records::
  4156. if record.exists():
  4157. ...
  4158. By convention, new records are returned as existing.
  4159. """
  4160. new_ids, ids = partition(lambda i: isinstance(i, NewId), self._ids)
  4161. if not ids:
  4162. return self
  4163. query = Query(self.env.cr, self._table, self._table_query)
  4164. query.add_where(f'"{self._table}".id IN %s', [tuple(ids)])
  4165. query_str, params = query.select()
  4166. self.env.cr.execute(query_str, params)
  4167. valid_ids = set([r[0] for r in self._cr.fetchall()] + new_ids)
  4168. return self.browse(i for i in self._ids if i in valid_ids)
  4169. def _check_recursion(self, parent=None):
  4170. """
  4171. Verifies that there is no loop in a hierarchical structure of records,
  4172. by following the parent relationship using the **parent** field until a
  4173. loop is detected or until a top-level record is found.
  4174. :param parent: optional parent field name (default: ``self._parent_name``)
  4175. :return: **True** if no loop was found, **False** otherwise.
  4176. """
  4177. if not parent:
  4178. parent = self._parent_name
  4179. # must ignore 'active' flag, ir.rules, etc. => direct SQL query
  4180. cr = self._cr
  4181. self.flush_model([parent])
  4182. query = 'SELECT "%s" FROM "%s" WHERE id = %%s' % (parent, self._table)
  4183. for id in self.ids:
  4184. current_id = id
  4185. while current_id:
  4186. cr.execute(query, (current_id,))
  4187. result = cr.fetchone()
  4188. current_id = result[0] if result else None
  4189. if current_id == id:
  4190. return False
  4191. return True
  4192. def _check_m2m_recursion(self, field_name):
  4193. """
  4194. Verifies that there is no loop in a directed graph of records, by
  4195. following a many2many relationship with the given field name.
  4196. :param field_name: field to check
  4197. :return: **True** if no loop was found, **False** otherwise.
  4198. """
  4199. field = self._fields.get(field_name)
  4200. if not (field and field.type == 'many2many' and
  4201. field.comodel_name == self._name and field.store):
  4202. # field must be a many2many on itself
  4203. raise ValueError('invalid field_name: %r' % (field_name,))
  4204. self.flush_model([field_name])
  4205. cr = self._cr
  4206. query = 'SELECT "%s", "%s" FROM "%s" WHERE "%s" IN %%s AND "%s" IS NOT NULL' % \
  4207. (field.column1, field.column2, field.relation, field.column1, field.column2)
  4208. succs = defaultdict(set) # transitive closure of successors
  4209. preds = defaultdict(set) # transitive closure of predecessors
  4210. todo, done = set(self.ids), set()
  4211. while todo:
  4212. # retrieve the respective successors of the nodes in 'todo'
  4213. cr.execute(query, [tuple(todo)])
  4214. done.update(todo)
  4215. todo.clear()
  4216. for id1, id2 in cr.fetchall():
  4217. # connect id1 and its predecessors to id2 and its successors
  4218. for x, y in itertools.product([id1] + list(preds[id1]),
  4219. [id2] + list(succs[id2])):
  4220. if x == y:
  4221. return False # we found a cycle here!
  4222. succs[x].add(y)
  4223. preds[y].add(x)
  4224. if id2 not in done:
  4225. todo.add(id2)
  4226. return True
  4227. def _get_external_ids(self):
  4228. """Retrieve the External ID(s) of any database record.
  4229. **Synopsis**: ``_get_external_ids() -> { 'id': ['module.external_id'] }``
  4230. :return: map of ids to the list of their fully qualified External IDs
  4231. in the form ``module.key``, or an empty list when there's no External
  4232. ID for a record, e.g.::
  4233. { 'id': ['module.ext_id', 'module.ext_id_bis'],
  4234. 'id2': [] }
  4235. """
  4236. result = defaultdict(list)
  4237. domain = [('model', '=', self._name), ('res_id', 'in', self.ids)]
  4238. for data in self.env['ir.model.data'].sudo().search_read(domain, ['module', 'name', 'res_id'], order='id'):
  4239. result[data['res_id']].append('%(module)s.%(name)s' % data)
  4240. return {
  4241. record.id: result[record._origin.id]
  4242. for record in self
  4243. }
  4244. def get_external_id(self):
  4245. """Retrieve the External ID of any database record, if there
  4246. is one. This method works as a possible implementation
  4247. for a function field, to be able to add it to any
  4248. model object easily, referencing it as ``Model.get_external_id``.
  4249. When multiple External IDs exist for a record, only one
  4250. of them is returned (randomly).
  4251. :return: map of ids to their fully qualified XML ID,
  4252. defaulting to an empty string when there's none
  4253. (to be usable as a function field),
  4254. e.g.::
  4255. { 'id': 'module.ext_id',
  4256. 'id2': '' }
  4257. """
  4258. results = self._get_external_ids()
  4259. return {key: val[0] if val else ''
  4260. for key, val in results.items()}
  4261. def get_xml_id(self):
  4262. warnings.warn(
  4263. 'get_xml_id() is deprecated method, use get_external_id() instead',
  4264. DeprecationWarning, stacklevel=2,
  4265. )
  4266. return self.get_external_id()
  4267. # Transience
  4268. @classmethod
  4269. def is_transient(cls):
  4270. """ Return whether the model is transient.
  4271. See :class:`TransientModel`.
  4272. """
  4273. return cls._transient
  4274. @api.model
  4275. def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None, **read_kwargs):
  4276. """Perform a :meth:`search` followed by a :meth:`read`.
  4277. :param domain: Search domain, see ``args`` parameter in :meth:`search`.
  4278. Defaults to an empty domain that will match all records.
  4279. :param fields: List of fields to read, see ``fields`` parameter in :meth:`read`.
  4280. Defaults to all fields.
  4281. :param int offset: Number of records to skip, see ``offset`` parameter in :meth:`search`.
  4282. Defaults to 0.
  4283. :param int limit: Maximum number of records to return, see ``limit`` parameter in :meth:`search`.
  4284. Defaults to no limit.
  4285. :param order: Columns to sort result, see ``order`` parameter in :meth:`search`.
  4286. Defaults to no sort.
  4287. :param read_kwargs: All read keywords arguments used to call
  4288. ``read(..., **read_kwargs)`` method e.g. you can use
  4289. ``search_read(..., load='')`` in order to avoid computing name_get
  4290. :return: List of dictionaries containing the asked fields.
  4291. :rtype: list(dict).
  4292. """
  4293. records = self.search(domain or [], offset=offset, limit=limit, order=order)
  4294. if not records:
  4295. return []
  4296. if fields and fields == ['id']:
  4297. # shortcut read if we only want the ids
  4298. return [{'id': record.id} for record in records]
  4299. # read() ignores active_test, but it would forward it to any downstream search call
  4300. # (e.g. for x2m or function fields), and this is not the desired behavior, the flag
  4301. # was presumably only meant for the main search().
  4302. # TODO: Move this to read() directly?
  4303. if 'active_test' in self._context:
  4304. context = dict(self._context)
  4305. del context['active_test']
  4306. records = records.with_context(context)
  4307. result = records.read(fields, **read_kwargs)
  4308. if len(result) <= 1:
  4309. return result
  4310. # reorder read
  4311. index = {vals['id']: vals for vals in result}
  4312. return [index[record.id] for record in records if record.id in index]
  4313. def toggle_active(self):
  4314. "Inverses the value of :attr:`active` on the records in ``self``."
  4315. active_recs = self.filtered(self._active_name)
  4316. active_recs[self._active_name] = False
  4317. (self - active_recs)[self._active_name] = True
  4318. def action_archive(self):
  4319. """Sets :attr:`active` to ``False`` on a recordset, by calling
  4320. :meth:`toggle_active` on its currently active records.
  4321. """
  4322. return self.filtered(lambda record: record[self._active_name]).toggle_active()
  4323. def action_unarchive(self):
  4324. """Sets :attr:`active` to ``True`` on a recordset, by calling
  4325. :meth:`toggle_active` on its currently inactive records.
  4326. """
  4327. return self.filtered(lambda record: not record[self._active_name]).toggle_active()
  4328. def _register_hook(self):
  4329. """ stuff to do right after the registry is built """
  4330. def _unregister_hook(self):
  4331. """ Clean up what `~._register_hook` has done. """
  4332. @classmethod
  4333. def _patch_method(cls, name, method):
  4334. """ Monkey-patch a method for all instances of this model. This replaces
  4335. the method called ``name`` by ``method`` in the given class.
  4336. The original method is then accessible via ``method.origin``, and it
  4337. can be restored with :meth:`~._revert_method`.
  4338. Example::
  4339. def do_write(self, values):
  4340. # do stuff, and call the original method
  4341. return do_write.origin(self, values)
  4342. # patch method write of model
  4343. model._patch_method('write', do_write)
  4344. # this will call do_write
  4345. records = model.search([...])
  4346. records.write(...)
  4347. # restore the original method
  4348. model._revert_method('write')
  4349. """
  4350. origin = getattr(cls, name)
  4351. method.origin = origin
  4352. # propagate decorators from origin to method, and apply api decorator
  4353. wrapped = api.propagate(origin, method)
  4354. wrapped.origin = origin
  4355. setattr(cls, name, wrapped)
  4356. @classmethod
  4357. def _revert_method(cls, name):
  4358. """ Revert the original method called ``name`` in the given class.
  4359. See :meth:`~._patch_method`.
  4360. """
  4361. method = getattr(cls, name)
  4362. setattr(cls, name, method.origin)
  4363. #
  4364. # Instance creation
  4365. #
  4366. # An instance represents an ordered collection of records in a given
  4367. # execution environment. The instance object refers to the environment, and
  4368. # the records themselves are represented by their cache dictionary. The 'id'
  4369. # of each record is found in its corresponding cache dictionary.
  4370. #
  4371. # This design has the following advantages:
  4372. # - cache access is direct and thus fast;
  4373. # - one can consider records without an 'id' (see new records);
  4374. # - the global cache is only an index to "resolve" a record 'id'.
  4375. #
  4376. def __init__(self, env, ids, prefetch_ids):
  4377. """ Create a recordset instance.
  4378. :param env: an environment
  4379. :param ids: a tuple of record ids
  4380. :param prefetch_ids: a reversible iterable of record ids (for prefetching)
  4381. """
  4382. self.env = env
  4383. self._ids = ids
  4384. self._prefetch_ids = prefetch_ids
  4385. def browse(self, ids=None):
  4386. """ browse([ids]) -> records
  4387. Returns a recordset for the ids provided as parameter in the current
  4388. environment.
  4389. .. code-block:: python
  4390. self.browse([7, 18, 12])
  4391. res.partner(7, 18, 12)
  4392. :param ids: id(s)
  4393. :type ids: int or iterable(int) or None
  4394. :return: recordset
  4395. """
  4396. if not ids:
  4397. ids = ()
  4398. elif ids.__class__ is int:
  4399. ids = (ids,)
  4400. else:
  4401. ids = tuple(ids)
  4402. return self.__class__(self.env, ids, ids)
  4403. #
  4404. # Internal properties, for manipulating the instance's implementation
  4405. #
  4406. @property
  4407. def ids(self):
  4408. """ Return the list of actual record ids corresponding to ``self``. """
  4409. return list(origin_ids(self._ids))
  4410. # backward-compatibility with former browse records
  4411. _cr = property(lambda self: self.env.cr)
  4412. _uid = property(lambda self: self.env.uid)
  4413. _context = property(lambda self: self.env.context)
  4414. #
  4415. # Conversion methods
  4416. #
  4417. def ensure_one(self):
  4418. """Verify that the current recordset holds a single record.
  4419. :raise odoo.exceptions.ValueError: ``len(self) != 1``
  4420. """
  4421. try:
  4422. # unpack to ensure there is only one value is faster than len when true and
  4423. # has a significant impact as this check is largely called
  4424. _id, = self._ids
  4425. return self
  4426. except ValueError:
  4427. raise ValueError("Expected singleton: %s" % self)
  4428. def with_env(self, env):
  4429. """Return a new version of this recordset attached to the provided environment.
  4430. :param env:
  4431. :type env: :class:`~odoo.api.Environment`
  4432. .. note::
  4433. The returned recordset has the same prefetch object as ``self``.
  4434. """
  4435. return self.__class__(env, self._ids, self._prefetch_ids)
  4436. def sudo(self, flag=True):
  4437. """ sudo([flag=True])
  4438. Returns a new version of this recordset with superuser mode enabled or
  4439. disabled, depending on `flag`. The superuser mode does not change the
  4440. current user, and simply bypasses access rights checks.
  4441. .. warning::
  4442. Using ``sudo`` could cause data access to cross the
  4443. boundaries of record rules, possibly mixing records that
  4444. are meant to be isolated (e.g. records from different
  4445. companies in multi-company environments).
  4446. It may lead to un-intuitive results in methods which select one
  4447. record among many - for example getting the default company, or
  4448. selecting a Bill of Materials.
  4449. .. note::
  4450. The returned recordset has the same prefetch object as ``self``.
  4451. """
  4452. assert isinstance(flag, bool)
  4453. return self.with_env(self.env(su=flag))
  4454. def with_user(self, user):
  4455. """ with_user(user)
  4456. Return a new version of this recordset attached to the given user, in
  4457. non-superuser mode, unless `user` is the superuser (by convention, the
  4458. superuser is always in superuser mode.)
  4459. """
  4460. if not user:
  4461. return self
  4462. return self.with_env(self.env(user=user, su=False))
  4463. def with_company(self, company):
  4464. """ with_company(company)
  4465. Return a new version of this recordset with a modified context, such that::
  4466. result.env.company = company
  4467. result.env.companies = self.env.companies | company
  4468. :param company: main company of the new environment.
  4469. :type company: :class:`~odoo.addons.base.models.res_company` or int
  4470. .. warning::
  4471. When using an unauthorized company for current user,
  4472. accessing the company(ies) on the environment may trigger
  4473. an AccessError if not done in a sudoed environment.
  4474. """
  4475. if not company:
  4476. # With company = None/False/0/[]/empty recordset: keep current environment
  4477. return self
  4478. company_id = int(company)
  4479. allowed_company_ids = self.env.context.get('allowed_company_ids', [])
  4480. if allowed_company_ids and company_id == allowed_company_ids[0]:
  4481. return self
  4482. # Copy the allowed_company_ids list
  4483. # to avoid modifying the context of the current environment.
  4484. allowed_company_ids = list(allowed_company_ids)
  4485. if company_id in allowed_company_ids:
  4486. allowed_company_ids.remove(company_id)
  4487. allowed_company_ids.insert(0, company_id)
  4488. return self.with_context(allowed_company_ids=allowed_company_ids)
  4489. def with_context(self, *args, **kwargs):
  4490. """ with_context([context][, **overrides]) -> Model
  4491. Returns a new version of this recordset attached to an extended
  4492. context.
  4493. The extended context is either the provided ``context`` in which
  4494. ``overrides`` are merged or the *current* context in which
  4495. ``overrides`` are merged e.g.::
  4496. # current context is {'key1': True}
  4497. r2 = records.with_context({}, key2=True)
  4498. # -> r2._context is {'key2': True}
  4499. r2 = records.with_context(key2=True)
  4500. # -> r2._context is {'key1': True, 'key2': True}
  4501. .. note:
  4502. The returned recordset has the same prefetch object as ``self``.
  4503. """ # noqa: RST210
  4504. if (args and 'force_company' in args[0]) or 'force_company' in kwargs:
  4505. _logger.warning(
  4506. "Context key 'force_company' is no longer supported. "
  4507. "Use with_company(company) instead.",
  4508. stack_info=True,
  4509. )
  4510. if (args and 'company' in args[0]) or 'company' in kwargs:
  4511. _logger.warning(
  4512. "Context key 'company' is not recommended, because "
  4513. "of its special meaning in @depends_context.",
  4514. stack_info=True,
  4515. )
  4516. context = dict(args[0] if args else self._context, **kwargs)
  4517. if 'allowed_company_ids' not in context and 'allowed_company_ids' in self._context:
  4518. # Force 'allowed_company_ids' to be kept when context is overridden
  4519. # without 'allowed_company_ids'
  4520. context['allowed_company_ids'] = self._context['allowed_company_ids']
  4521. return self.with_env(self.env(context=context))
  4522. def with_prefetch(self, prefetch_ids=None):
  4523. """ with_prefetch([prefetch_ids]) -> records
  4524. Return a new version of this recordset that uses the given prefetch ids,
  4525. or ``self``'s ids if not given.
  4526. """
  4527. if prefetch_ids is None:
  4528. prefetch_ids = self._ids
  4529. return self.__class__(self.env, self._ids, prefetch_ids)
  4530. def _update_cache(self, values, validate=True):
  4531. """ Update the cache of ``self`` with ``values``.
  4532. :param values: dict of field values, in any format.
  4533. :param validate: whether values must be checked
  4534. """
  4535. self.ensure_one()
  4536. cache = self.env.cache
  4537. fields = self._fields
  4538. try:
  4539. field_values = [(fields[name], value) for name, value in values.items()]
  4540. except KeyError as e:
  4541. raise ValueError("Invalid field %r on model %r" % (e.args[0], self._name))
  4542. # convert monetary fields after other columns for correct value rounding
  4543. for field, value in sorted(field_values, key=lambda item: item[0].write_sequence):
  4544. value = field.convert_to_cache(value, self, validate)
  4545. cache.set(self, field, value, check_dirty=False)
  4546. # set inverse fields on new records in the comodel
  4547. if field.relational:
  4548. inv_recs = self[field.name].filtered(lambda r: not r.id)
  4549. if not inv_recs:
  4550. continue
  4551. for invf in self.pool.field_inverses[field]:
  4552. # DLE P98: `test_40_new_fields`
  4553. # /home/dle/src/odoo/master-nochange-fp/odoo/addons/test_new_api/tests/test_new_fields.py
  4554. # Be careful to not break `test_onchange_taxes_1`, `test_onchange_taxes_2`, `test_onchange_taxes_3`
  4555. # If you attempt to find a better solution
  4556. for inv_rec in inv_recs:
  4557. if not cache.contains(inv_rec, invf):
  4558. val = invf.convert_to_cache(self, inv_rec, validate=False)
  4559. cache.set(inv_rec, invf, val)
  4560. else:
  4561. invf._update(inv_rec, self)
  4562. def _convert_to_record(self, values):
  4563. """ Convert the ``values`` dictionary from the cache format to the
  4564. record format.
  4565. """
  4566. return {
  4567. name: self._fields[name].convert_to_record(value, self)
  4568. for name, value in values.items()
  4569. }
  4570. def _convert_to_write(self, values):
  4571. """ Convert the ``values`` dictionary into the format of :meth:`write`. """
  4572. fields = self._fields
  4573. result = {}
  4574. for name, value in values.items():
  4575. if name in fields:
  4576. field = fields[name]
  4577. value = field.convert_to_write(value, self)
  4578. if not isinstance(value, NewId):
  4579. result[name] = value
  4580. return result
  4581. #
  4582. # Record traversal and update
  4583. #
  4584. def _mapped_func(self, func):
  4585. """ Apply function ``func`` on all records in ``self``, and return the
  4586. result as a list or a recordset (if ``func`` returns recordsets).
  4587. """
  4588. if self:
  4589. vals = [func(rec) for rec in self]
  4590. if isinstance(vals[0], BaseModel):
  4591. return vals[0].union(*vals) # union of all recordsets
  4592. return vals
  4593. else:
  4594. vals = func(self)
  4595. return vals if isinstance(vals, BaseModel) else []
  4596. def mapped(self, func):
  4597. """Apply ``func`` on all records in ``self``, and return the result as a
  4598. list or a recordset (if ``func`` return recordsets). In the latter
  4599. case, the order of the returned recordset is arbitrary.
  4600. :param func: a function or a dot-separated sequence of field names
  4601. :type func: callable or str
  4602. :return: self if func is falsy, result of func applied to all ``self`` records.
  4603. :rtype: list or recordset
  4604. .. code-block:: python3
  4605. # returns a list of summing two fields for each record in the set
  4606. records.mapped(lambda r: r.field1 + r.field2)
  4607. The provided function can be a string to get field values:
  4608. .. code-block:: python3
  4609. # returns a list of names
  4610. records.mapped('name')
  4611. # returns a recordset of partners
  4612. records.mapped('partner_id')
  4613. # returns the union of all partner banks, with duplicates removed
  4614. records.mapped('partner_id.bank_ids')
  4615. """
  4616. if not func:
  4617. return self # support for an empty path of fields
  4618. if isinstance(func, str):
  4619. recs = self
  4620. for name in func.split('.'):
  4621. recs = recs._fields[name].mapped(recs)
  4622. return recs
  4623. else:
  4624. return self._mapped_func(func)
  4625. def filtered(self, func):
  4626. """Return the records in ``self`` satisfying ``func``.
  4627. :param func: a function or a dot-separated sequence of field names
  4628. :type func: callable or str
  4629. :return: recordset of records satisfying func, may be empty.
  4630. .. code-block:: python3
  4631. # only keep records whose company is the current user's
  4632. records.filtered(lambda r: r.company_id == user.company_id)
  4633. # only keep records whose partner is a company
  4634. records.filtered("partner_id.is_company")
  4635. """
  4636. if isinstance(func, str):
  4637. name = func
  4638. func = lambda rec: any(rec.mapped(name))
  4639. # populate cache
  4640. self.mapped(name)
  4641. return self.browse([rec.id for rec in self if func(rec)])
  4642. def filtered_domain(self, domain):
  4643. """Return the records in ``self`` satisfying the domain and keeping the same order.
  4644. :param domain: :ref:`A search domain <reference/orm/domains>`.
  4645. """
  4646. if not domain or not self:
  4647. return self
  4648. stack = []
  4649. for leaf in reversed(domain):
  4650. if leaf == '|':
  4651. stack.append(stack.pop() | stack.pop())
  4652. elif leaf == '!':
  4653. stack.append(set(self._ids) - stack.pop())
  4654. elif leaf == '&':
  4655. stack.append(stack.pop() & stack.pop())
  4656. elif leaf == expression.TRUE_LEAF:
  4657. stack.append(set(self._ids))
  4658. elif leaf == expression.FALSE_LEAF:
  4659. stack.append(set())
  4660. else:
  4661. (key, comparator, value) = leaf
  4662. if comparator in ('child_of', 'parent_of'):
  4663. stack.append(set(self.search([('id', 'in', self.ids), leaf], order='id')._ids))
  4664. continue
  4665. if key.endswith('.id'):
  4666. key = key[:-3]
  4667. if key == 'id':
  4668. key = ''
  4669. # determine the field with the final type for values
  4670. field = None
  4671. if key:
  4672. model = self.browse()
  4673. for fname in key.split('.'):
  4674. field = model._fields[fname]
  4675. model = model[fname]
  4676. if comparator in ('like', 'ilike', '=like', '=ilike', 'not ilike', 'not like'):
  4677. value_esc = value.replace('_', '?').replace('%', '*').replace('[', '?')
  4678. if comparator in ('in', 'not in'):
  4679. if isinstance(value, (list, tuple)):
  4680. value = set(value)
  4681. else:
  4682. value = (value,)
  4683. if field and field.type in ('date', 'datetime'):
  4684. value = {Datetime.to_datetime(v) for v in value}
  4685. elif field and field.type in ('date', 'datetime'):
  4686. value = Datetime.to_datetime(value)
  4687. matching_ids = set()
  4688. for record in self:
  4689. data = record.mapped(key)
  4690. if isinstance(data, BaseModel):
  4691. v = value
  4692. if isinstance(value, (list, tuple, set)) and value:
  4693. v = next(iter(value))
  4694. if isinstance(v, str):
  4695. data = data.mapped('display_name')
  4696. else:
  4697. data = data and data.ids or [False]
  4698. elif field and field.type in ('date', 'datetime'):
  4699. data = [Datetime.to_datetime(d) for d in data]
  4700. if comparator == '=':
  4701. ok = value in data
  4702. elif comparator in ('!=', '<>'):
  4703. ok = value not in data
  4704. elif comparator == '=?':
  4705. ok = not value or (value in data)
  4706. elif comparator == 'in':
  4707. ok = value and any(x in value for x in data)
  4708. elif comparator == 'not in':
  4709. ok = not (value and any(x in value for x in data))
  4710. elif comparator == '<':
  4711. ok = any(x is not None and x < value for x in data)
  4712. elif comparator == '>':
  4713. ok = any(x is not None and x > value for x in data)
  4714. elif comparator == '<=':
  4715. ok = any(x is not None and x <= value for x in data)
  4716. elif comparator == '>=':
  4717. ok = any(x is not None and x >= value for x in data)
  4718. elif comparator == 'ilike':
  4719. data = [(x or "").lower() for x in data]
  4720. ok = fnmatch.filter(data, '*' + (value_esc or '').lower() + '*')
  4721. elif comparator == 'not ilike':
  4722. value = value.lower()
  4723. ok = not any(value in (x or "").lower() for x in data)
  4724. elif comparator == 'like':
  4725. data = [(x or "") for x in data]
  4726. ok = fnmatch.filter(data, value and '*' + value_esc + '*')
  4727. elif comparator == 'not like':
  4728. ok = not any(value in (x or "") for x in data)
  4729. elif comparator == '=like':
  4730. data = [(x or "") for x in data]
  4731. ok = fnmatch.filter(data, value_esc)
  4732. elif comparator == '=ilike':
  4733. data = [(x or "").lower() for x in data]
  4734. ok = fnmatch.filter(data, value and value_esc.lower())
  4735. else:
  4736. raise ValueError(f"Invalid term domain '{leaf}', operator '{comparator}' doesn't exist.")
  4737. if ok:
  4738. matching_ids.add(record.id)
  4739. stack.append(matching_ids)
  4740. while len(stack) > 1:
  4741. stack.append(stack.pop() & stack.pop())
  4742. [result_ids] = stack
  4743. return self.browse(id_ for id_ in self._ids if id_ in result_ids)
  4744. def sorted(self, key=None, reverse=False):
  4745. """Return the recordset ``self`` ordered by ``key``.
  4746. :param key: either a function of one argument that returns a
  4747. comparison key for each record, or a field name, or ``None``, in
  4748. which case records are ordered according the default model's order
  4749. :type key: callable or str or None
  4750. :param bool reverse: if ``True``, return the result in reverse order
  4751. .. code-block:: python3
  4752. # sort records by name
  4753. records.sorted(key=lambda r: r.name)
  4754. """
  4755. if key is None:
  4756. recs = self.search([('id', 'in', self.ids)])
  4757. return self.browse(reversed(recs._ids)) if reverse else recs
  4758. if isinstance(key, str):
  4759. key = itemgetter(key)
  4760. return self.browse(item.id for item in sorted(self, key=key, reverse=reverse))
  4761. def update(self, values):
  4762. """ Update the records in ``self`` with ``values``. """
  4763. for name, value in values.items():
  4764. self[name] = value
  4765. @api.model
  4766. def flush(self, fnames=None, records=None):
  4767. """ Process all the pending computations (on all models), and flush all
  4768. the pending updates to the database.
  4769. :param list[str] fnames: list of field names to flush. If given,
  4770. limit the processing to the given fields of the current model.
  4771. :param Model records: if given (together with ``fnames``), limit the
  4772. processing to the given records.
  4773. """
  4774. warnings.warn(
  4775. "Deprecated method flush(), use flush_model(), flush_recordset() or env.flush_all() instead",
  4776. DeprecationWarning, stacklevel=2,
  4777. )
  4778. if fnames is None:
  4779. self.env.flush_all()
  4780. elif records is None:
  4781. self.flush_model(fnames)
  4782. else:
  4783. records.flush_recordset(fnames)
  4784. def flush_model(self, fnames=None):
  4785. """ Process the pending computations and database updates on ``self``'s
  4786. model. When the parameter is given, the method guarantees that at least
  4787. the given fields are flushed to the database. More fields can be
  4788. flushed, though.
  4789. :param fnames: optional iterable of field names to flush
  4790. """
  4791. self._recompute_model(fnames)
  4792. self._flush(fnames)
  4793. def flush_recordset(self, fnames=None):
  4794. """ Process the pending computations and database updates on the records
  4795. ``self``. When the parameter is given, the method guarantees that at
  4796. least the given fields on records ``self`` are flushed to the database.
  4797. More fields and records can be flushed, though.
  4798. :param fnames: optional iterable of field names to flush
  4799. """
  4800. self._recompute_recordset(fnames)
  4801. fields_ = None if fnames is None else (self._fields[fname] for fname in fnames)
  4802. if self.env.cache.has_dirty_fields(self, fields_):
  4803. self._flush(fnames)
  4804. def _flush(self, fnames=None):
  4805. def process(model, id_vals):
  4806. # group record ids by vals, to update in batch when possible
  4807. updates = defaultdict(list)
  4808. for id_, vals in id_vals.items():
  4809. updates[frozendict(vals)].append(id_)
  4810. for vals, ids in updates.items():
  4811. model.browse(ids)._write(vals)
  4812. # DLE P76: test_onchange_one2many_with_domain_on_related_field
  4813. # ```
  4814. # email.important = True
  4815. # self.assertIn(email, discussion.important_emails)
  4816. # ```
  4817. # When a search on a field coming from a related occurs (the domain
  4818. # on discussion.important_emails field), make sure the related field
  4819. # is flushed
  4820. if fnames is None:
  4821. fields = self._fields.values()
  4822. else:
  4823. fields = [self._fields[fname] for fname in fnames]
  4824. model_fields = defaultdict(list)
  4825. for field in fields:
  4826. model_fields[field.model_name].append(field)
  4827. if field.related_field:
  4828. model_fields[field.related_field.model_name].append(field.related_field)
  4829. for model_name, fields_ in model_fields.items():
  4830. dirty_fields = self.env.cache.get_dirty_fields()
  4831. if any(field in dirty_fields for field in fields_):
  4832. # if any field is context-dependent, the values to flush should
  4833. # be found with a context where the context keys are all None
  4834. context_none = dict.fromkeys(
  4835. key
  4836. for field in fields_
  4837. for key in self.pool.field_depends_context[field]
  4838. )
  4839. model = self.env(context=context_none)[model_name]
  4840. id_vals = defaultdict(dict)
  4841. for field in model._fields.values():
  4842. ids = self.env.cache.clear_dirty_field(field)
  4843. if not ids:
  4844. continue
  4845. records = model.browse(ids)
  4846. values = list(self.env.cache.get_values(records, field))
  4847. assert len(values) == len(records), \
  4848. f"Could not find all values of {field} to flush them\n" \
  4849. f" Context: {self.env.context}\n" \
  4850. f" Cache: {self.env.cache!r}"
  4851. for record, value in zip(records, values):
  4852. if not field.translate:
  4853. value = field.convert_to_write(value, record)
  4854. value = field.convert_to_column(value, record)
  4855. else:
  4856. value = field._convert_from_cache_to_column(value)
  4857. id_vals[record.id][field.name] = value
  4858. process(model, id_vals)
  4859. # flush the inverse of one2many fields, too
  4860. for field in fields:
  4861. if field.type == 'one2many' and field.inverse_name:
  4862. self.env[field.comodel_name].flush_model([field.inverse_name])
  4863. #
  4864. # New records - represent records that do not exist in the database yet;
  4865. # they are used to perform onchanges.
  4866. #
  4867. @api.model
  4868. def new(self, values=None, origin=None, ref=None):
  4869. """ new([values], [origin], [ref]) -> record
  4870. Return a new record instance attached to the current environment and
  4871. initialized with the provided ``value``. The record is *not* created
  4872. in database, it only exists in memory.
  4873. One can pass an ``origin`` record, which is the actual record behind the
  4874. result. It is retrieved as ``record._origin``. Two new records with the
  4875. same origin record are considered equal.
  4876. One can also pass a ``ref`` value to identify the record among other new
  4877. records. The reference is encapsulated in the ``id`` of the record.
  4878. """
  4879. if values is None:
  4880. values = {}
  4881. if origin is not None:
  4882. origin = origin.id
  4883. record = self.browse((NewId(origin, ref),))
  4884. record._update_cache(values, validate=False)
  4885. return record
  4886. @property
  4887. def _origin(self):
  4888. """ Return the actual records corresponding to ``self``. """
  4889. ids = tuple(origin_ids(self._ids))
  4890. prefetch_ids = OriginIds(self._prefetch_ids)
  4891. return self.__class__(self.env, ids, prefetch_ids)
  4892. #
  4893. # "Dunder" methods
  4894. #
  4895. def __bool__(self):
  4896. """ Test whether ``self`` is nonempty. """
  4897. return True if self._ids else False # fast version of bool(self._ids)
  4898. __nonzero__ = __bool__
  4899. def __len__(self):
  4900. """ Return the size of ``self``. """
  4901. return len(self._ids)
  4902. def __iter__(self):
  4903. """ Return an iterator over ``self``. """
  4904. if len(self._ids) > PREFETCH_MAX and self._prefetch_ids is self._ids:
  4905. for ids in self.env.cr.split_for_in_conditions(self._ids):
  4906. for id_ in ids:
  4907. yield self.__class__(self.env, (id_,), ids)
  4908. else:
  4909. for id_ in self._ids:
  4910. yield self.__class__(self.env, (id_,), self._prefetch_ids)
  4911. def __reversed__(self):
  4912. """ Return an reversed iterator over ``self``. """
  4913. if len(self._ids) > PREFETCH_MAX and self._prefetch_ids is self._ids:
  4914. for ids in self.env.cr.split_for_in_conditions(reversed(self._ids)):
  4915. for id_ in ids:
  4916. yield self.__class__(self.env, (id_,), ids)
  4917. elif self._ids:
  4918. prefetch_ids = ReversedIterable(self._prefetch_ids)
  4919. for id_ in reversed(self._ids):
  4920. yield self.__class__(self.env, (id_,), prefetch_ids)
  4921. def __contains__(self, item):
  4922. """ Test whether ``item`` (record or field name) is an element of ``self``.
  4923. In the first case, the test is fully equivalent to::
  4924. any(item == record for record in self)
  4925. """
  4926. try:
  4927. if self._name == item._name:
  4928. return len(item) == 1 and item.id in self._ids
  4929. raise TypeError(f"inconsistent models in: {item} in {self}")
  4930. except AttributeError:
  4931. if isinstance(item, str):
  4932. return item in self._fields
  4933. raise TypeError(f"unsupported operand types in: {item!r} in {self}")
  4934. def __add__(self, other):
  4935. """ Return the concatenation of two recordsets. """
  4936. return self.concat(other)
  4937. def concat(self, *args):
  4938. """ Return the concatenation of ``self`` with all the arguments (in
  4939. linear time complexity).
  4940. """
  4941. ids = list(self._ids)
  4942. for arg in args:
  4943. try:
  4944. if arg._name != self._name:
  4945. raise TypeError(f"inconsistent models in: {self} + {arg}")
  4946. ids.extend(arg._ids)
  4947. except AttributeError:
  4948. raise TypeError(f"unsupported operand types in: {self} + {arg!r}")
  4949. return self.browse(ids)
  4950. def __sub__(self, other):
  4951. """ Return the recordset of all the records in ``self`` that are not in
  4952. ``other``. Note that recordset order is preserved.
  4953. """
  4954. try:
  4955. if self._name != other._name:
  4956. raise TypeError(f"inconsistent models in: {self} - {other}")
  4957. other_ids = set(other._ids)
  4958. return self.browse([id for id in self._ids if id not in other_ids])
  4959. except AttributeError:
  4960. raise TypeError(f"unsupported operand types in: {self} - {other!r}")
  4961. def __and__(self, other):
  4962. """ Return the intersection of two recordsets.
  4963. Note that first occurrence order is preserved.
  4964. """
  4965. try:
  4966. if self._name != other._name:
  4967. raise TypeError(f"inconsistent models in: {self} & {other}")
  4968. other_ids = set(other._ids)
  4969. return self.browse(OrderedSet(id for id in self._ids if id in other_ids))
  4970. except AttributeError:
  4971. raise TypeError(f"unsupported operand types in: {self} & {other!r}")
  4972. def __or__(self, other):
  4973. """ Return the union of two recordsets.
  4974. Note that first occurrence order is preserved.
  4975. """
  4976. return self.union(other)
  4977. def union(self, *args):
  4978. """ Return the union of ``self`` with all the arguments (in linear time
  4979. complexity, with first occurrence order preserved).
  4980. """
  4981. ids = list(self._ids)
  4982. for arg in args:
  4983. try:
  4984. if arg._name != self._name:
  4985. raise TypeError(f"inconsistent models in: {self} | {arg}")
  4986. ids.extend(arg._ids)
  4987. except AttributeError:
  4988. raise TypeError(f"unsupported operand types in: {self} | {arg!r}")
  4989. return self.browse(OrderedSet(ids))
  4990. def __eq__(self, other):
  4991. """ Test whether two recordsets are equivalent (up to reordering). """
  4992. try:
  4993. return self._name == other._name and set(self._ids) == set(other._ids)
  4994. except AttributeError:
  4995. if other:
  4996. warnings.warn(f"unsupported operand type(s) for \"==\": '{self._name}()' == '{other!r}'", stacklevel=2)
  4997. return NotImplemented
  4998. def __lt__(self, other):
  4999. try:
  5000. if self._name == other._name:
  5001. return set(self._ids) < set(other._ids)
  5002. except AttributeError:
  5003. pass
  5004. return NotImplemented
  5005. def __le__(self, other):
  5006. try:
  5007. if self._name == other._name:
  5008. # these are much cheaper checks than a proper subset check, so
  5009. # optimise for checking if a null or singleton are subsets of a
  5010. # recordset
  5011. if not self or self in other:
  5012. return True
  5013. return set(self._ids) <= set(other._ids)
  5014. except AttributeError:
  5015. pass
  5016. return NotImplemented
  5017. def __gt__(self, other):
  5018. try:
  5019. if self._name == other._name:
  5020. return set(self._ids) > set(other._ids)
  5021. except AttributeError:
  5022. pass
  5023. return NotImplemented
  5024. def __ge__(self, other):
  5025. try:
  5026. if self._name == other._name:
  5027. if not other or other in self:
  5028. return True
  5029. return set(self._ids) >= set(other._ids)
  5030. except AttributeError:
  5031. pass
  5032. return NotImplemented
  5033. def __int__(self):
  5034. return self.id or 0
  5035. def __repr__(self):
  5036. return f"{self._name}{self._ids!r}"
  5037. def __hash__(self):
  5038. return hash((self._name, frozenset(self._ids)))
  5039. def __getitem__(self, key):
  5040. """ If ``key`` is an integer or a slice, return the corresponding record
  5041. selection as an instance (attached to ``self.env``).
  5042. Otherwise read the field ``key`` of the first record in ``self``.
  5043. Examples::
  5044. inst = model.search(dom) # inst is a recordset
  5045. r4 = inst[3] # fourth record in inst
  5046. rs = inst[10:20] # subset of inst
  5047. nm = rs['name'] # name of first record in inst
  5048. """
  5049. if isinstance(key, str):
  5050. # important: one must call the field's getter
  5051. return self._fields[key].__get__(self, type(self))
  5052. elif isinstance(key, slice):
  5053. return self.browse(self._ids[key])
  5054. else:
  5055. return self.browse((self._ids[key],))
  5056. def __setitem__(self, key, value):
  5057. """ Assign the field ``key`` to ``value`` in record ``self``. """
  5058. # important: one must call the field's setter
  5059. return self._fields[key].__set__(self, value)
  5060. #
  5061. # Cache and recomputation management
  5062. #
  5063. @property
  5064. def _cache(self):
  5065. """ Return the cache of ``self``, mapping field names to values. """
  5066. return RecordCache(self)
  5067. def _in_cache_without(self, field, limit=PREFETCH_MAX):
  5068. """ Return records to prefetch that have no value in cache for ``field``
  5069. (:class:`Field` instance), including ``self``.
  5070. Return at most ``limit`` records.
  5071. """
  5072. ids = expand_ids(self.id, self._prefetch_ids)
  5073. ids = self.env.cache.get_missing_ids(self.browse(ids), field)
  5074. if limit:
  5075. ids = itertools.islice(ids, limit)
  5076. # Those records are aimed at being either fetched, or computed. But the
  5077. # method '_fetch_field' is not correct with new records: it considers
  5078. # them as forbidden records, and clears their cache! On the other hand,
  5079. # compute methods are not invoked with a mix of real and new records for
  5080. # the sake of code simplicity.
  5081. return self.browse(ids)
  5082. @api.model
  5083. def refresh(self):
  5084. """ Clear the records cache.
  5085. .. deprecated:: 8.0
  5086. The record cache is automatically invalidated.
  5087. """
  5088. warnings.warn('refresh() is deprecated method, use invalidate_cache() instead',
  5089. DeprecationWarning, stacklevel=2)
  5090. self.env.invalidate_all()
  5091. @api.model
  5092. def invalidate_cache(self, fnames=None, ids=None):
  5093. """ Invalidate the record caches after some records have been modified.
  5094. If both ``fnames`` and ``ids`` are ``None``, the whole cache is cleared.
  5095. :param fnames: the list of modified fields, or ``None`` for all fields
  5096. :param ids: the list of modified record ids, or ``None`` for all
  5097. """
  5098. warnings.warn(
  5099. "Deprecated method invalidate_cache(), use invalidate_model(), invalidate_recordset() or env.invalidate_all() instead",
  5100. DeprecationWarning, stacklevel=2
  5101. )
  5102. if ids is not None:
  5103. self.browse(ids).invalidate_recordset(fnames)
  5104. elif fnames is not None:
  5105. self.invalidate_model(fnames)
  5106. else:
  5107. self.env.invalidate_all()
  5108. def invalidate_model(self, fnames=None, flush=True):
  5109. """ Invalidate the cache of all records of ``self``'s model, when the
  5110. cached values no longer correspond to the database values. If the
  5111. parameter is given, only the given fields are invalidated from cache.
  5112. :param fnames: optional iterable of field names to invalidate
  5113. :param flush: whether pending updates should be flushed before invalidation.
  5114. It is ``True`` by default, which ensures cache consistency.
  5115. Do not use this parameter unless you know what you are doing.
  5116. """
  5117. if flush:
  5118. self.flush_model(fnames)
  5119. self._invalidate_cache(fnames)
  5120. def invalidate_recordset(self, fnames=None, flush=True):
  5121. """ Invalidate the cache of the records in ``self``, when the cached
  5122. values no longer correspond to the database values. If the parameter
  5123. is given, only the given fields on ``self`` are invalidated from cache.
  5124. :param fnames: optional iterable of field names to invalidate
  5125. :param flush: whether pending updates should be flushed before invalidation.
  5126. It is ``True`` by default, which ensures cache consistency.
  5127. Do not use this parameter unless you know what you are doing.
  5128. """
  5129. if flush:
  5130. self.flush_recordset(fnames)
  5131. self._invalidate_cache(fnames, self._ids)
  5132. def _invalidate_cache(self, fnames=None, ids=None):
  5133. if fnames is None:
  5134. fields = self._fields.values()
  5135. else:
  5136. fields = [self._fields[fname] for fname in fnames]
  5137. spec = []
  5138. for field in fields:
  5139. spec.append((field, ids))
  5140. # TODO VSC: used to remove the inverse of many_to_one from the cache, though we might not need it anymore
  5141. for invf in self.pool.field_inverses[field]:
  5142. self.env[invf.model_name].flush_model([invf.name])
  5143. spec.append((invf, None))
  5144. self.env.cache.invalidate(spec)
  5145. def modified(self, fnames, create=False, before=False):
  5146. """ Notify that fields will be or have been modified on ``self``. This
  5147. invalidates the cache where necessary, and prepares the recomputation of
  5148. dependent stored fields.
  5149. :param fnames: iterable of field names modified on records ``self``
  5150. :param create: whether called in the context of record creation
  5151. :param before: whether called before modifying records ``self``
  5152. """
  5153. if not self or not fnames:
  5154. return
  5155. # The triggers of a field F is a tree that contains the fields that
  5156. # depend on F, together with the fields to inverse to find out which
  5157. # records to recompute.
  5158. #
  5159. # For instance, assume that G depends on F, H depends on X.F, I depends
  5160. # on W.X.F, and J depends on Y.F. The triggers of F will be the tree:
  5161. #
  5162. # [G]
  5163. # X/ \Y
  5164. # [H] [J]
  5165. # W/
  5166. # [I]
  5167. #
  5168. # This tree provides perfect support for the trigger mechanism:
  5169. # when F is # modified on records,
  5170. # - mark G to recompute on records,
  5171. # - mark H to recompute on inverse(X, records),
  5172. # - mark I to recompute on inverse(W, inverse(X, records)),
  5173. # - mark J to recompute on inverse(Y, records).
  5174. # The fields' trigger trees are merged in order to evaluate all triggers
  5175. # at once. For non-stored computed fields, `_modified_triggers` might
  5176. # traverse the tree (at the cost of extra queries) only to know which
  5177. # records to invalidate in cache. But in many cases, most of these
  5178. # fields have no data in cache, so they can be ignored from the start.
  5179. # This allows us to discard subtrees from the merged tree when they
  5180. # only contain such fields.
  5181. cache = self.env.cache
  5182. tree = self.pool.get_trigger_tree(
  5183. [self._fields[fname] for fname in fnames],
  5184. select=lambda field: (field.compute and field.store) or cache.contains_field(field),
  5185. )
  5186. if not tree:
  5187. return
  5188. # determine what to compute (through an iterator)
  5189. tocompute = self.sudo().with_context(active_test=False)._modified_triggers(tree, create)
  5190. # When called after modification, one should traverse backwards
  5191. # dependencies by taking into account all fields already known to be
  5192. # recomputed. In that case, we mark fieds to compute as soon as
  5193. # possible.
  5194. #
  5195. # When called before modification, one should mark fields to compute
  5196. # after having inversed all dependencies. This is because we
  5197. # determine what currently depends on self, and it should not be
  5198. # recomputed before the modification!
  5199. if before:
  5200. tocompute = list(tocompute)
  5201. # process what to compute
  5202. for field, records, create in tocompute:
  5203. records -= self.env.protected(field)
  5204. if not records:
  5205. continue
  5206. if field.compute and field.store:
  5207. if field.recursive:
  5208. recursively_marked = self.env.not_to_compute(field, records)
  5209. self.env.add_to_compute(field, records)
  5210. else:
  5211. # Don't force the recomputation of compute fields which are
  5212. # not stored as this is not really necessary.
  5213. if field.recursive:
  5214. recursively_marked = records & self.env.cache.get_records(records, field)
  5215. self.env.cache.invalidate([(field, records._ids)])
  5216. # recursively trigger recomputation of field's dependents
  5217. if field.recursive:
  5218. recursively_marked.modified([field.name], create)
  5219. def _modified_triggers(self, tree, create=False):
  5220. """ Return an iterator traversing a tree of field triggers on ``self``,
  5221. traversing backwards field dependencies along the way, and yielding
  5222. tuple ``(field, records, created)`` to recompute.
  5223. """
  5224. if not self:
  5225. return
  5226. # first yield what to compute
  5227. for field in tree.root:
  5228. yield field, self, create
  5229. # then traverse dependencies backwards, and proceed recursively
  5230. for field, subtree in tree.items():
  5231. if create and field.type in ('many2one', 'many2one_reference'):
  5232. # upon creation, no other record has a reference to self
  5233. continue
  5234. # subtree is another tree of dependencies
  5235. model = self.env[field.model_name]
  5236. for invf in model.pool.field_inverses[field]:
  5237. # use an inverse of field without domain
  5238. if not (invf.type in ('one2many', 'many2many') and invf.domain):
  5239. if invf.type == 'many2one_reference':
  5240. rec_ids = OrderedSet()
  5241. for rec in self:
  5242. try:
  5243. if rec[invf.model_field] == field.model_name:
  5244. rec_ids.add(rec[invf.name])
  5245. except MissingError:
  5246. continue
  5247. records = model.browse(rec_ids)
  5248. else:
  5249. try:
  5250. records = self[invf.name]
  5251. except MissingError:
  5252. records = self.exists()[invf.name]
  5253. # TODO: find a better fix
  5254. if field.model_name == records._name:
  5255. if not any(self._ids):
  5256. # if self are new, records should be new as well
  5257. records = records.browse(it and NewId(it) for it in records._ids)
  5258. break
  5259. else:
  5260. new_records = self.filtered(lambda r: not r.id)
  5261. real_records = self - new_records
  5262. records = model.browse()
  5263. if real_records:
  5264. records = model.search([(field.name, 'in', real_records.ids)], order='id')
  5265. if new_records:
  5266. cache_records = self.env.cache.get_records(model, field)
  5267. records |= cache_records.filtered(lambda r: set(r[field.name]._ids) & set(self._ids))
  5268. yield from records._modified_triggers(subtree)
  5269. @api.model
  5270. def recompute(self, fnames=None, records=None):
  5271. """ Recompute all function fields (or the given ``fnames`` if present).
  5272. The fields and records to recompute have been determined by method
  5273. :meth:`modified`.
  5274. """
  5275. warnings.warn(
  5276. "Deprecated method recompute(), use flush_model(), flush_recordset() or env.flush_all() instead",
  5277. DeprecationWarning, stacklevel=2,
  5278. )
  5279. if fnames is None:
  5280. self.env._recompute_all()
  5281. elif records is None:
  5282. self._recompute_model(fnames)
  5283. else:
  5284. records._recompute_recordset(fnames)
  5285. def _recompute_model(self, fnames=None):
  5286. """ Process the pending computations of the fields of ``self``'s model.
  5287. :param fnames: optional iterable of field names to compute
  5288. """
  5289. if fnames is None:
  5290. fields = self._fields.values()
  5291. else:
  5292. fields = [self._fields[fname] for fname in fnames]
  5293. for field in fields:
  5294. if field.compute and field.store:
  5295. self._recompute_field(field)
  5296. def _recompute_recordset(self, fnames=None):
  5297. """ Process the pending computations of the fields of the records in ``self``.
  5298. :param fnames: optional iterable of field names to compute
  5299. """
  5300. if fnames is None:
  5301. fields = self._fields.values()
  5302. else:
  5303. fields = [self._fields[fname] for fname in fnames]
  5304. for field in fields:
  5305. if field.compute and field.store:
  5306. self._recompute_field(field, self._ids)
  5307. def _recompute_field(self, field, ids=None):
  5308. ids_to_compute = self.env.all.tocompute.get(field, ())
  5309. if ids is None:
  5310. ids = ids_to_compute
  5311. else:
  5312. ids = [id_ for id_ in ids if id_ in ids_to_compute]
  5313. if not ids:
  5314. return
  5315. # do not force recomputation on new records; those will be
  5316. # recomputed by accessing the field on the records
  5317. records = self.browse(tuple(id_ for id_ in ids if id_))
  5318. field.recompute(records)
  5319. #
  5320. # Generic onchange method
  5321. #
  5322. def _has_onchange(self, field, other_fields):
  5323. """ Return whether ``field`` should trigger an onchange event in the
  5324. presence of ``other_fields``.
  5325. """
  5326. return (field.name in self._onchange_methods) or any(
  5327. dep in other_fields
  5328. for dep in self.pool.get_dependent_fields(field.base_field)
  5329. )
  5330. def _onchange_eval(self, field_name, onchange, result):
  5331. """ Apply onchange method(s) for field ``field_name`` with spec ``onchange``
  5332. on record ``self``. Value assignments are applied on ``self``, while
  5333. domain and warning messages are put in dictionary ``result``.
  5334. """
  5335. onchange = onchange.strip()
  5336. def process(res):
  5337. if not res:
  5338. return
  5339. if res.get('value'):
  5340. res['value'].pop('id', None)
  5341. self.update({key: val for key, val in res['value'].items() if key in self._fields})
  5342. if res.get('domain'):
  5343. _logger.warning(
  5344. "onchange method %s returned a domain, this is deprecated",
  5345. method.__qualname__
  5346. )
  5347. result.setdefault('domain', {}).update(res['domain'])
  5348. if res.get('warning'):
  5349. result['warnings'].add((
  5350. res['warning'].get('title') or _("Warning"),
  5351. res['warning'].get('message') or "",
  5352. res['warning'].get('type') or "",
  5353. ))
  5354. if onchange in ("1", "true"):
  5355. for method in self._onchange_methods.get(field_name, ()):
  5356. method_res = method(self)
  5357. process(method_res)
  5358. return
  5359. def onchange(self, values, field_name, field_onchange):
  5360. """ Perform an onchange on the given field.
  5361. :param values: dictionary mapping field names to values, giving the
  5362. current state of modification
  5363. :param field_name: name of the modified field, or list of field
  5364. names (in view order), or False
  5365. :param field_onchange: dictionary mapping field names to their
  5366. on_change attribute
  5367. When ``field_name`` is falsy, the method first adds default values
  5368. to ``values``, computes the remaining fields, applies onchange
  5369. methods to them, and return all the fields in ``field_onchange``.
  5370. """
  5371. # this is for tests using `Form`
  5372. self.env.flush_all()
  5373. env = self.env
  5374. if isinstance(field_name, list):
  5375. names = field_name
  5376. elif field_name:
  5377. names = [field_name]
  5378. else:
  5379. names = []
  5380. first_call = not names
  5381. if any(name not in self._fields for name in names):
  5382. return {}
  5383. def PrefixTree(model, dotnames):
  5384. """ Return a prefix tree for sequences of field names. """
  5385. if not dotnames:
  5386. return {}
  5387. # group dotnames by prefix
  5388. suffixes = defaultdict(list)
  5389. for dotname in dotnames:
  5390. # name, *names = dotname.split('.', 1)
  5391. names = dotname.split('.', 1)
  5392. name = names.pop(0)
  5393. suffixes[name].extend(names)
  5394. # fill in prefix tree in fields order
  5395. tree = OrderedDict()
  5396. for name, field in model._fields.items():
  5397. if name in suffixes:
  5398. tree[name] = subtree = PrefixTree(model[name], suffixes[name])
  5399. if subtree and field.type == 'one2many':
  5400. subtree.pop(field.inverse_name, None)
  5401. return tree
  5402. class Snapshot(dict):
  5403. """ A dict with the values of a record, following a prefix tree. """
  5404. __slots__ = ()
  5405. def __init__(self, record, tree, fetch=True):
  5406. # put record in dict to include it when comparing snapshots
  5407. super(Snapshot, self).__init__({'<record>': record, '<tree>': tree})
  5408. if fetch:
  5409. for name in tree:
  5410. self.fetch(name)
  5411. def fetch(self, name):
  5412. """ Set the value of field ``name`` from the record's value. """
  5413. record = self['<record>']
  5414. tree = self['<tree>']
  5415. if record._fields[name].type in ('one2many', 'many2many'):
  5416. # x2many fields are serialized as a list of line snapshots
  5417. self[name] = [Snapshot(line, tree[name]) for line in record[name]]
  5418. else:
  5419. self[name] = record[name]
  5420. def has_changed(self, name):
  5421. """ Return whether a field on record has changed. """
  5422. if name not in self:
  5423. return True
  5424. record = self['<record>']
  5425. subnames = self['<tree>'][name]
  5426. if record._fields[name].type not in ('one2many', 'many2many'):
  5427. return self[name] != record[name]
  5428. return (
  5429. len(self[name]) != len(record[name])
  5430. or (
  5431. set(line_snapshot["<record>"].id for line_snapshot in self[name])
  5432. != set(record[name]._ids)
  5433. )
  5434. or any(
  5435. line_snapshot.has_changed(subname)
  5436. for line_snapshot in self[name]
  5437. for subname in subnames
  5438. )
  5439. )
  5440. def diff(self, other, force=False):
  5441. """ Return the values in ``self`` that differ from ``other``.
  5442. Requires record cache invalidation for correct output!
  5443. """
  5444. record = self['<record>']
  5445. result = {}
  5446. for name, subnames in self['<tree>'].items():
  5447. if name == 'id':
  5448. continue
  5449. field = record._fields[name]
  5450. if (field.type == 'properties' and field.definition_record in field_name
  5451. and other.get(name) == self[name] == []):
  5452. # TODO: The parent field on "record" can be False, if it was changed,
  5453. # (even if if was changed to a not Falsy value) because of
  5454. # >>> initial_values = dict(values, **dict.fromkeys(names, False))
  5455. # If it's the case when we will read the properties field on this record,
  5456. # it will return False as well (no parent == no definition)
  5457. # So record at the following line, will always return a empty properties
  5458. # because the definition record is always False if it triggered the onchange
  5459. # >>> snapshot0 = Snapshot(record, nametree, fetch=(not first_call))
  5460. # but we need "snapshot0" to have the old value to be able
  5461. # to compare it with the new one and trigger the onchange if necessary.
  5462. # In that particular case, "other.get(name)" must contains the
  5463. # non empty properties value.
  5464. result[name] = []
  5465. continue
  5466. if not force and other.get(name) == self[name]:
  5467. continue
  5468. if field.type not in ('one2many', 'many2many'):
  5469. result[name] = field.convert_to_onchange(self[name], record, {})
  5470. else:
  5471. # x2many fields: serialize value as commands
  5472. result[name] = commands = [Command.clear()]
  5473. # The purpose of the following line is to enable the prefetching.
  5474. # In the loop below, line._prefetch_ids actually depends on the
  5475. # value of record[name] in cache (see prefetch_ids on x2many
  5476. # fields). But the cache has been invalidated before calling
  5477. # diff(), therefore evaluating line._prefetch_ids with an empty
  5478. # cache simply returns nothing, which discards the prefetching
  5479. # optimization!
  5480. record._cache[name] = tuple(
  5481. line_snapshot['<record>'].id for line_snapshot in self[name]
  5482. )
  5483. for line_snapshot in self[name]:
  5484. line = line_snapshot['<record>']
  5485. line = line._origin or line
  5486. if not line.id:
  5487. # new line: send diff from scratch
  5488. line_diff = line_snapshot.diff({})
  5489. commands.append((Command.CREATE, line.id.ref or 0, line_diff))
  5490. else:
  5491. # existing line: check diff from database
  5492. # (requires a clean record cache!)
  5493. line_diff = line_snapshot.diff(Snapshot(line, subnames))
  5494. if line_diff:
  5495. # send all fields because the web client
  5496. # might need them to evaluate modifiers
  5497. line_diff = line_snapshot.diff({})
  5498. commands.append(Command.update(line.id, line_diff))
  5499. else:
  5500. commands.append(Command.link(line.id))
  5501. return result
  5502. nametree = PrefixTree(self.browse(), field_onchange)
  5503. if first_call:
  5504. names = [name for name in values if name != 'id']
  5505. missing_names = [name for name in nametree if name not in values]
  5506. defaults = self.default_get(missing_names)
  5507. for name in missing_names:
  5508. values[name] = defaults.get(name, False)
  5509. if name in defaults:
  5510. names.append(name)
  5511. # prefetch x2many lines: this speeds up the initial snapshot by avoiding
  5512. # computing fields on new records as much as possible, as that can be
  5513. # costly and is not necessary at all
  5514. for name, subnames in nametree.items():
  5515. if subnames and values.get(name):
  5516. # retrieve all line ids in commands
  5517. line_ids = set()
  5518. for cmd in values[name]:
  5519. if cmd[0] in (Command.UPDATE, Command.LINK):
  5520. line_ids.add(cmd[1])
  5521. elif cmd[0] == Command.SET:
  5522. line_ids.update(cmd[2])
  5523. # prefetch stored fields on lines
  5524. lines = self[name].browse(line_ids)
  5525. fnames = [subname
  5526. for subname in subnames
  5527. if lines._fields[subname].base_field.store]
  5528. lines._read(fnames)
  5529. # copy the cache of lines to their corresponding new records;
  5530. # this avoids computing computed stored fields on new_lines
  5531. new_lines = lines.browse(map(NewId, line_ids))
  5532. cache = self.env.cache
  5533. for fname in fnames:
  5534. field = lines._fields[fname]
  5535. if not field.translate:
  5536. cache.update(new_lines, field, [
  5537. field.convert_to_cache(value, new_line, validate=False)
  5538. for value, new_line in zip(cache.get_values(lines, field), new_lines)
  5539. ])
  5540. else:
  5541. cache.update_raw(
  5542. new_lines, field, map(copy.copy, cache.get_values(lines, field)),
  5543. )
  5544. # Isolate changed values, to handle inconsistent data sent from the
  5545. # client side: when a form view contains two one2many fields that
  5546. # overlap, the lines that appear in both fields may be sent with
  5547. # different data. Consider, for instance:
  5548. #
  5549. # foo_ids: [line with value=1, ...]
  5550. # bar_ids: [line with value=1, ...]
  5551. #
  5552. # If value=2 is set on 'line' in 'bar_ids', the client sends
  5553. #
  5554. # foo_ids: [line with value=1, ...]
  5555. # bar_ids: [line with value=2, ...]
  5556. #
  5557. # The idea is to put 'foo_ids' in cache first, so that the snapshot
  5558. # contains value=1 for line in 'foo_ids'. The snapshot is then updated
  5559. # with the value of `bar_ids`, which will contain value=2 on line.
  5560. #
  5561. # The issue also occurs with other fields. For instance, an onchange on
  5562. # a move line has a value for the field 'move_id' that contains the
  5563. # values of the move, among which the one2many that contains the line
  5564. # itself, with old values!
  5565. #
  5566. changed_values = {name: values[name] for name in names}
  5567. # set changed values to null in initial_values; not setting them
  5568. # triggers default_get() on the new record when creating snapshot0
  5569. initial_values = dict(values, **dict.fromkeys(names, False))
  5570. # do not force delegate fields to False
  5571. for parent_name in self._inherits.values():
  5572. if not initial_values.get(parent_name, True):
  5573. initial_values.pop(parent_name)
  5574. # create a new record with values
  5575. record = self.new(initial_values, origin=self)
  5576. # make parent records match with the form values; this ensures that
  5577. # computed fields on parent records have all their dependencies at
  5578. # their expected value
  5579. for name in initial_values:
  5580. field = self._fields.get(name)
  5581. if field and field.inherited:
  5582. parent_name, name = field.related.split('.', 1)
  5583. record[parent_name]._update_cache({name: record[name]})
  5584. # make a snapshot based on the initial values of record
  5585. snapshot0 = Snapshot(record, nametree, fetch=(not first_call))
  5586. # store changed values in cache; also trigger recomputations based on
  5587. # subfields (e.g., line.a has been modified, line.b is computed stored
  5588. # and depends on line.a, but line.b is not in the form view)
  5589. record._update_cache(changed_values, validate=False)
  5590. # update snapshot0 with changed values
  5591. for name in names:
  5592. snapshot0.fetch(name)
  5593. # Determine which field(s) should be triggered an onchange. On the first
  5594. # call, 'names' only contains fields with a default. If 'self' is a new
  5595. # line in a one2many field, 'names' also contains the one2many's inverse
  5596. # field, and that field may not be in nametree.
  5597. todo = list(unique(itertools.chain(names, nametree))) if first_call else list(names)
  5598. done = set()
  5599. # mark fields to do as modified to trigger recomputations
  5600. protected = [self._fields[name] for name in names]
  5601. with self.env.protecting(protected, record):
  5602. record.modified(todo)
  5603. for name in todo:
  5604. field = self._fields[name]
  5605. if field.inherited:
  5606. # modifying an inherited field should modify the parent
  5607. # record accordingly; because we don't actually assign the
  5608. # modified field on the record, the modification on the
  5609. # parent record has to be done explicitly
  5610. parent = record[field.related.split('.')[0]]
  5611. parent[name] = record[name]
  5612. result = {'warnings': OrderedSet()}
  5613. # process names in order
  5614. while todo:
  5615. # apply field-specific onchange methods
  5616. for name in todo:
  5617. if field_onchange.get(name):
  5618. record._onchange_eval(name, field_onchange[name], result)
  5619. done.add(name)
  5620. if not env.context.get('recursive_onchanges', True):
  5621. break
  5622. # determine which fields to process for the next pass
  5623. todo = [
  5624. name
  5625. for name in nametree
  5626. if name not in done and snapshot0.has_changed(name)
  5627. ]
  5628. # make the snapshot with the final values of record
  5629. snapshot1 = Snapshot(record, nametree)
  5630. # determine values that have changed by comparing snapshots
  5631. self.env.invalidate_all()
  5632. result['value'] = snapshot1.diff(snapshot0, force=first_call)
  5633. # format warnings
  5634. warnings = result.pop('warnings')
  5635. if len(warnings) == 1:
  5636. title, message, type = warnings.pop()
  5637. if not type:
  5638. type = 'dialog'
  5639. result['warning'] = dict(title=title, message=message, type=type)
  5640. elif len(warnings) > 1:
  5641. # concatenate warning titles and messages
  5642. title = _("Warnings")
  5643. message = '\n\n'.join([warn_title + '\n\n' + warn_message for warn_title, warn_message, warn_type in warnings])
  5644. result['warning'] = dict(title=title, message=message, type='dialog')
  5645. return result
  5646. def _get_placeholder_filename(self, field):
  5647. """ Returns the filename of the placeholder to use,
  5648. set on web/static/img by default, or the
  5649. complete path to access it (eg: module/path/to/image.png).
  5650. """
  5651. return False
  5652. def _populate_factories(self):
  5653. """ Generates a factory for the different fields of the model.
  5654. ``factory`` is a generator of values (dict of field values).
  5655. Factory skeleton::
  5656. def generator(iterator, field_name, model_name):
  5657. for counter, values in enumerate(iterator):
  5658. # values.update(dict())
  5659. yield values
  5660. See :mod:`odoo.tools.populate` for population tools and applications.
  5661. :returns: list of pairs(field_name, factory) where `factory` is a generator function.
  5662. :rtype: list(tuple(str, generator))
  5663. .. note::
  5664. It is the responsibility of the generator to handle the field_name correctly.
  5665. The generator could generate values for multiple fields together. In this case,
  5666. the field_name should be more a "field_group" (should be begin by a "_"), covering
  5667. the different fields updated by the generator (e.g. "_address" for a generator
  5668. updating multiple address fields).
  5669. """
  5670. return []
  5671. @property
  5672. def _populate_sizes(self):
  5673. """ Return a dict mapping symbolic sizes (``'small'``, ``'medium'``, ``'large'``) to integers,
  5674. giving the minimal number of records that :meth:`_populate` should create.
  5675. The default population sizes are:
  5676. * ``small`` : 10
  5677. * ``medium`` : 100
  5678. * ``large`` : 1000
  5679. """
  5680. return {
  5681. 'small': 10, # minimal representative set
  5682. 'medium': 100, # average database load
  5683. 'large': 1000, # maxi database load
  5684. }
  5685. @property
  5686. def _populate_dependencies(self):
  5687. """ Return the list of models which have to be populated before the current one.
  5688. :rtype: list
  5689. """
  5690. return []
  5691. def _populate(self, size):
  5692. """ Create records to populate this model.
  5693. :param str size: symbolic size for the number of records: ``'small'``, ``'medium'`` or ``'large'``
  5694. """
  5695. batch_size = 1000
  5696. min_size = self._populate_sizes[size]
  5697. record_count = 0
  5698. create_values = []
  5699. complete = False
  5700. field_generators = self._populate_factories()
  5701. if not field_generators:
  5702. return self.browse() # maybe create an automatic generator?
  5703. records_batches = []
  5704. generator = populate.chain_factories(field_generators, self._name)
  5705. while record_count <= min_size or not complete:
  5706. values = next(generator)
  5707. complete = values.pop('__complete')
  5708. create_values.append(values)
  5709. record_count += 1
  5710. if len(create_values) >= batch_size:
  5711. _logger.info('Batch: %s/%s', record_count, min_size)
  5712. records_batches.append(self.create(create_values))
  5713. self.env.cr.commit()
  5714. create_values = []
  5715. if create_values:
  5716. records_batches.append(self.create(create_values))
  5717. return self.concat(*records_batches)
  5718. collections.abc.Set.register(BaseModel)
  5719. # not exactly true as BaseModel doesn't have index or count
  5720. collections.abc.Sequence.register(BaseModel)
  5721. class RecordCache(MutableMapping):
  5722. """ A mapping from field names to values, to read and update the cache of a record. """
  5723. __slots__ = ['_record']
  5724. def __init__(self, record):
  5725. assert len(record) == 1, "Unexpected RecordCache(%s)" % record
  5726. self._record = record
  5727. def __contains__(self, name):
  5728. """ Return whether `record` has a cached value for field ``name``. """
  5729. field = self._record._fields[name]
  5730. return self._record.env.cache.contains(self._record, field)
  5731. def __getitem__(self, name):
  5732. """ Return the cached value of field ``name`` for `record`. """
  5733. field = self._record._fields[name]
  5734. return self._record.env.cache.get(self._record, field)
  5735. def __setitem__(self, name, value):
  5736. """ Assign the cached value of field ``name`` for ``record``. """
  5737. field = self._record._fields[name]
  5738. self._record.env.cache.set(self._record, field, value)
  5739. def __delitem__(self, name):
  5740. """ Remove the cached value of field ``name`` for ``record``. """
  5741. field = self._record._fields[name]
  5742. self._record.env.cache.remove(self._record, field)
  5743. def __iter__(self):
  5744. """ Iterate over the field names with a cached value. """
  5745. for field in self._record.env.cache.get_fields(self._record):
  5746. yield field.name
  5747. def __len__(self):
  5748. """ Return the number of fields with a cached value. """
  5749. return sum(1 for name in self)
  5750. AbstractModel = BaseModel
  5751. class Model(AbstractModel):
  5752. """ Main super-class for regular database-persisted Odoo models.
  5753. Odoo models are created by inheriting from this class::
  5754. class user(Model):
  5755. ...
  5756. The system will later instantiate the class once per database (on
  5757. which the class' module is installed).
  5758. """
  5759. _auto = True # automatically create database backend
  5760. _register = False # not visible in ORM registry, meant to be python-inherited only
  5761. _abstract = False # not abstract
  5762. _transient = False # not transient
  5763. class TransientModel(Model):
  5764. """ Model super-class for transient records, meant to be temporarily
  5765. persistent, and regularly vacuum-cleaned.
  5766. A TransientModel has a simplified access rights management, all users can
  5767. create new records, and may only access the records they created. The
  5768. superuser has unrestricted access to all TransientModel records.
  5769. """
  5770. _auto = True # automatically create database backend
  5771. _register = False # not visible in ORM registry, meant to be python-inherited only
  5772. _abstract = False # not abstract
  5773. _transient = True # transient
  5774. @api.autovacuum
  5775. def _transient_vacuum(self):
  5776. """Clean the transient records.
  5777. This unlinks old records from the transient model tables whenever the
  5778. :attr:`_transient_max_count` or :attr:`_transient_max_hours` conditions
  5779. (if any) are reached.
  5780. Actual cleaning will happen only once every 5 minutes. This means this
  5781. method can be called frequently (e.g. whenever a new record is created).
  5782. Example with both max_hours and max_count active:
  5783. Suppose max_hours = 0.2 (aka 12 minutes), max_count = 20, there are
  5784. 55 rows in the table, 10 created/changed in the last 5 minutes, an
  5785. additional 12 created/changed between 5 and 10 minutes ago, the rest
  5786. created/changed more than 12 minutes ago.
  5787. - age based vacuum will leave the 22 rows created/changed in the last 12
  5788. minutes
  5789. - count based vacuum will wipe out another 12 rows. Not just 2,
  5790. otherwise each addition would immediately cause the maximum to be
  5791. reached again.
  5792. - the 10 rows that have been created/changed the last 5 minutes will NOT
  5793. be deleted
  5794. """
  5795. if self._transient_max_hours:
  5796. # Age-based expiration
  5797. self._transient_clean_rows_older_than(self._transient_max_hours * 60 * 60)
  5798. if self._transient_max_count:
  5799. # Count-based expiration
  5800. self._transient_clean_old_rows(self._transient_max_count)
  5801. def _transient_clean_old_rows(self, max_count):
  5802. # Check how many rows we have in the table
  5803. query = 'SELECT count(*) FROM "{}"'.format(self._table)
  5804. self._cr.execute(query)
  5805. [count] = self._cr.fetchone()
  5806. if count > max_count:
  5807. self._transient_clean_rows_older_than(300)
  5808. def _transient_clean_rows_older_than(self, seconds):
  5809. # Never delete rows used in last 5 minutes
  5810. seconds = max(seconds, 300)
  5811. query = """
  5812. SELECT id FROM "{}"
  5813. WHERE COALESCE(write_date, create_date, (now() AT TIME ZONE 'UTC'))::timestamp
  5814. < (now() AT TIME ZONE 'UTC') - interval %s
  5815. """.format(self._table)
  5816. self._cr.execute(query, ["%s seconds" % seconds])
  5817. ids = [x[0] for x in self._cr.fetchall()]
  5818. self.sudo().browse(ids).unlink()
  5819. def itemgetter_tuple(items):
  5820. """ Fixes itemgetter inconsistency (useful in some cases) of not returning
  5821. a tuple if len(items) == 1: always returns an n-tuple where n = len(items)
  5822. """
  5823. if len(items) == 0:
  5824. return lambda a: ()
  5825. if len(items) == 1:
  5826. return lambda gettable: (gettable[items[0]],)
  5827. return operator.itemgetter(*items)
  5828. def convert_pgerror_not_null(model, fields, info, e):
  5829. if e.diag.table_name != model._table:
  5830. return {'message': _(u"Missing required value for the field '%s'", e.diag.column_name)}
  5831. field_name = e.diag.column_name
  5832. field = fields[field_name]
  5833. message = _(u"Missing required value for the field '%s' (%s)", field['string'], field_name)
  5834. return {
  5835. 'message': message,
  5836. 'field': field_name,
  5837. }
  5838. def convert_pgerror_unique(model, fields, info, e):
  5839. # new cursor since we're probably in an error handler in a blown
  5840. # transaction which may not have been rollbacked/cleaned yet
  5841. with closing(model.env.registry.cursor()) as cr_tmp:
  5842. cr_tmp.execute("""
  5843. SELECT
  5844. conname AS "constraint name",
  5845. t.relname AS "table name",
  5846. ARRAY(
  5847. SELECT attname FROM pg_attribute
  5848. WHERE attrelid = conrelid
  5849. AND attnum = ANY(conkey)
  5850. ) as "columns"
  5851. FROM pg_constraint
  5852. JOIN pg_class t ON t.oid = conrelid
  5853. WHERE conname = %s
  5854. """, [e.diag.constraint_name])
  5855. constraint, table, ufields = cr_tmp.fetchone() or (None, None, None)
  5856. # if the unique constraint is on an expression or on an other table
  5857. if not ufields or model._table != table:
  5858. return {'message': tools.ustr(e)}
  5859. # TODO: add stuff from e.diag.message_hint? provides details about the constraint & duplication values but may be localized...
  5860. if len(ufields) == 1:
  5861. field_name = ufields[0]
  5862. field = fields[field_name]
  5863. message = _(
  5864. u"The value for the field '%s' already exists (this is probably '%s' in the current model).",
  5865. field_name,
  5866. field['string']
  5867. )
  5868. return {
  5869. 'message': message,
  5870. 'field': field_name,
  5871. }
  5872. field_strings = [fields[fname]['string'] for fname in ufields]
  5873. message = _(u"The values for the fields '%s' already exist (they are probably '%s' in the current model).") % (', '.join(ufields), ', '.join(field_strings))
  5874. return {
  5875. 'message': message,
  5876. # no field, unclear which one we should pick and they could be in any order
  5877. }
  5878. def convert_pgerror_constraint(model, fields, info, e):
  5879. sql_constraints = dict([(('%s_%s') % (e.diag.table_name, x[0]), x) for x in model._sql_constraints])
  5880. if e.diag.constraint_name in sql_constraints.keys():
  5881. return {'message': "'%s'" % sql_constraints[e.diag.constraint_name][2]}
  5882. return {'message': tools.ustr(e)}
  5883. PGERROR_TO_OE = defaultdict(
  5884. # shape of mapped converters
  5885. lambda: (lambda model, fvg, info, pgerror: {'message': tools.ustr(pgerror)}), {
  5886. '23502': convert_pgerror_not_null,
  5887. '23505': convert_pgerror_unique,
  5888. '23514': convert_pgerror_constraint,
  5889. })
  5890. def lazy_name_get(self):
  5891. """ Evaluate self.name_get() lazily. """
  5892. names = tools.lazy(lambda: dict(self.name_get()))
  5893. return [(rid, tools.lazy(operator.getitem, names, rid)) for rid in self.ids]
  5894. # keep those imports here to avoid dependency cycle errors
  5895. # pylint: disable=wrong-import-position
  5896. from . import fields
  5897. from .osv import expression
  5898. from .fields import Field, Datetime, Command