Skip to content

Commit

Permalink
🚑 web_website fixes after odoo update odoo/odoo@b6d32de
Browse files Browse the repository at this point in the history
  • Loading branch information
KolushovAlexandr committed Dec 3, 2018
1 parent c5f42a9 commit cb4589d
Showing 1 changed file with 90 additions and 4 deletions.
94 changes: 90 additions & 4 deletions web_website/models/ir_property.py
@@ -1,6 +1,8 @@
# Copyright 2018 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from odoo import models, fields, api
from odoo.addons.base.models.ir_property import TYPE2FIELD, TYPE2CLEAN


GET_CONTEXT = dict(
Expand Down Expand Up @@ -98,10 +100,94 @@ def get(self, name, model, res_id=False):

@api.model
def get_multi(self, name, model, ids):
return super(IrProperty, self._check_website_dependent(
name, model,
**GET_CONTEXT
)).get_multi(name, model, ids)
# Due to https://github.com/odoo/odoo/commit/b6d32de31e0e18a506ae06dc27561d1d078f3ab1 commit
# We override the super method to make it website dependent
# Differences in query requests and in the setting default_value
if not ids:
return {}

website_id = self._context.get('website_id') or None
field = self.env[model]._fields[name]
field_id = self.env['ir.model.fields']._get(model, name).id
company_id = (self._context.get('force_company')
or self.env['res.company']._company_default_get(model, field_id).id)
if field.type == 'many2one':
comodel = self.env[field.comodel_name]
model_pos = len(model) + 2
value_pos = len(comodel._name) + 2
# retrieve values: both p.res_id and p.value_reference are formatted
# as "<rec._name>,<rec.id>"; the purpose of the LEFT JOIN is to
# return the value id if it exists, NULL otherwise
query = """
SELECT substr(p.res_id, %s)::integer, r.id
FROM ir_property p
LEFT JOIN {} r ON substr(p.value_reference, %s)::integer=r.id
WHERE p.fields_id=%s
AND (p.company_id=%s OR p.company_id IS NULL)
AND (p.website_id=%s OR p.website_id IS NULL)
AND (p.res_id IN %s OR p.res_id IS NULL)
ORDER BY p.website_id NULLS FIRST
""".format(comodel._table)
# query_website is used for correct website priority
query_website = """
SELECT substr(p.res_id, %s)::integer, r.id
FROM ir_property p
LEFT JOIN {} r ON substr(p.value_reference, %s)::integer=r.id
WHERE p.fields_id=%s
AND (p.company_id=%s OR p.company_id IS NULL)
AND p.website_id=%s
AND (p.res_id IN %s OR p.res_id IS NULL)
ORDER BY p.website_id NULLS FIRST
""".format(comodel._table)
params = [model_pos, value_pos, field_id, company_id, website_id]
clean = comodel.browse

elif field.type in TYPE2FIELD:
model_pos = len(model) + 2
# retrieve values: p.res_id is formatted as "<rec._name>,<rec.id>"
query = """
SELECT substr(p.res_id, %s)::integer, p.{}
FROM ir_property p
WHERE p.fields_id=%s
AND (p.company_id=%s OR p.company_id IS NULL)
AND (p.website_id=%s OR p.website_id IS NULL)
AND (p.res_id IN %s OR p.res_id IS NULL)
ORDER BY p.website_id NULLS FIRST
""".format(TYPE2FIELD[field.type])
# query_website is used for correct website priority
query_website = """
SELECT substr(p.res_id, %s)::integer, p.{}
FROM ir_property p
WHERE p.fields_id=%s
AND (p.company_id=%s OR p.company_id IS NULL)
AND p.website_id=%s
AND (p.res_id IN %s OR p.res_id IS NULL)
ORDER BY p.website_id NULLS FIRST
""".format(TYPE2FIELD[field.type])
params = [model_pos, field_id, company_id, website_id]
clean = TYPE2CLEAN[field.type]

else:
return dict.fromkeys(ids, False)

# retrieve values
cr = self.env.cr
result = {}
result_website = {}
refs = {"%s,%s" % (model, id) for id in ids}
for sub_refs in cr.split_for_in_conditions(refs):
cr.execute(query, params + [sub_refs])
fetch_result = cr.fetchall()
result.update(fetch_result)
result_website.update(cr.execute(query_website, params + [sub_refs]) or {})

# set the default value to the ids that are not in result
default_value = fetch_result.pop()
if not type(default_value) is str:
default_value = default_value[1]
for id in ids:
result[id] = clean(result_website.get(id, default_value))
return result

@api.model
def search_multi(self, name, model, operator, value):
Expand Down

0 comments on commit cb4589d

Please sign in to comment.