Commit 70d4e6d2 by Scott Duckworth

define UserKey.validate_unique()

parent 73e7b814
......@@ -22,3 +22,22 @@ class UserKey(models.Model):
self.fingerprint = sshkey_fingerprint(self.key)
except Exception, e:
raise ValidationError('Not a valid SSH key: ' + str(e))
def validate_unique(self, exclude=None):
if is None:
objects = type(self).objects
objects = type(self).objects.exclude(
if exclude is None or 'name' not in exclude:
if objects.filter(user=self.user,
raise ValidationError({'name': ['A key with this name already exists']})
if exclude is None or 'key' not in exclude:
other = objects.get(fingerprint=self.fingerprint, key=self.key)
if self.user == other.user:
message = 'You already have that key on file (%s)' %
message = 'Somebody else already has that key on file'
raise ValidationError({'key': [message]})
except type(self).DoesNotExist:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment