root / ui / userdata / models.py @ c72a830d
History | View | Annotate | Download (993 Bytes)
1 |
from django.db import models |
---|---|
2 |
from django.conf import settings |
3 |
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS |
4 |
|
5 |
from synnefo.db import models as synnefo_models |
6 |
|
7 |
User = synnefo_models.SynnefoUser |
8 |
|
9 |
class ProfileModel(models.Model): |
10 |
"""
|
11 |
Abstract model, provides a basic interface for models that store
|
12 |
user specific information
|
13 |
"""
|
14 |
|
15 |
user = models.ForeignKey(User) |
16 |
|
17 |
class Meta: |
18 |
abstract = True
|
19 |
app_label = 'userdata'
|
20 |
|
21 |
|
22 |
class PublicKeyPair(ProfileModel): |
23 |
"""
|
24 |
Public key model
|
25 |
"""
|
26 |
name = models.CharField(max_length=255, null=False, blank=False) |
27 |
content = models.TextField() |
28 |
|
29 |
class Meta: |
30 |
app_label = 'userdata'
|
31 |
|
32 |
def clean(self): |
33 |
if PublicKeyPair.user_limit_exceeded(self.user): |
34 |
raise ValidationError("SSH keys limit exceeded.") |
35 |
|
36 |
@classmethod
|
37 |
def user_limit_exceeded(cls, user): |
38 |
return PublicKeyPair.objects.filter(user=user).count() >= settings.MAX_SSH_KEYS_PER_USER
|