No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
  5. # Licensed under GPL v3 or later
  6. import freitagslib.network as net
  7. from freitagslib.network import ITEM_ONLY, DEPOSIT_ONLY, ITEM_AND_DEPOSIT
  8. class DepositCommand(object):
  9. def __init__(self, amount):
  10. self._difference = amount
  11. def difference(self):
  12. return self._difference
  13. def add(self, deposit_command):
  14. assert(isinstance(deposit_command, DepositCommand))
  15. self._difference += deposit_command._difference
  16. def run(self, user_name):
  17. net.deposit(self._difference, net.DEPOSIT_CASH, user_name)
  18. def label(self):
  19. return '%.2f Euro einzahlen' % self._difference
  20. class BuyCommand(object):
  21. def __init__(self, item):
  22. self._item = item
  23. self._commodity = True
  24. self._deposit = False
  25. def _what_to_buy(self):
  26. if self._deposit and self._commodity:
  27. return ITEM_AND_DEPOSIT
  28. elif self._deposit:
  29. return DEPOSIT_ONLY
  30. elif self._commodity:
  31. return ITEM_ONLY
  32. else:
  33. raise ValueError("BuyCommand in illegal state")
  34. def difference(self):
  35. if self._deposit and self._commodity:
  36. return -(self._item.price + self._item.deposit)
  37. elif self._deposit:
  38. return -(self._item.deposit)
  39. elif self._commodity:
  40. return -(self._item.price)
  41. else:
  42. raise ValueError("BuyCommand in illegal state")
  43. def run(self, user_name):
  44. raise NotImplementedError('BuyCommand.run(user_name)')
  45. def item_name(self):
  46. return self._item.name
  47. def item_id(self):
  48. return self._item.id
  49. def commodity_label(self):
  50. if self._deposit or self.deposit_value() <= 0:
  51. return self._item.name
  52. else:
  53. return "%s (exkl. Pfand)" % self._item.name
  54. def deposit_label(self):
  55. return '%s Pfand' % self._item.name
  56. def includes_commodity(self):
  57. return self._commodity
  58. def includes_deposit(self):
  59. return self._deposit
  60. def include_commodity(self, commodity):
  61. self._commodity = commodity
  62. def include_deposit(self, deposit):
  63. self._deposit = deposit
  64. def commodity_value(self):
  65. return self._item.price
  66. def deposit_value(self):
  67. return self._item.deposit