X-Git-Url: https://git.mdrn.pl/cas.git/blobdiff_plain/5c901ee485c3abdf11dec71bdb7534f1f6a9eaa9..7a5433b8c3b904e06fcf8ac3533ea8297e5c7b2b:/src/ssh_keys/models.py?ds=sidebyside diff --git a/src/ssh_keys/models.py b/src/ssh_keys/models.py index 960a5ec..e4841ca 100644 --- a/src/ssh_keys/models.py +++ b/src/ssh_keys/models.py @@ -1,11 +1,32 @@ from django.conf import settings from django.db import models +from django.utils.translation import ugettext_lazy as _ +from .utils import get_key_details class SSHKey(models.Model): - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) - key = models.TextField() - created_at = models.DateTimeField(auto_now_add=True) + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name=_('user')) + key = models.TextField(_('key')) + comment = models.CharField(_('comment'), max_length=255, editable=False) + algorithm = models.CharField(_('algorithm'), max_length=32, editable=False) + bit_length = models.IntegerField(_('bit length'), null=True, editable=False) + md5_hash = models.CharField(_('MD5 hash'), max_length=128, editable=False) + created_at = models.DateTimeField(_('created at'), auto_now_add=True) + last_seen_at = models.DateTimeField(_('last seen at'), null=True, editable=False) class Meta: ordering = ['created_at'] + verbose_name = _('SSH key') + verbose_name_plural = _('SSH keys') + unique_together = [('algorithm', 'bit_length', 'md5_hash')] + + def __str__(self): + return self.comment + + def save(self, *args, **kwargs): + det = get_key_details(self.key) + self.comment = det['comment'][:255] + self.algorithm = det['algo'] + self.bit_length = det['bits'] + self.md5_hash = det['md5'] + return super().save(*args, **kwargs)