# -*- coding: utf-8 -*-

from django import forms
from main.models import MyUser
import re

class AddUserForm(forms.ModelForm):
    password2 = forms.CharField(max_length=128, label="Повтор", widget = forms.PasswordInput, required=False)
    
    class Meta:
        model = MyUser
        widgets = {
            'password':  forms.PasswordInput(render_value=True),
        }
        fields = ['userName', 'password', 'password2', 'firstName', 'lastName', 'active', 'expire', 'comment']
    
    def clean_password(self):
        pass1 = self.cleaned_data['password']
        pass2 = self.data['password2']
        
        if not re.match('^[A-Za-z0-9]{5}\$[A-Fa-f0-9]{32}$', pass1):
            if not re.match(r'^[A-Za-z0-9_]*$', pass1):
                raise forms.ValidationError("Недопустимые сиволы в пароле. Доступны цифры, латинские буквы и знак подчеркивания.")
            if pass1 != pass2:
                raise forms.ValidationError("Указанные пароли не совпадают.")
        return pass1

class AuthForm(forms.Form):
    username = forms.CharField(label="Имя пользователя", max_length=50)
    password = forms.CharField(label="Пароль", widget=forms.PasswordInput)
    