Archived
Remove code related to Inside
This commit is contained in:
1 parent
27160f1a52
commit
4b75b45ce2
3 files changed
+2
-325
No files matched your search
@@ -9,11 +9,6 @@ DB_USER = ''
|
||||
DB_PASS = ''
|
||||
DB_NAME = 'tekstmelding'
|
||||
|
||||
INSIDE_DB_HOST = 'localhost'
|
||||
INSIDE_DB_USER = ''
|
||||
INSIDE_DB_PASS = ''
|
||||
INSIDE_DB_NAME = 'inside'
|
||||
|
||||
# Authenticates Inside
|
||||
INSIDE_API_KEY = ''
|
||||
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import MySQLdb
|
||||
import MySQLdb.cursors
|
||||
import datetime
|
||||
|
||||
|
||||
class Inside(object):
|
||||
"""Inside."""
|
||||
|
||||
def __init__(self, host, username, password, db):
|
||||
self.conn = MySQLdb.connect(
|
||||
host=host,
|
||||
user=username,
|
||||
passwd=password,
|
||||
db=db,
|
||||
cursorclass=MySQLdb.cursors.DictCursor,
|
||||
charset='utf8')
|
||||
|
||||
def query_db(self, query, args=(), one=False, lastrowid=False):
|
||||
"""Queries the database."""
|
||||
assert not (one and lastrowid)
|
||||
|
||||
cur = self.conn.cursor()
|
||||
cur.execute(query, args)
|
||||
|
||||
if lastrowid:
|
||||
ret = cur.lastrowid
|
||||
else:
|
||||
rv = cur.fetchall()
|
||||
ret = (rv[0] if rv else None) if one else rv
|
||||
|
||||
self.conn.commit()
|
||||
cur.close()
|
||||
return ret
|
||||
|
||||
def get_user_by_phone(self, number):
|
||||
user = self.query_db("""
|
||||
SELECT user.*,
|
||||
(CASE WHEN (user.expires IS NULL) THEN 1 ELSE 0 END) AS expires_lifelong
|
||||
FROM din_user user, din_userphonenumber phone
|
||||
WHERE (phone.number = %s OR phone.number = %s) AND
|
||||
user.id = phone.user_id
|
||||
""", ['+' + number, number], one=True)
|
||||
return user
|
||||
|
||||
def user_is_expired(self, user):
|
||||
# A friendly reminder about how the 'expires' column works:
|
||||
# NULL means the membership should never expire
|
||||
# 0000-00-00 is the default and means the user has never had a membership
|
||||
# YYYY-MM-DD is the day the membership will or has expired
|
||||
#
|
||||
# datetime.date does not accept the date 0000-00-00, instead we end
|
||||
# up with None. As a workaround we do the NULL comparison with SQL
|
||||
# and store the result in 'expires_lifelong'.
|
||||
expires = user.get('expires', None)
|
||||
expires_lifelong = user.get('expires_lifelong', None)
|
||||
|
||||
# Lifelong membership?
|
||||
if expires_lifelong:
|
||||
return False
|
||||
|
||||
# Non-expired membership?
|
||||
elif type(expires) is datetime.date and expires >= datetime.date.today():
|
||||
return False
|
||||
|
||||
# Expired?
|
||||
elif not expires or (type(expires) is datetime.date and expires < datetime.date.today()):
|
||||
return True
|
||||
|
||||
else:
|
||||
assert False, "What a trainwreck, we shouldn't be here"
|
||||
|
||||
def renew_user(self, user_id, new_expire):
|
||||
self.query_db(
|
||||
"UPDATE din_user SET expires = %(expires)s WHERE id = %(id)s", {
|
||||
'expires': str(new_expire),
|
||||
'id': user_id
|
||||
})
|
||||
|
||||
self.query_db("""
|
||||
INSERT INTO din_userupdate
|
||||
(date, user_id_updated, comment, user_id_updated_by)
|
||||
VALUES
|
||||
(%(date)s, %(user_id_updated)s, %(comment)s, %(user_id_updated_by)s)
|
||||
""", {
|
||||
'date': str(datetime.datetime.now()),
|
||||
'user_id_updated': user_id,
|
||||
'comment': "Medlemskap fornyet med SMS.",
|
||||
'user_id_updated_by': user_id,
|
||||
})
|
||||
|
||||
def get_full_name(self, user):
|
||||
full_name = "%s %s" % (
|
||||
user.get('firstname', '').strip(),
|
||||
user.get('lastname', '').strip()
|
||||
)
|
||||
return full_name[:50].strip()
|
||||
+2
-221
@@ -9,7 +9,6 @@ import logging.config
|
||||
|
||||
from utils import generate_activation_code, MyJSONEncoder
|
||||
import sendega
|
||||
import inside
|
||||
|
||||
import config
|
||||
|
||||
@@ -88,16 +87,6 @@ def get_sendega():
|
||||
return g.sendega
|
||||
|
||||
|
||||
def get_inside():
|
||||
if not hasattr(g, 'inside'):
|
||||
g.inside = inside.Inside(
|
||||
host=app.config['INSIDE_DB_HOST'],
|
||||
username=app.config['INSIDE_DB_USER'],
|
||||
password=app.config['INSIDE_DB_PASS'],
|
||||
db=app.config['INSIDE_DB_NAME'])
|
||||
return g.inside
|
||||
|
||||
|
||||
def log_incoming(**kwargs):
|
||||
return query_db("""
|
||||
INSERT INTO incoming
|
||||
@@ -226,216 +215,6 @@ def notify_valid_membership(incoming_id=None, number=None, user=None):
|
||||
return 'OK'
|
||||
|
||||
|
||||
def notify_pending_new_membership(incoming_id=None, number=None, activation_code=None):
|
||||
content = u"Hei! Du har allerede betalt for et medlemskap. Aktiver det her: https://s.neuf.no/sms/%(number)s/%(activation_code)s" % ({
|
||||
'number': number,
|
||||
'activation_code': activation_code,
|
||||
})
|
||||
|
||||
outgoing_id = send_sms(
|
||||
destination=number,
|
||||
content=content,
|
||||
incoming_id=incoming_id)
|
||||
|
||||
app.logger.info(
|
||||
"Resent activation code for number:%s", number)
|
||||
|
||||
log_event(
|
||||
action='notify_pending_new_membership',
|
||||
incoming_id=incoming_id,
|
||||
outgoing_id=outgoing_id)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def notify_could_not_charge(incoming_id=None, dlr_id=None, number=None):
|
||||
app.logger.warning(
|
||||
"Attempt to charge number:%s as a response to incoming_id:%s failed",
|
||||
number, incoming_id)
|
||||
|
||||
content = u"Beklager, bestillingen kunne ikke gjennomføres. Spørsmål? medlem@studentersamfundet.no"
|
||||
|
||||
outgoing_id = send_sms(
|
||||
destination=number,
|
||||
content=content,
|
||||
incoming_id=incoming_id)
|
||||
|
||||
log_event(
|
||||
action='notify_could_not_charge',
|
||||
incoming_id=incoming_id,
|
||||
outgoing_id=outgoing_id,
|
||||
dlr_id=dlr_id)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def renew_membership(incoming_id=None, number=None, user=None):
|
||||
new_expire = datetime.date.today() + datetime.timedelta(days=365)
|
||||
|
||||
content = u"Hei %(name)s! Ditt medlemskap er nå gyldig ut %(new_expire)s. Spørsmål? medlem@studentersamfundet.no" % ({
|
||||
'name': get_inside().get_full_name(user),
|
||||
'new_expire': str(new_expire),
|
||||
})
|
||||
|
||||
# Log the event first, just in case the DLR is instant
|
||||
event_id = log_event(
|
||||
action='renew_membership',
|
||||
incoming_id=incoming_id,
|
||||
user_id=user['id'])
|
||||
|
||||
outgoing_id = send_sms(
|
||||
destination=number,
|
||||
content=content,
|
||||
incoming_id=incoming_id,
|
||||
billing=True,
|
||||
price=app.config['MEMBERSHIP_PRICE_KR'] * 100,
|
||||
)
|
||||
|
||||
update_event(event_id=event_id, outgoing_id=outgoing_id)
|
||||
|
||||
app.logger.info(
|
||||
"Membership renewal requested by user_id:%s number:%s new_expire:%s",
|
||||
user['id'], number, new_expire)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def renew_membership_delivered(incoming_id=None, dlr_id=None, user_id=None):
|
||||
new_expire = datetime.date.today() + datetime.timedelta(days=365)
|
||||
get_inside().renew_user(user_id=user_id, new_expire=new_expire)
|
||||
|
||||
app.logger.info(
|
||||
"Response to incoming_id:%s was delivered, user_id:%s renewed to %s",
|
||||
incoming_id, user_id, new_expire)
|
||||
|
||||
log_event(
|
||||
action='renew_membership_delivered',
|
||||
incoming_id=incoming_id,
|
||||
dlr_id=dlr_id)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def new_membership(incoming_id=None, number=None):
|
||||
activation_code = generate_activation_code()
|
||||
|
||||
content = u"Velkommen som medlem! Hent ditt medlemskort i baren på Chateau Neuf."
|
||||
|
||||
# Log the event first, just in case the DLR is instant
|
||||
event_id = log_event(
|
||||
action='new_membership',
|
||||
incoming_id=incoming_id,
|
||||
activation_code=activation_code)
|
||||
|
||||
outgoing_id = send_sms(
|
||||
destination=number,
|
||||
content=content,
|
||||
incoming_id=incoming_id,
|
||||
billing=True,
|
||||
price=app.config['MEMBERSHIP_PRICE_KR'] * 100)
|
||||
|
||||
update_event(event_id=event_id, outgoing_id=outgoing_id)
|
||||
|
||||
app.logger.info(
|
||||
"New membership requested by number:%s, activation code sent", number)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def new_membership_delivered(incoming_id=None, dlr_id=None):
|
||||
app.logger.info(
|
||||
"Response to incoming_id:%s was delivered, new membership", incoming_id)
|
||||
|
||||
log_event(
|
||||
action='new_membership_delivered',
|
||||
incoming_id=incoming_id,
|
||||
dlr_id=dlr_id)
|
||||
|
||||
return 'OK'
|
||||
|
||||
|
||||
def get_activation_code_purchase_date(number, activation_code):
|
||||
row = query_db("""
|
||||
SELECT event.timestamp FROM incoming, event
|
||||
WHERE incoming.msisdn = %s
|
||||
AND incoming.id = event.incoming_id
|
||||
AND event.action = 'new_membership'
|
||||
AND event.activation_code = %s""", [number, activation_code], one=True)
|
||||
return str(row.get('timestamp')) if row else None
|
||||
|
||||
|
||||
def has_pending_new_membership(number):
|
||||
# WAT
|
||||
row = query_db("""
|
||||
SELECT event.activation_code FROM event
|
||||
WHERE event.incoming_id IN (
|
||||
SELECT event.incoming_id FROM incoming, event, event AS event2
|
||||
WHERE incoming.msisdn = %s
|
||||
AND incoming.id = event.incoming_id
|
||||
AND event.action = 'new_membership'
|
||||
AND event.timestamp > DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
|
||||
AND event2.incoming_id = event.incoming_id
|
||||
AND event2.action = 'new_membership_delivered')
|
||||
AND activation_code IS NOT NULL""", [number], one=True)
|
||||
|
||||
# What about card_number,phonenumber-tuples from kassa.neuf.no?
|
||||
# TODO: lookup phone number-bound-memberships from kassa, not from incoming sms-s
|
||||
# TODO: or should the place calling this function check an inside-table?
|
||||
return row.get('activation_code') if row else None
|
||||
|
||||
|
||||
@app.route('/inside-code-purchase-date')
|
||||
def inside_code_purchase_date():
|
||||
number = request.args.get('number')
|
||||
activation_code = request.args.get('activation_code')
|
||||
api_key = request.args.get('api_key')
|
||||
|
||||
if api_key != app.config['INSIDE_API_KEY']:
|
||||
abort(403) # Forbidden
|
||||
|
||||
if None in (number, activation_code):
|
||||
return ''
|
||||
|
||||
purchase_date = get_activation_code_purchase_date(number, activation_code)
|
||||
|
||||
app.logger.info(
|
||||
'Inside checked purchase date for '
|
||||
'number:%s activation_code:%s purchase_date:%s',
|
||||
number, activation_code, purchase_date)
|
||||
|
||||
return purchase_date or ''
|
||||
|
||||
|
||||
@app.route('/kassa/pending-membership')
|
||||
def kassa_pending_membership():
|
||||
""" Checks has_pending_new_membership on specified number and returns activation code and purchase date. """
|
||||
number = request.args.get('number')
|
||||
api_key = request.args.get('api_key')
|
||||
|
||||
if api_key != app.config['INSIDE_API_KEY']:
|
||||
abort(403) # Forbidden
|
||||
|
||||
if number is None or len(number) == 0:
|
||||
return jsonify(**{'error': 'Missing or empty param number'})
|
||||
|
||||
if number[0] == '+':
|
||||
number = number.replace('+', '')
|
||||
|
||||
if not number.isdigit():
|
||||
return jsonify(**{'error': "Param number is not numerical '{}'".format(number)})
|
||||
|
||||
activation_code = has_pending_new_membership(number)
|
||||
if activation_code is None:
|
||||
return jsonify(**{'result': None})
|
||||
|
||||
result = {
|
||||
'number': number,
|
||||
'activation_code': activation_code,
|
||||
'purchase_date': get_activation_code_purchase_date(number, activation_code) if activation_code else None
|
||||
}
|
||||
return jsonify(**{'result': result})
|
||||
|
||||
|
||||
@app.route('/kassa/new-membership-card', methods=['POST'])
|
||||
def kassa_notify_new_card():
|
||||
""" Notify user Adds a pending membership to specified number """
|
||||
@@ -594,6 +373,7 @@ def stats_memberships():
|
||||
headers = {'Access-Control-Allow-Origin': '*'}
|
||||
return (jsonify(**result), 200, headers)
|
||||
|
||||
|
||||
@app.route('/stats/memberships/series', methods=['GET'])
|
||||
def stats_memberships_stats():
|
||||
start_datetime = request.args.get('start', '2015-08-01')
|
||||
@@ -610,6 +390,7 @@ def stats_memberships_stats():
|
||||
headers = {'Access-Control-Allow-Origin': '*'}
|
||||
return (jsonify(**result), 200, headers)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def main():
|
||||
return 'Tekstmelding!'
|
||||
|
||||
Reference in new issue
Block a user