You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.8 KiB
103 lines
3.8 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.contrib.auth.forms import ReadOnlyPasswordHashField
|
|
from models import User, TranslatorProfile
|
|
from country.models import Country
|
|
from city.models import City
|
|
from company.models import Company
|
|
from organiser.models import Organiser
|
|
#functions
|
|
from functions.form_check import translit_with_separator
|
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
password1 = forms.CharField(label='Пароль', widget=forms.PasswordInput(render_value=False))
|
|
password2 = forms.CharField(label='Повторите пароль', widget=forms.PasswordInput(render_value=False))
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ('email', 'first_name', 'last_name')
|
|
|
|
def clean_email(self):
|
|
"""
|
|
checking if user already exist
|
|
"""
|
|
email = self.cleaned_data.get('email')
|
|
try:
|
|
User.objects.get(email=email)
|
|
except User.DoesNotExist:
|
|
return email
|
|
raise forms.ValidationError('Пользователь с таким email уже существует')
|
|
|
|
def clean_password2(self):
|
|
password1 = self.cleaned_data.get('password1')
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if password1 and password2 and password1 != password2:
|
|
raise forms.ValidationError('Пароли не совпадают')
|
|
return password2
|
|
|
|
|
|
def save(self, commit=True):
|
|
user = super(UserCreationForm, self).save(commit=False)
|
|
user.set_password(self.cleaned_data['password2'])
|
|
|
|
if commit:
|
|
user.save()
|
|
|
|
return user
|
|
|
|
class UserChangeForm(forms.ModelForm):
|
|
password = ReadOnlyPasswordHashField()
|
|
|
|
class Meta:
|
|
model = User
|
|
|
|
def clean_password(self):
|
|
return self.initial['password']
|
|
|
|
class UserForm(forms.ModelForm):
|
|
country = forms.ModelChoiceField(label='Страна', queryset=Country.objects.all(), empty_label=None, required=False)
|
|
city = forms.ModelChoiceField(label='Город', queryset=City.objects.all(), empty_label=None, required=False)
|
|
company = forms.ModelChoiceField(label='Компания', queryset=Company.objects.all(), empty_label=None, required=False)
|
|
organiser = forms.ModelChoiceField(label='Организатор', queryset=Organiser.objects.all(), empty_label=None, required=False)
|
|
title = forms.CharField(widget=forms.TextInput(attrs={'style':'width: 550px'}), required=False)
|
|
descriptions = forms.CharField(widget=forms.TextInput(attrs={'style':'width: 550px'}), required=False)
|
|
keywords = forms.CharField(widget=forms.TextInput(attrs={'style':'width: 550px'}), required=False)
|
|
|
|
class Meta:
|
|
model = User
|
|
exclude = ('last_login', 'password', 'is_active', 'is_admin', 'is_superuser', 'is_staff'
|
|
'date_joined', 'date_registered', 'date_modified')
|
|
|
|
|
|
def clean_phone(self):
|
|
"""
|
|
phone code checking
|
|
"""
|
|
cleaned_data = super(UserForm, self).clean()
|
|
phone = cleaned_data.get('phone')
|
|
if not phone:
|
|
return
|
|
|
|
deduct = ('-','(',')','.',' ')
|
|
for elem in deduct:
|
|
phone = phone.replace(elem, '')
|
|
if phone.isdigit():
|
|
return phone
|
|
else:
|
|
raise forms.ValidationError('Введите правильный код страны')
|
|
|
|
def clean_web_page(self):
|
|
cleaned_data = super(UserForm, self).clean()
|
|
web_page = cleaned_data.get('web_page')
|
|
import socket
|
|
try:
|
|
socket.getaddrinfo(web_page, 80)
|
|
return web_page
|
|
except:
|
|
return forms.ValidationError('Введите правильный адрес страници')
|
|
|
|
class TranslatorForm(forms.ModelForm):
|
|
class Meta:
|
|
model = TranslatorProfile
|
|
exclude = ('user') |