from django.utils.translation import gettext_lazy as _ from django import forms from .models import SharedPassword class AuthForm(forms.Form): pwdb_check = forms.CharField(widget=forms.PasswordInput, label="") class NewSharedPasswordForm(forms.ModelForm): name = forms.CharField(max_length=512, label=_("Name")) url = forms.CharField( max_length=512, widget=forms.URLInput, required=False, label="URL" ) description = forms.CharField( widget=forms.Textarea, required=False, label=_("Description") ) password = forms.CharField( max_length=1024, widget=forms.PasswordInput, label=_("Password") ) def save_m2m(self): pass def save(self, commit=True): if self.errors: raise ValueError( "The %s could not be %s because the data didn't validate." % ( self.instance._meta.object_name, "created" if self.instance._state.adding else "changed", ) ) password = SharedPassword.new( self.cleaned_data["name"], self.cleaned_data["password"] ) if self.cleaned_data["url"]: password.url = self.cleaned_data["url"] if self.cleaned_data["description"]: password.description = self.cleaned_data["description"] password.save() return password class Meta: model = SharedPassword exclude = ["iv", "encrypted_password"] class EditSharedPasswordForm(forms.ModelForm): name = forms.CharField(max_length=512, label=_("Name")) password = forms.CharField( max_length=1024, widget=forms.PasswordInput, required=False, label=_("Password") ) def save_m2m(self): pass def save(self, commit=True): if self.errors: raise ValueError( "The %s could not be %s because the data didn't validate." % ( self.instance._meta.object_name, "created" if self.instance._state.adding else "changed", ) ) password = self.instance if self.cleaned_data["password"]: password.set_password(self.cleaned_data["password"]) password.save() return password class Meta: model = SharedPassword exclude = ["iv", "encrypted_password"]