<commit_before># -*- coding: utf-8 -*-
#########################################################################
#This module intergrates Open ERP with the magento core                 #
#Core settings are stored here                                          #
#########################################################################
#                                                                       #
# Copyright (C) 2011-2013 Akretion   <NAME>                     #
# Copyright (C) 2011-2013 <NAME>                   #
#                                                                       #
#This program is free software: you can redistribute it and/or modify   #
#it under the terms of the GNU 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 General Public License for more details.                           #
#                                                                       #
#You should have received a copy of the GNU General Public License      #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#########################################################################

import logging
from datetime import datetime

from openerp.osv import fields, orm


from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import openerp.addons.connector as connector
from openerp.addons.connector.session import ConnectorSession
from .unit.import_synchronizer import (import_batch,
                                       partner_import_batch,
                                       sale_order_import_batch,
                                       )

_logger = logging.getLogger(__name__)


class magento_backend(orm.Model):
    _name = 'magento.backend'
    _doc = 'Magento Backend'
    _inherit = 'connector.backend'

    _backend_type = 'magento'

    def _select_versions(self, cr, uid, context=None):
        """ Available versions

        Can be inherited to add custom versions.
        """
        return [('1.5', '1.5'),
                ('1.7', '1.7')]

    _columns = {
        'version': fields.selection(
            _select_versions,
            string='Version',
            required=True),
        'location': fields.char('Location'),
        'username': fields.char('Username'),
        'password': fields.char('Password'),
        'website_ids': fields.one2many(
            'magento.website', 'backend_id',
            string='Website', readonly=True),
        'default_lang_id': fields.many2one(
                'res.lang',
                'Default Language',
                help="Choose the language which will be used for the "
                     "Default Value in Magento"),
        'default_category_id': fields.many2one(
            'product.category',
            string='Default Product Category',
            help='If a default category is selected, products imported '
                 'without a category will be linked to it.'),

        # add a field `auto_activate` -> activate a cron
        'import_products_from_date': fields.datetime('Import products from date'),
        'import_categories_from_date': fields.datetime('Import categories from date'),
        'catalog_price_tax_included': fields.boolean('Prices include tax')
    }

    def synchronize_metadata(self, cr, uid, ids, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        session = ConnectorSession(cr, uid, context=context)
        for backend_id in ids:
            for model in ('magento.website',
                          'magento.store',
                          'magento.storeview'):
                # import directly, do not delay because this
                # is a fast operation, a direct return is fine
                # and it is simpler to import them sequentially
                import_batch(session, model, backend_id)

        return True

    def import_partners(self, cr, uid, ids, context=None):
        """ Import partners from all websites """
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        for backend in self.browse(cr, uid, ids, context=context):
            for website in backend.website_ids:
                website.import_partners()
        return True

    def import_customer_groups(self, cr, uid, ids, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        session = ConnectorSession(cr, uid, context=context)
        for backend_id in ids:
            import_batch.delay(session, 'magento.res.partner.category',
                               backend_id)

        return True

    def _import_from_date(self, cr, uid, ids, model, from_date_field, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        session = ConnectorSession(cr, uid, context=context)
        import_start_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        for backend in self.browse(cr, uid, ids, context=context):
            from_date = getattr(backend, from_date_field)
            if from_date:
                from_date = datetime.strptime(from_date,
                                              DEFAULT_SERVER_DATETIME_FORMAT)
            else:
                from_date = None
            import_batch.delay(session, model,
                               backend.id, filters={'from_date': from_date})<commit_msg>[IMP] add method to check if magento structure has been synchronized, if not, synchronize it<commit_after># -*- coding: utf-8 -*-
#########################################################################
#This module intergrates Open ERP with the magento core                 #
#Core settings are stored here                                          #
#########################################################################
#                                                                       #
# Copyright (C) 2011-2013 Akretion   <NAME>                     #
# Copyright (C) 2011-2013 Camptocamp <NAME>                   #
#                                                                       #
#This program is free software: you can redistribute it and/or modify   #
#it under the terms of the GNU 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 General Public License for more details.                           #
#                                                                       #
#You should have received a copy of the GNU General Public License      #
#along with this program.  If not, see <http://www.gnu.org/licenses/>.  #
#########################################################################

import logging
from datetime import datetime

from openerp.osv import fields, orm


from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
import openerp.addons.connector as connector
from openerp.addons.connector.session import ConnectorSession
from .unit.import_synchronizer import (import_batch,
                                       partner_import_batch,
                                       sale_order_import_batch,
                                       )

_logger = logging.getLogger(__name__)


class magento_backend(orm.Model):
    _name = 'magento.backend'
    _doc = 'Magento Backend'
    _inherit = 'connector.backend'

    _backend_type = 'magento'

    def _select_versions(self, cr, uid, context=None):
        """ Available versions

        Can be inherited to add custom versions.
        """
        return [('1.5', '1.5'),
                ('1.7', '1.7')]

    _columns = {
        'version': fields.selection(
            _select_versions,
            string='Version',
            required=True),
        'location': fields.char('Location'),
        'username': fields.char('Username'),
        'password': fields.char('Password'),
        'website_ids': fields.one2many(
            'magento.website', 'backend_id',
            string='Website', readonly=True),
        'default_lang_id': fields.many2one(
                'res.lang',
                'Default Language',
                help="Choose the language which will be used for the "
                     "Default Value in Magento"),
        'default_category_id': fields.many2one(
            'product.category',
            string='Default Product Category',
            help='If a default category is selected, products imported '
                 'without a category will be linked to it.'),

        # add a field `auto_activate` -> activate a cron
        'import_products_from_date': fields.datetime('Import products from date'),
        'import_categories_from_date': fields.datetime('Import categories from date'),
        'catalog_price_tax_included': fields.boolean('Prices include tax')
    }

    def check_magento_structure(self, cr, uid, ids, context=None):
        """ Used in each data import.
        Verify if a website exists for each backend before starting the import.
        """
        for backend_id in ids:
            website_ids = self.pool['magento.website'].search(
                cr, uid, [('backend_id', '=', backend_id)], context=context)
            if not website_ids:
                self.synchronize_metadata(cr, uid, backend_id, context=context)
        return True

    def synchronize_metadata(self, cr, uid, ids, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        session = ConnectorSession(cr, uid, context=context)
        for backend_id in ids:
            for model in ('magento.website',
                          'magento.store',
                          'magento.storeview'):
                # import directly, do not delay because this
                # is a fast operation, a direct return is fine
                # and it is simpler to import them sequentially
                import_batch(session, model, backend_id)

        return True

    def import_partners(self, cr, uid, ids, context=None):
        """ Import partners from all websites """
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        self.check_magento_structure(cr, uid, ids, context=context)
        for backend in self.browse(cr, uid, ids, context=context):
            for website in backend.website_ids:
                website.import_partners()
        return True

    def import_customer_groups(self, cr, uid, ids, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        self.check_magento_structure(cr, uid, ids, context=context)
        session = ConnectorSession(cr, uid, context=context)
        for backend_id in ids:
            import_batch.delay(session, 'magento.res.partner.category',
                               backend_id)

        return True

    def _import_from_date(self, cr, uid, ids, model, from_date_field, context=None):
        if not hasattr(ids, '__iter__'):
            ids = [ids]
        self.check_magento_structure(cr, uid, ids, context=context)
        session = ConnectorSession(cr, uid, context=context)
        import_start_time = datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)
        for backend in self.browse(cr, uid, ids, context=context):
            from_date = getattr(backend, from_date_field)
            if from_date:
                from_date = datetime.strptime(from_date,
                                              DEFAULT_SERVER_DATETIME_FORMAT)
            else:
                from_date = None
            import_batch.delay(session, model,
                               backend.id, filters={'from_date': from_date})
{'fraction_non_alphanumeric': 0.11786207456745999, 'fraction_numerical': 0.004711233855901226, 'mean_word_length': 1.8519805420430855, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1346133', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2917, 'n_words': 907}