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 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(csvutil.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.º 3
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.º 4
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)