From 21620ebd4f1ba19ea11d25326cc8058ab89a011b Mon Sep 17 00:00:00 2001 From: Bachurin Sergey Date: Mon, 18 Aug 2014 16:07:05 +0300 Subject: [PATCH] admin fixes, 15 days to license end --- project/customer/admin.py | 2 +- project/customer/context_processors.py | 26 ++++++++++++++++++++++++++ project/customer/models.py | 22 +++++++++++++--------- project/myauth/views.py | 15 +++++++++------ project/settings.py | 1 + project/templates/base.html | 5 ++++- 6 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 project/customer/context_processors.py diff --git a/project/customer/admin.py b/project/customer/admin.py index 51ef866..3636430 100644 --- a/project/customer/admin.py +++ b/project/customer/admin.py @@ -11,7 +11,7 @@ class UserProfileAdmin(admin.ModelAdmin): form = forms.UserProfileAdminForm class LicenseAdmin(admin.ModelAdmin): - list_display = ('get_email', 'term', 'status', 'order_date', 'date_from', 'date_to') + list_display = ('get_company', 'term', 'status', 'order_date', 'date_from', 'date_to') list_display_links = list_display search_fields = ('user__email', ) diff --git a/project/customer/context_processors.py b/project/customer/context_processors.py new file mode 100644 index 0000000..b870091 --- /dev/null +++ b/project/customer/context_processors.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from datetime import datetime, timedelta +from django.core.cache import cache +from .models import License + + +def license_check_soon_ends(request): + try: + now = datetime.today() + license_15days = cache.get('license_15_%s' % (request.user.username,), None) + if license_15days is None: + licenses_ends = License.objects.filter(company=request.user.profile, date_to__gte=now - timedelta(15), status__in=[-1, 1, 2], deleted=False) + next_licenses = License.objects.filter(company=request.user.profile, date_from__gte=now - timedelta(15), status=1, deleted=False) + if licenses_ends and not next_licenses: + days_to_end = licenses_ends[0].date_to + cache.set('license_15_%s' % (request.user.username,), days_to_end) + license_15days = days_to_end + else: + license_15days = '' + + return { + #'license_15days': license_15days == 'Y' + 'license_15days': license_15days + } + except: + return { } diff --git a/project/customer/models.py b/project/customer/models.py index e40f4cc..543d00f 100644 --- a/project/customer/models.py +++ b/project/customer/models.py @@ -12,6 +12,7 @@ from django.db.models import Max from django.core.urlresolvers import reverse from . import consts, managers, utils +from project.myauth.models import DokUser PROFILE_IMAGES_UPLOAD_DIR = 'customer/profile/' # куда сохранять загруженные изображения @@ -167,7 +168,7 @@ class UserProfile(models.Model): def get_first_user(self): try: - first_user = DokUser.objects.filter(profile=self) + first_user = DokUser.objects.filter(profile=self)[0] return first_user except: return None @@ -234,7 +235,10 @@ class UserProfile(models.Model): return (u'%s %s' % (phone_code, self.phone,)).strip() def get_email(self): - return self.user.email + try: + return self.get_first_user().email + except: + return None def get_full_fax(self): """(Код города) Номер факса.""" @@ -405,7 +409,7 @@ class License(models.Model): def __unicode__(self): return u'%s - %s %s (%d %s)' % ( - self.user.profile.get_company_name(), + self.company.get_company_name(), self.term, numeral.choose_plural(self.term, u"месяц, месяца, месяцев"), self.pay_sum, @@ -414,21 +418,21 @@ class License(models.Model): def save(self, *args, **kwargs): if not self.__prev_date and self.paid_date: - max_date_license = License.objects.filter(user=self.user).aggregate(Max('date_to'))['date_to__max'] + max_date_license = License.objects.filter(company=self.company).aggregate(Max('date_to'))['date_to__max'] today = datetime.now().date() if max_date_license < today: max_date_license = today - timedelta(1) self.date_from = max_date_license + relativedelta(days=1) self.date_to = self.date_from + relativedelta(months=self.term, days=-1) - self.user.profile.active = True - self.user.profile.save() + self.company.active = True + self.company.save() self.status = 1 - utils.check_one_profile(self.user.profile, License, datetime.now(), manual=True) + utils.check_one_profile(self.company, License, datetime.now(), manual=True) super(License, self).save(*args, **kwargs) - def get_email(self): - return self.user.email + def get_company(self): + return self.company.get_company_name() def get_action_link(self): diff --git a/project/myauth/views.py b/project/myauth/views.py index dfd98c5..f194469 100644 --- a/project/myauth/views.py +++ b/project/myauth/views.py @@ -209,14 +209,15 @@ def change_email(request): @csrf_protect def login(request): """Вход в систему.""" - if request.session.get('login_count', None): - request.session['login_count'] += 1 - else: - form_class = forms.LoginForm + if request.session.get('login_count', None) is None: request.session['login_count'] = 0 - if request.session['login_count'] > 0: + if request.session['login_count'] > 1: form_class = forms.CaptchedLoginForm - form_prefix = 'login' + else: + form_class = forms.LoginForm + + #form_prefix = 'login' + form_prefix = '' template_name = 'myauth/login.html' success_url = 'customer_index' if request.method == 'POST': @@ -232,6 +233,8 @@ def login(request): return redirect(success_url) else: request.session['login_count'] += 1 + if request.session['login_count'] > 1: + form = forms.CaptchedLoginForm(data=request.POST, prefix=form_prefix) else: form = form_class(prefix=form_prefix) diff --git a/project/settings.py b/project/settings.py index 31aed89..7898b6d 100644 --- a/project/settings.py +++ b/project/settings.py @@ -130,6 +130,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'cms.context_processors.cms_settings', 'sekizai.context_processors.sekizai', 'project.callback.context_processors.add_forms', + 'project.customer.context_processors.license_check_soon_ends', ) diff --git a/project/templates/base.html b/project/templates/base.html index f724017..5f0a0c1 100644 --- a/project/templates/base.html +++ b/project/templates/base.html @@ -12,12 +12,15 @@ {% cms_toolbar %} -{% if messages %} +{% if messages or license_15days %}
{% endif %}