1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
from django.http import HttpResponseRedirect, HttpResponse
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django import forms
from django.shortcuts import render_to_response, redirect
from django.utils.hashcompat import md5_constructor, sha_constructor
from django.forms import ModelForm
from django import forms
from models import Tout
import django.utils.simplejson as json
def index(request):
return render_to_response('index.html')
class Doctor(ModelForm):
class Meta:
model = Tout
widgets = {
'password': forms.PasswordInput(),'password2': forms.PasswordInput()
}
default_error_messages = {
'already exists': _(u'A user with that username already exists'),
'already exists': _(u'Already use , choise another please'),
'short_password': _(u'Password has to be at least 8 characters long'),
'is not the same password': _(u'The two password fields didn\'t match'),
'required': _(u'you must choice your sex.'),
'required': _('you must choice your countrie'),
'required': _(u'your town please.'),
'required': _('you must choice your speciality'),
}
def clean_username(self):
username = self.cleaned_data["username"]
try:
User.objects.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.default_error_messages['Already exists'])
def clean_email(self):
"""
Validates that an active user exists with the given e-mail address.
"""
email = self.cleaned_data["email"]
self.users_cache = User.objects.filter(
email__iexact=email,
is_active=True
)
if len(self.users_cache) == 1:
raise forms.ValidationError(self.default_error_messages['Already exists'])
return email
def clean_password(self):
password = self.cleaned_data['password']
length = len(password)
if length < 8:
raise forms.ValidationError(self.default_error_messages['short password'])
return password
def clean_password2(self):
# Make sure the new password and confirmation match
password = self.cleaned_data.get('password')
password2 = self.cleaned_data.get('password2')
if password != password2:
raise forms.ValidationError(self.default_error_messages['it is not the same password'])
return password2
def account(request):
if request.method == 'POST':
form_d = Doctor(request.POST)
if form_d.is_valid():
username = form_d.cleaned_data['username']
last_name = form_d.cleaned_data['last_name']
first_name = form_d.cleaned_data['first_name']
email = form_d.cleaned_data['email']
password = form_d.cleaned_data['password']
password2 = form_d.cleaned_data['password2']
sex = form_d.cleaned_data['sex']
country = form_d.cleaned_data['country']
town = form_d.cleaned_data['town']
speciality = form_d.cleaned_data['speciality']
form_d.save()
if request.is_ajax():
return HttpResponse(content=json.dumps({'success' : '/success'}), mimetype='application/json')
return redirect('success') # Redirect after POST
elif request.is_ajax():
errors = json.dumps(forms.errors)
return HttpResponse(errors, mimetype='application/json')
else:
form_d = Doctor()
return render_to_response('account.html', {'form_d':form_d})
def clean_password(self):
password = self.cleaned_data['password']
length = len(password)
if length < 8:
raise forms.ValidationError("Password has to be at least 8 characters long.")
return password
def success(request):
return render_to_response('success.html', {'success' : success}, context_instance=RequestContext(request)) |
Partager