-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkismau.py
More file actions
executable file
·405 lines (319 loc) · 15 KB
/
Copy pathkismau.py
File metadata and controls
executable file
·405 lines (319 loc) · 15 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#! /usr/bin/env python
# coding: utf-8
## kismau.py
## This file contains the main GUI and event handling logic for the Soci krysse-system.
## The main logic is contained in the class "Vindu" which is the main graphical entity the system is comprised off.
## All buying of goods is taken care of in the class "HandleKurv"
## Created by: Uknown Alien
## Date: Some time in the past
## Not created by (For the sake of having a name and a date): Tormod Haugland
## Date: 10/03/2015
import wx, os, codecs, time, datetime, pg, glob, random, logging, logging.config, re, sys
# For scheduling jobs
import threading
# Import our almighty DbHandler
from dbhandler import DbHandler as DbHandler
# konstanter for programmet - huske å endre dette...
# tegnkoder
BILDEVISERSEKUNDER = 10
ENTER = 13
# Brukt til å finne noen viktige bilder
BASE_DIR = "/home/ksg/soci_kismau/"
# hovedvinduet - parent til de andre og lytter for keystrokes
class Vindu(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, wx.ID_ANY, title)
self.SetCursor(wx.StockCursor(wx.CURSOR_BLANK))
self.wnd = wx.Panel(self, -1)
self.kortnummer = ""
self.bildeviser = Bildeviser(self, -1)
self.bildeviser.Show()
self.handlekurv = None
self.admin = None
self.melding = None
wx.EVT_CHAR(self.wnd, self.Trykk)
self.wnd.SetFocus()
self.Fit()
self.Show(True)
self.ShowFullScreen(True)
# Add this window-instance as the main window-root of the program
db.set_root(self)
def Trykk(self, event):
if isinstance(self.handlekurv, Handlekurv):
self.handlekurv.Trykk(event)
elif 'fffff' in self.kortnummer: # hvis man taster spritspritspritspritsprit skal programmet avslutte og automatisk startes på nytt (feilrettingsgreie)
self.Close(True)
elif isinstance(self.admin, Bilde):
self.admin.Destroy()
self.admin = None
elif event.GetKeyCode() == ENTER:
kortnummer = None
if len(self.kortnummer) == 10:
kortnummer = self.kortnummer
else:
match = re.findall('([0-9]{6,10})_$', self.kortnummer)
if len(match) == 0: # ingen treff på regex - altså ikke trukket et samfundetkort
self.melding = Melding(self, u"Ikke kort: %s" % self.kortnummer, wx.RED, wx.BLACK) # DEBUG
return
else:
kortnummer = match[len(match) - 1]
print(kortnummer)
person = db.getPersonFromCard(kortnummer)
if (not person):
# Some cards may have a hidden 0
if (kortnummer[0] == '0'):
second_attempt = kortnummer[1:]
person = db.getPersonFromCard(second_attempt)
# This didn't work either
if (not person):
self.melding = Melding(self, "Kortnummeret %s er ukjent." % kortnummer, wx.RED, wx.BLACK)
else:
if (person["saldo"] < 0):
self.melding = Melding(self, "Du er svart!", wx.RED, wx.BLACK)
elif (person["saldo"] < 10):
self.melding = Melding(self, "Du er ikke svart,\n men du har strengt talt ikke råd til noe...",
wx.WHITE, wx.BLACK)
else:
self.NyHandlekurv(person)
# Legacy code. Keep for now?
# if 'ccff' in self.kortnummer:
# self.admin = SisteKryss(self)
# self.admin.Show()
# elif person[2] == False: #personen er svart...
# self.melding = Melding(self, "Du er svart!", wx.RED, wx.BLACK)
# else:
# self.NyHandlekurv(person[0:2])
self.kortnummer = "" # null ut inntastingen
else:
self.kortnummer = self.kortnummer + chr(event.GetKeyCode())
def NyHandlekurv(self, person):
self.handlekurv = Handlekurv(self, -1, person)
def FjernHandlekurv(self, tekst, bakgrunn, forgrunn):
self.handlekurv.Destroy()
self.handlekurv = None
self.melding = Melding(self, tekst, bakgrunn, forgrunn)
class Melding(wx.Panel):
def __init__(self, parent, tekst, bakgrunn=wx.BLACK, forgrunn=wx.WHITE):
wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.GetDisplaySize())
self.SetBackgroundColour(bakgrunn)
tekst = wx.StaticText(self, -1, tekst, (0, wx.GetDisplaySize().GetHeight() / 2),
(wx.GetDisplaySize().GetWidth(), -1), style=wx.ALIGN_CENTRE)
tekst.SetFont(wx.Font(26, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
tekst.SetForegroundColour(forgrunn)
self.timer = wx.PyTimer(self.Destroy)
self.timer.Start(2000, wx.TIMER_ONE_SHOT)
class Bilde(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, wx.DefaultPosition, wx.GetDisplaySize())
self.SetBackgroundColour(wx.BLACK)
self.Hide()
class SisteKryss(Bilde):
def __init__(self, parent):
Bilde.__init__(self, parent)
tekst = wx.StaticText(self, -1, u"Krysset de 24 siste timene - rangert etter siste kryss:\n\n", (0, 50),
(wx.GetDisplaySize().GetWidth(), -1), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(24, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
t = u""
q = db.query(
"SELECT navn, TO_CHAR(MAX(k.kryssetid),'DD.MM. HH24:MI') FROM \"SociPersoner\" p JOIN \"SociKryss\" k ON p.id=k.person GROUP BY p.id,p.navn ORDER BY MAX(k.kryssetid) DESC LIMIT 20;")
for l in q.getresult():
t = t + u"%s: %s\n" % (l[1].decode('utf-8'), l[0].decode('utf-8'))
tekst = wx.StaticText(self, -1, t, (10, 100))
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(12, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
class Omsetning(Bilde):
def __init__(self, parent):
Bilde.__init__(self, parent)
tekst = wx.StaticText(self, -1, u"Omsetning På Societeten\n\n", (0, 100), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(40, wx.FONTFAMILY_SWISS, wx.ITALIC, wx.NORMAL))
# tekst.Center()
t = ""
r = db.query(
"SELECT SUM(pris) FROM \"Kryss\" WHERE TO_CHAR(kryssetid+interval '4 hours','YYYY MM DD') = TO_CHAR(NOW()+interval '4 hours','YYYY MM DD');")
if (r == None):
return
r = r.getresult()[0][0]
if r != None:
t = t + u"I kveld: %s kr\n\n" % r
r = db.query(
"SELECT SUM(pris) FROM \"Kryss\" WHERE TO_CHAR(kryssetid,'YYYY IW') = TO_CHAR(NOW(),'YYYY IW');").getresult()[
0][0]
if r != None:
t = t + u"Denne uka: %s kr\n\n" % r
r = db.query(
"SELECT SUM(pris) FROM \"Kryss\" WHERE TO_CHAR(kryssetid,'YYYY MM') = TO_CHAR(NOW(),'YYYY MM');").getresult()[
0][0]
if r != None:
t = t + u"Denne måneden: %s kr\n\n" % r
r = db.query(
"SELECT SUM(pris) FROM \"Kryss\" WHERE EXTRACT(year FROM kryssetid) = EXTRACT(year FROM NOW());").getresult()[
0][0]
if r != None:
t = t + u"I år: %s kr\n\n" % r
tekst = wx.StaticText(self, -1, t, (100, 200))
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(24, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
# tekst.Centre()
class Bursdager(Bilde):
def __init__(self, parent):
Bilde.__init__(self, parent)
dberror = False
try:
r = db.query(
"SELECT * FROM \"Personer\" WHERE EXTRACT(day FROM fodselsdato) = EXTRACT(day FROM NOW()) AND EXTRACT(month FROM fodselsdato) = EXTRACT(month FROM NOW()) ORDER BY navn;");
except pg.InternalError:
dberror = True
if not dberror and r.ntuples() > 0:
tekst = wx.StaticText(self, -1, u"Dagens Bursdagsbarn", (0, 100), (wx.GetDisplaySize().GetWidth(), -1),
style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(40, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
t = ""
for k in r.dictresult():
t = t + k['navn'].decode('utf-8') + "\n\n"
t = t + "\n\nGratulerer!"
tekst = wx.StaticText(self, -1, t, (0, 300), (wx.GetDisplaySize().GetWidth(), -1), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(24, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
else:
t = u"Alt var bedre før! :)"
tekst = wx.StaticText(self, -1, t, (0, 100), (wx.GetDisplaySize().GetWidth(), -1), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(24, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
class Galleri(Bilde):
def __init__(self, parent):
Bilde.__init__(self, parent)
filer = glob.glob("galleri/*.jpg")
if len(filer) > 0:
self.bilde = wx.StaticBitmap(self, -1, wx.Image(random.choice(filer)).Scale(wx.GetDisplaySize().GetWidth(),
wx.GetDisplaySize().GetHeight()).ConvertToBitmap(),
(0, 0), style=0)
class Bildeviser(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, wx.DefaultPosition, wx.GetDisplaySize())
self.SetBackgroundColour(wx.BLACK)
# self.bilder = [Omsetning, Galleri, Bursdager]
self.bilder = [Omsetning, Galleri, Bursdager]
self.bildenummer = 0
self.dettebilde = None
self.nestebilde = None
self.timer = wx.PyTimer(self.Oppdater)
self.timer.Start(BILDEVISERSEKUNDER * 1000, wx.TIMER_CONTINUOUS)
self.LastBilde()
def Oppdater(self):
self.VisBilde()
self.LastBilde()
def VisBilde(self):
if isinstance(self.dettebilde, wx.Panel):
self.dettebilde.Destroy()
self.dettebilde = self.nestebilde
self.dettebilde.Show()
def LastBilde(self):
self.nestebilde = self.bilder[self.bildenummer](self)
if len(self.bilder) > self.bildenummer + 1:
self.bildenummer = self.bildenummer + 1
else:
self.bildenummer = 0
class Handlekurv(wx.Panel):
def __init__(self, parent, id, person):
wx.Panel.__init__(self, parent, id, wx.DefaultPosition, wx.GetDisplaySize())
self.person = person
self.SetBackgroundColour(wx.BLACK)
tekst = wx.StaticText(self, -1, "%s" % self.person['id'], (50, 20), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(18, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
tekst = wx.StaticText(self, -1, "%s" % self.person['navn'], (50, 40), style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(24, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
self.kurv = []
self.x = 0
self.y = 100
# Variable holding the sum of the HandleKurv
self.sum = 0
def Trykk(self, event):
key = chr(event.GetKeyCode())
if chr(event.GetKeyCode()) == 'r':
self.LagreKjop()
elif chr(event.GetKeyCode()) == 'w':
self.Kanseller()
else:
self.LeggTilVare(key)
def LeggTilVare(self, key):
print key
if not varer.Test(key):
return False
vare = varer.Get(key)
print vare
# If the person does not have enough money to buy the vare
if (vare['pris'] + self.sum > self.person['saldo']):
text = wx.StaticText(self, -1, "Du har ikke råd til denne varen, dessverre.", (50, 75),
style=wx.ALIGN_CENTRE)
text.SetForegroundColour(wx.RED)
text.SetFont(wx.Font(20, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
self.timer = wx.PyTimer(text.Destroy)
self.timer.Start(2000, wx.TIMER_ONE_SHOT)
return
self.kurv.append((vare['id'], 1, vare['pris']))
self.sum += vare['pris']
bilde = vare['bilde']
if self.x + bilde.GetWidth() >= wx.GetDisplaySize().GetWidth() - 100:
self.x = 0
self.y = self.y + bilde.GetHeight() + 10
if self.y + bilde.GetHeight() >= wx.GetDisplaySize().GetHeight() - 10:
tekst = wx.StaticText(self, -1, ". . .",
(wx.GetDisplaySize().GetWidth() / 2 - 10, wx.GetDisplaySize().GetHeight() - 70),
style=wx.ALIGN_CENTRE)
tekst.SetForegroundColour(wx.WHITE)
tekst.SetFont(wx.Font(48, wx.FONTFAMILY_SWISS, wx.BOLD, wx.NORMAL))
return
wx.StaticBitmap(self, -1, bilde, (self.x + 50, self.y))
self.x = self.x + bilde.GetWidth() + 10
"""
TODO: Make sure the entire handlekurv is comitted at once.
"""
def LagreKjop(self):
good = True
for vare in self.kurv:
# Run the registerKryss method. This returns true on a sucessful query, and else false.
good &= db.registerKryss(self.person, vare)
# Legacy code. Store until new stuff is proven to work
# try:
# vindu.db.query("INSERT INTO \"SociKryss\" (person,vare,kryssetid,pris) VALUES ('%s','%s',NOW(),'%d');" % (self.person['id'], k[0], k[1]))
# except (pg.InternalError, pg.ProgrammingError):
# logger.error("klarte ikke lagre kryss på (%s) %s" %self.person)
# good = False
# self.GetParent().FjernHandlekurv(u'Klarte ikke lagre kryssene. Prøv igjen!', wx.RED, wx.BLACK)
if good:
self.GetParent().FjernHandlekurv('Kryssene er lagret', wx.GREEN, wx.BLACK)
def Kanseller(self):
self.GetParent().FjernHandlekurv('Kryssingen ble kansellert', wx.RED, wx.BLACK)
class Varer:
def __init__(self, parent, id):
self.varer = {}
r = db.query("SELECT * FROM \"Varer\" WHERE bokstav IS NOT NULL;")
if (r == None):
return
for v in r.dictresult():
if len(glob.glob(BASE_DIR + 'varebilder/' + v['bildenavn'] + '.gif')) > 0:
self.varer[v['bokstav']] = {'id': v['id'], 'navn': v['navn'], 'pris': v['pris'], 'bilde': wx.Image(
BASE_DIR + 'varebilder/' + v['bildenavn'] + '.gif').ConvertToBitmap()}
def Get(self, key):
return self.varer[key]
def Test(self, key):
for k, v in self.varer.iteritems():
if (k == key):
return True
return False
logging.config.fileConfig('log/logging.conf')
logger = logging.getLogger('kismau')
# Start the database-connection
db = DbHandler()
# Start the Wx-application
app = wx.PySimpleApp()
# Create the main window-frame
vindu = Vindu(None, -1, 'Societeten')
# Create the varer-instance
varer = Varer(None, -1)
app.MainLoop()