예제 #1
0
 def images_archive_finder(self):
     pattern = "(%s_\d+)(\.\w+){0,3}\.zip" % slugger(self.brand.lower().strip())
     pattern = re.compile(pattern)
     for file in os.listdir(TEMP_DIR):
         if re.match(pattern, file):
             if zipfile.is_zipfile(os.path.join(TEMP_DIR, file)):
                 self.images_archive = file
                 break
예제 #2
0
    def mapper(self, total_dict):
        if self.flag:
            brand = Brand.objects.create(
                                             name = self.brand,
                                             description = self.brand,
                                             slug = slugger(self.brand)
                                            )
        else:
            try:
                brand = Brand.objects.get(name = self.brand)
            except Exception as e:
                logging.info("[ERROR in MAPPER]: %s" % e )
                sys.exit(1)
        for entity in total_dict.values():
            if entity:
                entity_aromart_category_ids = self._category_checker(entity, total_dict)

                if not len(entity_aromart_category_ids):
                    continue

                slug = slugify(self.brand) + "-" + slugger(entity["v_products_name_1"].strip().decode("utf-8"))#.decode("utf-8"))
                slug = slug[0:250]
                name =  entity["v_products_name_1"].decode("utf-8")[0:250]
                short_name = entity["v_products_name_1"].decode("utf-8")[0:250]

                product, created  = Product.objects.get_or_create(
                    brand = brand,
                    name =  name,     # max_length=255)
                    sku = entity["v_products_model"],        # max_length=50
                    slug = slug,
                    short_name = short_name
                )
                if created:
    #                short_name = models.CharField(max_length=255)
                    product.description = entity["v_products_description_1"]
                    product.title = entity["v_products_meta_title_1"]
                    product.meta_keywords = entity["v_products_meta_keywords_1"].decode("utf-8")[0:250]
                    product.meta_description = entity["v_products_meta_description_1"].decode("utf-8")[0:250]

                    parsed_price = decimal.Decimal(price_splitter(str(entity["v_products_price"])))

                    product.base_price = decimal.Decimal(round(parsed_price * decimal.Decimal(0.6), 2))
                    product.price = parsed_price

                    categories = Category.objects.filter(pk__in = entity_aromart_category_ids)
                    for category in categories:
                        product.category.add(category)
                    #TODO: Прописати алгоритм для створення короткої назви товару
                    #TODO: Прописати алглритм збереження картинки "v_products_image"
                    if self.images_archive and "v_products_image" in entity.keys() and entity["v_products_image"]:
                        image_file_name = entity["v_products_image"]
                        zip_file = zipfile.ZipFile(os.path.join(TEMP_DIR, self.images_archive))
                        for filename in zip_file.namelist():
                            filename_splitted = filename.split("/")[-1]
                            if  filename_splitted == image_file_name:
#                                print filename_splitted
                                path = zip_file.extract(filename)
                                title = self.brand + " " + filename
                                with open(path, "rb") as i_f:
                                    file = ImageFile(i_f)
                                    pi = ProductImage.objects.create(
                                                                        title = product.name,
                                                                        product = product,
                                                                        image = file,
                                                                        primary = True
                                    )
                                    pi.save()
                else:
                    if product.description != entity["v_products_description_1"].decode("utf-8"):
                        product.description = entity["v_products_description_1"]
                    if product.title != entity["v_products_meta_title_1"].decode("utf-8"):
                        product.title = entity["v_products_meta_title_1"]
                    if product.meta_keywords != entity["v_products_meta_keywords_1"].decode("utf-8")[0:250]:
                        product.meta_keywords = entity["v_products_meta_keywords_1"]
                    if product.meta_description != entity["v_products_meta_description_1"].decode("utf-8")[0:250]:
                        product.meta_description = entity["v_products_meta_description_1"]
                    if product.price !=  decimal.Decimal(price_splitter(str(entity["v_products_price"]))):
                        product.price = decimal.Decimal(price_splitter(str(entity["v_products_price"])))
                    # TODO: Вибрати усі категорії перевірити з розпарсеними і скоригувати при потребі
                product.save()