From 15a61d500b7845afe4be8f478b656d21df8e52aa Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 10:34:44 +0300 Subject: [PATCH 1/6] added logger to file --- project/settings.py | 32 ++++++++++++++++++++++++-------- project/views.py | 33 +++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/project/settings.py b/project/settings.py index 7a8ab32c..b4ed67dc 100644 --- a/project/settings.py +++ b/project/settings.py @@ -25,7 +25,8 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = os.getenv('SECRET_KEY', 'jelm*91lj(_-o20+6^a+bgv!4s6e_efry^#+f#=1ak&s1xr-2j') +SECRET_KEY = os.getenv( + 'SECRET_KEY', 'jelm*91lj(_-o20+6^a+bgv!4s6e_efry^#+f#=1ak&s1xr-2j') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = os.getenv('DEBUG', False) @@ -200,6 +201,11 @@ LOGGING = { 'level': 'WARNING', 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', }, + 'file': { + 'level': 'DEBUG', + 'class': 'logging.FileHandler', + 'filename': '/app/media/main.log', + }, }, 'loggers': { 'django.request': { @@ -214,9 +220,13 @@ LOGGING = { 'handlers': ['console', ], }, '': { - 'handlers': ['sentry',], + 'handlers': ['sentry', ], 'level': "INFO", }, + 'roistat': { + 'handlers': ['file'], + 'level': 'DEBUG' + }, } } @@ -258,7 +268,8 @@ SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY') # SMS # https://github.com/twilio/twilio-python -TWILIO_ACCOUNT = os.getenv('TWILIO_ACCOUNT', 'ACdf4a96b776cc764bc3ec0f0e136ba550') +TWILIO_ACCOUNT = os.getenv( + 'TWILIO_ACCOUNT', 'ACdf4a96b776cc764bc3ec0f0e136ba550') TWILIO_TOKEN = os.getenv('TWILIO_TOKEN', '559a6b1fce121759c9af2dcbb3f755ea') TWILIO_FROM_PHONE = os.getenv('TWILIO_FROM_PHONE', '+37128914409') @@ -297,8 +308,10 @@ REST_FRAMEWORK = { # Celery settings -CELERY_BROKER_URL = os.getenv('REDIS_SERVICE_HOST', 'redis://redis:6379/') + os.getenv('REDIS_BROKER_DB', '0') -CELERY_RESULT_BACKEND = os.getenv('REDIS_SERVICE_HOST', 'redis://redis:6379/') + os.getenv('REDIS_RESULTS_DB', '1') +CELERY_BROKER_URL = os.getenv( + 'REDIS_SERVICE_HOST', 'redis://redis:6379/') + os.getenv('REDIS_BROKER_DB', '0') +CELERY_RESULT_BACKEND = os.getenv( + 'REDIS_SERVICE_HOST', 'redis://redis:6379/') + os.getenv('REDIS_RESULTS_DB', '1') CELERY_TASK_SERIALIZER = 'json' CELERY_BEAT_SCHEDULE = { @@ -350,8 +363,10 @@ except ImportError: pass else: Paymentwall.set_api_type(Paymentwall.API_GOODS) - Paymentwall.set_app_key(os.getenv('PAYMENTWALL_APP_KEY', 'd6f02b90cf6b16220932f4037578aff7')) - Paymentwall.set_secret_key(os.getenv('PAYMENTWALL_SECRET_KEY', '4ea515bf94e34cf28646c2e12a7b8707')) + Paymentwall.set_app_key( + os.getenv('PAYMENTWALL_APP_KEY', 'd6f02b90cf6b16220932f4037578aff7')) + Paymentwall.set_secret_key( + os.getenv('PAYMENTWALL_SECRET_KEY', '4ea515bf94e34cf28646c2e12a7b8707')) # Mixpanel settings MIX_TOKEN = os.getenv('MIXPANEL_TOKEN', '79bd6bfd98667ed977737e6810b8abcd') @@ -361,7 +376,8 @@ MIX_TOKEN = os.getenv('MIXPANEL_TOKEN', '79bd6bfd98667ed977737e6810b8abcd') if DEBUG: CORS_ORIGIN_ALLOW_ALL = True else: - CORS_ORIGIN_WHITELIST = os.getenv('CORS_ORIGIN_WHITELIST', 'lilcity.9ev.ru:8080').split(',') + CORS_ORIGIN_WHITELIST = os.getenv( + 'CORS_ORIGIN_WHITELIST', 'lilcity.9ev.ru:8080').split(',') # Swagger doc settings diff --git a/project/views.py b/project/views.py index 16559a07..53edf673 100644 --- a/project/views.py +++ b/project/views.py @@ -31,13 +31,15 @@ class IndexView(TemplateView): now_time = now() try: - school_schedule = SchoolSchedule.objects.get(weekday=now_time.isoweekday(), is_camp=True) + school_schedule = SchoolSchedule.objects.get( + weekday=now_time.isoweekday(), is_camp=True) except SchoolSchedule.DoesNotExist: online = False online_coming_soon = False school_schedule = None else: - end_at = datetime.combine(now_time.today(), school_schedule.start_at) + end_at = datetime.combine( + now_time.today(), school_schedule.start_at) online = ( school_schedule.start_at <= now_time.time() and (end_at + timedelta(hours=1)).time() >= now_time.time() and @@ -45,7 +47,8 @@ class IndexView(TemplateView): ) online_coming_soon = ( school_schedule.start_at > now_time.time() and - datetime.combine(datetime.today(), school_schedule.start_at) - timedelta(hours=12) + datetime.combine( + datetime.today(), school_schedule.start_at) - timedelta(hours=12) <= datetime.combine(datetime.today(), now_time.time()) and school_schedule.current_camp_lesson ) @@ -91,14 +94,17 @@ class IndexView(TemplateView): if user_gift_certificate: try: - user_gift_certificate = short_url.decode_url(user_gift_certificate) - user_gift_certificate = UserGiftCertificate.objects.get(pk=user_gift_certificate, bonuses_sent__isnull=True) + user_gift_certificate = short_url.decode_url( + user_gift_certificate) + user_gift_certificate = UserGiftCertificate.objects.get( + pk=user_gift_certificate, bonuses_sent__isnull=True) except: user_gift_certificate = None review_images = list(map(str, range(1, 107))) shuffle(review_images) - teachers = User.objects.filter(role=User.TEACHER_ROLE, show_in_mainpage=True) + teachers = User.objects.filter( + role=User.TEACHER_ROLE, show_in_mainpage=True) if teachers.count() % 2 == 0: teachers = teachers[:teachers.count() - 1] @@ -128,6 +134,7 @@ class IndexView(TemplateView): 'is_purchased_future': False, }) + return context @@ -136,7 +143,8 @@ class SchoolSchedulesView(TemplateView): def get_context_data(self): context = super().get_context_data() - context['school_schedules'] = SchoolSchedule.objects.filter(is_camp=True) + context['school_schedules'] = SchoolSchedule.objects.filter( + is_camp=True) return context @@ -163,12 +171,13 @@ class PackagesView(TemplateView): context['last_school_payment'] = last_school_payment if last_school_payment: next_month = (last_school_payment.date_end + timedelta(1)).month - context['next_buy_date'] = last_school_payment.date_end + timedelta(1) + context['next_buy_date'] = last_school_payment.date_end + \ + timedelta(1) context['school_months_left'] = (school_end.month - next_month - if next_month <= school_end.month - else (school_end.month + 13) - next_month) + if next_month <= school_end.month + else (school_end.month + 13) - next_month) else: context['school_months_left'] = (school_end.month - today.month - if today.month <= school_end.month - else (school_end.month + 13) - today.month) + if today.month <= school_end.month + else (school_end.month + 13) - today.month) return context From 4aa8db63a72edd328037147eeca9c4e6b9c78c06 Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 12:31:03 +0300 Subject: [PATCH 2/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB-=D0=B2=D0=BE=20=D1=83=D1=87=D0=B8=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D0=B5=D0=B9=20>=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project/views.py b/project/views.py index 53edf673..9c3ac5b2 100644 --- a/project/views.py +++ b/project/views.py @@ -105,7 +105,8 @@ class IndexView(TemplateView): shuffle(review_images) teachers = User.objects.filter( role=User.TEACHER_ROLE, show_in_mainpage=True) - if teachers.count() % 2 == 0: + + if teachers.count() % 2 == 0 and teachers.count() > 2: teachers = teachers[:teachers.count() - 1] context.update({ From c76fb4340943a4edcae8fe29936180fea4a06c0b Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 13:08:59 +0300 Subject: [PATCH 3/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BB?= =?UTF-8?q?=D0=BE=D0=B3=D0=B3=D0=B5=D1=80=20=D0=BD=D0=B0=20home?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/views.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/project/views.py b/project/views.py index 9c3ac5b2..24d27c89 100644 --- a/project/views.py +++ b/project/views.py @@ -12,6 +12,9 @@ from apps.course.models import Course from apps.school.models import SchoolSchedule from apps.payment.models import SchoolPayment, UserGiftCertificate, Payment, DrawingCampPayment from apps.content.models import Banner, Package +import logging + +logger_roistat = logging.getLogger('roistat') User = get_user_model() @@ -136,6 +139,8 @@ class IndexView(TemplateView): }) + logger_roistat.debug('hi from home page!') + return context From b1038610013d609e9a9cbd0b1fa88802174566e7 Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 13:17:30 +0300 Subject: [PATCH 4/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20verbose=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=82=D1=82=D0=B5?= =?UTF-8?q?=D1=80=20=D0=BA=20=D0=BB=D0=BE=D0=B3=D1=83=20=D0=B2=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project/settings.py b/project/settings.py index b4ed67dc..17b0d706 100644 --- a/project/settings.py +++ b/project/settings.py @@ -205,6 +205,7 @@ LOGGING = { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/app/media/main.log', + 'formatter': 'verbose', }, }, 'loggers': { From f37e10138f25edd7737441d606da95cdceb985ae Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 13:56:12 +0300 Subject: [PATCH 5/6] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=20=D0=BB=D0=BE=D0=B3=D0=B3=D0=B5=D1=80=20+=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=201)=20headers=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=20=D0=B2=20?= =?UTF-8?q?=D1=80=D0=BE=D0=B9=D1=81=D1=82=D0=B0=D1=82=202)=20=D1=8F=D0=B2?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BA=D0=BE=D0=BD=D0=B2=D0=B5=D1=80=D1=82=D0=BD?= =?UTF-8?q?=D1=83=D0=BB=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=20=D0=BF?= =?UTF-8?q?=D0=B8=D1=82=D0=BE=D0=BD=D0=B0=20=D0=B2=20json,=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=B2=20=D0=B4=D0=BE=D0=BA=D1=83=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BA=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/payment/tasks.py | 21 +++++++++++++++++---- project/views.py | 5 ----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/apps/payment/tasks.py b/apps/payment/tasks.py index cc2e2f03..b85b460d 100644 --- a/apps/payment/tasks.py +++ b/apps/payment/tasks.py @@ -1,5 +1,6 @@ import logging import requests +import json from mixpanel import Mixpanel @@ -8,6 +9,7 @@ from django.conf import settings from project.celery import app logger = logging.getLogger(__name__) +logger_roistat = logging.getLogger('roistat') @app.task @@ -53,13 +55,24 @@ def transaction_to_roistat(user_id, payment_id, event_name, amount, time, status }] project = settings.ROISTAT_PROJECT key = settings.ROISTAT_KEY - url = settings.ROISTAT_API_URL + f'/project/add-orders?key={key}&project={project}' - resp = requests.post(url, json=body) + url = settings.ROISTAT_API_URL + \ + f'/project/add-orders?key={key}&project={project}' + + payload = json.dumps(body) + headers = {'content-type': 'application/json'} + + resp = requests.request("POST", url, data=payload, headers=headers) + try: resp_json = resp.json() except: resp_json = None + if resp.status_code != 200 or not resp_json or not resp_json.get('processed'): - logger.error('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % (payment_id, amount, resp.text)) + logger.error('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % ( + payment_id, amount, resp.text)) + roistat_logger.debug('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % ( + payment_id, amount, resp.text)) else: - logger.info('TRANSACTION_TO_ROISTAT: ' + str(resp)) + logger.info('TRANSACTION_TO_ROISTAT: ' + str(resp.text)) + roistat_logger.debug('TRANSACTION_TO_ROISTAT: ' + str(resp.text)) diff --git a/project/views.py b/project/views.py index 24d27c89..9c3ac5b2 100644 --- a/project/views.py +++ b/project/views.py @@ -12,9 +12,6 @@ from apps.course.models import Course from apps.school.models import SchoolSchedule from apps.payment.models import SchoolPayment, UserGiftCertificate, Payment, DrawingCampPayment from apps.content.models import Banner, Package -import logging - -logger_roistat = logging.getLogger('roistat') User = get_user_model() @@ -139,8 +136,6 @@ class IndexView(TemplateView): }) - logger_roistat.debug('hi from home page!') - return context From 9b6ef36ba92c381911982efea25c48b26215885f Mon Sep 17 00:00:00 2001 From: Gleb Mikhaylov Date: Mon, 26 Aug 2019 16:38:58 +0300 Subject: [PATCH 6/6] fixed logger name --- apps/payment/tasks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/payment/tasks.py b/apps/payment/tasks.py index b85b460d..30879228 100644 --- a/apps/payment/tasks.py +++ b/apps/payment/tasks.py @@ -71,8 +71,8 @@ def transaction_to_roistat(user_id, payment_id, event_name, amount, time, status if resp.status_code != 200 or not resp_json or not resp_json.get('processed'): logger.error('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % ( payment_id, amount, resp.text)) - roistat_logger.debug('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % ( + logger_roistat.debug('TRANSACTION_TO_ROISTAT for payment # %d, %s RUB: %s' % ( payment_id, amount, resp.text)) else: logger.info('TRANSACTION_TO_ROISTAT: ' + str(resp.text)) - roistat_logger.debug('TRANSACTION_TO_ROISTAT: ' + str(resp.text)) + logger_roistat.debug('TRANSACTION_TO_ROISTAT: ' + str(resp.text))