maas-devel team mailing list archive
-
maas-devel team
-
Mailing list archive
-
Message #00015
[Merge] lp:~rvb/maas/maas-basic-model into lp:maas
Raphaël Badin has proposed merging lp:~rvb/maas/maas-basic-model into lp:maas.
Requested reviews:
MaaS Developers (maas-devel)
For more details, see:
https://code.launchpad.net/~rvb/maas/maas-basic-model/+merge/88890
This branch updates the model classes: Node and MACAddress and adds model tests.
= Tests =
./bin/test
--
https://code.launchpad.net/~rvb/maas/maas-basic-model/+merge/88890
Your team MaaS Developers is requested to review the proposed merge of lp:~rvb/maas/maas-basic-model into lp:maas.
=== modified file 'src/maasserver/models.py'
--- src/maasserver/models.py 2012-01-16 16:25:49 +0000
+++ src/maasserver/models.py 2012-01-17 16:35:56 +0000
@@ -1,6 +1,29 @@
import datetime
+import re
+from uuid import uuid1
+
from django.db import models
from django.contrib import admin
+from django.core.validators import RegexValidator
+
+
+class CommonInfo(models.Model):
+ created = models.DateField(editable=False)
+ updated = models.DateTimeField(editable=False)
+
+ class Meta:
+ abstract = True
+
+ def save(self):
+ if not self.id:
+ self.created = datetime.date.today()
+ self.updated = datetime.datetime.today()
+ super(CommonInfo, self).save()
+
+
+def generate_node_system_id():
+ return 'node-' + str(uuid1())
+
NODE_STATUS_CHOICES = (
(u'NEW', u'New'),
@@ -11,17 +34,35 @@
)
-class Node(models.Model):
- name = models.CharField(max_length=30)
+class Node(CommonInfo):
+ system_id = models.CharField(
+ max_length=41, unique=True, editable=False,
+ default=generate_node_system_id)
+ hostname = models.CharField(max_length=255)
status = models.CharField(max_length=10, choices=NODE_STATUS_CHOICES)
- created = models.DateField(editable=False)
- updated = models.DateTimeField(editable=False)
-
- def save(self):
- if not self.id:
- self.created = datetime.date.today()
- self.updated = datetime.datetime.today()
- super(Node, self).save()
-
-
+
+ def __unicode__(self):
+ return self.system_id
+
+
+mac_re = re.compile(r'^([0-9a-fA-F]{2}(:|$)){6}$')
+
+
+validate_mac_address = RegexValidator(
+ regex = mac_re,
+ message = u'Enter a valid MAC address (e.g. AA:BB:CC:DD:EE:FF).')
+
+
+class MACAddress(CommonInfo):
+ mac_address = models.CharField(
+ max_length=17, validators=[validate_mac_address])
+ node = models.ForeignKey(Node)
+
+ def __unicode__(self):
+ return self.mac_address
+
+
+# Register the models in the admin site.
admin.site.register(Node)
+admin.site.register(MACAddress)
+
=== added file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py 1970-01-01 00:00:00 +0000
+++ src/maasserver/tests/test_models.py 2012-01-17 16:35:56 +0000
@@ -0,0 +1,30 @@
+"""
+Test maasserver models.
+"""
+
+from django.test import TestCase
+from maasserver.models import Node, MACAddress
+from django.core.exceptions import ValidationError
+
+
+class NodeTest(TestCase):
+
+ def test_system_id(self):
+ node = Node()
+ self.assertEqual(len(node.system_id), 41)
+ self.assertTrue(node.system_id.startswith('node-'))
+
+
+class MACAddressTest(TestCase):
+
+ def test_mac_address_invalid(self):
+ node = Node()
+ node.save()
+ mac = MACAddress(mac_address='AA:BB:CCXDD:EE:FF', node=node)
+ self.assertRaises(ValidationError, mac.full_clean)
+
+ def test_mac_address_valid(self):
+ node = Node()
+ node.save()
+ mac = MACAddress(mac_address='AA:BB:CC:DD:EE:FF', node=node)
+ mac.full_clean() # No exception.
Follow ups