def form_valid(self, form): # there would be only sample data for single-shops envs shop = Shop.objects.first() # uninstall products if form.cleaned_data.get("products", False): for product in Product.objects.filter(pk__in=sample_manager.get_installed_products(shop)): product.soft_delete() # uninstall categories if form.cleaned_data.get("categories", False): for category in Category.objects.filter(pk__in=sample_manager.get_installed_categories(shop)): category.soft_delete() # uninstall carousel if 'shuup.front.apps.carousel' in settings.INSTALLED_APPS and \ form.cleaned_data.get("carousel", False): carousel = sample_manager.get_installed_carousel(shop) if carousel: from shuup.front.apps.carousel.models import Carousel Carousel.objects.filter(pk=carousel).delete() sample_manager.clear_installed_samples(shop) messages.success(self.request, _("Sample data were consolidated")) return HttpResponseRedirect(reverse("shuup_admin:dashboard"))
def test_forms(settings): shop = get_default_shop() # check whether the fields are dynamically added manager.clear_installed_samples(shop) consolidate_form = ConsolidateObjectsForm(**{"shop":shop}) assert len(consolidate_form.fields) == 0 # field categories appears categories = [CategoryFactory().pk, CategoryFactory().pk, CategoryFactory().pk] manager.save_categories(shop, categories) consolidate_form = ConsolidateObjectsForm(**{"shop":shop}) assert "categories" in consolidate_form.fields # field products appears products = [ProductFactory().pk, ProductFactory().pk, ProductFactory().pk] manager.save_products(shop, products) consolidate_form = ConsolidateObjectsForm(**{"shop":shop}) assert "products" in consolidate_form.fields # field carousel appears carousel = Carousel.objects.create(name="stuff") manager.save_carousel(shop, carousel.pk) consolidate_form = ConsolidateObjectsForm(**{"shop":shop}) assert "carousel" in consolidate_form.fields
def populate_samples(): manager.clear_installed_samples(shop) categories = [CategoryFactory().pk, CategoryFactory().pk, CategoryFactory().pk] products = [ProductFactory().pk, ProductFactory().pk, ProductFactory().pk, ProductFactory().pk] carousel = Carousel.objects.create(name="crazy stuff").pk manager.save_categories(shop, categories) manager.save_products(shop, products) manager.save_carousel(shop, carousel)
def test_sample_data_manager(): shop = get_default_shop() assert manager.get_installed_business_segment(shop) is None assert manager.get_installed_products(shop) == [] assert manager.get_installed_categories(shop) == [] assert manager.get_installed_carousel(shop) is None assert manager.has_installed_samples(shop) is False BUSINESS_SEG = "default" PRODUCTS = [1, 2, 3] CATEGORIES = [4, 5, 6] CAROUSEL = 1 manager.save_categories(shop, CATEGORIES) manager.save_products(shop, PRODUCTS) manager.save_carousel(shop, CAROUSEL) manager.save_business_segment(shop, BUSINESS_SEG) assert manager.get_installed_business_segment(shop) == BUSINESS_SEG assert manager.get_installed_products(shop) == PRODUCTS assert manager.get_installed_categories(shop) == CATEGORIES assert manager.get_installed_carousel(shop) == CAROUSEL assert manager.has_installed_samples(shop) is True new_shop = Shop.objects.create() assert manager.get_installed_business_segment(new_shop) is None assert manager.get_installed_products(new_shop) == [] assert manager.get_installed_categories(new_shop) == [] assert manager.get_installed_carousel(new_shop) is None assert manager.has_installed_samples(new_shop) is False manager.clear_installed_samples(shop) assert manager.get_installed_business_segment(shop) is None assert manager.get_installed_products(shop) == [] assert manager.get_installed_categories(shop) == [] assert manager.get_installed_carousel(shop) is None assert manager.has_installed_samples(shop) is False