|
|
|
@ -18,39 +18,43 @@ if __name__ == '__main__':
|
|
|
|
|
sys.path.append('.')
|
|
|
|
|
from freitagslib.item import Item
|
|
|
|
|
from freitagslib.tools import require_type
|
|
|
|
|
from freitagslib.settings import settings, Settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEPOSIT_CASH = 1
|
|
|
|
|
DEPOSIT_BANK = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_BASE_URL = 'https://k4ever.freitagsrunde.org:443/api2/'
|
|
|
|
|
ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT = range(3)
|
|
|
|
|
|
|
|
|
|
_auth = base64.encodestring('%s:%s' % (os.environ['BARCODE_PLUGIN_USER'], os.environ['BARCODE_PLUGIN_PASS'])).rstrip()
|
|
|
|
|
_headers = {'Authorization':'Basic %s' % _auth}
|
|
|
|
|
|
|
|
|
|
ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT = range(3)
|
|
|
|
|
def _urlopen(*args):
|
|
|
|
|
if settings.CLIENT_DEBUG:
|
|
|
|
|
_h = urllib2.HTTPHandler(debuglevel=1)
|
|
|
|
|
_opener = urllib2.build_opener(_h)
|
|
|
|
|
_func = _opener.open
|
|
|
|
|
else:
|
|
|
|
|
_func = urllib2.urlopen
|
|
|
|
|
return _func(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if bool(os.environ.get('BARCODE_PLUGIN_DEBUG', False)):
|
|
|
|
|
_h = urllib2.HTTPHandler(debuglevel=1)
|
|
|
|
|
_opener = urllib2.build_opener(_h)
|
|
|
|
|
_urlopen = _opener.open
|
|
|
|
|
else:
|
|
|
|
|
_urlopen = urllib2.urlopen
|
|
|
|
|
def _get_default_headers():
|
|
|
|
|
_auth = base64.encodestring('%s:%s' % (settings.SERVER_USER, settings.SERVER_PASSWORD)).rstrip()
|
|
|
|
|
_headers = {'Authorization':'Basic %s' % _auth}
|
|
|
|
|
return _headers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _request(api_rel_url, data, method, headers=None):
|
|
|
|
|
url = _BASE_URL + api_rel_url
|
|
|
|
|
url = settings.SERVER_URL + api_rel_url
|
|
|
|
|
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
|
data = urllib.urlencode(data)
|
|
|
|
|
|
|
|
|
|
# Add default headers
|
|
|
|
|
if headers is None:
|
|
|
|
|
final_headers = _headers
|
|
|
|
|
final_headers = _get_default_headers()
|
|
|
|
|
else:
|
|
|
|
|
final_headers = copy.copy(_headers)
|
|
|
|
|
final_headers = copy.copy(_get_default_headers())
|
|
|
|
|
final_headers.update(headers)
|
|
|
|
|
|
|
|
|
|
if method == 'GET':
|
|
|
|
@ -71,16 +75,6 @@ def get_user_name_from_auth_blob(authblob):
|
|
|
|
|
return d['username']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def buy_item(item_id, what_about_it, user_name):
|
|
|
|
|
require_type(int, item_id)
|
|
|
|
|
assert(what_about_it in (ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT))
|
|
|
|
|
|
|
|
|
|
content = _request('buyable/item/' + urllib.quote(str(item_id)),
|
|
|
|
|
{'user':user_name, 'deposit':what_about_it, 'amount':1}, method='POST')
|
|
|
|
|
if content != 'Created':
|
|
|
|
|
raise ValueError('Server says "%s"' % content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def bulk_buy(buycommands, user_name):
|
|
|
|
|
item_ids = tuple(e.item_id() for e in buycommands if e.includes_commodity())
|
|
|
|
|
deposit_ids = tuple(e.item_id() for e in buycommands if e.includes_deposit())
|
|
|
|
@ -99,7 +93,8 @@ def bulk_buy(buycommands, user_name):
|
|
|
|
|
'Content-Length':len(data)
|
|
|
|
|
}
|
|
|
|
|
content = _request('buyable/item/bulkbuy/', data, 'POST', headers=headers)
|
|
|
|
|
if content != 'Created':
|
|
|
|
|
d = json.loads(content)
|
|
|
|
|
if not d['success']:
|
|
|
|
|
raise ValueError('Server says "%s"' % content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -123,15 +118,17 @@ def deposit(amount, transaction_type, user_name):
|
|
|
|
|
|
|
|
|
|
content = _request('account/transactions/transact/',
|
|
|
|
|
{'user':user_name, 'amount':amount, 'type':transaction_type}, method='POST')
|
|
|
|
|
if content != 'OK':
|
|
|
|
|
d = json.loads(content)
|
|
|
|
|
if not d['success']:
|
|
|
|
|
raise ValueError('Server says "%s"' % content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
user_name = get_user_name_from_auth_blob(os.environ['BARCODE_PLUGIN_TEST_AUTH_BLOB'])
|
|
|
|
|
print('User name: ' + user_name)
|
|
|
|
|
if not settings.load('freitagskasse.conf', (('TEST_AUTH_BLOB', str, None), )):
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
buy_item(1, True, user_name)
|
|
|
|
|
user_name = get_user_name_from_auth_blob(settings.TEST_AUTH_BLOB)
|
|
|
|
|
print('User name: ' + user_name)
|
|
|
|
|
|
|
|
|
|
balance = get_balance(user_name)
|
|
|
|
|
print('Balance is %f, type is "%s"' % (balance, type(balance).__name__))
|
|
|
|
|