From 39c36eb2235d9a39b50321eb132e4706b6484cd6 Mon Sep 17 00:00:00 2001 From: Gena Date: Wed, 17 Jun 2015 00:20:51 +0600 Subject: [PATCH] auto --- accounts/admin.py | 2 +- accounts/forms.py | 36 +++++++++++- .../migrations/0003_auto_20150616_2302.py | 24 ++++++++ accounts/models.py | 4 +- accounts/urls.py | 13 +++-- accounts/utils.py | 7 +++ accounts/views.py | 58 ++++++++++++++++++- batiskaf/settings.py | 2 + .../templates/jinja2/accounts/index.jinja | 52 +++++++++++++++++ .../templates/jinja2/accounts/login.jinja | 34 ++++++++++- .../templates/jinja2/accounts/login_sms.jinja | 37 ++++++++++++ .../jinja2/accounts/order_detail.jinja | 55 ++++++++++++++++++ batiskaf/templates/jinja2/base.jinja | 23 ++++++-- batiskaf/templates/jinja2/cart_detail.jinja | 22 +++---- store/migrations/0019_orderdata_profile.py | 21 +++++++ store/migrations/0020_auto_20150616_2302.py | 25 ++++++++ store/migrations/0021_orderdata_status.py | 19 ++++++ store/models.py | 25 +++++++- store/views.py | 39 +++++++++++-- 19 files changed, 465 insertions(+), 33 deletions(-) create mode 100644 accounts/migrations/0003_auto_20150616_2302.py create mode 100644 accounts/utils.py create mode 100644 batiskaf/templates/jinja2/accounts/index.jinja create mode 100644 batiskaf/templates/jinja2/accounts/login_sms.jinja create mode 100644 batiskaf/templates/jinja2/accounts/order_detail.jinja create mode 100644 store/migrations/0019_orderdata_profile.py create mode 100644 store/migrations/0020_auto_20150616_2302.py create mode 100644 store/migrations/0021_orderdata_status.py diff --git a/accounts/admin.py b/accounts/admin.py index 0d0327a..2b60e53 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -58,7 +58,7 @@ class ProfileAdmin(UserAdmin): add_fieldsets = ( (None, { 'classes': ('wide',), - 'fields': ('phone', 'email', 'first_name', 'last_name', 'password1', 'password2')} + 'fields': ('phone', 'email', 'first_name', 'last_name', 'password1', 'password2', 'temp_password')} ), ) search_fields = ('phone', 'email',) diff --git a/accounts/forms.py b/accounts/forms.py index 6f59cf3..8708ff2 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -1,4 +1,38 @@ from django import forms +from accounts.models import Profile +from random import randint +from accounts.utils import normalize_phone + class LoginForm(forms.Form): - phone = forms.CharField(label='Телефон', max_length=15) \ No newline at end of file + phone = forms.CharField(label='Номер мобильного телефона', max_length=45, required=True) + + def clean_phone(self): + data = normalize_phone(self.cleaned_data['phone']) + try: + profile = Profile.objects.get(phone=data) + profile.temp_password = randint(1000000, 9999999) + self.temp_password = profile.temp_password + profile.save() + except Profile.DoesNotExist: + raise forms.ValidationError("Вы еще не совершали покупки в нашем магазине") + return data + + + +class LoginSmsForm(forms.Form): + sms = forms.IntegerField(label='Одноразовый пароль', required=True) + + def clean_sms(self): + data = self.cleaned_data['sms'] + phone = normalize_phone(self.phone) + try: + profile = Profile.objects.get(phone=phone, temp_password=data) + self.profile = profile + #profile.temp_password = randint(1000000, 9999999) + #self.temp_password = profile.temp_password + #profile.save() + except Profile.DoesNotExist: + raise forms.ValidationError("Неверный одноразовый пароль") + return data + diff --git a/accounts/migrations/0003_auto_20150616_2302.py b/accounts/migrations/0003_auto_20150616_2302.py new file mode 100644 index 0000000..ea2ccf2 --- /dev/null +++ b/accounts/migrations/0003_auto_20150616_2302.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0002_auto_20150611_2306'), + ] + + operations = [ + migrations.AddField( + model_name='profile', + name='temp_password', + field=models.IntegerField(blank=True, null=True, verbose_name='Одноразовый пароль', default=None), + ), + migrations.AlterField( + model_name='profile', + name='phone', + field=models.CharField(verbose_name='Номер мобильного телефона', max_length=15, unique=True, db_index=True), + ), + ] diff --git a/accounts/models.py b/accounts/models.py index b3688fa..535c431 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -28,13 +28,15 @@ class ProfileManager(BaseUserManager): class Profile(AbstractBaseUser): - phone = models.CharField('Телефон', max_length=15, unique=True, db_index=True) + phone = models.CharField('Номер мобильного телефона', max_length=15, unique=True, db_index=True) email = models.EmailField('Email', blank=False, null=False, default=None, unique=True, db_index=True) first_name = models.CharField('Имя', max_length=30, blank=True) last_name = models.CharField('Фамилия', max_length=30, blank=True) date_joined = models.DateTimeField('Регистрация', default=datetime.now) is_superuser = models.BooleanField('Админ', default=False) is_active = models.BooleanField('Активный', default=True, db_index=True) + temp_password = models.IntegerField('Одноразовый пароль', default=None, blank=True, null=True) + USERNAME_FIELD = 'phone' REQUIRED_FIELDS = ['email'] diff --git a/accounts/urls.py b/accounts/urls.py index db313f2..0046f67 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -3,8 +3,13 @@ from django.views.generic import RedirectView from .views import * urlpatterns = patterns('', - url(r'^$', RedirectView.as_view( - url='/', permanent=True), name='accounts_index'), - url(r'^login/$', LoginView.as_view(), + url(r'^$', account_index, name='accounts_index'), + url(r'^order/(?P\d+)/$', order_detail, name='account_order_detail'), + url(r'^login/$', login_view, name='accounts_login'), - ) + url(r'^login/sms/$', login_sms_view, + name='accounts_login_sms'), + url(r'^logout/$', account_logout, + name='accounts_logout'), + +) \ No newline at end of file diff --git a/accounts/utils.py b/accounts/utils.py new file mode 100644 index 0000000..b44c918 --- /dev/null +++ b/accounts/utils.py @@ -0,0 +1,7 @@ +import re + +def normalize_phone(phone): + retval = re.sub("\D", "", phone) + if len(retval) > 10: + retval = retval[len(retval)-10:] + return retval \ No newline at end of file diff --git a/accounts/views.py b/accounts/views.py index b1a284d..22807ef 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -1,7 +1,61 @@ -from django.shortcuts import render +from django.contrib.auth.decorators import login_required +from django.shortcuts import render, get_object_or_404, redirect from django.views.generic import FormView from .forms import * +from store.models import OrderData +from django.contrib.auth import logout, authenticate, login +from .utils import normalize_phone +from random import randint +import requests +from django.conf import settings class LoginView(FormView): form_class = LoginForm - template_name = 'accounts/login.jinja' \ No newline at end of file + template_name = 'accounts/login.jinja' + +def login_view(request): + form = LoginForm(request.POST or None) + if form.is_valid(): + phone = form.cleaned_data['phone'] + params = dict( + login='Jango.kz', + psw='AcEMXtLGz042Fc1ZJUSl', + phones='7{}'.format(phone), + mes='Batiskaf.kz odnorazoviy parol: {}'.format(form.temp_password) + ) + requests.get('http://smsc.ru/sys/send.php', params=params) + return redirect('/account/login/sms/?phone='+phone) + c = dict(form=form) + return render(request, 'accounts/login.jinja', c) + +def login_sms_view(request): + form = LoginSmsForm(request.POST or None) + phone = request.GET.get('phone', None) + form.phone = phone + if form.is_valid(): + profile = form.profile + profile.set_password(settings.PROFILE_TEMP_PASSWORD) + profile.save() + user = authenticate(username=profile.phone, password=settings.PROFILE_TEMP_PASSWORD) + if user.is_active: + login(request, user) + return redirect('/account/') + + c = dict(form=form) + return render(request, 'accounts/login_sms.jinja', c) + +@login_required +def account_index(request): + return render(request, 'accounts/index.jinja') + +@login_required +def account_logout(request): + logout(request) + return redirect('/') + +@login_required +def order_detail(request, pk): + order = get_object_or_404(OrderData, pk=pk, profile=request.user) + c = dict(order=order) + return render(request, 'accounts/order_detail.jinja', c) + diff --git a/batiskaf/settings.py b/batiskaf/settings.py index 0d33a4a..0cea3be 100644 --- a/batiskaf/settings.py +++ b/batiskaf/settings.py @@ -255,3 +255,5 @@ THUMBNAIL_PROCESSORS = ( 'easy_thumbnails.processors.scale_and_crop', 'easy_thumbnails.processors.background', ) + +PROFILE_TEMP_PASSWORD = 'dE6Hyo9heWck5yiM2dIs' \ No newline at end of file diff --git a/batiskaf/templates/jinja2/accounts/index.jinja b/batiskaf/templates/jinja2/accounts/index.jinja new file mode 100644 index 0000000..aa5d3de --- /dev/null +++ b/batiskaf/templates/jinja2/accounts/index.jinja @@ -0,0 +1,52 @@ +{% extends 'base.jinja' %} +{% block title %} + Личный кабинет +{% endblock %} + +{% block content %} + +

Ваши заказы, {{ request.user.first_name }}:

+ +
+
+
+
+ + + + + + + + + + + + + {% for order in request.user.orders.all() %} + + + + + + + + + + {% endfor %} + +
Номер заказаДатаКому/КудаСпособ доставкиСуммаСтатус
{{ order.pk }}{{ order.created.strftime('%d.%m.%Y') }}{{ order.first_name }} {{ order.last_name }}
+ г. {{ order.get_city_display() }}
+ {{ order.address }}
{{ order.get_deliv_type_display() }}{{ order.amount }}{{ order.get_status_display() }}
+
+
+
+{% endblock %} + diff --git a/batiskaf/templates/jinja2/accounts/login.jinja b/batiskaf/templates/jinja2/accounts/login.jinja index f51e4ed..d589903 100644 --- a/batiskaf/templates/jinja2/accounts/login.jinja +++ b/batiskaf/templates/jinja2/accounts/login.jinja @@ -1,4 +1,36 @@ {% extends 'base.jinja' %} +{% block title %} + Вход в личный кабинет +{% endblock %} {% block content %} -{{ form }} + +

Вход в личный кабинет


+
+
+

Введите номер телефона

+ +

Если вы уже совершали покупки в нашем интернет-магазине, на ваш номер телефона будет отправлено SMS-сообщение с одноразовым паролем для входа.

+ + +
+
+ +
+
{% endblock %} diff --git a/batiskaf/templates/jinja2/accounts/login_sms.jinja b/batiskaf/templates/jinja2/accounts/login_sms.jinja new file mode 100644 index 0000000..6886f83 --- /dev/null +++ b/batiskaf/templates/jinja2/accounts/login_sms.jinja @@ -0,0 +1,37 @@ +{% extends 'base.jinja' %} +{% block title %} + Ввод одноразового пароля +{% endblock %} +{% block content %} + +

Ввод одноразового пароля


+
+
+

Проверьте телефон

+ +

На Ваш номер было отправлено SMS-сообщение с одноразовым паролем для входа. Введите его в поле ниже.

+ + +
+
+
+
+ {{ form|bootstrap }} +
+ +
+
+
+
+
+{% endblock %} diff --git a/batiskaf/templates/jinja2/accounts/order_detail.jinja b/batiskaf/templates/jinja2/accounts/order_detail.jinja new file mode 100644 index 0000000..993dae1 --- /dev/null +++ b/batiskaf/templates/jinja2/accounts/order_detail.jinja @@ -0,0 +1,55 @@ +{% extends 'base.jinja' %} +{% block title %} + Заказ номер {{ order.pk }} +{% endblock %} +{% block content %} + +

Заказ №{{ order.pk }}

+ +
+
+
+ + + + + + + + + + + {% for item, count in order.get_items() %} + + + + + + + {% endfor %} + + +
ФотоТоварСтоимостьКоличество
+ {% set im = item.product.main_image()|thumbnail("80x80") %} + {{ item.product.title }} + + {{ item.product.title }}
+ {{ item.variation }} +
{{ item.get_price() }} ₸{{ count }} шт. + +
+
+
+{% endblock %} + diff --git a/batiskaf/templates/jinja2/base.jinja b/batiskaf/templates/jinja2/base.jinja index f10a8d5..41ea593 100644 --- a/batiskaf/templates/jinja2/base.jinja +++ b/batiskaf/templates/jinja2/base.jinja @@ -4,7 +4,8 @@ - {% block title %}Интернет-Магазин Батискаф-Казахстан{% endblock %} | Батискаф-Казахстан, Батискаф, Снаряжение для дайвинга, Снаряжение для подводной охоты, Интернет-Магазин снаряжения для дайвинга и подводной охоты + {% block title %}Интернет-Магазин Батискаф-Казахстан{% endblock %} | Батискаф-Казахстан, Батискаф, Снаряжение + для дайвинга, Снаряжение для подводной охоты, Интернет-Магазин снаряжения для дайвинга и подводной охоты @@ -82,7 +83,7 @@ Написать нам | Написать в Skype + src="/static/img/skype.png" alt="Написать в Skype" title="Написать в Skype" width="20" height="20"/> @@ -92,7 +93,8 @@
- Батискаф-Казахстан + Батискаф-Казахстан
@@ -102,10 +104,19 @@

Добро пожаловать в интернет-магазин Батискаф!

- + {{ (request|cart).items|length }}{% endif %} + + {% if request.user.is_authenticated() %} + + + | Выйти + {% else %} + | Войти + {% endif %} +

diff --git a/batiskaf/templates/jinja2/cart_detail.jinja b/batiskaf/templates/jinja2/cart_detail.jinja index 9eb974a..23f9125 100644 --- a/batiskaf/templates/jinja2/cart_detail.jinja +++ b/batiskaf/templates/jinja2/cart_detail.jinja @@ -1,6 +1,6 @@ {% extends 'base.jinja' %} {% block title %} -Корзина товаров + Корзина товаров {% endblock %} {% block content %}