← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~rvb/maas/maas-ssh-keys-models into lp:maas

 

Raphaël Badin has proposed merging lp:~rvb/maas/maas-ssh-keys-models into lp:maas with lp:~rvb/maas/maas-ssh-keys as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~rvb/maas/maas-ssh-keys-models/+merge/99732

This branch adds SSHKey.display_html that returns a compact HTML representation of a public SSH key.
-- 
https://code.launchpad.net/~rvb/maas/maas-ssh-keys-models/+merge/99732
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~rvb/maas/maas-ssh-keys-models into lp:maas.
=== modified file 'src/maasserver/models.py'
--- src/maasserver/models.py	2012-03-28 14:01:18 +0000
+++ src/maasserver/models.py	2012-03-28 14:01:19 +0000
@@ -46,6 +46,7 @@
 from django.db import models
 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.exceptions import (
     CannotDeleteUserException,
     PermissionDenied,
@@ -699,6 +700,9 @@
         raise ValidationError("Invalid SSH public key.")
 
 
+MAX_KEY_DISPLAY = 30
+
+
 class SSHKey(CommonInfo):
     """A `SSHKey` represents a user public SSH key.
 
@@ -720,6 +724,22 @@
     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
+        """
+        key = self.key.strip()
+        key_parts = key.split(' ')
+        if len(key_parts) == 3 and len(key_parts[2]) < MAX_KEY_DISPLAY:
+            comment = key_parts[2]
+            return mark_safe(
+                '%s&hellip; %s' % (
+                    key[:MAX_KEY_DISPLAY - len(comment)], comment))
+        else:
+            return mark_safe('%s&hellip;' % key[:MAX_KEY_DISPLAY])
+
 
 class FileStorageManager(models.Manager):
     """Manager for `FileStorage` objects.

=== added file 'src/maasserver/tests/data/test_rsa_long_comment.pub'
--- src/maasserver/tests/data/test_rsa_long_comment.pub	1970-01-01 00:00:00 +0000
+++ src/maasserver/tests/data/test_rsa_long_comment.pub	2012-03-28 14:01:19 +0000
@@ -0,0 +1,1 @@
+ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDdrzzDZNwyMVBvBTT6kBnrfPZv/AUbkxj7G5CaMTdw6xkKthV22EntD3lxaQxRKzQTfCc2d/CC1K4ushCcRs1S6SQ2zJ2jDq1UmOUkDMgvNh4JVhJYSKc6mu8i3s7oGSmBado5wvtlpSzMrscOpf8Qe/wmT5fH12KB9ipJqoFNQMVbVcVarE/v6wpn3GZC62YRb5iaz9/M+t92Qhu50W2u+KfouqtKB2lwIDDKZMww38ExtdMouh2FZpxaoh4Uey5bRp3tM3JgnWcX6fyUOp2gxJRPIlD9rrZhX5IkEkZM8MQbdPTQLgIf98oFph5RG6w1t02BvI9nJKM7KkKEfBHt this_is_a_very_long_comment__this_is_a_very_long_comment__this_is_a_very_long_comment__this_is_a_very_long_comment__this_is_a_very_long_comment

=== modified file 'src/maasserver/tests/test_models.py'
--- src/maasserver/tests/test_models.py	2012-03-28 14:01:18 +0000
+++ src/maasserver/tests/test_models.py	2012-03-28 14:01:19 +0000
@@ -576,6 +576,21 @@
         self.assertRaises(
             ValidationError, key.full_clean)
 
+    def test_sshkey_display_if_comment_size_limited(self):
+        key_string = get_data('data/test_rsa.pub')
+        user = factory.make_user()
+        key = SSHKey(key=key_string, user=user)
+        display = key.display_html()
+        self.assertEqual(
+            'ssh-rsa AAAA&hellip; ubuntu@server-7476', display)
+
+    def test_sshkey_display_if_very_long_comment(self):
+        key_string = get_data('data/test_rsa_long_comment.pub')
+        user = factory.make_user()
+        key = SSHKey(key=key_string, user=user)
+        display = key.display_html()
+        self.assertEqual('ssh-rsa AAAAB3NzaC1yc2EAAAADAQ&hellip;', display)
+
 
 class SSHKeyManagerTest(TestCase):
     """Testing for the :class:`SSHKeyManager` model manager."""