# -*- coding: utf-8 -*-
##############################################################################
#
#    Authors: Guewen Baconnier, Sébastien Beau
#    Copyright (C) 2010 BEAU Sébastien
#    Copyright 2011-2013 Camptocamp SA
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, orm


# TODO magento.delivery.carrier & move specific stuff
class delivery_carrier(orm.Model):
    """ Adds Magento specific fields to ``delivery.carrier``

    ``magento_code``

        Code of the carrier delivery method in Magento.
        Example: ``colissimo_express``

    ``magento_tracking_title``

        Display name of the carrier for the tracking in Magento.
        Example: Colissimo Express

    ``magento_carrier_code``

        General code of the carrier, the first part of the ``magento_code``.
        Example: ``colissimo`` for the method ``colissimo_express``.

    ``magento_export_tracking``

        Defines if the tracking numbers should be exported to Magento.
    """
    _inherit = "delivery.carrier"

    def _carrier_code(self, cr, uid, ids, name, args, context=None):
        res = dict.fromkeys(ids, False)
        for carrier in self.browse(cr, uid, ids, context=context):
            if carrier.magento_code:
                res[carrier.id] = carrier.magento_code.split('_')[0]
        return res

    _columns = {
        'magento_code': fields.char(
            'Magento Carrier Code',
            size=64,
            required=False),
        'magento_tracking_title': fields.char(
            'Magento Tracking Title',
            size=64,
            required=False),
        # in Magento, the delivery method is something like that:
        # tntmodule2_tnt_basic
        # where the first part before the _ is always the carrier code
        # in this example, the carrier code is tntmodule2
        'magento_carrier_code': fields.function(
            _carrier_code,
            string='Magento Base Carrier Code',
            size=32,
            type='char'),
        'magento_export_tracking': fields.boolean('Export tracking numbers')
    }

    _defaults = {
        'magento_export_tracking': True,
    }

@@ -1008,19 +1008,23 @@
 nerp
-.osv
  import
+ models,
  fie
@@ -1028,19 +1028,19 @@
 fields, 
-orm
+api
 %0A%0A%0A# TOD
@@ -1115,11 +1115,14 @@
 ier(
-orm
+models
 .Mod
@@ -1771,354 +1771,30 @@
     
-def _carrier_code(self, cr, uid, ids, name, args, context=None):%0A        res = dict.fromkeys(ids, False)%0A        for carrier in self.browse(cr, uid, ids, context=context):%0A            if carrier.magento_code:%0A                res%5Bcarrier.id%5D = carrier.magento_code.split('_')%5B0%5D%0A        return res%0A%0A    _columns = %7B%0A        'magento_code':
+magento_code =
  fields.
 char
@@ -1781,33 +1781,33 @@
 o_code = fields.
-c
+C
 har(%0A           
@@ -1795,36 +1795,39 @@
 s.Char(%0A        
- 
-   
+string=
 'Magento Carrier
@@ -1838,33 +1838,8 @@
 e',%0A
-            size=64,%0A    
     
@@ -1856,28 +1856,28 @@
 ed=False
-)
 ,%0A    
+)%0A
     
-'
 magento_
@@ -1890,26 +1890,26 @@
 ng_title
-':
+ =
  fields.
 char(%0A  
@@ -1900,17 +1900,17 @@
  fields.
-c
+C
 har(%0A   
@@ -1914,20 +1914,23 @@
         
-    
+string=
 'Magento
@@ -1951,33 +1951,8 @@
 e',%0A
-            size=64,%0A    
     
@@ -1969,23 +1969,24 @@
 ed=False
-)
 ,%0A    
+)%0A
     # in
@@ -2039,20 +2039,16 @@
 e that:%0A
-    
     # tn
@@ -2066,20 +2066,16 @@
 t_basic%0A
-    
     # wh
@@ -2104,16 +2104,22 @@
 ore the 
+first 
 _ is alw
@@ -2139,20 +2139,16 @@
 er code%0A
-    
     # in
@@ -2193,29 +2193,24 @@
 module2%0A    
-   
- '
 magento_carr
@@ -2217,26 +2217,26 @@
 ier_code
-':
+ =
  fields.
 function
@@ -2231,16 +2231,12 @@
 lds.
-function
+Char
 (%0A  
@@ -2241,20 +2241,33 @@
         
-    
+compute='_compute
 _carrier
@@ -2271,22 +2271,19 @@
 ier_code
+'
 ,%0A
-    
         
@@ -2326,60 +2326,14 @@
     
+)%0A
     
-    size=32,%0A            type='char'),%0A        '
 mage
@@ -2355,18 +2355,18 @@
 king
-':
+ =
  fields.
 bool
@@ -2365,16 +2365,23 @@
 lds.
-b
+B
 oolean(
+string=
 'Exp
@@ -2405,78 +2405,281 @@
 ers'
-)
+,
 %0A    
-%7D%0A%0A
+ 
     
-_defaults = %7B%0A        'magento_export_tracking': True,%0A    %7D
+                                    default=True)%0A%0A    @api.depends('magento_code')%0A    def _compute_carrier_code(self):%0A        for carrier in self:%0A            if carrier.magento_code:%0A                self.magento_carrier_code = carrier.magento_code.split('_')%5B0%5D
 %0A

Use new API in delivery.py