def post(self, request, *args, **kwargs): mapping = dict() for field in request.POST.keys(): if field.startswith("remap["): # remove the remap[] part field_name = field.replace("remap[", "")[:-1] mapping[field_name] = self.request.POST.getlist(field) supplier = get_supplier(request) shop = get_shop(request) run_task( "shuup.importer.tasks.import_file", stored=True, queue="data_import", importer=request.GET["importer"], import_mode=request.POST.get("import_mode") or ImportMode.CREATE_UPDATE.value, file_name=request.GET["n"], language=request.GET.get("lang"), shop_id=shop.pk, supplier_id=supplier.pk if supplier else None, user_id=request.user.pk, mapping=mapping, ) messages.success(request, _("The import was queued!")) return redirect(reverse("shuup_admin:importer.import"))
def update_stock(self, product_id, *args, **kwargs): """ Supplier module update stock should always bump product cache and send `shuup.core.signals.stocks_updated` signal. """ supplier_id = self.supplier.pk sv, _ = StockCount.objects.select_related("product").get_or_create( supplier_id=supplier_id, product_id=product_id) # kind not supported if sv.product.kind not in self.get_supported_product_kinds_values(): return # item doesn't manage stocks if not sv.stock_managed: # make sure to index products either way run_task("shuup.simple_supplier.tasks.index_product", product=product_id, supplier=self.supplier.pk) return values = get_current_stock_value(supplier_id=supplier_id, product_id=product_id) sv.logical_count = values["logical_count"] sv.physical_count = values["physical_count"] latest_event = StockAdjustment.objects.filter( supplier=supplier_id, product=product_id, type=StockAdjustmentType.INVENTORY).last() if latest_event: sv.stock_value_value = latest_event.purchase_price_value * sv.logical_count # TODO: get rid of this and move to shuup.notify app instead, through signals if self.supplier.stock_managed and has_installed("shuup.notify"): if sv.alert_limit and sv.physical_count < sv.alert_limit: product = Product.objects.filter(id=product_id).first() if product: from .notify_events import AlertLimitReached for shop in self.supplier.shops.all(): supplier_email = self.supplier.contact_address.email if self.supplier.contact_address else "" shop_email = shop.contact_address.email if shop.contact_address else "" AlertLimitReached( supplier=self.supplier, product=product, shop_email=shop_email, supplier_email=supplier_email, ).run(shop=shop) sv.save(update_fields=("logical_count", "physical_count", "stock_value_value")) context_cache.bump_cache_for_product(product_id) stocks_updated.send(type(self), shops=self.supplier.shops.all(), product_ids=[product_id], supplier=self.supplier) run_task("shuup.simple_supplier.tasks.index_product", product=product_id, supplier=self.supplier.pk)
def on_object_saved(sender, object, **kwargs): # update stocks after a shop product/product is saved if isinstance(object, ShopProduct): transaction.on_commit(lambda: run_task( "shuup.simple_supplier.tasks.update_shop_product_stocks", shop_product=object.pk)) if isinstance(object, Product): transaction.on_commit(lambda: run_task( "shuup.simple_supplier.tasks.update_product_stocks", product=object.pk))
def on_object_saved(sender, object, **kwargs): # make sure to index the prices of the product when a product is saved if isinstance(object, ShopProduct): transaction.on_commit( lambda: run_task("shuup.core.catalog.tasks.index_shop_product", shop_product_id=object.pk)) if isinstance(object, Product): transaction.on_commit(lambda: run_task( "shuup.core.catalog.tasks.index_product", product_id=object.pk))
def on_user_requested_reset_password(sender, shop, user, reset_domain_url, reset_url_name, **kwargs): if Script.objects.filter( enabled=True, event_identifier=PasswordReset.identifier).exists(): run_task("shuup.notify.tasks.send_user_reset_password_email", user_id=user.pk, shop_id=shop.pk, reset_domain_url=reset_domain_url, reset_url_name=reset_url_name) # handled! return True return False
def post(self, request, *args, **kwargs): contact = self.get_object() contact.add_log_entry("Info! User anonymization requested.", kind=LogEntryKind.NOTE, user=self.request.user) with atomic(): user_id = self.request.user.pk if self.request.user.pk else None run_task( "shuup.gdpr.tasks.anonymize", shop_id=self.request.shop.pk, contact_id=contact.pk, user_id=user_id, ) messages.success(request, _("Contact was anonymized.")) return HttpResponseRedirect( reverse("shuup_admin:contact.detail", kwargs=dict(pk=contact.pk)))
def test_run_task_with_objects(admin_user): shop = factories.get_default_shop() supplier = factories.get_default_supplier(shop) task, result = run_task( "shuup.utils.text.snake_case", value="test ing", stored=True, queue="random", shop_id=shop.pk, supplier_id=supplier.pk, user_id=admin_user.pk, ) bg_task = BackgroundTask.objects.get(identifier=task.identifier) assert bg_task.supplier == supplier assert bg_task.shop == shop assert bg_task.user == admin_user
def on_index_catalog_shop_product(sender, shop_product, **kwargs): shop_product_id = shop_product.pk if isinstance( shop_product, ShopProduct) else shop_product run_task("shuup.simple_supplier.tasks.index_shop_product", shop_product=shop_product_id)
def on_happy_hour_object_saved(sender, object: HappyHour, **kwargs): transaction.on_commit(lambda: run_task( "shuup.discounts.tasks.reindex_happy_hour", happy_hour_id=object.pk))
def on_discount_object_saved(sender, object: Discount, **kwargs): transaction.on_commit(lambda: run_task( "shuup.discounts.tasks.reindex_discount", discount_id=object.pk))
def test_run_task(): result = run_task("shuup.utils.text.snake_case", value="test ing") assert result == "test_ing"
def test_run_task_with_exception(): _, result = run_task("shuup.utils.text.identifierify", value=3) assert result.error_log with pytest.raises(TaskNotSerializableError): run_task("shuup.utils.text.identifierify", value=object)
def test_run_task(): task, result = run_task("shuup.utils.text.snake_case", value="test ing") assert result == "test_ing" assert task.identifier
def clone_product(self, shop_product: ShopProduct): # clone product product = shop_product.product new_product = copy_model_instance(product) new_product.sku = "{}-{}".format(product.sku, Product.objects.count()) new_product.name = ("{name} - Copy").format(name=product.name) new_product.save() for trans in product.translations.all(): trans_product_data = get_data_dict(trans) trans_product_data["master"] = new_product new_trans = Product._parler_meta.get_model_by_related_name( "translations").objects.get_or_create( language_code=trans.language_code, master=new_product)[0] for (key, value) in trans_product_data.items(): setattr(new_trans, key, value) new_trans.save() # clone shop product new_shop_product = copy_model_instance(shop_product) new_shop_product.product = new_product new_shop_product.save() for trans in shop_product.translations.all(): trans_shop_product_data = get_data_dict(trans) trans_shop_product_data["master"] = new_shop_product ShopProduct._parler_meta.get_model_by_related_name( "translations").objects.get_or_create( **trans_shop_product_data) # clone suppliers if self.current_supplier: new_shop_product.suppliers.add(self.current_supplier) else: new_shop_product.suppliers.set(shop_product.suppliers.all()) new_shop_product.categories.set(shop_product.categories.all()) # clone attributes for original_product_attribute in product.attributes.all(): product_attribute = ProductAttribute.objects.create( product=new_product, attribute=original_product_attribute.attribute, ) product_attribute.value = original_product_attribute.value product_attribute.save() # clone media for media in product.media.all(): media_copy = copy_model_instance(media) media_copy.product = new_product media_copy.file = media.file media.shops.add(shop_product.shop) if product.primary_image == media: new_product.primary_image = media_copy for trans in media.translations.all(): trans_product_media_data = get_data_dict(trans) trans_product_media_data["master"] = new_shop_product ProductMedia._parler_meta.get_model_by_related_name( "translations").objects.create(**trans_product_media_data) media_copy.save() product_copied.send(sender=type(self), shop=shop_product.shop, suppliers=self.current_supplier, copied=product, copy=new_product) transaction.on_commit( lambda: run_task("shuup.core.catalog.tasks.index_shop_product", shop_product_id=new_product.pk)) return new_shop_product