1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 15:45:23 +00:00
webvirtcloud/accounts/forms.py

76 lines
2.4 KiB
Python
Raw Permalink Normal View History

2020-10-14 08:37:46 +00:00
from appsettings.settings import app_settings
from django.contrib.auth import get_user_model
2020-10-19 08:26:08 +00:00
from django.forms import EmailField, Form, ModelForm, ValidationError
from django.utils.translation import gettext_lazy as _
2020-06-16 12:35:08 +00:00
2020-10-14 08:37:46 +00:00
from .models import UserInstance, UserSSHKey
from .utils import validate_ssh_key
2020-06-16 12:35:08 +00:00
class UserInstanceForm(ModelForm):
def __init__(self, *args, **kwargs):
super(UserInstanceForm, self).__init__(*args, **kwargs)
# Make user and instance fields not editable after creation
2022-11-02 05:54:35 +00:00
instance = getattr(self, "instance", None)
2020-06-16 12:35:08 +00:00
if instance and instance.id is not None:
2022-11-02 05:54:35 +00:00
self.fields["user"].disabled = True
self.fields["instance"].disabled = True
2020-06-16 12:35:08 +00:00
def clean_instance(self):
2022-11-02 05:54:35 +00:00
instance = self.cleaned_data["instance"]
if app_settings.ALLOW_INSTANCE_MULTIPLE_OWNER == "False":
2020-06-16 12:35:08 +00:00
exists = UserInstance.objects.filter(instance=instance)
if exists:
2022-11-02 05:54:35 +00:00
raise ValidationError(_("Instance owned by another user"))
2020-06-16 12:35:08 +00:00
return instance
class Meta:
model = UserInstance
2022-11-02 05:54:35 +00:00
fields = "__all__"
2020-10-14 08:37:46 +00:00
class ProfileForm(ModelForm):
class Meta:
model = get_user_model()
2022-11-02 05:54:35 +00:00
fields = ("first_name", "last_name", "email")
2020-10-14 08:37:46 +00:00
class UserSSHKeyForm(ModelForm):
def __init__(self, *args, **kwargs):
2022-11-02 05:54:35 +00:00
self.user = kwargs.pop("user", None)
2020-10-14 08:37:46 +00:00
self.publickeys = UserSSHKey.objects.filter(user=self.user)
super().__init__(*args, **kwargs)
def clean_keyname(self):
for key in self.publickeys:
2022-11-02 05:54:35 +00:00
if self.cleaned_data["keyname"] == key.keyname:
2020-10-14 08:37:46 +00:00
raise ValidationError(_("Key name already exist"))
2022-11-02 05:54:35 +00:00
return self.cleaned_data["keyname"]
2020-10-14 08:37:46 +00:00
def clean_keypublic(self):
for key in self.publickeys:
2022-11-02 05:54:35 +00:00
if self.cleaned_data["keypublic"] == key.keypublic:
2020-10-14 08:37:46 +00:00
raise ValidationError(_("Public key already exist"))
2022-11-02 05:54:35 +00:00
if not validate_ssh_key(self.cleaned_data["keypublic"]):
raise ValidationError(_("Invalid key"))
return self.cleaned_data["keypublic"]
2020-10-14 08:37:46 +00:00
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
2022-11-02 05:54:35 +00:00
fields = ("keyname", "keypublic")
2020-10-19 08:26:08 +00:00
class EmailOTPForm(Form):
2022-11-02 05:54:35 +00:00
email = EmailField(label=_("Email"))