diff --git a/accounts/forms.py b/accounts/forms.py index 8c05b75..e3bcd42 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,9 +1,10 @@ +from appsettings.settings import app_settings +from django.contrib.auth import get_user_model from django.forms import ModelForm, ValidationError from django.utils.translation import ugettext_lazy as _ -from appsettings.models import AppSettings - -from .models import UserInstance +from .models import UserInstance, UserSSHKey +from .utils import validate_ssh_key class UserInstanceForm(ModelForm): @@ -18,7 +19,7 @@ class UserInstanceForm(ModelForm): def clean_instance(self): instance = self.cleaned_data['instance'] - if AppSettings.objects.get(key="ALLOW_INSTANCE_MULTIPLE_OWNER").value == 'False': + if app_settings.ALLOW_INSTANCE_MULTIPLE_OWNER == 'False': exists = UserInstance.objects.filter(instance=instance) if exists: raise ValidationError(_('Instance owned by another user')) @@ -28,3 +29,43 @@ class UserInstanceForm(ModelForm): class Meta: model = UserInstance fields = '__all__' + + +class ProfileForm(ModelForm): + class Meta: + model = get_user_model() + fields = ('first_name', 'last_name', 'email') + + +class UserSSHKeyForm(ModelForm): + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user', None) + self.publickeys = UserSSHKey.objects.filter(user=self.user) + super().__init__(*args, **kwargs) + + def clean_keyname(self): + for key in self.publickeys: + if self.cleaned_data['keyname'] == key.keyname: + raise ValidationError(_("Key name already exist")) + + return self.cleaned_data['keyname'] + + def clean_keypublic(self): + for key in self.publickeys: + if self.cleaned_data['keypublic'] == key.keypublic: + raise ValidationError(_("Public key already exist")) + + if not validate_ssh_key(self.cleaned_data['keypublic']): + raise ValidationError(_('Invalid key')) + return self.cleaned_data['keypublic'] + + def save(self, commit=True): + ssh_key = super().save(commit=False) + ssh_key.user = self.user + if commit: + ssh_key.save() + return ssh_key + + class Meta: + model = UserSSHKey + fields = ('keyname', 'keypublic') diff --git a/accounts/models.py b/accounts/models.py index 33b120c..15e0ddf 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -1,9 +1,7 @@ -from django.conf import settings from django.contrib.auth.models import User from django.core.validators import MinValueValidator from django.db import models from django.utils.translation import ugettext_lazy as _ - from instances.models import Instance @@ -11,6 +9,7 @@ class UserInstanceManager(models.Manager): def get_queryset(self): return super().get_queryset().select_related('instance', 'user') + class UserInstance(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) instance = models.ForeignKey(Instance, on_delete=models.CASCADE) diff --git a/accounts/templates/account.html b/accounts/templates/account.html index 2deacf8..54e3ad9 100644 --- a/accounts/templates/account.html +++ b/accounts/templates/account.html @@ -5,20 +5,15 @@ {% load qr_code %} {% block title %}{% trans "User Profile" %} - {{ user }}{% endblock %} +{% block page_header %}{% trans "User Profile" %}: {{ user }}{% endblock page_header %} + +{% block page_header_extra %} + + {% icon 'plus' %} + +{% endblock page_header_extra %} + {% block content %} - -
-
- - {% icon 'plus' %} - - -
-
- - - {% include 'errors_block.html' %} -