def __init__(self, **kwargs): shop = kwargs.pop("shop") super(SampleObjectsWizardForm, self).__init__(**kwargs) if sample_manager.get_installed_categories(shop): self.fields["categories"].initial = True self.fields["categories"].widget = forms.CheckboxInput(attrs={'disabled': True}) if sample_manager.get_installed_products(shop): self.fields["products"].initial = True self.fields["products"].widget = forms.CheckboxInput(attrs={'disabled': True}) # no really choices to make - change to a hidden field widget if len(BUSINESS_SEGMENTS) == 1: self.fields["business_segment"].widget = forms.HiddenInput() # installed data means only one choice - the current installed one installed_bs = sample_manager.get_installed_business_segment(shop) if installed_bs: self.fields["business_segment"].initial = installed_bs self.fields["business_segment"].choices = [(installed_bs, BUSINESS_SEGMENTS[installed_bs]["name"])] # add the carousel option if its module is installed if 'shuup.front.apps.carousel' in settings.INSTALLED_APPS: has_installed_carousel = (sample_manager.get_installed_carousel(shop) is not None) self.fields["carousel"] = forms.BooleanField( label=_("Install Carousel"), initial=has_installed_carousel, required=False, widget=forms.CheckboxInput(attrs={"disabled": has_installed_carousel}), help_text=_("Enable this to install a sample carousel.") )
def __init__(self, **kwargs): shop = kwargs.pop("shop") super(SampleObjectsWizardForm, self).__init__(**kwargs) if sample_manager.get_installed_categories(shop): self.fields["categories"].initial = True self.fields["categories"].widget = forms.CheckboxInput(attrs={'disabled': True}) if sample_manager.get_installed_products(shop): self.fields["products"].initial = True self.fields["products"].widget = forms.CheckboxInput(attrs={'disabled': True}) # no really choices to make - change to a hidden field widget if len(BUSINESS_SEGMENTS) == 1: self.fields["business_segment"].widget = forms.HiddenInput() # installed data means only one choice - the current installed one installed_bs = sample_manager.get_installed_business_segment(shop) if installed_bs: self.fields["business_segment"].initial = installed_bs self.fields["business_segment"].choices = [(installed_bs, BUSINESS_SEGMENTS[installed_bs]["name"])] # add the carousel option if its module is installed if 'shuup.front.apps.carousel' in settings.INSTALLED_APPS: has_installed_carousel = (sample_manager.get_installed_carousel(shop) is not None) self.fields["carousel"] = forms.BooleanField( label=_("Install Carousel"), initial=has_installed_carousel, required=False, widget=forms.CheckboxInput(attrs={"disabled": has_installed_carousel}), help_text=_("Check this to install a sample carousel.") )
def form_valid(self, form): current_language = translation.get_language() default_language = getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None) # change the language to the PARLER_DEFAULT_LANGUAGE # so sample data will have data on fallback languages if default_language: translation.activate(default_language) shop = self.object form_data = form["sample"].cleaned_data business_segment = form_data["business_segment"] current_categories = sample_manager.get_installed_categories(shop) current_products = sample_manager.get_installed_products(shop) current_carousel = sample_manager.get_installed_carousel(shop) # only saves the business segment if there is no data installed # otherwise user can't change the segment if sample_manager.has_installed_samples(shop): business_segment = sample_manager.get_installed_business_segment( shop) else: sample_manager.save_business_segment(shop, business_segment) # user wants to install sample categories if form_data.get("categories", False) and not current_categories: categories = self._create_sample_categories(shop, business_segment) if categories: sample_manager.save_categories(shop, categories) # user wants to install sample products if form_data.get("products", False) and not current_products: products = self._create_sample_products(shop, business_segment) if products: sample_manager.save_products(shop, products) # user wants a carousel if form_data.get("carousel") and not current_carousel: carousel = self._create_sample_carousel(shop, business_segment) if carousel: sample_manager.save_carousel(shop, carousel.pk) # back to current language translation.activate(current_language) # user will no longer see this pane configuration.set(None, "sample_data_wizard_completed", True) # reindex the product catalog call_command("reindex_product_catalog")
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
def form_valid(self, form): current_language = translation.get_language() default_language = getattr(settings, "PARLER_DEFAULT_LANGUAGE_CODE", None) # change the language to the PARLER_DEFAULT_LANGUAGE # so sample data will have data on fallback languages if default_language: translation.activate(default_language) shop = self.object form_data = form["sample"].cleaned_data business_segment = form_data["business_segment"] current_categories = sample_manager.get_installed_categories(shop) current_products = sample_manager.get_installed_products(shop) current_carousel = sample_manager.get_installed_carousel(shop) # only saves the business segment if there is no data installed # otherwise user can't change the segment if sample_manager.has_installed_samples(shop): business_segment = sample_manager.get_installed_business_segment(shop) else: sample_manager.save_business_segment(shop, business_segment) # user wants to install sample categories if form_data.get("categories", False) and not current_categories: categories = self._create_sample_categories(shop, business_segment) if categories: sample_manager.save_categories(shop, categories) # user wants to install sample products if form_data.get("products", False) and not current_products: products = self._create_sample_products(shop, business_segment) if products: sample_manager.save_products(shop, products) # user wants a carousel if form_data.get("carousel") and not current_carousel: carousel = self._create_sample_carousel(shop, business_segment) if carousel: sample_manager.save_carousel(shop, carousel.pk) # back to current language translation.activate(current_language) # user will no longer see this pane configuration.set(None, "sample_data_wizard_completed", True)
def test_consolidate_objects(rf, admin_user): shop = get_default_shop() # just visit to make sure GET is ok request = apply_request_middleware(rf.get("/"), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 200 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 clear_objs(): Product.objects.all().delete() Category.objects.all().delete() Carousel.objects.all().delete() # consolidate everything populate_samples() data = {"categories": False, "products": False, "carousel": False} request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.count() == 3 assert Product.objects.count() == 4 assert Carousel.objects.count() == 1 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 # consolidate nothing clear_objs() populate_samples() data = {"products": True, "categories": True, "carousel": True} request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.all_except_deleted().count() == 0 assert Product.objects.all_except_deleted().count() == 0 assert Carousel.objects.count() == 0 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 # consolidate some clear_objs() populate_samples() data = {"products": False, "categories": False, "carousel": True} request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.all_except_deleted().count() == 3 assert Product.objects.all_except_deleted().count() == 4 assert Carousel.objects.count() == 0 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
def test_consolidate_objects(rf, admin_user): shop = get_default_shop() # just visit to make sure GET is ok request = apply_request_middleware(rf.get("/"), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 200 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 clear_objs(): Product.objects.all().delete() Category.objects.all().delete() Carousel.objects.all().delete() # consolidate everything populate_samples() data = { "categories": False, "products": False, "carousel": False } request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.count() == 3 assert Product.objects.count() == 4 assert Carousel.objects.count() == 1 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 # consolidate nothing clear_objs() populate_samples() data = { "products": True, "categories": True, "carousel": True } request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.all_except_deleted().count() == 0 assert Product.objects.all_except_deleted().count() == 0 assert Carousel.objects.count() == 0 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 # consolidate some clear_objs() populate_samples() data = { "products": False, "categories": False, "carousel": True } request = apply_request_middleware(rf.post("/", data=data), user=admin_user) response = ConsolidateSampleObjectsView.as_view()(request) assert response.status_code == 302 assert response["Location"] == reverse("shuup_admin:dashboard") assert Category.objects.all_except_deleted().count() == 3 assert Product.objects.all_except_deleted().count() == 4 assert Carousel.objects.count() == 0 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