Commits

Raimon Esteve (Zikzakmedia) committed ef7365e

Active Record

  • Participants
  • Parent commits 8e4a4b4

Comments (0)

Files changed (10)

+Version 2.6.0 - 2012-10-08
+* Simplify module information with python configuration
+* Active Record
+
 Version 2.4.0 - 2012-06-26
 * Initial release
 =================
 
 The currency module of the Tryton application platform.
-See __tryton__.py
 
 Installing
 ----------
 #The COPYRIGHT file at the top level of this repository contains 
 #the full copyright notices and license terms.
 
+from trytond.pool import Pool
 from .shop import *
 from .sale import *
 from .user import *
+
+def register():
+    Pool.register(
+        SaleShop,
+        SaleShopResUserRel,
+        Sale,
+        User,
+        module='sale_shop', type_='model')

__tryton__.py

-#This file is part sale_shop module for Tryton.
-#The COPYRIGHT file at the top level of this repository contains 
-#the full copyright notices and license terms.
-{
-    'name': 'Sale Shop',
-    'name_ca_ES': 'Vendes per botiga',
-    'name_es_ES': 'Ventas por tienda',
-    'version': '2.4.0',
-    'author': 'Zikzakmedia',
-    'email': 'zikzak@zikzakmedia.com',
-    'website': 'http://www.zikzakmedia.com/',
-    'description': '''This module allows to manage shops by users: 
-- Set relationship between shops and users.
-- Search, filter and list by shop.''',
-    'description_ca_ES': '''Aquest mòdul permet gestionar botigues per usuaris:
- - Relaciona botigues amb usuaris.
- - Cerca, filtra i llista per botiga.''',
-    'description_es_ES': '''Este módulo permite gestionar tiendas por usuarios:
- - Relaciona tiendas con usuarios.
- - Busca, filtra y lista por tienda.''',
-    'depends': [
-        'ir',
-        'res',
-        'sale_price_list',
-    ],
-    'xml': [
-        'shop.xml',
-        'sale.xml',
-        'user.xml',
-    ],
-    'translation': [
-        'locale/ca_ES.po',
-        'locale/es_ES.po',
-    ]
-}
 #the full copyright notices and license terms.
 
 from trytond.model import ModelView, ModelSQL, fields
-from trytond.tools import safe_eval, datetime_strftime
 from trytond.transaction import Transaction
-from trytond.pool import Pool
-from trytond.pyson import Eval, Bool
+from trytond.pool import Pool, PoolMeta
 
-class Sale(ModelSQL, ModelView):
+__all__ = ['Sale']
+__metaclass__ = PoolMeta
+
+class Sale:
     'Sale'
-    _name = 'sale.sale'
-    _description = __doc__
+    __name__ = 'sale.sale'
     shop = fields.Many2One('sale.shop', 'Shop', required=True, readonly=True)
 
-    def __init__(self):
-        super(Sale, self).__init__()
-        self._error_messages.update({
+    @classmethod
+    def __setup__(cls):
+        super(Sale, cls).__setup__()
+        cls._error_messages.update({
+                'not_sale_shop': 'What shop would like to sell? Go to preferences',
                 'edit_sale_by_shop': 'You cannot edit this order because you do not '
                     'have permission to edit edit in this shop.',
             })
 
-    def default_shop(self):
-        user_obj = Pool().get('res.user')
-        user = user_obj.browse(Transaction().user)
+    @staticmethod
+    def default_shop():
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
+
         return user.shop and user.shop.id or False
 
-    def on_change_party(self, values):
-        user_obj = Pool().get('res.user')
-        shop_obj = Pool().get('sale.shop')
-        res = super(Sale, self).on_change_party(values)
-        user = user_obj.browse(Transaction().user)
+    def on_change_party(self):
+        User = Pool().get('res.user')
+        Shop = Pool().get('sale.shop')
+        res = super(Sale, self).on_change_party()
+        user = User.browse([Transaction().user])[0]
         if user.shop:
             if not res.get('price_list') and res.get('invoice_address'):
-                res['price_list.rec_name'] = shop_obj.browse(
-                    user.shop.id).price_list.rec_name
+                res['price_list.rec_name'] = Shop.browse([
+                    user.shop])[0].price_list.rec_name
             if not res.get('payment_term') and res.get('invoice_address'):
-                res['payment_term.rec_name'] = shop_obj.browse(
-                    user.shop.id).payment_term.rec_name
+                res['payment_term.rec_name'] = Shop.browse([
+                    user.shop])[0].payment_term.rec_name
         return res
 
-    def default_invoice_method(self):
-        user_obj = Pool().get('res.user')
-        shop_obj = Pool().get('sale.shop')
-        user = user_obj.browse(Transaction().user)
-        return user.shop and shop_obj.browse(user.shop.id).sale_invoice_method or 'manual'
+    @staticmethod
+    def default_invoice_method():
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
+        return user.shop and user.shop.sale_invoice_method or 'manual'
 
-    def default_shipment_method(self):
-        user_obj = Pool().get('res.user')
-        shop_obj = Pool().get('sale.shop')
-        user = user_obj.browse(Transaction().user)
-        return user.shop and shop_obj.browse(user.shop.id).sale_shipment_method or 'manual'
+    @staticmethod
+    def default_shipment_method():
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
+        return user.shop and user.shop.sale_shipment_method or 'manual'
 
-    def default_warehouse(self):
-        user_obj = Pool().get('res.user')
-        shop_obj = Pool().get('sale.shop')
-        location_obj = Pool().get('stock.location')
-        user = user_obj.browse(Transaction().user)
+    @staticmethod
+    def default_warehouse():
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
         if user.shop:
-            return shop_obj.browse(user.shop.id).warehouse.id
+            return user.shop.warehouse.id
         else:
-            location_ids = location_obj.search(self.warehouse.domain)
-            if len(location_ids) == 1:
-                return location_ids[0]
+            Location = Pool().get('stock.location')
+            return Location.search([('type', '=', 'warehouse')], limit=1)[0].id
 
-    def set_reference(self, ids):
+    @classmethod
+    def set_reference(cls, records):
         '''
         Rewrite fill the reference field with the sale sequence from sale.shop
         '''
-        sequence_obj = Pool().get('ir.sequence')
-        config_obj = Pool().get('sale.configuration')
-        user_obj = Pool().get('res.user')
-        shop_obj = Pool().get('sale.shop')
+        Sequence = Pool().get('ir.sequence')
+        Config = Pool().get('sale.configuration')
+        User = Pool().get('res.user')
+        Shop = Pool().get('sale.shop')
 
-        user = user_obj.browse(Transaction().user)
-        config = config_obj.browse(1)
-        sales = self.browse(ids)
+        user = User.browse([Transaction().user])[0]
+        config = Config.browse(1)[0]
+        sales = records
         for sale in sales:
             if sale.reference:
                 continue
             if user.shop:
-                sequence_id = shop_obj.browse(user.shop.id).sale_sequence.id
+                sequence_id = Shop.browse([user.shop.id])[0].sale_sequence.id
             else:
                 sequence_id = config.sale_sequence.id
-            reference = sequence_obj.get_id(sequence_id)
-            self.write(sale.id, {
+            reference = Sequence.get_id(sequence_id)
+            cls.write(sale.id, {
                 'reference': reference,
                 })
 
-    def write(self, ids, vals):
+    @classmethod
+    def create(cls, vals):
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
+        
+        if not user.shop:
+            cls.raise_user_error('not_sale_shop')
+
+        vals = vals.copy()
+        vals['shop'] = user.shop.id
+        return super(Sale, cls).create(vals)
+
+    @classmethod
+    def write(cls, sales, vals):
         '''
         Only edit Sale users available edit in this shop
         '''
-        sale_obj = Pool().get('sale.sale')
-        user_obj = Pool().get('res.user')
-        user = user_obj.browse(Transaction().user)
-
+        User = Pool().get('res.user')
+        user = User.browse([Transaction().user])[0]
+        print user.shops
         shops = [s.id for s in user.shops]
-        for sale in sale_obj.browse(ids):
+        for sale in sales:
             if not sale.shop.id in shops:
-                self.raise_user_error('edit_sale_by_shop')
-        res = super(Sale, self).write(ids, vals)
+                cls.raise_user_error('edit_sale_by_shop')
+        res = super(Sale, cls).write(sales, vals)
         return res
-
-Sale()
 
 from setuptools import setup
 import re
+import os
+import ConfigParser
 
-info = eval(open('__tryton__.py').read())
+config = ConfigParser.ConfigParser()
+config.readfp(open('tryton.cfg'))
+info = dict(config.items('tryton'))
+for key in ('depends', 'extras_depend', 'xml'):
+    if key in info:
+        info[key] = info[key].strip().splitlines()
 major_version, minor_version, _ = info.get('version', '0.0.1').split('.', 2)
 major_version = int(major_version)
 minor_version = int(minor_version)
 
 setup(name='trytond_sale_shop',
     version=info.get('version', '0.0.1'),
-    description=info.get('description', ''),
-    author=info.get('author', 'Zikzakmedia'),
-    author_email=info.get('email', 'zikzak@zikzakmedia.com'),
-    url=info.get('website', 'http://www.zikzakmedia.com'),
+    description='This module allows to manage shops by users',
+    author='Zikzakmedia SL',
+    author_email='zikzak@zikzakmedia.com',
+    url='http://www.zikzakmedia.com',
     download_url="https://bitbucket.org/zikzakmedia/trytond-sale_shop",
     package_dir={'trytond.modules.sale_shop': '.'},
     packages=[
 #the full copyright notices and license terms.
 
 from trytond.model import ModelView, ModelSQL, fields
-from trytond.tools import safe_eval, datetime_strftime
-from trytond.transaction import Transaction
 from trytond.pool import Pool
 from trytond.pyson import Eval, Bool
 
+__all__ = ['SaleShop', 'SaleShopResUserRel']
+
 class SaleShop(ModelSQL, ModelView):
     'Sale Shop'
-    _name = 'sale.shop'
-    _description = __doc__
+    __name__ = 'sale.shop'
     name = fields.Char('Shop Name', required=True, select=True)
     users = fields.Many2Many('sale.shop-res.user', 'shop', 'user', 'Users')
     warehouse = fields.Many2One('stock.location', "Warehouse", required=True,
                 'required': Bool(Eval('context', {}).get('company', 0)),
                 }))
 
-SaleShop()
 
 class SaleShopResUserRel(ModelSQL):
     'Sale Shop - Res User'
-    _name = 'sale.shop-res.user'
+    __name__ = 'sale.shop-res.user'
     _table = 'sale_shop_res_user_rel'
-    _description = __doc__
     shop = fields.Many2One('sale.shop', 'Shop',
             ondelete='RESTRICT', select=1, required=True)
     user = fields.Many2One('res.user', 'User',
             ondelete='RESTRICT', required=True)
-
-SaleShopResUserRel()
+[tryton]
+version=2.6.0
+depends:
+    ir
+    res
+    sale_price_list
+xml:
+    shop.xml
+    sale.xml
+    user.xml
 #the full copyright notices and license terms.
 
 from trytond.model import ModelView, ModelSQL, fields
-from trytond.tools import safe_eval, datetime_strftime
-from trytond.transaction import Transaction
-from trytond.pyson import Not, Bool, Eval, Equal, PYSONEncoder, Date
-from trytond.pool import Pool
+from trytond.pyson import Eval
+from trytond.pool import Pool, PoolMeta
 
-class User(ModelSQL, ModelView):
+__all__ = ['User']
+__metaclass__ = PoolMeta
+
+class User:
     "User"
-    _name = "res.user"
-    _description = __doc__
+    __name__ = "res.user"
 
     shops = fields.Many2Many('sale.shop-res.user', 'user', 'shop', 'Shops')
     shop = fields.Many2One('sale.shop', 'Shop',
             domain=[('id', 'in', Eval('shops', []))],
             depends=['shops'],
     )
-    def __init__(self):
-        super(User, self).__init__()
-        self._preferences_fields.extend([
+
+    @classmethod
+    def __setup__(cls):
+        super(User, cls).__setup__()
+        cls._preferences_fields.extend([
             'shop',
             'shops',
         ])
 
-    def write(self, ids, vals):
-        return super(User, self).write(ids, vals)
+    @classmethod
+    def write(cls, users, vals):
+        return super(User, cls).write(users, vals)
 
-User()
                         position="after">
                         <label name="shop"/>
                         <field name="shop" widget="selection"/>
-                        <field name="shops" invisible="1"/>
                     </xpath>
                 </data>
                 ]]>