def refresh_relations_task():
    relation_created_num = 0
    address_updated_num = 0
    magento = magento_connect()
    customer_list = magento.customer.list()

    if customer_list:
        for customer in customer_list:
            if customer.get("customer_id"):
                # customer_info = magento.customer.info(int(customer['customer_id']))

                # Create or update the addresses first.
                if int(customer.get("default_shipping", 0)):
                    shipping_address = magento.customer_address.info(int(customer["default_shipping"]))
                    shipping_address_obj = get_address_obj(shipping_address)
                else:
                    shipping_address_obj = None

                if int(customer.get("default_billing", 0)):
                    billing_address = magento.customer_address.info(int(customer["default_billing"]))
                    billing_address_obj = get_address_obj(billing_address)
                else:
                    billing_address_obj = None

                updated_values = {
                    "name": customer.get("firstname") + " " + customer.get("lastname"),
                    "phone_number": customer.get("telephone"),
                    "email": customer.get("email"),
                    "magento_customer_id": customer.get("customer_id"),
                    "magento_default_billing": customer.get("default_billing"),
                    "magento_default_shipping": customer.get("default_shipping"),
                    "magento_group_id": customer.get("group_id"),
                    "magento_store_id": customer.get("store_id"),
                    "magento_website_id": customer.get("website_id"),
                }

                relation_obj, relation_created = Relation.objects.update_or_create(
                    magento_customer_id=customer.get("customer_id"), defaults=updated_values
                )

                if relation_created:
                    relation_created_num += 1

                #  Add foreign keys to shipping and billing addresses
                if shipping_address_obj:
                    address_updated_num += 1
                    relation_obj.addresses.add(shipping_address_obj)

                if billing_address_obj:
                    address_updated_num += 1
                    relation_obj.addresses.add(billing_address_obj)

    response_object = {"relation_created_num": relation_created_num, "address_updated_num": address_updated_num}

    return response_object
def refresh_category_tree_task():
    magento = magento_connect()
    category_tree = magento.catalog_category.tree()

    if category_tree:
        category_tree_object = CategoryTree.load()
        CategoryTree.objects.update_or_create(id=category_tree_object.id, defaults={'category_tree': category_tree})
        response_object = {'Category tree refreshed successfully.'}
    else:
        response_object = {'No category tree was retrieved from Magento.'}

    return response_object
def refresh_stores_task():
    magento = magento_connect()
    store_list = magento.core_store.list()
    stores_created_num = 0
    stores_total = len(store_list)

    if store_list:
        for store in store_list:
            obj, created = Store.objects.update_or_create(store_id=store['store_id'],
                                                          defaults=store)
            if created:
                stores_created_num += 1

    response_object = { 'stores_total': stores_total,
                        'stores_created_num': stores_created_num }
    return response_object
def refresh_attributes_task():
    magento = magento_connect()
    attribute_set_list = magento.catalog_product_attribute_set.list()
    sets_created_num = 0
    attribute_created_num = 0
    sets_total = len(attribute_set_list)

    if attribute_set_list:
        for attribute_set in attribute_set_list:
            attribute_set_obj, created = AttributeSet.objects.update_or_create(
                                            set_id=attribute_set.get('set_id'),
                                            defaults=attribute_set)

            if created:
                sets_created_num += 1

            attribute_list = magento.catalog_product_attribute.list(attribute_set.get('set_id'))

            for attribute in attribute_list:
                attribute_info = magento.catalog_product_attribute.info(attribute.get('attribute_id'))

                if check_attribute(attribute_info):
                    updated_values = {  'code': attribute.get('code'),
                                        'attribute_id': attribute.get('attribute_id'),
                                        'required': attribute.get('required'),
                                        'scope': attribute.get('scope'),
                                        'attribute_type': attribute.get('type'),
                                        'attribute_set': attribute_set_obj,
                                        'magento_attribute_set_id': attribute_set.get('set_id')
                                     }

                    attribute_obj, attribute_created = ProductAttribute.objects.update_or_create(
                                                        attribute_id=attribute.get('attribute_id'),
                                                        defaults=updated_values)
                    if attribute_created:
                        attribute_created_num += 1

    response_object = { 'sets_total': sets_total,
                        'sets_created_num': sets_created_num,
                        'attribute_created_num': attribute_created_num }
    return response_object
def refresh_products_task():
    products_created_num = 0
    magento = magento_connect()
    product_list = magento.catalog_product.list()

    if product_list:
        for product in product_list:

            updated_values = {  'name': product.get('name', 'No name specified'),
                                'magento_product_id': product.get('product_id'),
                                'sku': product.get('sku'),
                                'product_type': product.get('type'),
                             }


            obj, created = Product.objects.update_or_create(magento_product_id=product.get('product_id'),
                                                            defaults=updated_values)
            if created:
                products_created_num += 1

    response_object = { 'products_created_num': products_created_num}
    return response_object