~sebastien.beau/e-commerce-addons/oerp6.1-stable-improve-shipping-export

« back to all changes in this revision

Viewing changes to product_links_sync/product.py

  • Committer: sebastien beau
  • Date: 2012-05-12 23:06:47 UTC
  • Revision ID: sebastien.beau@akretion.com.br-20120512230647-ugjygn7y942avr8l
[ADD] add product_link_sync abstract module for syncronizing product_link with external system

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###############################################################################
 
3
#                                                                             #
 
4
#   product_links_sync for OpenERP                                  #
 
5
#   Copyright (C) 2012 Akretion Sébastien BEAU <sebastien.beau@akretion.com>   #
 
6
#                                                                             #
 
7
#   This program is free software: you can redistribute it and/or modify      #
 
8
#   it under the terms of the GNU Affero General Public License as            #
 
9
#   published by the Free Software Foundation, either version 3 of the        #
 
10
#   License, or (at your option) any later version.                           #
 
11
#                                                                             #
 
12
#   This program is distributed in the hope that it will be useful,           #
 
13
#   but WITHOUT ANY WARRANTY; without even the implied warranty of            #
 
14
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
 
15
#   GNU Affero General Public License for more details.                       #
 
16
#                                                                             #
 
17
#   You should have received a copy of the GNU Affero General Public License  #
 
18
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.     #
 
19
#                                                                             #
 
20
###############################################################################
 
21
 
 
22
from osv import osv, fields
 
23
import netsvc
 
24
from tools import DEFAULT_SERVER_DATETIME_FORMAT
 
25
from datetime import datetime
 
26
from base_external_referentials.decorator import only_for_referential
 
27
 
 
28
 
 
29
class product_product(osv.osv):
 
30
    _inherit = "product.product"
 
31
 
 
32
    _columns = {
 
33
        'product_link_write_last_date': fields.datetime('Product Link Last Write Date'),
 
34
    }
 
35
 
 
36
    def _update_product_link_last_date(self, cr, uid, vals, context=None):
 
37
        if 'product_link_ids' in vals:
 
38
            vals['product_link_write_last_date'] = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
 
39
        return vals
 
40
 
 
41
    def create(self, cr, uid, vals, context=None):
 
42
        vals = self._update_product_link_last_date(cr, uid, vals, context=context)
 
43
        return super(product_product, self).create(cr, uid, vals, context=context)
 
44
 
 
45
    def write(self, cr, uid, ids, vals, context=None):
 
46
        vals = self._update_product_link_last_date(cr, uid, vals, context=context)
 
47
        return super(product_product, self).write(cr, uid, ids, vals, context=context)
 
48
 
 
49
    def _get_query_and_params_for_ids_and_date(self, cr, uid, external_session, ids=None, last_exported_date=None, context=None):
 
50
        if context.get('export_product') != 'link':
 
51
            return super(product_product, self)._get_query_and_params_for_ids_and_date(cr, uid, 
 
52
                        external_session, ids=ids, last_exported_date=last_exported_date, context=context)
 
53
        else:
 
54
            # We have to export all product link that believe to a product which have the link modify
 
55
            # or have been created in the external system since the last export
 
56
            params = ()
 
57
            query = """
 
58
                SELECT GREATEST(product_link_write_last_date, ir_model_data.create_date) as update_date,
 
59
                    product_product.id as id, ir_model_data.res_id, product_link_write_last_date
 
60
                    FROM product_product
 
61
                LEFT JOIN ir_model_data
 
62
                    ON product_product.id = ir_model_data.res_id
 
63
                    AND ir_model_data.model = 'product.product'
 
64
                    AND ir_model_data.module = 'extref/%(ref_name)s'
 
65
                WHERE product_link_write_last_date is not NULL
 
66
                """%{
 
67
                        'ref_name': external_session.referential_id.name,
 
68
                }
 
69
            if ids:
 
70
                query += " AND product_product.id in %s"
 
71
                params += (tuple(ids),)
 
72
            if last_exported_date:
 
73
                query += " AND (GREATEST(product_link_write_last_date,ir_model_data.create_date) > %s)"
 
74
                params += (last_exported_date,)
 
75
 
 
76
            return query, params
 
77
 
 
78
    def get_field_to_export(self, cr, uid, ids, mapping, mapping_id, context=None):
 
79
        fields_to_read = super(product_product, self).get_field_to_export(cr, uid, ids, mapping, mapping_id, context=context)
 
80
        if context.get('export_product') == 'link':
 
81
            fields_to_read = ['product_link_ids']
 
82
        else:
 
83
            if 'product_link_ids' in fields_to_read: fields_to_read.remove('product_link_ids')
 
84
        return fields_to_read
 
85
 
 
86
#    def export_links_for_product(self, cr, uid, ids, context=None):
 
87
#        """ Not implemented in this abstract module"""
 
88
#        return False
 
89
 
 
90
    @only_for_referential(ref_categ ='Multichannel Sale')
 
91
    def _get_last_exported_date(self, cr, uid, external_session, context):
 
92
        shop = external_session.sync_from_object
 
93
        if context.get('export_product') == 'link':
 
94
            return shop.last_products_links_export_date
 
95
        else:
 
96
            return super(product_product, self)._get_last_exported_date(cr, uid, external_session, context)
 
97
 
 
98
    @only_for_referential(ref_categ ='Multichannel Sale')
 
99
    def _set_last_exported_date(self, cr, uid, external_session, date, context):
 
100
        shop = external_session.sync_from_object
 
101
        if context.get('export_product') == 'link':
 
102
            return self.pool.get('sale.shop').write(cr, uid, shop.id, {'last_products_links_export_date': date}, context=context)
 
103
        else:
 
104
            return super(product_product, self)._set_last_exported_date(cr, uid, external_session, date, context)
 
105
 
 
106
 
 
107
 
 
108
 
 
109
 
 
110
 
 
111
 
 
112
 
 
113
 
 
114