From 1b7aaca19f3b68514920792cca616219fd959834 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Sat, 21 Jan 2012 22:07:13 +0100 Subject: [PATCH 01/11] getProfile ==> get_profile... --- k4ever/api2/handlers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/k4ever/api2/handlers.py b/k4ever/api2/handlers.py index 678dc71..49dc7d1 100644 --- a/k4ever/api2/handlers.py +++ b/k4ever/api2/handlers.py @@ -116,7 +116,7 @@ class BuyableItemHandler(BaseHandler): p.save(saveOrder=False) order.save() - return {'success': True, 'balance': request.user.getProfile().balance} + return {'success': True, 'balance': request.user.get_profile().balance} def bulkBuy(self, request): """Buy a :class:`Buyable ` item. @@ -179,7 +179,7 @@ class BuyableItemHandler(BaseHandler): p.save(saveOrder=False) order.save() - return {'success': True, 'balance': request.user.getProfile().balance} + return {'success': True, 'balance': request.user.get_profile().balance} class BuyableTypeHandler(BaseHandler): @@ -290,7 +290,7 @@ class TransactionTransactHandler(BaseHandler): return getError(rc.BAD_REQUEST, "Your TransactionType could not be found") trans = Transaction(user=request.user, transactionType=tType, amount=amount, checked=not tType.needsCheck) trans.save() - return {'success': True, 'balance': request.user.getProfile().balance} + return {'success': True, 'balance': request.user.get_profile().balance} class TransactionTypeHandler(BaseHandler): """Handler for :class:`Transaction Types ` @@ -348,7 +348,7 @@ class TransactionVirtualHandler(BaseHandler): return getError(rc.BAD_REQUEST, "The recipient user does not exist.") trans = VirtualTransaction(user=request.user, recipient=recipient, amount=amount, comment=comment) trans.save() - return {'success': True, 'balance': request.user.getProfile().balance} + return {'success': True, 'balance': request.user.get_profile().balance} class AccountBalanceHandler(BaseHandler): """Handler for the user's account balance""" From 9b18c4508ea3bb8a8dc3fbc6cdb3304a1edfa40a Mon Sep 17 00:00:00 2001 From: Konrad Mohrfeldt Date: Mon, 23 Jan 2012 00:10:32 +0100 Subject: [PATCH 02/11] update balance when buying items via javascript. fixes #10 --- k4ever/media/js/k4ever.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/k4ever/media/js/k4ever.js b/k4ever/media/js/k4ever.js index e3ff5d3..b09c105 100644 --- a/k4ever/media/js/k4ever.js +++ b/k4ever/media/js/k4ever.js @@ -25,6 +25,13 @@ jQuery(document).ready(function($) { }, options), callback); }; + var updateBalance = function(newBalance) { + var balanceString = $("span.balance").text().split(": "); + balanceString[1] = parseFloat(newBalance).toFixed(2).replace(".", ",") + " €"; + + $("span.balance").text(balanceString.join(": ")); + } + $.extend($.gritter.options, { position: "bottom-right", fade_in_speed: 250, @@ -61,6 +68,7 @@ jQuery(document).ready(function($) { }); dataContainer.children().removeClass("locked"); + updateBalance(data.balance); }, options); }); @@ -121,7 +129,7 @@ jQuery(document).ready(function($) { if(status === "success") { selected_item.addClass("success"); - //TODO neuer kontostand wär schnaffte + updateBalance(data.balance); } else { selected_item.addClass("failure"); } From 63ac10aad4d4b118d161bba0b5a3b1abdc39c514 Mon Sep 17 00:00:00 2001 From: Konrad Mohrfeldt Date: Mon, 23 Jan 2012 00:49:53 +0100 Subject: [PATCH 03/11] added exception templates. fixes #74 --- k4ever/main/templates/403.html | 15 +++++++++++++++ k4ever/main/templates/404.html | 17 +++++++++++++++++ k4ever/main/templates/500.html | 31 +++++++++++++++++++++++++++++++ k4ever/main/templates/base.html | 4 +++- 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 k4ever/main/templates/403.html create mode 100644 k4ever/main/templates/404.html create mode 100644 k4ever/main/templates/500.html diff --git a/k4ever/main/templates/403.html b/k4ever/main/templates/403.html new file mode 100644 index 0000000..a2473c8 --- /dev/null +++ b/k4ever/main/templates/403.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% comment %} +# This file is part of k4ever, a point-of-sale system +# Contact............ +# Website............ http://k4ever.someserver.de/ +# Bug tracker........ http://k4ever.someserver.de/report +# +# Licensed under GNU Affero General Public License v3 or later +{% endcomment %} + +{% block "content" %} +

Whoops! Wir glauben du gehörst hier nicht hin...

+

...zumindest sagt uns das dein Ausweis. Wie dem auch sei: Bis hierhin und nicht weiter

+{% endblock %} \ No newline at end of file diff --git a/k4ever/main/templates/404.html b/k4ever/main/templates/404.html new file mode 100644 index 0000000..e3431a9 --- /dev/null +++ b/k4ever/main/templates/404.html @@ -0,0 +1,17 @@ +{% extends "base.html" %} + +{% comment %} +# This file is part of k4ever, a point-of-sale system +# Contact............ +# Website............ http://k4ever.someserver.de/ +# Bug tracker........ http://k4ever.someserver.de/report +# +# Licensed under GNU Affero General Public License v3 or later +{% endcomment %} + +{% block "content" %} +

Whoops! Da ist was schiefgegangen

+

Die Seite, die du angefordert hast, konnte nicht gefunden werden.

+ +

Zahllose Ponies sind bereits auf der Suche nach ihr.

+{% endblock %} \ No newline at end of file diff --git a/k4ever/main/templates/500.html b/k4ever/main/templates/500.html new file mode 100644 index 0000000..f9eb85b --- /dev/null +++ b/k4ever/main/templates/500.html @@ -0,0 +1,31 @@ +{% extends "base.html" %} + +{% comment %} +# This file is part of k4ever, a point-of-sale system +# Contact............ +# Website............ http://k4ever.someserver.de/ +# Bug tracker........ http://k4ever.someserver.de/report +# +# Licensed under GNU Affero General Public License v3 or later +{% endcomment %} + +{% block head %} + + {% block extrastyle %}{% endblock %} + + + + + + +{% endblock head %} + +{% block "content" %} +

Whoops! Da ist was schiefgegangen

+

Bei der Bearbeitung deiner Anfrage ist ein Fehler aufgetreten für den du nichts kannst. Sorry!

+ +

Bitte nutze den Zurück-Button deines Browsers um zurückzufahren.

+{% endblock %} diff --git a/k4ever/main/templates/base.html b/k4ever/main/templates/base.html index 96e2da2..a496239 100644 --- a/k4ever/main/templates/base.html +++ b/k4ever/main/templates/base.html @@ -11,11 +11,12 @@ + {% block head %} {% block extrastyle %}{% endblock %} @@ -23,6 +24,7 @@ {% block extrahead %}{% endblock %} + {% endblock head %} Freitagsrundenkasse{% block "title" %}{% endblock %} From 3be9d20a581239e94d609ad6503df0d458b549b3 Mon Sep 17 00:00:00 2001 From: Konrad Mohrfeldt Date: Mon, 23 Jan 2012 00:50:35 +0100 Subject: [PATCH 04/11] remove Freitagsrunden-specific content from template --- k4ever/main/templates/registration/loginForm.html | 5 ----- 1 file changed, 5 deletions(-) diff --git a/k4ever/main/templates/registration/loginForm.html b/k4ever/main/templates/registration/loginForm.html index a0355df..13dfca2 100644 --- a/k4ever/main/templates/registration/loginForm.html +++ b/k4ever/main/templates/registration/loginForm.html @@ -29,8 +29,3 @@ - -
-

Das Kassensystem ist 'Aktiv' - nutzt euren Frunden-Account

-
- From 25c562ec16059ab61672b34640a082c31c95e321 Mon Sep 17 00:00:00 2001 From: tkroenert Date: Mon, 23 Jan 2012 10:25:30 +0100 Subject: [PATCH 05/11] website: removed "kassensystem ist aktiv"-banner --- k4ever/main/templates/registration/loginForm.html | 6 ------ 1 file changed, 6 deletions(-) diff --git a/k4ever/main/templates/registration/loginForm.html b/k4ever/main/templates/registration/loginForm.html index a0355df..803e98d 100644 --- a/k4ever/main/templates/registration/loginForm.html +++ b/k4ever/main/templates/registration/loginForm.html @@ -28,9 +28,3 @@ - - -
-

Das Kassensystem ist 'Aktiv' - nutzt euren Frunden-Account

-
- From e26563b94671eebffba76f21f658361036d6ca00 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 31 Jan 2012 02:12:51 +0100 Subject: [PATCH 06/11] Remove obsolete function buy_item() --- client-barcode/freitagslib/commands.py | 2 +- client-barcode/freitagslib/network.py | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/client-barcode/freitagslib/commands.py b/client-barcode/freitagslib/commands.py index eaf1d42..758207e 100644 --- a/client-barcode/freitagslib/commands.py +++ b/client-barcode/freitagslib/commands.py @@ -53,7 +53,7 @@ class BuyCommand(object): raise ValueError("BuyCommand in illegal state") def run(self, user_name): - net.buy_item(self._item.id, what_about_it=self._what_to_buy(), user_name=user_name) + raise NotImplementedError('BuyCommand.run(user_name)') def item_name(self): return self._item.name diff --git a/client-barcode/freitagslib/network.py b/client-barcode/freitagslib/network.py index 3dff65b..7727430 100644 --- a/client-barcode/freitagslib/network.py +++ b/client-barcode/freitagslib/network.py @@ -71,16 +71,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()) @@ -131,8 +121,6 @@ if __name__ == '__main__': user_name = get_user_name_from_auth_blob(os.environ['BARCODE_PLUGIN_TEST_AUTH_BLOB']) print('User name: ' + user_name) - buy_item(1, True, user_name) - balance = get_balance(user_name) print('Balance is %f, type is "%s"' % (balance, type(balance).__name__)) From 135fc1044cdc593efb6827e26ce429635e07e7c7 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 31 Jan 2012 02:13:07 +0100 Subject: [PATCH 07/11] Make configurable: server URL, user, password, debugging --- client-barcode/freitagskasse.py | 5 +++ client-barcode/freitagslib/network.py | 35 +++++++++------ client-barcode/freitagslib/settings.py | 61 ++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 14 deletions(-) create mode 100644 client-barcode/freitagslib/settings.py diff --git a/client-barcode/freitagskasse.py b/client-barcode/freitagskasse.py index 9a6cebc..8a334ab 100644 --- a/client-barcode/freitagskasse.py +++ b/client-barcode/freitagskasse.py @@ -8,6 +8,7 @@ from __future__ import print_function import freitagslib.network as net from freitagslib.commands import BuyCommand, DepositCommand from freitagslib.encoding import asciify +from freitagslib.settings import settings, Settings import colorama from colorama import Fore, Style @@ -519,6 +520,10 @@ def read_line(f, timeout, timeout_func): def main(): colorama.init() + + if not settings.load("freitagskasse.conf"): + sys.exit(1) + status = Status() global scroll_line1,scroll_line2 global myDisplay diff --git a/client-barcode/freitagslib/network.py b/client-barcode/freitagslib/network.py index 7727430..ebf77a2 100644 --- a/client-barcode/freitagslib/network.py +++ b/client-barcode/freitagslib/network.py @@ -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': @@ -118,7 +122,10 @@ def deposit(amount, transaction_type, user_name): if __name__ == '__main__': - user_name = get_user_name_from_auth_blob(os.environ['BARCODE_PLUGIN_TEST_AUTH_BLOB']) + if not settings.load('freitagskasse.conf', (('TEST_AUTH_BLOB', str, None), )): + sys.exit(1) + + user_name = get_user_name_from_auth_blob(settings.TEST_AUTH_BLOB) print('User name: ' + user_name) balance = get_balance(user_name) diff --git a/client-barcode/freitagslib/settings.py b/client-barcode/freitagslib/settings.py new file mode 100644 index 0000000..7fd4220 --- /dev/null +++ b/client-barcode/freitagslib/settings.py @@ -0,0 +1,61 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# Copyright (C) 2011 Sebastian Pipping +# Licensed under GPL v3 or later + +from __future__ import print_function +import sys +import ConfigParser + + +_KEYS = ( + # Key, type, default + ('CLIENT_DEBUG', bool, False), # Should go first to enable debugging as early as possible + ('SERVER_URL', str, None), + ('SERVER_USER', str, None), + ('SERVER_PASSWORD', str, None), +) + +_DEFAULTS = dict([(key, str(default)) + for (key, _type, default) + in _KEYS + if default is not None]) + + +class Settings: + def __init__(self): + pass + + def load(self, filename, _additional_keys=None): + additional_keys = () if _additional_keys is None else _additional_keys + + cp = ConfigParser.ConfigParser(_DEFAULTS) + if not cp.read(filename): + print('FATAL: Config file %s could not be parsed.' % (filename), file=sys.stderr) + return False + + valid = True + for key, _type, dummy in _KEYS + additional_keys: + try: + if _type is bool: + value = cp.getboolean('client-barcode', key) + else: + value = cp.get('client-barcode', key) + except ValueError: + print('ERROR: Key %s in config file %s must be of type %s.' \ + % (key, filename, _type.__name__), file=sys.stderr) + valid = False + except ConfigParser.NoOptionError: + print('ERROR: Config file %s misses required key %s.' \ + % (filename, key), file=sys.stderr) + valid = False + else: + setattr(self, key, value) + if self.CLIENT_DEBUG: + print('DEBUG: CONFIG: %s=%s("%s")' % (key, type(value).__name__, value)) + + return valid + + +settings = Settings() From 62f0f02fa7c7b2f013a756e1846b966931434a51 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 31 Jan 2012 01:48:53 +0100 Subject: [PATCH 08/11] Migrate to JSON response --- client-barcode/freitagslib/network.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client-barcode/freitagslib/network.py b/client-barcode/freitagslib/network.py index ebf77a2..b93d649 100644 --- a/client-barcode/freitagslib/network.py +++ b/client-barcode/freitagslib/network.py @@ -93,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) @@ -117,7 +118,8 @@ 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) From 692284f41dfd487ff1f82f3ee0b5b3cef9827599 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 31 Jan 2012 02:24:05 +0100 Subject: [PATCH 09/11] Extract constant for config file section .. and improve config error reporting --- client-barcode/freitagslib/settings.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/client-barcode/freitagslib/settings.py b/client-barcode/freitagslib/settings.py index 7fd4220..e52e42e 100644 --- a/client-barcode/freitagslib/settings.py +++ b/client-barcode/freitagslib/settings.py @@ -9,6 +9,8 @@ import sys import ConfigParser +_SECTION = 'client-barcode' + _KEYS = ( # Key, type, default ('CLIENT_DEBUG', bool, False), # Should go first to enable debugging as early as possible @@ -32,28 +34,28 @@ class Settings: cp = ConfigParser.ConfigParser(_DEFAULTS) if not cp.read(filename): - print('FATAL: Config file %s could not be parsed.' % (filename), file=sys.stderr) + print('FATAL: Config file "%s" could not be parsed.' % (filename), file=sys.stderr) return False valid = True for key, _type, dummy in _KEYS + additional_keys: try: if _type is bool: - value = cp.getboolean('client-barcode', key) + value = cp.getboolean(_SECTION, key) else: - value = cp.get('client-barcode', key) + value = cp.get(_SECTION, key) except ValueError: - print('ERROR: Key %s in config file %s must be of type %s.' \ + print('ERROR: Key "%s" in config file "%s" must be of type "%s".' \ % (key, filename, _type.__name__), file=sys.stderr) valid = False except ConfigParser.NoOptionError: - print('ERROR: Config file %s misses required key %s.' \ - % (filename, key), file=sys.stderr) + print('ERROR: Config file "%s" misses required key "%s.%s".' \ + % (filename, _SECTION, key), file=sys.stderr) valid = False else: setattr(self, key, value) if self.CLIENT_DEBUG: - print('DEBUG: CONFIG: %s=%s("%s")' % (key, type(value).__name__, value)) + print('DEBUG: CONFIG: %s.%s=%s("%s")' % (_SECTION, key, type(value).__name__, value)) return valid From 098ff4786ae2e692011971d3ef082bfc5ccff0d4 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Tue, 31 Jan 2012 02:27:36 +0100 Subject: [PATCH 10/11] Add sample config --- client-barcode/freitagskasse.conf.sample | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 client-barcode/freitagskasse.conf.sample diff --git a/client-barcode/freitagskasse.conf.sample b/client-barcode/freitagskasse.conf.sample new file mode 100644 index 0000000..3cae8bd --- /dev/null +++ b/client-barcode/freitagskasse.conf.sample @@ -0,0 +1,5 @@ +[client-barcode] +SERVER_URL = https://example.org:443/api2/ +SERVER_USER = user +SERVER_PASSWORD = password +#CLIENT_DEBUG = 1 From c7a140843d8fe92656c6411a85e39ce5866f2415 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Wed, 1 Feb 2012 22:39:11 +0100 Subject: [PATCH 11/11] Fix preislister config --- preislister/main.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/preislister/main.py b/preislister/main.py index a424d2b..b746e51 100755 --- a/preislister/main.py +++ b/preislister/main.py @@ -15,6 +15,7 @@ import shutil sys.path.insert(0, '../client-barcode') # TODO from freitagslib.network import _request from freitagslib.item import Item +from freitagslib.settings import settings, Settings _NAME_BLACKLIST = ('foo', ) @@ -38,6 +39,9 @@ if __name__ == '__main__': sys.exit(1) output_filename = sys.argv[1] + if not settings.load('../client-barcode/freitagskasse.conf'): + sys.exit(1) + content = _request('buyable/item/', None, method='GET') doc = json.loads(content) items = [Item(e) for e in doc]