Exemplo n.º 1
0
    def __call__(self, wizard, form, step, data, files):
        subscription_data = wizard.get_cleaned_data_for_step('subscription') or {}
        products_data = wizard.get_cleaned_data_for_step('products') or {}
        extents_data = wizard.get_cleaned_data_for_step('extents') or {}

        size = subscription_data.get('size', None)
        products = products_data.get('products', [])
        carrier = subscription_data.get('carrier', None)

        products_tree = cache.get('products_tree') or sw.get_products_tree(pm.Product.objects)
        if not cache.get('products_tree'): cache.set('products_tree', products_tree)

        for product in products:
            if extents_data.get('choice_supplier_product_%d' % product.id, 'false') == 'false': continue
            weight = size.weight - size.weight*settings.PACKAGING_WEIGHT_RATE/100
            price = size.default_price().price
            weight_level = carrier.carrierlevel_set.filter(weight__gte=size.weight)
            if weight_level: price -= weight_level[0].price
            price = price*extents_data.get('product_%d' % product.id, 1)/100

            root = sw.find_product(products_tree, product.id)
            qs = sm.Product.objects.filter(status='p', product__in=sw.products_tree_to_list(root)).select_related('main_image', 'main_price', 'main_price__currency', 'main_price__tax', 'main_price__supplier').prefetch_related('criterias', 'suppliers').order_by('-date_created')
            form.fields['supplier_product_%d' % product.id] = f = forms.forms.ModelMultipleChoiceField(widget=forms.createall.MyImageCheckboxSelectMultiple, queryset=qs, label=product.name)
            supplier_products_list = qs.all()
            f.choices = [(p.id, '%s|#~|%s|#~|%s' % (p.name, p.main_image.image if p.main_image else '', (p.main_price.get_after_tax_price_with_fee() if carrier.apply_suppliers_fee else p.main_price.get_after_tax_price())/price*100)) for p in supplier_products_list]

        return form
Exemplo n.º 2
0
    def __call__(self, wizard, form, step, data, files):
        cart_data = wizard.get_cleaned_data_for_step('cart') or {}
        form.thematic = get_thematic(cart_data)
        if form.thematic:
            __key = 'thematic_extents_%d' % form.thematic.id
            thematic_extents = cache.get(__key) or form.thematic.thematicextent_set.select_related('product').all() if form.thematic else []
            if not cache.get(__key): cache.set(__key, thematic_extents)

            form.thematic_products = [e.product for e in thematic_extents]

        form.modified = True if data else False

        form.products_tree = cache.get('products_tree') or sw.get_products_tree(pm.Product.objects)
        if not cache.get('products_tree'): cache.set('products_tree', form.products_tree)

        return form
Exemplo n.º 3
0
    def get_form(self, step=None, data=None, files=None):
        form = super().get_form(step, data, files)

        # determine the step if not given
        if step is None: step = self.steps.current

        try:
            thematic = models.Thematic.objects.select_related().get(id=self.kwargs.get('thematic_id', None))
        except models.Thematic.DoesNotExist:
            thematic = None

        thematic_products = [e.product for e in thematic.thematicextent_set.all()] if thematic else []
        form.thematic_products = thematic_products

        if step == '0':
            if thematic:
                for k, f in [('size', thematic.size),
                             ('carrier', thematic.carrier),
                             ('frequency', thematic.frequency),
                             ('start', thematic.start_duration)]:
                    if f: form.fields[k].initial = f

                if thematic.end_duration:
                    delta = relativedelta(Week.fromstring(thematic.end_duration).day(1),
                                          Week.fromstring(thematic.start_duration).day(1))
                    form.fields['duration'].initial = delta.months

                for field, locked in [('size', thematic.locked_size),
                                      ('carrier', thematic.locked_carrier),
                                      ('receive_only_once', thematic.locked_receive_only_once),
                                      ('frequency', thematic.locked_frequency),
                                      ('start', thematic.locked_start),
                                      ('duration', thematic.locked_duration),
                                      ('criterias', thematic.locked_criterias)]:
                    if locked:
                        form.fields[field].widget.attrs['class'] = form.fields[field].widget.attrs.get('class', '') + ' disabled'

                if thematic.criterias:
                    form.fields['criterias'].initial = [v.id for v in thematic.criterias.all()]

            form.products_tree = cache.get('products_tree') or sw.get_products_tree(pm.Product.objects)
            if not cache.get('products_tree'): cache.set('products_tree', form.products_tree)

            form.carriers = cache.get('create_carriers') or models.Carrier.objects.select_related().all()
            if not cache.get('create_carriers'): cache.set('create_carriers', form.carriers)

            form.sizes = cache.get('create_sizes') or models.Size.objects.select_related().all()
            if not cache.get('create_sizes'): cache.set('create_sizes', form.sizes)

            if not thematic: form.fields['customized'].initial = True

        elif step == '1':
            products = []
            for product in pm.Product.objects.order_by('name').all():
                if int( self.request.POST.get('product_%d' % product.id, 0) ):
                    products.append(product)

            if not products:
                raise forms.forms.ValidationError("no product was selected")

            extents = [e.extent for e in thematic.thematicextent_set.all()] if thematic else []
            shared_extent = int((100 - sum(extents))/(len(products) - len(extents))) if (len(products) - len(extents)) else 0

            form.selected_products = []
            for product in products:
                extent = None
                if product in thematic_products:
                    extent = thematic.thematicextent_set.get(product=product)
                form.selected_products.append((product, extent.extent if extent else shared_extent))

            messages.info(self.request, _('In order to lock a product percent, please check the corresponding checkbox.'))

        return form