← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/maas/migrate-sshkey into lp:maas

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/migrate-sshkey into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jtv/maas/migrate-sshkey/+merge/107188

As per the migration plan.  This moves SSHKey and friends out into its own module.  Apart from the obvious (such as the migration announcements disappearing, and some imports changing) there are no other changes.


Jeroen
-- 
https://code.launchpad.net/~jtv/maas/migrate-sshkey/+merge/107188
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~jtv/maas/migrate-sshkey into lp:maas.
=== modified file 'src/maasserver/models/__init__.py'
--- src/maasserver/models/__init__.py	2012-05-24 09:41:34 +0000
+++ src/maasserver/models/__init__.py	2012-05-24 10:19:22 +0000
@@ -29,8 +29,6 @@
     "UserProfile",
     ]
 
-import binascii
-from cgi import escape
 from logging import getLogger
 import os
 import re
@@ -54,11 +52,9 @@
     Model,
     OneToOneField,
     Q,
-    TextField,
     )
 from django.db.models.signals import post_save
 from django.shortcuts import get_object_or_404
-from django.utils.safestring import mark_safe
 from maasserver import DefaultMeta
 from maasserver.fields import JSONObjectField
 from maasserver.enum import (
@@ -79,6 +75,7 @@
 from maasserver.models.cleansave import CleanSave
 from maasserver.models.config import Config
 from maasserver.models.filestorage import FileStorage
+from maasserver.models.sshkey import SSHKey
 from maasserver.models.timestampedmodel import TimestampedModel
 from metadataserver import nodeinituser
 from piston.models import (
@@ -90,10 +87,6 @@
     POWER_TYPE_CHOICES,
     )
 from provisioningserver.tasks import power_on
-from twisted.conch.ssh.keys import (
-    BadKeyError,
-    Key,
-    )
 
 # Special users internal to MAAS.
 SYSTEM_USERS = [
@@ -795,119 +788,6 @@
 User._meta.get_field('email')._unique = True
 
 
-# Due for model migration on 2012-05-25
-class SSHKeyManager(Manager):
-    """A utility to manage the colletion of `SSHKey`s."""
-
-    def get_keys_for_user(self, user):
-        """Return the text of the ssh keys associated with a user."""
-        return SSHKey.objects.filter(user=user).values_list('key', flat=True)
-
-
-# Due for model migration on 2012-05-25
-def validate_ssh_public_key(value):
-    """Validate that the given value contains a valid SSH public key."""
-    try:
-        key = Key.fromString(value)
-        if not key.isPublic():
-            raise ValidationError(
-                "Invalid SSH public key (this key is a private key).")
-    except (BadKeyError, binascii.Error):
-        raise ValidationError("Invalid SSH public key.")
-
-
-# Due for model migration on 2012-05-25
-HELLIPSIS = '…'
-
-
-# Due for model migration on 2012-05-25
-def get_html_display_for_key(key, size):
-    """Return a compact HTML representation of this key with a boundary on
-    the size of the resulting string.
-
-    A key typically looks like this: 'key_type key_string comment'.
-    What we want here is display the key_type and, if possible (i.e. if it
-    fits in the boundary that `size` gives us), the comment.  If possible we
-    also want to display a truncated key_string.  If the comment is too big
-    to fit in, we simply display a cropped version of the whole string.
-
-    :param key: The key for which we want an HTML representation.
-    :type name: basestring
-    :param size: The maximum size of the representation.  This may not be
-        met exactly.
-    :type size: int
-    :return: The HTML representation of this key.
-    :rtype: basestring
-    """
-    key = key.strip()
-    key_parts = key.split(' ', 2)
-
-    if len(key_parts) == 3:
-        key_type = key_parts[0]
-        key_string = key_parts[1]
-        comment = key_parts[2]
-        room_for_key = (
-            size - (len(key_type) + len(comment) + len(HELLIPSIS) + 2))
-        if room_for_key > 0:
-            return '%s %.*s%s %s' % (
-                escape(key_type, quote=True),
-                room_for_key,
-                escape(key_string, quote=True),
-                HELLIPSIS,
-                escape(comment, quote=True))
-
-    if len(key) > size:
-        return '%.*s%s' % (
-            size - len(HELLIPSIS),
-            escape(key, quote=True),
-            HELLIPSIS)
-    else:
-        return escape(key, quote=True)
-
-
-# Due for model migration on 2012-05-25
-MAX_KEY_DISPLAY = 50
-
-
-# Due for model migration on 2012-05-25
-class SSHKey(CleanSave, TimestampedModel):
-    """A `SSHKey` represents a user public SSH key.
-
-    Users will be able to access `Node`s using any of their registered keys.
-
-    :ivar user: The user which owns the key.
-    :ivar key: The ssh public key.
-    """
-
-    objects = SSHKeyManager()
-
-    user = ForeignKey(User, null=False, editable=False)
-
-    key = TextField(
-        null=False, editable=True, validators=[validate_ssh_public_key])
-
-    class Meta(DefaultMeta):
-        verbose_name = "SSH key"
-        unique_together = ('user', 'key')
-
-    def unique_error_message(self, model_class, unique_check):
-        if unique_check == ('user', 'key'):
-                return "This key has already been added for this user."
-        return super(
-            SSHKey, self).unique_error_message(model_class, unique_check)
-
-    def __unicode__(self):
-        return self.key
-
-    def display_html(self):
-        """Return a compact HTML representation of this key.
-
-        :return: The HTML representation of this key.
-        :rtype: basestring
-        """
-        return mark_safe(get_html_display_for_key(self.key, MAX_KEY_DISPLAY))
-
-
 # Register the models in the admin site.
 admin.site.register(Consumer)
 admin.site.register(Config)

=== added file 'src/maasserver/models/sshkey.py'
--- src/maasserver/models/sshkey.py	1970-01-01 00:00:00 +0000
+++ src/maasserver/models/sshkey.py	2012-05-24 10:19:22 +0000
@@ -0,0 +1,142 @@
+# Copyright 2012 Canonical Ltd.  This software is licensed under the
+# GNU Affero General Public License version 3 (see the file LICENSE).
+
+""":class:`SSHKey` and friends."""
+
+from __future__ import (
+    absolute_import,
+    print_function,
+    unicode_literals,
+    )
+
+__metaclass__ = type
+__all__ = [
+    'SSHKey',
+    ]
+
+
+import binascii
+from cgi import escape
+
+from django.contrib.auth.models import User
+from django.core.exceptions import ValidationError
+from django.db.models import (
+    ForeignKey,
+    Manager,
+    TextField,
+    )
+from django.utils.safestring import mark_safe
+from maasserver import DefaultMeta
+from maasserver.models.cleansave import CleanSave
+from maasserver.models.timestampedmodel import TimestampedModel
+from twisted.conch.ssh.keys import (
+    BadKeyError,
+    Key,
+    )
+
+
+class SSHKeyManager(Manager):
+    """A utility to manage the colletion of `SSHKey`s."""
+
+    def get_keys_for_user(self, user):
+        """Return the text of the ssh keys associated with a user."""
+        return SSHKey.objects.filter(user=user).values_list('key', flat=True)
+
+
+def validate_ssh_public_key(value):
+    """Validate that the given value contains a valid SSH public key."""
+    try:
+        key = Key.fromString(value)
+        if not key.isPublic():
+            raise ValidationError(
+                "Invalid SSH public key (this key is a private key).")
+    except (BadKeyError, binascii.Error):
+        raise ValidationError("Invalid SSH public key.")
+
+
+HELLIPSIS = '…'
+
+
+def get_html_display_for_key(key, size):
+    """Return a compact HTML representation of this key with a boundary on
+    the size of the resulting string.
+
+    A key typically looks like this: 'key_type key_string comment'.
+    What we want here is display the key_type and, if possible (i.e. if it
+    fits in the boundary that `size` gives us), the comment.  If possible we
+    also want to display a truncated key_string.  If the comment is too big
+    to fit in, we simply display a cropped version of the whole string.
+
+    :param key: The key for which we want an HTML representation.
+    :type name: basestring
+    :param size: The maximum size of the representation.  This may not be
+        met exactly.
+    :type size: int
+    :return: The HTML representation of this key.
+    :rtype: basestring
+    """
+    key = key.strip()
+    key_parts = key.split(' ', 2)
+
+    if len(key_parts) == 3:
+        key_type = key_parts[0]
+        key_string = key_parts[1]
+        comment = key_parts[2]
+        room_for_key = (
+            size - (len(key_type) + len(comment) + len(HELLIPSIS) + 2))
+        if room_for_key > 0:
+            return '%s %.*s%s %s' % (
+                escape(key_type, quote=True),
+                room_for_key,
+                escape(key_string, quote=True),
+                HELLIPSIS,
+                escape(comment, quote=True))
+
+    if len(key) > size:
+        return '%.*s%s' % (
+            size - len(HELLIPSIS),
+            escape(key, quote=True),
+            HELLIPSIS)
+    else:
+        return escape(key, quote=True)
+
+
+MAX_KEY_DISPLAY = 50
+
+
+class SSHKey(CleanSave, TimestampedModel):
+    """A `SSHKey` represents a user public SSH key.
+
+    Users will be able to access `Node`s using any of their registered keys.
+
+    :ivar user: The user which owns the key.
+    :ivar key: The ssh public key.
+    """
+
+    objects = SSHKeyManager()
+
+    user = ForeignKey(User, null=False, editable=False)
+
+    key = TextField(
+        null=False, editable=True, validators=[validate_ssh_public_key])
+
+    class Meta(DefaultMeta):
+        verbose_name = "SSH key"
+        unique_together = ('user', 'key')
+
+    def unique_error_message(self, model_class, unique_check):
+        if unique_check == ('user', 'key'):
+                return "This key has already been added for this user."
+        return super(
+            SSHKey, self).unique_error_message(model_class, unique_check)
+
+    def __unicode__(self):
+        return self.key
+
+    def display_html(self):
+        """Return a compact HTML representation of this key.
+
+        :return: The HTML representation of this key.
+        :rtype: basestring
+        """
+        return mark_safe(get_html_display_for_key(self.key, MAX_KEY_DISPLAY))

=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py	2012-05-24 09:41:34 +0000
+++ src/maasserver/tests/test_models.py	2012-05-24 10:19:22 +0000
@@ -42,8 +42,6 @@
     GENERIC_CONSUMER,
     get_auth_tokens,
     get_db_state,
-    get_html_display_for_key,
-    HELLIPSIS,
     MACAddress,
     Node,
     NODE_TRANSITIONS,
@@ -51,6 +49,10 @@
     SSHKey,
     SYSTEM_USERS,
     UserProfile,
+    )
+from maasserver.models.sshkey import (
+    get_html_display_for_key,
+    HELLIPSIS,
     validate_ssh_public_key,
     )
 from maasserver.provisioning import get_provisioning_api_proxy