← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/node-create-bug-987625 into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/node-create-bug-987625 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #987625 in MAAS: "Node.created doesn't store creation time"
  https://bugs.launchpad.net/maas/+bug/987625

For more details, see:
https://code.launchpad.net/~rvb/maas/node-create-bug-987625/+merge/103297

This branch fixes CommonInfo.created type.  It was a DateField and it's now a DateTimeField.  Not that South supports the changing the type in-place as the two types are compatible data types.  Of course the converted values will have 00:00 as the recorded time but it's the best we can.
-- 
https://code.launchpad.net/~rvb/maas/node-create-bug-987625/+merge/103297
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/node-create-bug-987625 into lp:maas.
=== added file 'src/maasserver/migrations/0007_common_info_created_add_time.py'
--- src/maasserver/migrations/0007_common_info_created_add_time.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/migrations/0007_common_info_created_add_time.py	2012-04-24 14:30:42 +0000
@@ -0,0 +1,151 @@
+# flake8: noqa
+# SKIP this file when reformatting.
+# The rest of this file was generated by South.
+
+# encoding: utf-8
+# encoding: utf-8
+import datetime
+
+from django.db import models
+from south.db import db
+from south.v2 import SchemaMigration
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Changing field 'MACAddress.created'
+        db.alter_column('maasserver_macaddress', 'created', self.gf('django.db.models.fields.DateTimeField')())
+
+        # Changing field 'Node.created'
+        db.alter_column(u'maasserver_node', 'created', self.gf('django.db.models.fields.DateTimeField')())
+
+        # Changing field 'SSHKey.created'
+        db.alter_column('maasserver_sshkey', 'created', self.gf('django.db.models.fields.DateTimeField')())
+
+
+    def backwards(self, orm):
+        
+        # Changing field 'MACAddress.created'
+        db.alter_column('maasserver_macaddress', 'created', self.gf('django.db.models.fields.DateField')())
+
+        # Changing field 'Node.created'
+        db.alter_column(u'maasserver_node', 'created', self.gf('django.db.models.fields.DateField')())
+
+        # Changing field 'SSHKey.created'
+        db.alter_column('maasserver_sshkey', 'created', self.gf('django.db.models.fields.DateField')())
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'unique': 'True', 'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'maasserver.config': {
+            'Meta': {'object_name': 'Config'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'value': ('maasserver.fields.JSONObjectField', [], {'null': 'True'})
+        },
+        'maasserver.filestorage': {
+            'Meta': {'object_name': 'FileStorage'},
+            'data': ('django.db.models.fields.files.FileField', [], {'max_length': '255'}),
+            'filename': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '200'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
+        },
+        'maasserver.macaddress': {
+            'Meta': {'object_name': 'MACAddress'},
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'mac_address': ('maasserver.fields.MACAddressField', [], {'unique': 'True'}),
+            'node': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['maasserver.Node']"}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        u'maasserver.node': {
+            'Meta': {'object_name': 'Node'},
+            'after_commissioning_action': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'architecture': ('django.db.models.fields.CharField', [], {'default': "u'i386'", 'max_length': '10'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'error': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '255', 'blank': 'True'}),
+            'hostname': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '255', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'owner': ('django.db.models.fields.related.ForeignKey', [], {'default': 'None', 'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+            'power_type': ('django.db.models.fields.CharField', [], {'default': "u''", 'max_length': '10', 'blank': 'True'}),
+            'status': ('django.db.models.fields.IntegerField', [], {'default': '0', 'max_length': '10'}),
+            'system_id': ('django.db.models.fields.CharField', [], {'default': "u'node-805d1534-8e17-11e1-b3ff-00219bd0a2de'", 'unique': 'True', 'max_length': '41'}),
+            'token': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Token']", 'null': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {})
+        },
+        'maasserver.sshkey': {
+            'Meta': {'unique_together': "((u'user', u'key'),)", 'object_name': 'SSHKey'},
+            'created': ('django.db.models.fields.DateTimeField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'key': ('django.db.models.fields.TextField', [], {}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']"})
+        },
+        'maasserver.userprofile': {
+            'Meta': {'object_name': 'UserProfile'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'user': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True'})
+        },
+        'piston.consumer': {
+            'Meta': {'object_name': 'Consumer'},
+            'description': ('django.db.models.fields.TextField', [], {}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'key': ('django.db.models.fields.CharField', [], {'max_length': '18'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'secret': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'status': ('django.db.models.fields.CharField', [], {'default': "'pending'", 'max_length': '16'}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'consumers'", 'null': 'True', 'to': "orm['auth.User']"})
+        },
+        'piston.token': {
+            'Meta': {'object_name': 'Token'},
+            'callback': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'callback_confirmed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'consumer': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['piston.Consumer']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_approved': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'key': ('django.db.models.fields.CharField', [], {'max_length': '18'}),
+            'secret': ('django.db.models.fields.CharField', [], {'max_length': '32'}),
+            'timestamp': ('django.db.models.fields.IntegerField', [], {'default': '1335276738L'}),
+            'token_type': ('django.db.models.fields.IntegerField', [], {}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'tokens'", 'null': 'True', 'to': "orm['auth.User']"}),
+            'verifier': ('django.db.models.fields.CharField', [], {'max_length': '10'})
+        }
+    }
+
+    complete_apps = ['maasserver']

=== modified file 'src/maasserver/models/__init__.py'
--- src/maasserver/models/__init__.py	2012-04-23 08:29:42 +0000
+++ src/maasserver/models/__init__.py	2012-04-24 14:30:42 +0000
@@ -114,13 +114,14 @@
     class Meta(DefaultMeta):
         abstract = True
 
-    created = models.DateField(editable=False)
+    created = models.DateTimeField(editable=False)
     updated = models.DateTimeField(editable=False)
 
     def save(self, skip_check=False, *args, **kwargs):
+        now = datetime.datetime.today()
         if not self.id:
-            self.created = datetime.date.today()
-        self.updated = datetime.datetime.today()
+            self.created = now
+        self.updated = now
         if not skip_check:
             self.full_clean()
         return super(CommonInfo, self).save(*args, **kwargs)

=== modified file 'src/maasserver/tests/models.py'
--- src/maasserver/tests/models.py	2012-04-16 10:00:51 +0000
+++ src/maasserver/tests/models.py	2012-04-24 14:30:42 +0000
@@ -16,6 +16,7 @@
 
 from django.db import models
 from maasserver.fields import JSONObjectField
+from maasserver.models import CommonInfo
 
 
 class JSONFieldModel(models.Model):
@@ -25,3 +26,9 @@
 
 class MessagesTestModel(models.Model):
     name = models.CharField(max_length=255, unique=False)
+
+
+class CommonInfoTestModel(CommonInfo):
+    # This model inherits from CommonInfo so it will have a 'created'
+    # field and an 'updated' field.
+    pass

=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py	2012-04-20 15:16:41 +0000
+++ src/maasserver/tests/test_models.py	2012-04-24 14:30:42 +0000
@@ -61,7 +61,11 @@
 from maasserver.testing import get_data
 from maasserver.testing.enum import map_enum
 from maasserver.testing.factory import factory
-from maasserver.testing.testcase import TestCase
+from maasserver.testing.testcase import (
+    TestCase,
+    TestModelTestCase,
+    )
+from maasserver.tests.models import CommonInfoTestModel
 from metadataserver.models import (
     NodeCommissionResult,
     NodeUserData,
@@ -81,6 +85,41 @@
     )
 
 
+class CommonInfoTest(TestModelTestCase):
+    """Testing for the class `CommonInfo`."""
+
+    app = 'maasserver.tests'
+
+    def test_created_populated_when_object_saved(self):
+        obj = CommonInfoTestModel()
+        obj.save()
+        self.assertIsNotNone(obj.created)
+
+    def test_updated_populated_when_object_saved(self):
+        obj = CommonInfoTestModel()
+        obj.save()
+        self.assertIsNotNone(obj.updated)
+
+    def test_updated_and_created_are_the_same_after_first_save(self):
+        obj = CommonInfoTestModel()
+        obj.save()
+        self.assertEqual(obj.created, obj.updated)
+
+    def test_updated_is_updated(self):
+        obj = CommonInfoTestModel()
+        obj.save()
+        updated = obj.updated
+        obj.save()
+        self.assertTrue(updated < obj.updated)
+
+    def test_created_not_modified_by_subsequent_call_to_save(self):
+        obj = CommonInfoTestModel()
+        obj.save()
+        created = obj.created
+        obj.save()
+        self.assertEqual(created, obj.created)
+
+
 class NodeTest(TestCase):
 
     def test_system_id(self):