Esempio n. 1
0
 def get_context_data(self, **kwargs):
     ret = super(CatalogPricingGroup, self).get_context_data(**kwargs)
     try:
         group = PricingGroup.objects.get(
             name=self.group_name,
             date=self.date,
         )
     except PricingGroup.DoesNotExist:
         group = None
     else:
         variables = group.pricingvariable_set.order_by('name')
         if self.variables_formset is None:
             self.variables_formset = PricingVariableFormSet(
                 queryset=variables,
                 prefix='variables',
             )
         if self.device_form is None:
             self.device_form = PricingDeviceForm()
         if self.devices is None:
             self.devices = self.get_devices(group, variables)
         if self.formulas_formset is None:
             self.formulas_formset = PricingFormulaFormSet(
                 group,
                 queryset=group.pricingformula_set.all(),
                 prefix='formulas',
             )
     ret.update({
         'devices': self.devices,
         'variablesformset': self.variables_formset,
         'deviceform': self.device_form,
         'formulasformset': self.formulas_formset,
         'group': group,
     })
     return ret
Esempio n. 2
0
 def get_context_data(self, **kwargs):
     ret = super(CatalogPricingGroup, self).get_context_data(**kwargs)
     try:
         group = PricingGroup.objects.get(
             name=self.group_name,
             date=self.date,
         )
     except PricingGroup.DoesNotExist:
         group = None
     else:
         variables = group.pricingvariable_set.order_by('name')
         if self.variables_formset is None:
             self.variables_formset = PricingVariableFormSet(
                 queryset=variables,
                 prefix='variables',
             )
         if self.device_form is None:
             self.device_form = PricingDeviceForm()
         if self.devices is None:
             self.devices = self.get_devices(group, variables)
         if self.formulas_formset is None:
             self.formulas_formset = PricingFormulaFormSet(
                 group,
                 queryset=group.pricingformula_set.all(),
                 prefix='formulas',
             )
     ret.update({
         'devices': self.devices,
         'variablesformset': self.variables_formset,
         'deviceform': self.device_form,
         'formulasformset': self.formulas_formset,
         'group': group,
     })
     return ret
Esempio n. 3
0
 def handle_formulas_form(self, *args, **kwargs):
     self.parse_args()
     group = get_object_or_404(
         PricingGroup,
         name=self.group_name,
         date=self.date,
     )
     self.formulas_formset = PricingFormulaFormSet(
         group,
         self.request.POST,
         queryset=group.pricingformula_set.all(),
         prefix='formulas',
     )
     if not self.formulas_formset.is_valid():
         messages.error(self.request, "Errors in the formulas form.")
         return self.get(*args, **kwargs)
     for form in self.formulas_formset.extra_forms:
         if form.has_changed():
             form.instance.group = group
     self.formulas_formset.save()
     messages.success(self.request, "Group %s saved." % group.name)
     return HttpResponseRedirect(self.request.path)
Esempio n. 4
0
 def handle_formulas_form(self, *args, **kwargs):
     self.parse_args()
     group = get_object_or_404(
         PricingGroup,
         name=self.group_name,
         date=self.date,
     )
     self.formulas_formset = PricingFormulaFormSet(
         group,
         self.request.POST,
         queryset=group.pricingformula_set.all(),
         prefix='formulas',
     )
     if not self.formulas_formset.is_valid():
         messages.error(self.request, "Errors in the formulas form.")
         return self.get(*args, **kwargs)
     for form in self.formulas_formset.extra_forms:
         if form.has_changed():
             form.instance.group = group
     self.formulas_formset.save()
     messages.success(self.request, "Group %s saved." % group.name)
     return HttpResponseRedirect(self.request.path)
Esempio n. 5
0
class CatalogPricingGroup(CatalogPricing):
    def __init__(self, *args, **kwargs):
        super(CatalogPricingGroup, self).__init__(*args, **kwargs)
        self.variables_formset = None
        self.device_form = None
        self.devices = None
        self.formulas_formset = None

    def get_devices(self, group, variables, post=None):
        devices = list(group.devices.all())
        for device in devices:
            values = device.pricingvalue_set.filter(
                variable__group=group,
            ).order_by(
                'variable__name',
            )
            device.formset = PricingValueFormSet(
                post,
                queryset=values,
                prefix='values-%d' % device.id,
            )
        return devices

    def post(self, *args, **kwargs):
        if 'values-save' in self.request.POST:
            return self.handle_values_form(*args, **kwargs)
        elif 'formulas-save' in self.request.POST:
            return self.handle_formulas_form(*args, **kwargs)
        elif 'group-delete' in self.request.POST:
            self.parse_args()
            get_object_or_404(
                PricingGroup,
                name=self.group_name,
                date=self.date,
            ).delete()
        return self.get(*args, **kwargs)

    def handle_formulas_form(self, *args, **kwargs):
        self.parse_args()
        group = get_object_or_404(
            PricingGroup,
            name=self.group_name,
            date=self.date,
        )
        self.formulas_formset = PricingFormulaFormSet(
            group,
            self.request.POST,
            queryset=group.pricingformula_set.all(),
            prefix='formulas',
        )
        if not self.formulas_formset.is_valid():
            messages.error(self.request, "Errors in the formulas form.")
            return self.get(*args, **kwargs)
        for form in self.formulas_formset.extra_forms:
            if form.has_changed():
                form.instance.group = group
        self.formulas_formset.save()
        messages.success(self.request, "Group %s saved." % group.name)
        return HttpResponseRedirect(self.request.path)

    def handle_values_form(self, *args, **kwargs):
        self.parse_args()
        group = get_object_or_404(
            PricingGroup,
            name=self.group_name,
            date=self.date,
        )
        self.variables_formset = PricingVariableFormSet(
            self.request.POST,
            queryset=group.pricingvariable_set.all(),
            prefix='variables',
        )
        self.device_form = PricingDeviceForm(self.request.POST)
        variables = group.pricingvariable_set.order_by('name')
        self.devices = self.get_devices(group, variables, self.request.POST)
        values_formsets = [d.formset for d in self.devices]
        if not all([
            self.variables_formset.is_valid(),
            self.device_form.is_valid(),
            all(fs.is_valid() for fs in values_formsets),
        ]):
            messages.error(self.request, "Errors in the variables form.")
            return self.get(*args, **kwargs)
        for device in self.devices:
            device.formset.save()
        for name in self.request.POST:
            if name.startswith('device-delete-'):
                device_id = int(name[len('device-delete-'):])
                device = get_object_or_404(Device, id=device_id)
                group.devices.remove(device)
                device.pricingvalue_set.filter(variable__group=group).delete()
                group.save()
        for form in self.variables_formset.extra_forms:
            if form.has_changed():
                form.instance.group = group
        self.variables_formset.save()
        for form in self.variables_formset.extra_forms:
            if form.has_changed():
                for device in self.devices:
                    value = PricingValue(
                        device=device,
                        variable=form.instance,
                        value=0,
                    )
                    value.save()
        device = self.device_form.cleaned_data['device']
        if device:
            if group.devices.filter(id=device.id).exists():
                messages.warning(
                    self.request,
                    "Device %s is already in group %s." % (
                        device.name,
                        group.name,
                    ),
                )
            else:
                group.devices.add(device)
                group.save()
                for variable in variables.all():
                    value = PricingValue(
                        device=device,
                        variable=variable,
                        value=0,
                    )
                    value.save()
                messages.success(
                    self.request,
                    "Device %s added to group %s." % (device.name, group.name),
                )
        messages.success(self.request, "Group %s saved." % group.name)
        return HttpResponseRedirect(self.request.path)

    def get_context_data(self, **kwargs):
        ret = super(CatalogPricingGroup, self).get_context_data(**kwargs)
        try:
            group = PricingGroup.objects.get(
                name=self.group_name,
                date=self.date,
            )
        except PricingGroup.DoesNotExist:
            group = None
        else:
            variables = group.pricingvariable_set.order_by('name')
            if self.variables_formset is None:
                self.variables_formset = PricingVariableFormSet(
                    queryset=variables,
                    prefix='variables',
                )
            if self.device_form is None:
                self.device_form = PricingDeviceForm()
            if self.devices is None:
                self.devices = self.get_devices(group, variables)
            if self.formulas_formset is None:
                self.formulas_formset = PricingFormulaFormSet(
                    group,
                    queryset=group.pricingformula_set.all(),
                    prefix='formulas',
                )
        ret.update({
            'devices': self.devices,
            'variablesformset': self.variables_formset,
            'deviceform': self.device_form,
            'formulasformset': self.formulas_formset,
            'group': group,
        })
        return ret
Esempio n. 6
0
class CatalogPricingGroup(CatalogPricing):

    def __init__(self, *args, **kwargs):
        super(CatalogPricingGroup, self).__init__(*args, **kwargs)
        self.variables_formset = None
        self.device_form = None
        self.devices = None
        self.formulas_formset = None

    def get_devices(self, group, variables, post=None):
        devices = list(group.devices.all())
        for device in devices:
            values = device.pricingvalue_set.filter(
                variable__group=group,
            ).order_by(
                'variable__name',
            )
            device.formset = PricingValueFormSet(
                post,
                queryset=values,
                prefix='values-%d' % device.id,
            )
        return devices

    def post(self, *args, **kwargs):
        if 'values-save' in self.request.POST:
            return self.handle_values_form(*args, **kwargs)
        elif 'formulas-save' in self.request.POST:
            return self.handle_formulas_form(*args, **kwargs)
        elif 'group-delete' in self.request.POST:
            self.parse_args()
            get_object_or_404(
                PricingGroup,
                name=self.group_name,
                date=self.date,
            ).delete()
        return self.get(*args, **kwargs)

    def handle_formulas_form(self, *args, **kwargs):
        self.parse_args()
        group = get_object_or_404(
            PricingGroup,
            name=self.group_name,
            date=self.date,
        )
        self.formulas_formset = PricingFormulaFormSet(
            group,
            self.request.POST,
            queryset=group.pricingformula_set.all(),
            prefix='formulas',
        )
        if not self.formulas_formset.is_valid():
            messages.error(self.request, "Errors in the formulas form.")
            return self.get(*args, **kwargs)
        for form in self.formulas_formset.extra_forms:
            if form.has_changed():
                form.instance.group = group
        self.formulas_formset.save()
        messages.success(self.request, "Group %s saved." % group.name)
        return HttpResponseRedirect(self.request.path)

    def handle_values_form(self, *args, **kwargs):
        self.parse_args()
        group = get_object_or_404(
            PricingGroup,
            name=self.group_name,
            date=self.date,
        )
        self.variables_formset = PricingVariableFormSet(
            self.request.POST,
            queryset=group.pricingvariable_set.all(),
            prefix='variables',
        )
        self.device_form = PricingDeviceForm(self.request.POST)
        variables = group.pricingvariable_set.order_by('name')
        self.devices = self.get_devices(group, variables, self.request.POST)
        values_formsets = [d.formset for d in self.devices]
        if not all([
            self.variables_formset.is_valid(),
            self.device_form.is_valid(),
            all(fs.is_valid() for fs in values_formsets),
        ]):
            messages.error(self.request, "Errors in the variables form.")
            return self.get(*args, **kwargs)
        for device in self.devices:
            device.formset.save()
        for name in self.request.POST:
            if name.startswith('device-delete-'):
                device_id = int(name[len('device-delete-'):])
                device = get_object_or_404(Device, id=device_id)
                group.devices.remove(device)
                device.pricingvalue_set.filter(variable__group=group).delete()
                group.save()
        for form in self.variables_formset.extra_forms:
            if form.has_changed():
                form.instance.group = group
        self.variables_formset.save()
        for form in self.variables_formset.extra_forms:
            if form.has_changed():
                for device in self.devices:
                    value = PricingValue(
                        device=device,
                        variable=form.instance,
                        value=0,
                    )
                    value.save()
        device = self.device_form.cleaned_data['device']
        if device:
            if group.devices.filter(id=device.id).exists():
                messages.warning(
                    self.request,
                    "Device %s is already in group %s." % (
                        device.name,
                        group.name,
                    ),
                )
            else:
                group.devices.add(device)
                group.save()
                for variable in variables.all():
                    value = PricingValue(
                        device=device,
                        variable=variable,
                        value=0,
                    )
                    value.save()
                messages.success(
                    self.request,
                    "Device %s added to group %s." % (device.name, group.name),
                )
        messages.success(self.request, "Group %s saved." % group.name)
        return HttpResponseRedirect(self.request.path)

    def get_context_data(self, **kwargs):
        ret = super(CatalogPricingGroup, self).get_context_data(**kwargs)
        try:
            group = PricingGroup.objects.get(
                name=self.group_name,
                date=self.date,
            )
        except PricingGroup.DoesNotExist:
            group = None
        else:
            variables = group.pricingvariable_set.order_by('name')
            if self.variables_formset is None:
                self.variables_formset = PricingVariableFormSet(
                    queryset=variables,
                    prefix='variables',
                )
            if self.device_form is None:
                self.device_form = PricingDeviceForm()
            if self.devices is None:
                self.devices = self.get_devices(group, variables)
            if self.formulas_formset is None:
                self.formulas_formset = PricingFormulaFormSet(
                    group,
                    queryset=group.pricingformula_set.all(),
                    prefix='formulas',
                )
        ret.update({
            'devices': self.devices,
            'variablesformset': self.variables_formset,
            'deviceform': self.device_form,
            'formulasformset': self.formulas_formset,
            'group': group,
        })
        return ret