From 1870c3a2faed1d14110c0ac9fb09bb4a976c757a Mon Sep 17 00:00:00 2001 From: okt Date: Thu, 26 Jun 2014 15:43:17 +0300 Subject: [PATCH] celery for license check --- celerybeat-schedule | Bin 0 -> 12288 bytes project/__init__.py | 4 ++++ project/celery.py | 24 +++++++++++++++++++++ project/customer/admin.py | 9 ++++++-- project/customer/tasks.py | 24 +++++++++++++++++++++ project/myauth/views.py | 1 - project/settings.py | 16 ++++++++++++++ requirements-dev.txt | 40 ++++++++++++++++++++++++----------- requirements.txt | 43 ++++++++++++++++++++++++-------------- 9 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 celerybeat-schedule create mode 100644 project/celery.py create mode 100644 project/customer/tasks.py diff --git a/celerybeat-schedule b/celerybeat-schedule new file mode 100644 index 0000000000000000000000000000000000000000..5a09f0745990d663fc03fc8454cdf220e39ad257 GIT binary patch literal 12288 zcmeI1F>ljA6vxjUPIjV%QowT8QJ7fR}vRYWq`6&r;ZOeCol%0_;#W&ys zupm|919U+wymOLLqOu~8{+92(r+e?t_y653Co83tC(S+C!+Yw!3l632yFjVEwfoI} zfAmT1N&f0V{NcyTb;`}Loo8G8`Qz;MUH#iUJP3dQ2!H?xfB*=900@8p2!H?xfWY4) zuzN?}S6@4-bKSk_e(81&=-|5Zts^~nKmY_l00ck)1V8`;KmY_l00jOW0^TT$$ZKuj z-1?0u@RnAmTxOGkb8qaQFPp07r_U>+t9L%tuEM_edbvbnUvt6DWiZL~g3CPB1!v{5 zma>@iHpiKzv7T|hEcm>%%_Y@+>P=&kSLc~!JHKGM3F-m$#(uI2hsi2u@h8bD%Ear( zNhvNqN)n&!tNNHm;mudf<^zUwQ!y0c+6H=QY<0m+U^BBYK|3bB zHKtF~KZ^_2k~utU)kOmu4TpE=+bWec98>QXKu0m>!e){X>B45zmliry(NKbrUbs*M z+i9yhI(4~NWCk9~#?0BshWCRgh~mvor|Da6%)Bbo)E!-S|KIt~f2cP1{hj{yq~rg8 eivb1%KmY_l00ck)1V8`;KmY_l00jOU0zU!qy1U~5 literal 0 HcmV?d00001 diff --git a/project/__init__.py b/project/__init__.py index e69de29..236a1a7 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import +from .celery import app as celery_app + diff --git a/project/celery.py b/project/celery.py new file mode 100644 index 0000000..cf03aec --- /dev/null +++ b/project/celery.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import + +import os + +from celery import Celery + +from django.conf import settings + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings') + +app = Celery('proj') + +# Using a string here means the worker will not have to +# pickle the object when using Windows. +app.config_from_object('django.conf:settings') +app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) + + +#@app.task(bind=True) +#def debug_task(self): +# print('Request: {0!r}'.format(self.request)) diff --git a/project/customer/admin.py b/project/customer/admin.py index e11760b..ec024c2 100644 --- a/project/customer/admin.py +++ b/project/customer/admin.py @@ -6,10 +6,15 @@ import models class UserProfileAdmin(admin.ModelAdmin): - list_display = ('user', 'profile_type', 'name', 'inn',) + list_display = ('user', 'profile_type', 'name', 'inn', 'active') list_display_links = list_display form = forms.UserProfileAdminForm +class LicenseAdmin(admin.ModelAdmin): + list_display = ('user', 'term', 'status', 'order_date', 'date_from', 'date_to') + + list_display_links = list_display + #TODO прописать fieldsets # fieldsets = [ # (None, {'fields': ['user',]}), @@ -56,5 +61,5 @@ class ClientAdmin(admin.ModelAdmin): admin.site.register(models.UserProfile, UserProfileAdmin) admin.site.register(models.BankAccount, BankAccountAdmin) admin.site.register(models.Client, ClientAdmin) -admin.site.register(models.License) +admin.site.register(models.License, LicenseAdmin) admin.site.register(models.LicensePrice) diff --git a/project/customer/tasks.py b/project/customer/tasks.py new file mode 100644 index 0000000..221890c --- /dev/null +++ b/project/customer/tasks.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import +from datetime import datetime + +from celery import shared_task +from .models import License, UserProfile + + +@shared_task +def check_license(): + profiles = UserProfile.objects.all() + now = datetime.now() + + for profile in profiles: + licenses = License.objects.filter(user=profile.user, date_from__lte=now, date_to__gte=now, status__in=[-1, 1, 2]) + if licenses: + profile.active = True + else: + profile.active = False + profile.save() + licenses.filter(status=1).update(status=2) + + return None + diff --git a/project/myauth/views.py b/project/myauth/views.py index 18d167e..355f629 100644 --- a/project/myauth/views.py +++ b/project/myauth/views.py @@ -213,7 +213,6 @@ def login(request): form = form_class(data=request.POST, prefix=form_prefix) if form.is_valid(): auth.login(request, form.get_user()) - print request.user.profile.check_name_not_filled() if request.user.profile.check_name_not_filled(): success_url = 'customer_profile_edit' return redirect(success_url) diff --git a/project/settings.py b/project/settings.py index 39c6126..0f3627b 100644 --- a/project/settings.py +++ b/project/settings.py @@ -228,6 +228,22 @@ CMS_TEMPLATES = ( ('template_2.html', 'Template Two'), ) +BROKER_HOST = "localhost" +BROKER_PORT = 5672 +BROKER_USER = "user" +BROKER_PASSWORD = "pass" +BROKER_VHOST = "dok" +CELERY_TIMEZONE = 'Europe/Moscow' + +from datetime import timedelta + +CELERYBEAT_SCHEDULE = { + 'check-license': { + 'task': 'project.customer.tasks.check_license', + 'schedule': timedelta(seconds=30), + }, +} + try: from project.local_settings import * except ImportError: diff --git a/requirements-dev.txt b/requirements-dev.txt index 3a283d7..e4974ac 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,20 +1,36 @@ -Django==1.5.5 +Django==1.5.8 +Pillow==2.2.1 South==0.8.2 -pillow==2.2.1 -simplejson==3.3.1 -six==1.4.1 -xlrd==0.9.2 -xlwt==0.7.5 +amqp==1.4.5 +anyjson==0.3.3 +argparse==1.2.1 +billiard==3.3.0.18 +celery==3.1.12 +django-autocomplete-light==1.4.9 +django-classy-tags==0.5.1 +django-cms==3.0.2 +django-debug-toolbar==1.2.1 +django-filter==0.7 +django-mptt==0.6.0 +django-sekizai==0.7 +djangocms-admin-style==0.2.2 +flup==1.0.2 html5lib==0.95 +ipython==2.1.0 +kombu==3.0.20 pisa==3.0.33 +psycopg2 pyPdf==1.13 -reportlab==2.7 -django-filter==0.7 +python-dateutil==2.2 pytils==0.2.3 -psycopg2 -flup -django-autocomplete-light==1.4.9 -django-cms==3.0.2 +pytz==2014.4 +reportlab==2.7 +simplejson==3.3.1 +six==1.4.1 +sqlparse==0.1.11 +wsgiref==0.1.2 +xlrd==0.9.2 +xlwt==0.7.5 # dev django-devserver diff --git a/requirements.txt b/requirements.txt index 190cedc..7e50b9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,24 +1,35 @@ Django==1.5.8 +Pillow==2.2.1 South==0.8.2 -pillow==2.2.1 -simplejson==3.3.1 -six==1.4.1 -xlrd==0.9.2 -xlwt==0.7.5 +amqp==1.4.5 +anyjson==0.3.3 +argparse==1.2.1 +billiard==3.3.0.18 +celery==3.1.12 +django-autocomplete-light==1.4.9 +django-classy-tags==0.5.1 +django-cms==3.0.2 +django-debug-toolbar==1.2.1 +django-filter==0.7 +django-mptt==0.6.0 +django-sekizai==0.7 +djangocms-admin-style==0.2.2 +flup==1.0.2 html5lib==0.95 +ipython==2.1.0 +kombu==3.0.20 pisa==3.0.33 +psycopg2 pyPdf==1.13 -reportlab==2.7 -django-filter==0.7 +python-dateutil==2.2 pytils==0.2.3 -psycopg2 +pytz==2014.4 +reportlab==2.7 +simplejson==3.3.1 +six==1.4.1 +sqlparse==0.1.11 +wsgiref==0.1.2 +xlrd==0.9.2 +xlwt==0.7.5 flup -django-autocomplete-light==1.4.9 -django-cms==3.0.2 - -# dev -#django-devserver -#django-eml-email-backend -django-debug-toolbar -ipython