Permalink
Browse files

Merge pull request #5 from wmatyskiewicz/api

added slots in asset model, yield with assets for api, fixed migrations
  • Loading branch information...
2 parents c48f7cf + 481c82d commit ecd3cf171ddfe4858d2f9e414b79ccf7cf74e395 @deshipu deshipu committed May 8, 2013
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+from ralph_assets.models_assets import Asset
+
+def get_assets():
+ """Yields dicts describing all assets"""
+ for asset in Asset.objects.all():
+ yield {
+ 'name': asset.sn,
+ 'asset_id': asset.id,
+ 'ralph_id': device_info.ralph_device.id,
+ 'slots': asset.slots,
+ 'price': asset.price,
+ 'is_deprecated': asset.is_deprecated()
+ }
@@ -287,6 +287,7 @@ class Meta:
'delivery_date',
'invoice_date',
'production_use_date',
+ 'slots',
)
widgets = {
'request_date': DateWidget(),
@@ -372,6 +373,7 @@ class Meta:
'invoice_date',
'production_use_date',
'deleted',
+ 'slots',
)
widgets = {
'request_date': DateWidget(),
@@ -10,6 +10,8 @@
import os
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
from django.db import models
from django.utils.translation import ugettext_lazy as _
from lck.django.common.models import (
@@ -205,7 +207,11 @@ class Asset(TimeTrackable, EditorTrackable, SavingUser, SoftDeletable):
production_use_date = models.DateField(null=True, blank=True)
provider_order_date = models.DateField(null=True, blank=True)
category = models.ForeignKey('AssetCategory', null=True, blank=True)
-
+ slots = models.FloatField(
+ verbose_name='Slots (for blade centers)',
+ max_length=64,
+ default=0,
+ )
admin_objects = AssetAdminManager()
admin_objects_dc = DCAdminManager()
admin_objects_bo = BOAdminManager()
@@ -266,6 +272,16 @@ def __init__(self, *args, **kwargs):
self.saving_user = None
super(Asset, self).__init__(*args, **kwargs)
+ def is_deprecated(self):
+ if not self.support_period or not self.invoice_date:
+ return False
+ if isinstance(self.invoice_date, basestring):
+ self.invoice_date = datetime.strptime(self.invoice_date,'%Y-%m-%d')
+ deprecation_date = self.invoice_date + relativedelta(
+ months=self.support_period
+ )
+ return deprecation_date < datetime.today()
+
class DeviceInfo(TimeTrackable, SavingUser, SoftDeletable):
ralph_device = models.ForeignKey(
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+from django.test import TestCase
+
+from ralph_assets.models_assets import Asset
+from ralph_assets.tests.util import (
+ create_asset,
+ create_category,
+ create_model,
+ create_warehouse,
+ create_manufacturer,
+)
+
+
+class TestModelAsset(TestCase):
+ def setUp(self):
+ self.asset = create_asset(
+ sn='1111-1111-1111-1111',
+ invoice_date='2012-11-28',
+ support_period=1,
+ )
+ self.asset2 = create_asset(
+ sn='1111-1111-1111-1112',
+ invoice_date='2012-11-28',
+ support_period=120,
+ )
+
+
+ def test_is_deperecation(self):
+ self.assertEqual(self.asset.is_deprecated(), True)
+ self.assertEqual(self.asset2.is_deprecated(), False)
+
+
@@ -40,26 +40,25 @@
def create_manufacturer(name=DEFAULT_ASSET_DATA['manufacturer']):
- manufacturer = AssetManufacturer(name=name)
- manufacturer.save()
+ manufacturer, created = AssetManufacturer.objects.get_or_create(name=name)
return manufacturer
def create_warehouse(name=DEFAULT_ASSET_DATA['warehouse']):
- warehouse = Warehouse(name=name)
- warehouse.save()
+ warehouse, created = Warehouse.objects.get_or_create(name=name)
return warehouse
def create_model(name=DEFAULT_ASSET_DATA['model'], manufacturer=None):
"""name = string, manufacturer = string"""
if not manufacturer:
manufacturer = create_manufacturer()
- model = AssetModel(
- manufacturer=create_manufacturer(manufacturer),
- name=name,
- )
- model.save()
+ else:
+ manufacturer = create_manufacturer(manufacturer)
+ model, created = AssetModel.objects.get_or_create(name=name)
+ if created:
+ model.manufacturer = manufacturer
+ model.save()
return model

0 comments on commit ecd3cf1

Please sign in to comment.