diff --git a/project/callback/forms.py b/project/callback/forms.py index e10254b..3e02291 100644 --- a/project/callback/forms.py +++ b/project/callback/forms.py @@ -1,11 +1,15 @@ # -*- coding: utf-8 -*- from django import forms +from captcha.fields import CaptchaField + from . import models class ReqAvailForm(forms.ModelForm): """Форма заказа товара.""" + captcha = CaptchaField(label=u'Введите код с картинки') + class Meta: model = models.ReqAvail fields = ('name', 'phone', 'message') @@ -18,14 +22,22 @@ class ReqAvailForm(forms.ModelForm): self.fields['name'].widget.attrs['width'] = u'Ваше имя, отчество' def clean(self): - super(ReqAvailForm, self).clean() - name = self.cleaned_data.get('name', '').strip() - phone = self.cleaned_data.get('phone', '').strip() - - if not name: - raise forms.ValidationError(u'Укажите Ваше имя.') - - if not phone: - raise forms.ValidationError(u'Укажите Ваш телефон.') + cleaned_data = super(ReqAvailForm, self).clean() + if (cleaned_data.get('name') and cleaned_data.get('message') and cleaned_data.get('phone')) and not(cleaned_data.get('captcha')): + raise forms.ValidationError(u"Проверьте правильность кода проверки") + elif not (cleaned_data.get('name') and cleaned_data.get('message') and cleaned_data.get('phone') and cleaned_data.get('captcha')): + if self.errors.get('phone') and self.data.get('phone'): + raise forms.ValidationError(u"Проверьте правильность ввода контактов") + else: + raise forms.ValidationError(u"Заполните все поля") + return cleaned_data + #name = self.cleaned_data.get('name', '').strip() + #phone = self.cleaned_data.get('phone', '').strip() + + #if not name: + # raise forms.ValidationError(u'Укажите Ваше имя.') + + #if not phone: + # raise forms.ValidationError(u'Укажите Ваш телефон.') return self.cleaned_data diff --git a/project/myauth/forms.py b/project/myauth/forms.py index 6ce7989..6599fc6 100644 --- a/project/myauth/forms.py +++ b/project/myauth/forms.py @@ -2,10 +2,12 @@ from django import forms from django.conf import settings from django.contrib.auth import authenticate +from django.contrib.auth.forms import UserCreationForm, UserChangeForm + +from captcha.fields import CaptchaField from project.commons.forms import set_field_error from project.customer import consts as customer_consts -from django.contrib.auth.forms import UserCreationForm, UserChangeForm from models import DokUser @@ -182,6 +184,9 @@ class LoginForm(forms.Form): return self.user_cache +class CaptchedLoginForm(LoginForm): + captcha = CaptchaField(label=u'Введите код с картинки') + class CustomUserCreationForm(UserCreationForm): """ diff --git a/project/myauth/views.py b/project/myauth/views.py index 2e94dec..dfd98c5 100644 --- a/project/myauth/views.py +++ b/project/myauth/views.py @@ -209,11 +209,16 @@ def change_email(request): @csrf_protect def login(request): """Вход в систему.""" - form_class = forms.LoginForm + if request.session.get('login_count', None): + request.session['login_count'] += 1 + else: + form_class = forms.LoginForm + request.session['login_count'] = 0 + if request.session['login_count'] > 0: + form_class = forms.CaptchedLoginForm form_prefix = 'login' template_name = 'myauth/login.html' success_url = 'customer_index' - if request.method == 'POST': form = form_class(data=request.POST, prefix=form_prefix) if form.is_valid(): @@ -221,14 +226,12 @@ def login(request): old_session_key = request.user.profile.user_session_key request.user.profile.user_session_key = request.session.session_key request.user.profile.save() - #try: - # session = session.objects.get(session_key=old_session_key) - # session.delete() - #except: - # pass + del request.session['login_count'] if request.user.profile.check_name_not_filled(): success_url = 'customer_profile_edit' return redirect(success_url) + else: + request.session['login_count'] += 1 else: form = form_class(prefix=form_prefix) diff --git a/project/settings.py b/project/settings.py index 15cb498..31aed89 100644 --- a/project/settings.py +++ b/project/settings.py @@ -272,6 +272,8 @@ CELERYBEAT_SCHEDULE = { }, } +CAPTCHA_OUTPUT_FORMAT = u"%(hidden_field)s%(text_field)s %(image)s" + try: from project.local_settings import * except ImportError: diff --git a/project/static/img/refresh.png b/project/static/img/refresh.png new file mode 100644 index 0000000..d510628 Binary files /dev/null and b/project/static/img/refresh.png differ diff --git a/project/static/js/commons.js b/project/static/js/commons.js index 2cd5571..68e2522 100644 --- a/project/static/js/commons.js +++ b/project/static/js/commons.js @@ -1,5 +1,18 @@ $(document).ready(function() { $('.has-datepicker').datepicker({dateFormat: 'dd.mm.yy'}); + + $('.captcha_refresh').click(function(e) { + e.preventDefault(); + var $form = $(this).parents('form'); + var url = location.protocol + "//" + window.location.hostname + ":" + + location.port + "/captcha/refresh/"; + + $.getJSON(url, {}, function(json) { + $form.find('input[name="captcha_1"]').val(''); + $form.find('input[name="captcha_0"]').val(json.key); + $form.find('img.captcha').attr('src', json.image_url); + }); + }); }); function fetch_data(url, async) { diff --git a/project/static/js/dialogs.js b/project/static/js/dialogs.js index 0845795..e0bcd24 100644 --- a/project/static/js/dialogs.js +++ b/project/static/js/dialogs.js @@ -61,6 +61,10 @@ $(document).ready(function() { else { // process form errors if (data.form_errors) { + var captcha = $(form).find('img.captcha'); + if (captcha) { + $(form).find('a.captcha_refresh').click(); + } var errors = $('.errors-layout', form); var html = '