Convert print statements to logger calls

This commit is contained in:
Tanner Collin 2020-03-08 01:07:09 +00:00
parent 464db5cf28
commit 301b1603ef
6 changed files with 39 additions and 24 deletions

View File

@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)
from django.dispatch import receiver from django.dispatch import receiver
from simple_history.signals import ( from simple_history.signals import (
pre_create_historical_record, pre_create_historical_record,
@ -82,9 +85,9 @@ def post_create_historical_record_callback(
new=change_new, new=change_new,
) )
except BaseException as e: except BaseException as e:
print('Problem creating history index: {} - {}'.format(e.__class__.__name__, e)) logger.error('History Signal - {} - {}'.format(e.__class__.__name__, e))
print('sender', sender) logger.info(str(sender))
print('instance', instance) logger.info(str(instance))
print('history_instance', history_instance) logger.info(str(history_instance))
print('history_user', history_user) logger.info(str(history_change_reason))
print('using', using) logger.info(str(history_user))

View File

@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)
import io import io
import requests import requests
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -18,7 +21,7 @@ from . import models, serializers, utils_ldap
try: try:
from . import old_models from . import old_models
except ImportError: except ImportError:
print('Running without old portal data...') logger.info('Running without old portal data...')
old_models = None old_models = None
STATIC_FOLDER = 'data/static/' STATIC_FOLDER = 'data/static/'

View File

@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)
import requests import requests
from apiserver import secrets from apiserver import secrets
@ -13,7 +16,7 @@ def ldap_api(route, data):
r = requests.post(url, data=data, headers=headers, timeout=3) r = requests.post(url, data=data, headers=headers, timeout=3)
return r.status_code return r.status_code
except BaseException as e: except BaseException as e:
print('Problem GETting {}: {} - {}'.format(url, e.__class__.__name__, str(e))) logger.error('LDAP {} - {} - {}'.format(url, e.__class__.__name__, str(e)))
return None return None
def find_user(username): def find_user(username):

View File

@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)
import datetime import datetime
import json import json
import requests import requests
@ -87,7 +90,7 @@ def verify_paypal_ipn(data):
if r.text != 'VERIFIED': if r.text != 'VERIFIED':
return False return False
except BaseException as e: except BaseException as e:
print('Problem verifying IPN: {} - {}'.format(e.__class__.__name__, str(e))) logger.error('IPN verify - {} - {}'.format(e.__class__.__name__, str(e)))
return False return False
return True return True
@ -222,7 +225,7 @@ def check_training(data, training_id, amount):
training.paid_date = datetime.date.today() training.paid_date = datetime.date.today()
training.save() training.save()
print('Amount valid for training cost, id:', training.id) logger.info('IPN - Amount valid for training cost, id: ' + str(training.id))
return create_member_training_tx(data, member, training) return create_member_training_tx(data, member, training)
def create_category_tx(data, member, custom_json): def create_category_tx(data, member, custom_json):
@ -257,26 +260,26 @@ def process_paypal_ipn(data):
ipn = record_ipn(data) ipn = record_ipn(data)
if verify_paypal_ipn(data): if verify_paypal_ipn(data):
print('IPN verified') logger.info('IPN - verified')
else: else:
print('IPN verification failed') logger.error('IPN - verification failed')
update_ipn(ipn, 'Verification Failed') update_ipn(ipn, 'Verification Failed')
return False return False
amount = float(data.get('mc_gross', '0')) amount = float(data.get('mc_gross', '0'))
if data.get('payment_status', 'unknown') != 'Completed': if data.get('payment_status', 'unknown') != 'Completed':
print('Payment not yet completed, ignoring') logger.info('IPN - Payment not yet completed, ignoring')
update_ipn(ipn, 'Payment Incomplete') update_ipn(ipn, 'Payment Incomplete')
return False return False
if data.get('receiver_email', 'unknown') != OUR_EMAIL: if data.get('receiver_email', 'unknown') != OUR_EMAIL:
print('Payment not for us, ignoring') logger.info('IPN - Payment not for us, ignoring')
update_ipn(ipn, 'Invalid Receiver') update_ipn(ipn, 'Invalid Receiver')
return False return False
if data.get('mc_currency', 'unknown') != OUR_CURRENCY: if data.get('mc_currency', 'unknown') != OUR_CURRENCY:
print('Payment currency invalid, ignoring') logger.info('IPN - Payment currency invalid, ignoring')
update_ipn(ipn, 'Invalid Currency') update_ipn(ipn, 'Invalid Currency')
return False return False
@ -285,12 +288,12 @@ def process_paypal_ipn(data):
hints = models.PayPalHint.objects hints = models.PayPalHint.objects
if 'txn_id' not in data: if 'txn_id' not in data:
print('Missing transaction ID, ignoring') logger.info('IPN - Missing transaction ID, ignoring')
update_ipn(ipn, 'Missing ID') update_ipn(ipn, 'Missing ID')
return False return False
if transactions.filter(paypal_txn_id=data['txn_id']).exists(): if transactions.filter(paypal_txn_id=data['txn_id']).exists():
print('Duplicate transaction, ignoring') logger.info('IPN - Duplicate transaction, ignoring')
update_ipn(ipn, 'Duplicate') update_ipn(ipn, 'Duplicate')
return False return False
@ -302,7 +305,7 @@ def process_paypal_ipn(data):
if 'training' in custom_json: if 'training' in custom_json:
tx = check_training(data, custom_json['training'], amount) tx = check_training(data, custom_json['training'], amount)
if tx: if tx:
print('Training matched, adding hint and returning') logger.info('IPN - Training matched, adding hint and returning')
update_ipn(ipn, 'Accepted, training') update_ipn(ipn, 'Accepted, training')
hints.update_or_create( hints.update_or_create(
account=data.get('payer_id', 'unknown'), account=data.get('payer_id', 'unknown'),
@ -323,14 +326,14 @@ def process_paypal_ipn(data):
) )
if not members.filter(id=member_id).exists(): if not members.filter(id=member_id).exists():
print('Unable to associate with member, reporting') logger.info('IPN - Unable to associate with member, reporting')
update_ipn(ipn, 'Accepted, Unmatched Member') update_ipn(ipn, 'Accepted, Unmatched Member')
return create_unmatched_member_tx(data) return create_unmatched_member_tx(data)
member = members.get(id=member_id) member = members.get(id=member_id)
if custom_json.get('category', False) in ['Snacks', 'OnAcct', 'Donation']: if custom_json.get('category', False) in ['Snacks', 'OnAcct', 'Donation']:
print('Category matched') logger.info('IPN - Category matched')
update_ipn(ipn, 'Accepted, category') update_ipn(ipn, 'Accepted, category')
return create_category_tx(data, member, custom_json) return create_category_tx(data, member, custom_json)
@ -342,11 +345,11 @@ def process_paypal_ipn(data):
num_months = 0 num_months = 0
if num_months: if num_months:
print('Amount valid for membership dues, adding months') logger.info('IPN - Amount valid for membership dues, adding months')
update_ipn(ipn, 'Accepted, Member Dues') update_ipn(ipn, 'Accepted, Member Dues')
deal = custom_json.get('deal', False) deal = custom_json.get('deal', False)
return create_member_dues_tx(data, member, num_months, deal) return create_member_dues_tx(data, member, num_months, deal)
print('Unable to find a reason for payment, reporting') logger.info('IPN - Unable to find a reason for payment, reporting')
update_ipn(ipn, 'Accepted, Unmatched Purchase') update_ipn(ipn, 'Accepted, Unmatched Purchase')
return create_unmatched_purchase_tx(data, member) return create_unmatched_purchase_tx(data, member)

View File

@ -1,3 +1,6 @@
import logging
logger = logging.getLogger(__name__)
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.db.models import Max from django.db.models import Max
@ -348,7 +351,7 @@ class IpnView(views.APIView):
try: try:
utils_paypal.process_paypal_ipn(request.data) utils_paypal.process_paypal_ipn(request.data)
except BaseException as e: except BaseException as e:
print('Problem processing IPN: {} - {}'.format(e.__class__.__name__, str(e))) logger.error('IPN route - {} - {}'.format(e.__class__.__name__, str(e)))
finally: finally:
return Response(200) return Response(200)

View File

@ -30,7 +30,6 @@ SECRET_KEY = secrets.DJANGO_SECRET_KEY
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG_ENV = os.environ.get('DEBUG', False) DEBUG_ENV = os.environ.get('DEBUG', False)
DEBUG = DEBUG_ENV or False DEBUG = DEBUG_ENV or False
if DEBUG: print('Debug mode ON')
PRODUCTION_HOST = 'my.protospace.ca' PRODUCTION_HOST = 'my.protospace.ca'
@ -258,6 +257,7 @@ OLD_PASSWORD_FIELD_ENABLED = True
LOGOUT_ON_PASSWORD_CHANGE = False LOGOUT_ON_PASSWORD_CHANGE = False
ACCOUNT_PRESERVE_USERNAME_CASING = False ACCOUNT_PRESERVE_USERNAME_CASING = False
if DEBUG: logger.info('Debug mode ON')
logger.info('Test logging for each thread') logger.info('Test logging for each thread')
#import logging_tree #import logging_tree