def test_frozendict_hash(self): """ Ensure that a frozendict is hashable. """ # dict with simple values hash(frozendict({'name': 'Joe', 'age': 42})) # dict with tuples, lists, and embedded dicts hash( frozendict({ 'user_id': (42, 'Joe'), 'line_ids': [(0, 0, { 'values': [42] })], }))
def test_frozendict_immutable(self): """ Ensure that a frozendict is immutable. """ vals = {'name': 'Joe', 'age': 42} frozen_vals = frozendict(vals) # check __setitem__, __delitem__ with self.assertRaises(Exception): frozen_vals['surname'] = 'Jack' with self.assertRaises(Exception): frozen_vals['name'] = 'Jack' with self.assertRaises(Exception): del frozen_vals['name'] # check update, setdefault, pop, popitem, clear with self.assertRaises(Exception): frozen_vals.update({'surname': 'Jack'}) with self.assertRaises(Exception): frozen_vals.update({'name': 'Jack'}) with self.assertRaises(Exception): frozen_vals.setdefault('surname', 'Jack') with self.assertRaises(Exception): frozen_vals.pop('surname', 'Jack') with self.assertRaises(Exception): frozen_vals.pop('name', 'Jack') with self.assertRaises(Exception): frozen_vals.popitem() with self.assertRaises(Exception): frozen_vals.clear()
def _compute_qty(self): orderpoints_contexts = defaultdict( lambda: self.env['stock.warehouse.orderpoint']) for orderpoint in self: if not orderpoint.product_id or not orderpoint.location_id: orderpoint.qty_on_hand = False orderpoint.qty_forecast = False continue orderpoint_context = orderpoint._get_product_context() product_context = frozendict({ **self.env.context, **orderpoint_context }) orderpoints_contexts[product_context] |= orderpoint for orderpoint_context, orderpoints_by_context in orderpoints_contexts.items( ): products_qty = orderpoints_by_context.product_id.with_context( orderpoint_context)._product_available() products_qty_in_progress = orderpoints_by_context._quantity_in_progress( ) for orderpoint in orderpoints_by_context: orderpoint.qty_on_hand = products_qty[ orderpoint.product_id.id]['qty_available'] orderpoint.qty_forecast = products_qty[ orderpoint.product_id. id]['virtual_available'] + products_qty_in_progress[ orderpoint.id]
def _set_images(self): for fname in self._get_images_for_test(): fname_split = fname.split('.') if not fname_split[0] in _file_cache: with Image.open(os.path.join(dir_path, fname), 'r') as img: base64_img = image_to_base64(img, 'PNG') primary, secondary = self.env[ 'base.document.layout'].create( {})._parse_logo_colors(base64_img) _img = frozendict({ 'img': base64_img, 'colors': { 'primary_color': primary, 'secondary_color': secondary, }, }) _file_cache[fname_split[0]] = _img self.company_imgs = frozendict(_file_cache)
def _set_lang(self, lang, obj=None): self.localcontext.update(lang=lang) if obj is None and 'objects' in self.localcontext: obj = self.localcontext['objects'] if obj and obj.env.context['lang'] != lang: ctx_copy = dict(self.env.context) ctx_copy.update(lang=lang) obj.env.context = frozendict(ctx_copy) obj.invalidate_cache()
def toggle_starred(self): updates = defaultdict(set) for record in self: vals = {"starred": not record.starred} updates[tools.frozendict(vals)].add(record.id) with self.env.norecompute(): for vals, ids in updates.items(): self.browse(ids).write(dict(vals)) self.recompute()
def _inverse_content(self): updates = defaultdict(set) for record in self: values = self._get_content_inital_vals() binary = base64.b64decode(record.content or "") values = record._update_content_vals(values, binary) updates[tools.frozendict(values)].add(record.id) with self.env.norecompute(): for vals, ids in updates.items(): self.browse(ids).write(dict(vals))
def test_company_sticky_with_context(self): context = frozendict({'nothing_to_see_here': True}) companies_1 = frozendict({'allowed_company_ids': [1]}) companies_2 = frozendict({'allowed_company_ids': [2]}) User = self.env['res.users'].with_context(context) self.assertEqual(User.env.context, context) User = User.with_context(**companies_1) self.assertEqual(User.env.context, dict(context, **companies_1)) # 'allowed_company_ids' is replaced if present in keys User = User.with_context(**companies_2) self.assertEqual(User.env.context, dict(context, **companies_2)) # 'allowed_company_ids' is replaced if present in new context User = User.with_context(companies_1) self.assertEqual(User.env.context, companies_1) # 'allowed_company_ids' is sticky User = User.with_context(context) self.assertEqual(User.env.context, dict(context, **companies_1))
def __new__(cls, cr, uid, context): assert context is not None args = (cr, uid, context) # if env already exists, return it env, envs = None, cls.envs for env in envs: if env.args == args: return env # otherwise create environment, and add it in the set self = object.__new__(cls) self.cr, self.uid, self.context = self.args = (cr, uid, frozendict(context)) self.registry = Registry(cr.dbname) self.cache = envs.cache self._protected = defaultdict(frozenset) # {field: ids, ...} self.dirty = defaultdict(set) # {record: set(field_name), ...} self.all = envs envs.add(self) return self