Archived
Generate some activation codes
This commit is contained in:
1 parent
0be937c34f
commit
f68db21dc3
3 files changed
+73
-9
No files matched your search
@@ -0,0 +1 @@
|
||||
ALTER TABLE `din_sms_sent` ADD `activation_code` VARCHAR( 10 ) NULL ;
|
||||
+50
-9
@@ -1,12 +1,15 @@
|
||||
from flask import Flask, request, g, abort
|
||||
from flask import Flask, request, g, abort, jsonify
|
||||
import MySQLdb
|
||||
import MySQLdb.cursors
|
||||
import datetime
|
||||
|
||||
import config
|
||||
|
||||
from utils import CustomJSONEncoder, generate_activation_code
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object('config')
|
||||
app.json_encoder = CustomJSONEncoder
|
||||
|
||||
def connect_db():
|
||||
"""Connects to our database."""
|
||||
@@ -15,7 +18,8 @@ def connect_db():
|
||||
user = app.config['DATABASE_USER'],
|
||||
passwd = app.config['DATABASE_PASS'],
|
||||
db = app.config['DATABASE_NAME'],
|
||||
cursorclass = MySQLdb.cursors.DictCursor)
|
||||
cursorclass = MySQLdb.cursors.DictCursor,
|
||||
charset='utf8')
|
||||
return db
|
||||
|
||||
def get_db():
|
||||
@@ -31,19 +35,26 @@ def close_db(error):
|
||||
if hasattr(g, 'db'):
|
||||
g.db.close()
|
||||
|
||||
def query_db(query, args=(), one=False):
|
||||
def query_db(query, args=(), one=False, lastrowid=False):
|
||||
"""Queries the database.
|
||||
|
||||
Will use an existing or new database connection, create a cursor,
|
||||
execute the query, fetch all rows, close the cursor then return the rows.
|
||||
"""
|
||||
assert not (one and lastrowid)
|
||||
|
||||
cur = get_db().cursor()
|
||||
if app.config['DEBUG']:
|
||||
app.logger.debug("Query: %s\nArgs: %s", query, args)
|
||||
cur.execute(query, args)
|
||||
rv = cur.fetchall()
|
||||
|
||||
if lastrowid:
|
||||
ret = cur.lastrowid
|
||||
else:
|
||||
rv = cur.fetchall()
|
||||
ret = (rv[0] if rv else None) if one else rv
|
||||
cur.close()
|
||||
return (rv[0] if rv else None) if one else rv
|
||||
return ret
|
||||
|
||||
def get_user_by_phone(number):
|
||||
user = query_db("""
|
||||
@@ -55,12 +66,20 @@ def get_user_by_phone(number):
|
||||
return user
|
||||
|
||||
def log_incoming(userid, gsm, codeword, message, operator, shortno, action, ip, simulation):
|
||||
query_db("""
|
||||
return query_db("""
|
||||
INSERT INTO din_sms_received
|
||||
(userid, gsm, codeword, message, operator, shortno, action, IP, simulation)
|
||||
VALUES
|
||||
(%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
""", [userid, gsm, codeword, message, operator, shortno, action, ip, simulation])
|
||||
""", [userid, gsm, codeword, message, operator, shortno, action, ip, simulation], lastrowid=True)
|
||||
|
||||
def log_sent(response_to, msgid, sender, receiver, countrycode, message, operator, codeword, billing_price, use_dlr, simulation, activation_code):
|
||||
query_db("""
|
||||
INSERT INTO din_sms_sent
|
||||
(response_to, msgid, sender, receiver, countrycode, message, operator, codeword, billing_price, use_dlr, simulation, activation_code)
|
||||
VALUES
|
||||
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
""", [response_to, msgid, sender, receiver, countrycode, message, operator, codeword, billing_price, use_dlr, simulation, activation_code])
|
||||
|
||||
@app.route('/')
|
||||
def main():
|
||||
@@ -83,7 +102,7 @@ def callback():
|
||||
# but can we can't receive messages from operators. Right?
|
||||
user = get_user_by_phone("+47%s" % gsm)
|
||||
|
||||
log_incoming(
|
||||
id_incoming_sms = log_incoming(
|
||||
userid = int(user['id']) if user else None,
|
||||
gsm = gsm,
|
||||
codeword = codeword,
|
||||
@@ -94,7 +113,29 @@ def callback():
|
||||
ip = ip,
|
||||
simulation = 0)
|
||||
|
||||
return str(user.items()) if user else 'None'
|
||||
if app.config['DEBUG']:
|
||||
app.logger.debug("cursor.lastrowid: %s", id_incoming_sms)
|
||||
|
||||
if user:
|
||||
return jsonify(user.items())
|
||||
else:
|
||||
result = {'result': 'No existing member.', 'action': 'new_membership_send_code'}
|
||||
activation_code = generate_activation_code()
|
||||
log_sent(
|
||||
response_to = id_incoming_sms,
|
||||
msgid = 1337,
|
||||
sender = 'dns',
|
||||
receiver = gsm,
|
||||
countrycode = operator[:1],
|
||||
message = "Velkommen! Dette er et midlertidig medlemsbevis. Aktiver medlemskapet ditt her: https://s.neuf.no/sms?n=%s&c=%s" % (gsm, activation_code),
|
||||
operator = operator,
|
||||
codeword = codeword,
|
||||
billing_price = 230 * 100,
|
||||
use_dlr = 0,
|
||||
simulation = 0,
|
||||
activation_code = activation_code)
|
||||
|
||||
return jsonify(result)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
@@ -0,0 +1,22 @@
|
||||
from datetime import date
|
||||
from flask.json import JSONEncoder
|
||||
import string
|
||||
import random
|
||||
|
||||
class CustomJSONEncoder(JSONEncoder):
|
||||
def default(self, obj):
|
||||
try:
|
||||
if isinstance(obj, date):
|
||||
return obj.isoformat()
|
||||
iterable = iter(obj)
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
return list(iterable)
|
||||
return JSONEncoder.default(self, obj)
|
||||
|
||||
def generate_activation_code(length=5):
|
||||
chars = string.ascii_lowercase + string.digits
|
||||
# remove easily misintepreted chars
|
||||
chars = chars.translate(None,'0oil1wv')
|
||||
return ''.join(random.choice(chars) for _ in range(length))
|
||||
Reference in new issue
Block a user