Beispiel #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
Beispiel #2
0
 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)