Exemplo n.º 1
0
 def import_csv(self, group, uploaded_file):
     if uploaded_file.size > 4 * 1024 * 1024:
         messages.error(self.request, "File too big to import.")
         return
     f = cStringIO.StringIO(uploaded_file.read())
     rows = iter(UnicodeReader(f))
     header = list(rows.next())
     if header[0].strip() != 'sn':
         messages.error(
             self.request,
             "The first row should have 'sn' followed by variable names."
         )
         return
     variables = [name.strip() for name in header[1:]]
     devices = {}
     try:
         for row in rows:
             if not row:
                 continue
             devices[row[0]] = dict(zip(
                 variables,
                 (decimal.Decimal(v) for v in row[1:]),
             ))
     except ValueError as e:
         messages.error(self.request, "Invalid value: %s." % e)
         return
     variable_dict = {}
     for name in variables:
         v = PricingVariable(name=name, group=group)
         v.save()
         variable_dict[name] = v
     for sn, values in devices.iteritems():
         try:
             device = Device.objects.get(sn=sn)
         except Device.DoesNotExist:
             messages.warning(
                 self.request,
                 "Serial number %s not found, skipping." % sn
             )
             continue
         group.devices.add(device)
         for name, value in values.iteritems():
             PricingValue(
                 variable=variable_dict[name],
                 device=device,
                 value=value,
             ).save()
     group.save()
Exemplo n.º 2
0
 def test_disk_share(self):
     storage_dev = Device.create(
         sn='device',
         model_type=DeviceType.storage,
         model_name='storage device',
     )
     storage_dev.save()
     share_group = ComponentModelGroup(
         price=1,
         type=ComponentType.share,
         per_size=False,
         size_unit='',
         size_modifier=1,
     )
     share_group.save()
     share_model = ComponentModel(
         speed=0,
         cores=0,
         size=7,
         type=ComponentType.share,
         group=share_group,
         family='',
     )
     share_model.save()
     disk_share = DiskShare(
         device=storage_dev,
         model=share_model,
         share_id=1,
         label="share",
         size=7,
         snapshot_size=5,
         wwn='123456789012345678901234567890123',
         full=True,
     )
     disk_share.save()
     share_mount = DiskShareMount(
         share=disk_share,
         device=storage_dev,
         size=17,
     )
     share_mount.save()
     today = date.today()
     this_month = date(year=today.year, month=today.month, day=1)
     pricing_group = PricingGroup(
         name='group',
         date=this_month,
     )
     pricing_group.save()
     pricing_group.devices.add(storage_dev)
     pricing_group.save()
     pricing_formula = PricingFormula(
         group=pricing_group,
         component_group=share_group,
         formula='3+size+11*variable',
     )
     pricing_formula.save()
     pricing_variable = PricingVariable(
         name='variable',
         group=pricing_group,
         aggregate=PricingAggregate.sum,
     )
     pricing_variable.save()
     pricing_value = PricingValue(
         device=storage_dev,
         variable=pricing_variable,
         value=13,
     )
     pricing_value.save()
     variable_value = pricing_variable.get_value()
     self.assertEqual(variable_value, 13)
     formula_value = pricing_formula.get_value(size=7)
     self.assertEqual(formula_value, 3 + 7 + 11 * 13)
     share_price = disk_share.get_price()
     self.assertEqual(share_price, 3 + (7.0 + 5.0) / 1024 + 11 * 13)
     mount_price = share_mount.get_price()
     self.assertEqual(mount_price, 3 + 17.0 / 1024 + 11 * 13)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
0
 def test_disk_share(self):
     storage_dev = Device.create(
         sn='device',
         model_type=DeviceType.storage,
         model_name='storage device',
     )
     storage_dev.save()
     share_group = ComponentModelGroup(
         price=1,
         type=ComponentType.share,
         per_size=False,
         size_unit='',
         size_modifier=1,
     )
     share_group.save()
     share_model = ComponentModel(
         speed=0,
         cores=0,
         size=7,
         type=ComponentType.share,
         group=share_group,
         family='',
     )
     share_model.save()
     disk_share = DiskShare(
         device=storage_dev,
         model=share_model,
         share_id=1,
         label="share",
         size=7,
         snapshot_size=5,
         wwn='123456789012345678901234567890123',
         full=True,
     )
     disk_share.save()
     share_mount = DiskShareMount(
         share=disk_share,
         device=storage_dev,
         size=17,
     )
     share_mount.save()
     today = date.today()
     this_month = date(year=today.year, month=today.month, day=1)
     pricing_group = PricingGroup(
         name='group',
         date=this_month,
     )
     pricing_group.save()
     pricing_group.devices.add(storage_dev)
     pricing_group.save()
     pricing_formula = PricingFormula(
         group=pricing_group,
         component_group=share_group,
         formula='3+size+11*variable',
     )
     pricing_formula.save()
     pricing_variable = PricingVariable(
         name='variable',
         group=pricing_group,
         aggregate=PricingAggregate.sum,
     )
     pricing_variable.save()
     pricing_value = PricingValue(
         device=storage_dev,
         variable=pricing_variable,
         value=13,
     )
     pricing_value.save()
     variable_value = pricing_variable.get_value()
     self.assertEqual(variable_value, 13)
     formula_value = pricing_formula.get_value(size=7)
     self.assertEqual(formula_value, 3 + 7 + 11 * 13)
     share_price = disk_share.get_price()
     self.assertEqual(share_price, 3 + (7.0 + 5.0) / 1024 + 11 * 13)
     mount_price = share_mount.get_price()
     self.assertEqual(mount_price, 3 + 17.0 / 1024 + 11 * 13)