res_store.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in module root
  4. # directory
  5. ##############################################################################
  6. from openerp import models, fields, SUPERUSER_ID
  7. from openerp.osv import osv
  8. class res_store(models.Model):
  9. _name = "res.store"
  10. _description = 'Stores'
  11. _order = 'parent_id desc, name'
  12. name = fields.Char('Name', size=128, required=True,)
  13. parent_id = fields.Many2one('res.store', 'Parent Store', select=True)
  14. child_ids = fields.One2many('res.store', 'parent_id', 'Child Stores')
  15. journal_ids = fields.Many2many(
  16. 'account.journal', 'res_store_journal_rel', 'store_id',
  17. 'journal_id', 'Journals')
  18. company_id = fields.Many2one(
  19. 'res.company', 'Company', required=True,
  20. default=lambda self: self.env[
  21. 'res.company']._company_default_get('res.store'))
  22. user_ids = fields.Many2many(
  23. 'res.users', 'res_store_users_rel', 'cid', 'user_id', 'Accepted Users')
  24. _sql_constraints = [
  25. ('name_uniq', 'unique (name)', 'The company name must be unique !')
  26. ]
  27. _constraints = [
  28. (osv.osv._check_recursion,
  29. 'Error! You can not create recursive stores.', ['parent_id'])
  30. ]
  31. def name_search(self, cr, uid, name='', args=None, operator='ilike', context=None, limit=100):
  32. context = dict(context or {})
  33. if context.pop('user_preference', None):
  34. # We browse as superuser. Otherwise, the user would be able to
  35. # select only the currently visible stores (according to rules,
  36. # which are probably to allow to see the child stores) even if
  37. # she belongs to some other stores.
  38. user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid, context=context)
  39. store_ids = list(set([user.store_id.id] + [cmp.id for cmp in user.store_ids]))
  40. uid = SUPERUSER_ID
  41. args = (args or []) + [('id', 'in', store_ids)]
  42. return super(res_store, self).name_search(cr, uid, name=name, args=args, operator=operator, context=context, limit=limit)