Archived
Move some Sendega and Inside utilities
This commit is contained in:
1 parent
91bc493153
commit
6e31ee9645
2 files changed
+154
No files matched your search
@@ -0,0 +1,99 @@
|
||||
#!/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()
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import suds
|
||||
|
||||
|
||||
class Sendega(object):
|
||||
"""Sendega."""
|
||||
|
||||
def __init__(self, wsdl, username, password,
|
||||
sender_bulk, sender_billing, dlr_url,
|
||||
service_port='ContentSoap'):
|
||||
self.wsdl = wsdl
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.sender_bulk = sender_bulk
|
||||
self.sender_billing = sender_billing
|
||||
self.dlr_url = dlr_url
|
||||
self.service_port = service_port
|
||||
|
||||
self.client = suds.client.Client(wsdl)
|
||||
self.client.set_options(port=self.service_port)
|
||||
|
||||
def create_sms(self, destination, content, billing=False,
|
||||
price=0, reference=None):
|
||||
if isinstance(destination, (list, tuple, set)):
|
||||
assert len(destination) <= 100, "Max 100 destinations"
|
||||
destination = ','.join(destination)
|
||||
|
||||
args = {
|
||||
'username': self.username,
|
||||
'password': self.password,
|
||||
'sender': self.sender_billing if billing else self.sender_bulk,
|
||||
'destination': destination, # "4712345678", may be comma separated
|
||||
'pricegroup': price, # 100 = 1 kr
|
||||
'contentTypeID': 5 if billing else 1, # 1 = bulk, 5 = premium GAS
|
||||
'contentHeader': "", # empty string for other content types
|
||||
'content': content, # gets split automatically if length > 160
|
||||
'dlrUrl': self.dlr_url if billing else "",
|
||||
'ageLimit': 0, # end-user age limit for premium or adult services
|
||||
'extID': str(reference) if reference else "", # local unique ID, returned in DLR
|
||||
'sendDate': "", # delayed delivery, YYYY-MM-DD HH:MM:SS
|
||||
'refID': "", # used when sending premium SMS to some countries
|
||||
'priority': 0, # 0 = normal priority
|
||||
'gwID': 0, # specific gateway
|
||||
'pid': 0, # protocol id
|
||||
'dcs': 0, # data coding scheme
|
||||
}
|
||||
|
||||
return args
|
||||
|
||||
def send(self, args):
|
||||
result = self.client.service.Send(**args)
|
||||
|
||||
return dict(result)
|
||||
Reference in new issue
Block a user