-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaehlcettel.py
executable file
·363 lines (308 loc) · 11.2 KB
/
caehlcettel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python3
import json
import sys
import os
from decimal import Decimal
from datetime import datetime
import requests
from pyfiglet import Figlet
from rich import print
from rich.panel import Panel
from rich.text import Text
from textual.app import App, ComposeResult, RenderResult
from textual.binding import Binding
from textual.widget import Widget
from textual.widgets import Header, Footer, Static, Input, Button
from textual.reactive import reactive
from textual.containers import Grid
from textual.screen import Screen
from textual import events
from textual import log
# from rendering import print_zettel
DEFAULT_PRINTER = 'bondruccer.cbrp3.c-base.org'
class TotalContainer(Static):
def compose(self) -> ComposeResult:
yield CountLabel("Summe")
yield Total()
class Total(Static):
"""
Big number display renders that shows a number in a figlet font.
"""
sum = reactive(0.0)
def render(self) -> RenderResult:
font = Figlet(font='clb6x10')
return font.renderText(f'{self.sum:.2f}'.replace('.', ',')).rstrip("\n")
class DateTimeDisplay(Widget):
def on_mount(self):
self.set_interval(1, self.refresh)
def render(self) -> Panel:
time = datetime.now().strftime("%Y-%m-%d %H:%M")
return Panel(time, title="Datum / Uhrzeit")
class TitleDisplay(Static):
def on_mount(self):
self.set_interval(1, self.refresh)
def render(self) -> Panel:
time = datetime.now().strftime("%c")
return Panel(time, title="Datum / Uhrzeit")
class CountLabel(Static):
pass
class PositiveNumberInput(Input):
def on_key(self, event: events.Key) -> None:
try:
my_val = int(self.value)
except ValueError:
my_val = 0
if event.key == 'up':
self.value = str(my_val + 1)
if event.key == 'down':
if my_val == 0:
self.value = '0'
else:
self.value = str(my_val - 1)
return super().on_key(event)
class CountInput(Static):
"""An input widget with a title."""
def __init__(self, *args, **kwargs):
self.label = kwargs.pop('label')
self.default_bg = None
super().__init__(*args, **kwargs)
async def on_input_changed(self, message: Input.Changed) -> None:
if self.default_bg is None:
self.default_bg = self.styles.background
if message.value == '':
return
try:
if int(message.value) < 0:
raise ValueError("negative not allowed")
except ValueError:
# self.default_bg = self.styles.background
def reset_bg():
self.styles.background = self.default_bg
self.styles.background = 'red'
self.set_timer(1.0, reset_bg)
def compose(self) -> ComposeResult:
yield CountLabel(self.label)
yield PositiveNumberInput(placeholder="0", id=self.id)
class QuitScreen(Screen):
def compose(self) -> ComposeResult:
yield Grid(
Static("Bitte Barbot eingeben.", id="question"),
Button("Okay", variant="primary", id="okay_button"),
id="dialog",
)
def on_mount(self):
self.query_one('#okay_button').focus()
def on_button_pressed(self, event: Button.Pressed) -> None:
self.app.pop_screen()
class DateTimeDisplay(Static):
DATE_FORMAT = "%Y-%m-%d %H:%M"
time = reactive('Titten Gna')
def on_mount(self) -> None:
self.update_time()
self.set_interval(1.0, self.update_time)
def update_time(self) -> None:
self.time = f'Datum / Uhrzeit: [b]{datetime.now().strftime(self.DATE_FORMAT)}[/]'
def watch_time(self, time: float) -> None:
"""Called when the time attribute changes."""
self.update(time)
def generate_denominations():
count_type = os.environ.get('COUNT_TYPE', 'tresencasse')
if count_type == 'tresencasse':
return [
# ('500,00', '50000'),
('200,00', '20000'),
('100,00', '10000'),
('50,00', '5000'),
('20,00', '2000'),
('10,00', '1000'),
('5,00', '500'),
('2,00', '200'),
('1,00', '100'),
('0,50', '50'),
('0,20', '20'),
('0,10', '10'),
# not needed at the bar
# ('0,05', '5'),
# ('0,02', '2'),
# ('0,01', '1'),
]
elif count_type == 'board':
return [
# ('500,00', '50000'),
('200,00', '20000'),
('100,00', '10000'),
('50,00', '5000'),
('20,00', '2000'),
('10,00', '1000'),
('5,00', '500'),
('2,00', '200'),
('1,00', '100'),
('0,50', '50'),
('0,20', '20'),
('0,10', '10'),
('0,05', '5'),
('0,02', '2'),
('0,01', '1'),
('Safebag', 'safebag_in_cent'),
]
elif count_type == 'replicator':
return [
('200,00', '20000'),
('100,00', '10000'),
('50,00', '5000'),
('20,00', '2000'),
('10,00', '1000'),
('5,00', '500'),
('2,00', '200'),
('1,00', '100'),
('0,50', '50'),
('0,20', '20'),
('0,10', '10'),
]
else:
raise Exception("COUNT_TYPE=`%s` not supported")
class MainApp(App):
"""Demonstrates custom widgets"""
CSS_PATH = "caehlcettel.css"
BINDINGS = [
Binding(key="Ctrl+C", action="quit", description="Quit"),
Binding(key="f11", action="print", description="Print and quit"),
]
DENOMINATIONS = generate_denominations()
def compose(self) -> ComposeResult:
yield Header(show_clock=True)
for denom, id_name in self.DENOMINATIONS:
title=f"{denom}"
name=f"input_{denom}".replace(',', '')
if id_name.isnumeric():
my_id=f"id_input_{id_name}".replace(',', '')
else:
my_id=f"id_input_{id_name}"
yield CountInput(name=name, id=my_id, label=title)
yield TotalContainer()
yield Input(name="barbot", id="barbot", placeholder='Barbot')
yield DateTimeDisplay('Datum / Uhrzeit')
yield Footer()
def on_mount(self) -> None:
self.title = 'c-base console-based caehlcettel'
self.query(PositiveNumberInput)[0].focus()
def collect_values(self):
"""
Collect the entered values to get a JSON dict like
{
'number_of_00500': 23,
...
}
"""
json_data = {}
for number_input in self.query(PositiveNumberInput):
form_field_id = number_input.id.rsplit('_', 1)[1]
if form_field_id.isnumeric():
int_val = int(Decimal(form_field_id))
json_name = f"number_of_{str(int_val).zfill(5)}"
else:
json_name = number_input.id.replace('id_input_', '')
# The API does not accept a value that is `null` or a negative value.
the_value = 0
if number_input.value:
try:
the_value = int(number_input.value)
if the_value < 0:
the_value = 0 # negative values not allowed.
except ValueError:
pass
if the_value is None or the_value < 0:
the_value = 0
json_data[json_name] = the_value
# finished
return json_data
def calculate_total(self):
grand_total = Decimal(0)
for number_input in self.query(PositiveNumberInput):
field_id = number_input.id.rsplit('_', 1)[1]
if field_id.isnumeric():
denomination = Decimal(field_id) / 100
else:
denomination = Decimal('0.01')
# Now
if number_input.value:
try:
val = int(number_input.value)
if val < 0:
continue
grand_total += denomination * val
except ValueError:
pass
return grand_total
async def on_input_changed(self, message: Input.Changed) -> None:
grand_total = self.calculate_total()
self.query_one(Total).sum = grand_total
async def action_quit(self) -> None:
await self.shutdown()
async def action_print(self) -> None:
# check if barbot name field is empty.
barbot_name = self.query_one('#barbot').value.strip()
if not barbot_name:
self.push_screen(QuitScreen())
return
context = {
'state': [],
'total': self.calculate_total(),
'datetime': datetime.now().strftime('%Y-%m-%d, %H:%M Uhr'),
}
# Get the access tokens for the REST-API
access_token = os.environ.get('ACCESS_TOKEN', None)
if not access_token:
raise ValueError("Environment variable ACCESS_TOKEN not set!")
api_base_url = os.environ.get('API_BASE_URL', None)
if not api_base_url:
raise ValueError('Environment variable API_BASE_URL not set!')
# Create the JSON object that will be sent to the API
json_data = self.collect_values()
json_data["username"] = barbot_name
# json_data["count_type"] = count_type
json_data["count_type"] = os.environ.get('COUNT_TYPE', 'tresencasse')
counting_url = f'{api_base_url}/count/'
# Do the request
resp = requests.post(
url=counting_url,
json=json_data,
headers={
"Authorization": f"Token {access_token}"
},
timeout=5
)
if resp.status_code not in [200, 201, 204]:
self._exit_renderables.extend([
Text.from_markup(f"URL: [i blue underline]{counting_url}[/]\n"),
Text.from_markup(f'JSON content sent: {json.dumps(json_data, indent=2)}'),
Text.from_markup(f'HTTP status code: {resp.status_code}'),
Text.from_markup(f'HTTP response content: {resp.content}'),
])
resp.raise_for_status()
receipt_url = resp.json()['url']
print_url = receipt_url + 'print/'
print_resp = requests.get(
url=print_url,
params={
'printer': os.environ.get('PRINTER_HOSTNAME', DEFAULT_PRINTER),
},
headers={
"Authorization": f"Token {access_token}"
},
timeout=5
)
if print_resp.status_code not in [200, 201, 204]:
self._exit_renderables.extend([
Text.from_markup(f"URL: [i blue underline]{print_url}[/]\n"),
Text.from_markup(f'HTTP status code: {print_resp.status_code}'),
Text.from_markup(f'HTTP response content: {print_resp.content}'),
])
resp.raise_for_status()
self.exit()
if __name__ == '__main__':
try:
app = MainApp()
app.run()
except Exception as err:
print(err)