2011-10-05 11:35:56 +02:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
|
|
|
|
# Licensed under GPL v3 or later
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import simplejson as json
|
|
|
|
from decimal import Decimal
|
|
|
|
import os
|
2011-10-05 17:30:05 +02:00
|
|
|
import base64
|
2011-10-24 19:08:09 +02:00
|
|
|
import copy
|
2011-10-05 11:35:56 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
sys.path.append('.')
|
|
|
|
from freitagslib.item import Item
|
|
|
|
from freitagslib.tools import require_type
|
|
|
|
|
|
|
|
|
|
|
|
DEPOSIT_CASH = 1
|
|
|
|
DEPOSIT_BANK = 2
|
|
|
|
|
|
|
|
|
2011-10-14 18:57:24 +02:00
|
|
|
_BASE_URL = 'https://k4ever.freitagsrunde.org:443/api2/'
|
2011-10-05 11:35:56 +02:00
|
|
|
|
2011-10-05 17:30:05 +02:00
|
|
|
_auth = base64.encodestring('%s:%s' % (os.environ['BARCODE_PLUGIN_USER'], os.environ['BARCODE_PLUGIN_PASS'])).rstrip()
|
|
|
|
_headers = {'Authorization':'Basic %s' % _auth}
|
2011-10-05 11:35:56 +02:00
|
|
|
|
2011-10-10 21:58:35 +02:00
|
|
|
ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT = range(3)
|
|
|
|
|
2011-10-05 11:35:56 +02:00
|
|
|
|
2011-10-24 19:08:09 +02:00
|
|
|
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 _request(api_rel_url, data, method, headers=None):
|
2011-10-05 11:35:56 +02:00
|
|
|
url = _BASE_URL + api_rel_url
|
2011-10-24 19:08:09 +02:00
|
|
|
|
|
|
|
if isinstance(data, dict):
|
|
|
|
data = urllib.urlencode(data)
|
|
|
|
|
|
|
|
# Add default headers
|
|
|
|
if headers is None:
|
|
|
|
final_headers = _headers
|
|
|
|
else:
|
|
|
|
final_headers = copy.copy(_headers)
|
|
|
|
final_headers.update(headers)
|
2011-10-05 11:35:56 +02:00
|
|
|
|
|
|
|
if method == 'GET':
|
|
|
|
url = '%s?%s' % (url, data)
|
2011-10-24 19:08:09 +02:00
|
|
|
req = urllib2.Request(url, headers=final_headers)
|
2011-10-05 11:35:56 +02:00
|
|
|
elif method == 'POST':
|
2011-10-24 19:08:09 +02:00
|
|
|
req = urllib2.Request(url, data=data, headers=final_headers)
|
2011-10-05 11:35:56 +02:00
|
|
|
else:
|
|
|
|
raise ValueError('Unsupported method "%s"' % method)
|
|
|
|
|
2011-10-24 19:08:09 +02:00
|
|
|
response = _urlopen(req)
|
2011-10-05 11:35:56 +02:00
|
|
|
return response.read()
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_name_from_auth_blob(authblob):
|
|
|
|
content = _request('auth/user/', {'authblob':authblob}, method='GET')
|
|
|
|
d = json.loads(content)
|
|
|
|
return d['username']
|
|
|
|
|
|
|
|
|
2011-10-10 21:58:35 +02:00
|
|
|
def buy_item(item_id, what_about_it, user_name):
|
2011-10-05 11:35:56 +02:00
|
|
|
require_type(int, item_id)
|
2011-10-10 21:58:35 +02:00
|
|
|
assert(what_about_it in (ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT))
|
2011-10-05 11:35:56 +02:00
|
|
|
|
|
|
|
content = _request('buyable/item/' + urllib.quote(str(item_id)),
|
2011-10-10 21:58:35 +02:00
|
|
|
{'user':user_name, 'deposit':what_about_it, 'amount':1}, method='POST')
|
2011-10-05 11:35:56 +02:00
|
|
|
if content != 'Created':
|
|
|
|
raise ValueError('Server says "%s"' % content)
|
|
|
|
|
|
|
|
|
2011-10-24 19:08:09 +02:00
|
|
|
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())
|
2011-10-26 17:51:38 +02:00
|
|
|
if not item_ids and not deposit_ids:
|
|
|
|
return
|
2011-10-24 19:08:09 +02:00
|
|
|
|
|
|
|
request_dict = {
|
|
|
|
'user':user_name,
|
|
|
|
'items':item_ids,
|
|
|
|
'deposits':deposit_ids
|
|
|
|
}
|
|
|
|
data = json.dumps(request_dict, sort_keys=True, indent=4 * ' ')
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Content-Type':'application/json',
|
|
|
|
'Content-Length':len(data)
|
|
|
|
}
|
|
|
|
content = _request('buyable/item/bulkbuy/', data, 'POST', headers=headers)
|
|
|
|
if content != 'Created':
|
|
|
|
raise ValueError('Server says "%s"' % content)
|
|
|
|
|
|
|
|
|
2011-10-05 21:50:14 +02:00
|
|
|
def get_item(barcode):
|
|
|
|
content = _request('buyable/item/',
|
|
|
|
{'barcode':barcode}, method='GET')
|
2011-10-05 11:35:56 +02:00
|
|
|
d = json.loads(content)
|
|
|
|
return Item(d)
|
|
|
|
|
|
|
|
|
|
|
|
def get_balance(user_name):
|
|
|
|
content = _request('account/balance/',
|
|
|
|
{'user':user_name}, method='GET')
|
|
|
|
d = json.loads(content)
|
|
|
|
return Decimal(d['balance'])
|
|
|
|
|
|
|
|
|
|
|
|
def deposit(amount, transaction_type, user_name):
|
|
|
|
require_type(Decimal, amount)
|
|
|
|
require_type(int, transaction_type)
|
|
|
|
|
|
|
|
content = _request('account/transactions/transact/',
|
|
|
|
{'user':user_name, 'amount':amount, 'type':transaction_type}, method='POST')
|
|
|
|
if content != 'OK':
|
|
|
|
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)
|
|
|
|
|
2011-10-10 07:33:13 +02:00
|
|
|
buy_item(1, True, user_name)
|
2011-10-05 11:35:56 +02:00
|
|
|
|
|
|
|
balance = get_balance(user_name)
|
|
|
|
print('Balance is %f, type is "%s"' % (balance, type(balance).__name__))
|
|
|
|
|
|
|
|
deposit(Decimal('0.01'), DEPOSIT_CASH, user_name)
|
|
|
|
deposit(Decimal('0.01'), DEPOSIT_BANK, user_name)
|
|
|
|
|
2011-10-24 19:08:09 +02:00
|
|
|
def show(item):
|
|
|
|
print('Item "%s" is %f Euro each' % (item.name, item.price))
|
|
|
|
|
|
|
|
item5 = get_item(5)
|
|
|
|
item6 = get_item(6)
|
|
|
|
for item in (item5, item6):
|
|
|
|
show(item)
|
|
|
|
|
|
|
|
from freitagslib.commands import BuyCommand
|
|
|
|
buycommands = (BuyCommand(e) for e in (item5, item6))
|
|
|
|
bulk_buy(buycommands, user_name)
|