-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.py
executable file
·1479 lines (1258 loc) · 56.4 KB
/
ui.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# ui.py
#
# Copyright 2021 Thomas Castleman <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
"""UI for PyLibMan"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import common
import time
import random
class PyLibMan_UI(Gtk.Window):
"""Main UI Window"""
def __init__(self, pipe):
"""Initialize the Window"""
Gtk.Window.__init__(self, title="Python Library Manager")
self.grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.add(self.grid)
self.set_icon_name("dictionary")
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
# Make sure the whole class can access the pipe to the main thread
self.pipe = pipe
# Make user data available class-wide
self.user = None
# Make our tabs
self.page0 = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.page1 = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.page2 = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.page3 = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
self.page4 = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
# Book UID so we can get the data we need
self.book_uid = None
# enable window to receive key press events
self.connect("key-press-event", self.on_key_press_event)
self.reset("")
def on_key_press_event(self, widget, event):
"""Handles keyy press events for window"""
if event.keyval == 65307: #Esc key code
self.keys["esc"]("clicked")
elif event.keyval in (65421, 65293): # key codes for enter on both numpad and normal keyboard
self.keys["enter"]("clicked")
def reset(self, widget):
"""make/remake tabs"""
if not (isinstance(self.book_uid, int) or (self.book_uid is None)):
self.book_uid = None
children = self.page0.get_children()
for each0 in children:
self.page0.remove(each0)
children = self.page1.get_children()
for each0 in children:
self.page1.remove(each0)
children = self.page2.get_children()
for each0 in children:
self.page2.remove(each0)
children = self.page3.get_children()
for each0 in children:
self.page3.remove(each0)
children = self.page4.get_children()
for each0 in children:
self.page4.remove(each0)
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Check Out</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.page0.attach(label, 1, 1, 5, 1)
label1 = Gtk.Label()
label1.set_markup("""<b>Waiting for Book...</b>""")
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.page0.attach(label1, 1, 2, 5, 1)
label2 = Gtk.Label()
label2.set_markup("""----""")
label2.set_justify(Gtk.Justification.CENTER)
label2 = self._set_default_margins(label2)
self.page0.attach(label2, 1, 3, 5, 1)
button = Gtk.Button.new_with_label("Start Scanning")
button.connect("clicked", self.check_out_scanner)
button = self._set_default_margins(button)
self.page0.attach(button, 1, 4, 1, 1)
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Check In</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.page1.attach(label, 1, 1, 5, 1)
label1 = Gtk.Label()
label1.set_markup("""<b>Waiting for Book...</b>""")
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.page1.attach(label1, 1, 2, 5, 1)
label2 = Gtk.Label()
label2.set_markup("""----""")
label2.set_justify(Gtk.Justification.CENTER)
label2 = self._set_default_margins(label2)
self.page1.attach(label2, 1, 3, 5, 1)
button = Gtk.Button.new_with_label("Start Scanning")
button.connect("clicked", self.check_in_scanner)
button = self._set_default_margins(button)
self.page1.attach(button, 1, 4, 1, 1)
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Renew</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.page2.attach(label, 1, 1, 5, 1)
label1 = Gtk.Label()
label1.set_markup("""<b>Waiting for Book...</b>""")
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.page2.attach(label1, 1, 2, 5, 1)
label2 = Gtk.Label()
label2.set_markup("""----""")
label2.set_justify(Gtk.Justification.CENTER)
label2 = self._set_default_margins(label2)
self.page2.attach(label2, 1, 3, 5, 1)
button = Gtk.Button.new_with_label("Start Scanning")
button.connect("clicked", self.renew_scanner)
button = self._set_default_margins(button)
self.page2.attach(button, 1, 4, 1, 1)
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>User Administration</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.page3.attach(label, 1, 1, 5, 1)
button = Gtk.Button.new_with_label("Add User")
button.connect("clicked", self.add_user_ui)
button = self._set_default_margins(button)
self.page3.attach(button, 1, 2, 1, 1)
button = Gtk.Button.new_with_label("Remove User")
button.connect("clicked", self.remove_user_ui)
button = self._set_default_margins(button)
self.page3.attach(button, 2, 2, 1, 1)
# Also need Edit User and Relinquish Admin Privs
# button = Gtk.Button.new_with_label("Edit Book Info")
# button.connect("clicked", self.edit_book_ui)
# button = self._set_default_margins(button)
# self.page4.attach(button, 1, 3, 1, 1)
button = Gtk.Button.new_with_label("Relinquish Admin Rights")
button.connect("clicked", self.remove_admin_rights)
button = self._set_default_margins(button)
self.page3.attach(button, 2, 3, 1, 1)
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Book Administration</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.page4.attach(label, 1, 1, 5, 1)
button = Gtk.Button.new_with_label("Add Book")
button.connect("clicked", self.add_book_ui)
button = self._set_default_margins(button)
self.page4.attach(button, 1, 2, 1, 1)
button = Gtk.Button.new_with_label("Remove Book")
button.connect("clicked", self.remove_book_ui)
button = self._set_default_margins(button)
self.page4.attach(button, 2, 2, 1, 1)
button = Gtk.Button.new_with_label("View Books")
button.connect("clicked", self.view_titles_ui)
button = self._set_default_margins(button)
self.page4.attach(button, 2, 3, 1, 1)
button = Gtk.Button.new_with_label("Generate QR Codes")
button.connect("clicked", self.gen_qr_code_ui)
button = self._set_default_margins(button)
self.page4.attach(button, 3, 2, 1, 1)
# button = Gtk.Button.new_with_label("Edit Book Info")
# button.connect("clicked", self.edit_book_ui)
# button = self._set_default_margins(button)
# self.page4.attach(button, 1, 3, 1, 1)
if self.user is None:
self.main_menu("clicked")
else:
if self.user["privs"] == "admin":
self.admin_menu("clicked")
else:
self.user_menu("clicked")
def gen_qr_code_ui(self, widget):
"""Get UID to generate a QR code for"""
self.clear_window()
self.keys = {"esc": self.reset, "enter": self.gen_qr}
if not isinstance(self.book_uid, list):
self.book_uid = []
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Generate QR Code</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 5, 1)
label1 = Gtk.Label()
label1.set_markup("""<b>Waiting for Book UID...</b>""")
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.grid.attach(label1, 1, 2, 5, 1)
uid = Gtk.Entry()
uid.set_placeholder_text("UID (Unique ID)")
uid = self._set_default_margins(uid)
self.grid.attach(uid, 1, 3, 5, 1)
button1 = Gtk.Button.new_with_label("<-- Back")
button1.connect("clicked", self.reset)
button1 = self._set_default_margins(button1)
self.grid.attach(button1, 1, 4, 1, 1)
button = Gtk.Button.new_with_label("Generate QR Code")
button.connect("clicked", self.gen_qr)
button = self._set_default_margins(button)
self.grid.attach(button, 2, 4, 1, 1)
button2 = Gtk.Button.new_with_label("Print QR Code")
button2.connect("clicked", self.print_qr)
button2 = self._set_default_margins(button2)
self.grid.attach(button2, 3, 4, 1, 1)
self.show_all()
def print_qr(self, widget):
"""Prepare QR codes for printing"""
command = {"table": "barcode", "command": common.get_template("print")}
command["command"]["paths"] = self.book_uid
self.book_uid = []
children = self.grid.get_children()
for each in children:
if "<class 'gi.overrides.Gtk.Label'>" == str(type(each)):
if (("UID" in each.get_text()) or ("Stored At" in each.get_text()) or ("Printed" in each.get_text())):
details = each
break
self.pipe.send(command)
response = self.pipe.recv()
if isinstance(response, list):
response = "\n".join(response)
details.set_markup(f"""<b>QR Codes Ready to Print!</b>
Files are stored At:
{response}
<b>You may continue making QR Codes.</b>""")
self.show_all()
def gen_qr(self, widget):
"""Grab UID and generate QR code"""
command = {"table": "barcode", "command": common.get_template("make")}
command["command"]["type"] = "book"
children = self.grid.get_children()
for each in children:
if "<class 'gi.overrides.Gtk.Label'>" == str(type(each)):
if (("UID" in each.get_text()) or ("Stored At" in each.get_text()) or ("Printed" in each.get_text())):
details = each
break
for each in children:
if "<class 'gi.repository.Gtk.Entry'>" == str(type(each)):
if each.get_placeholder_text() == "UID (Unique ID)":
try:
command["command"]["uid"] = int(each.get_text())
except TypeError:
details.set_markup("""<b>Not a Valid UID. Try Again.</b>""")
self.show_all()
return
break
self.pipe.send(command)
response = self.pipe.recv()
self.book_uid.append(response)
details.set_markup(f"""<b>QR Code Stored At: {response}</b>""")
self.show_all()
def view_titles_ui(self, widget):
"""View Book Titles, or order to quickly find a specific book"""
self.clear_window()
self.keys = {"enter": self.view_books_ui,
"esc": self.reset}
cmd = common.get_template("get")
cmd["column"] = "name"
del cmd["filter"]
cmd = {"table": "book", "command": cmd}
self.pipe.send(cmd)
# While those threads are working, we can get our UI made
label = Gtk.Label()
label.set_markup("<span size='x-large'><b>View Books</b></span>")
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 2, 1)
label = Gtk.Label()
label.set_markup("""
<b>Pick a title to view info on</b>
""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 2, 2, 1)
titles = Gtk.ComboBoxText.new()
titles = self._set_default_margins(titles)
# hold off on finishing setting up this widget till the end to give the DB time to work
button = Gtk.Button.new_with_label("<-- Back")
button.connect("clicked", self.reset)
button = self._set_default_margins(button)
self.grid.attach(button, 1, 4, 1, 1)
button = Gtk.Button.new_with_label("View Book")
button.connect("clicked", self.view_books_ui)
button = self._set_default_margins(button)
self.grid.attach(button, 2, 4, 1, 1)
# NOW get our data out of the pipe
data = self.pipe.recv()
# make sure the data is a sorted list of unique entries
if isinstance(data, tuple):
data = list(data)
data = common.unique(data)
data.sort()
# add them to the drop down box
for each in data:
titles.append(each, each)
titles.connect("changed", self.select_title)
self.grid.attach(titles, 1, 3, 2, 1)
self.show_all()
def select_title(self, widget):
"""Grab book title to view"""
self.book_uid = widget.get_active_id()
def view_books_ui(self, widget):
"""View all books with a specific title"""
self.clear_window()
self.keys = {"enter": self.view_titles_ui,
"esc": self.view_titles_ui}
cmd = common.get_template("get")
cmd["filter"]["field"] = "name"
cmd["filter"]["compare"] = self.book_uid
cmd = {"table": "book", "command": cmd}
self.pipe.send(cmd)
label = Gtk.Label()
label.set_markup(f"<span size='x-large'><b>{self.book_uid}</b></span>")
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 2, 1)
data = self.pipe.recv()
for each in enumerate(data):
label = Gtk.Label()
label = self._set_default_margins(label)
label.set_markup(f"<b>UID</b>: {data[each[0]]['uid']} - <b>Published</b>: {data[each[0]]['published']} - <b>Status</b>: {data[each[0]]['check_in_status']['status'].replace('_', ' ')}")
self.grid.attach(label, 1, 2 + each[0], 2, 1)
button = Gtk.Button.new_with_label("<-- Back")
button.connect("clicked", self.view_titles_ui)
button = self._set_default_margins(button)
self.grid.attach(button, 1, 3 + len(data), 1, 1)
self.book_uid = None
self.show_all()
def remove_admin_rights(self, widget):
"""Remove admin rights"""
# have a UI confirming the user wants to do this
self.clear_window()
label = Gtk.Label()
label.set_markup("<span size='x-large'><b>Relinquish Admin Rights</b></span>")
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 2, 1)
label = Gtk.Label()
label.set_markup("""
<b>Are you sure you want to relinquish your administrator privileges?</b>
This action requires help from another administrator to undo.
""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 2, 2, 1)
button = Gtk.Button.new_with_label("Cancel")
button.connect("clicked", self.reset)
button = self._set_default_margins(button)
self.grid.attach(button, 1, 3, 1, 1)
button = Gtk.Button.new_with_label("Confirm")
button.connect("clicked", self._remove_admin_rights)
button = self._set_default_margins(button)
self.grid.attach(button, 2, 3, 1, 1)
self.show_all()
def _remove_admin_rights(self, widget):
"""Remove admin rights"""
# the change needs to be made to the DB too, to be persistant
# this will just make the change apply to this session
# logging out and back in would reset this value
self.user["privs"] = "user"
self.reset("clicked")
def check_out_scanner(self, widget):
"""Check out scanner"""
self.pipe.send("get_barcode")
children = self.page0.get_children()
element = []
for each in children:
# This is a bit of a hack but works
if "<class 'gi.overrides.Gtk.Label'>" == str(type(each)):
element.append(each)
for each in element:
if ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured")) or ("due back by" in each.get_text())):
details = each
elif (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Checked Out" in each.get_text())):
status = each
input = self.pipe.recv()
if input == []:
details.set_markup("Book Not Found")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Check Out Book":
self.page0.remove(each)
break
elif "status" in input[0]:
if input[0]["status"] == 0:
details.set_markup("An Error has occured")
elif "check_in_status" not in input[0]:
details.set_markup("Not a Book")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Check Out Book":
self.page0.remove(each)
break
else:
self.book_uid = input[0]["uid"]
text = f"""
<b>uid</b>: {input[0]["uid"]}
<b>Name</b>: {input[0]["name"]}
<b>Status</b>: {input[0]["check_in_status"]["status"]}"""
details.set_markup(text)
status.set_markup("<b>Book Found</b>")
# See if button exists. If not, add button to send checkout command
exists = False
for each in children:
if each.get_label() == "Check Out Book":
exists = True
if not exists:
button = Gtk.Button.new_with_label("Check Out Book")
button.connect("clicked", self.check_out)
button = self._set_default_margins(button)
self.page0.attach(button, 2, 4, 1, 1)
self.page0.show_all()
def check_out(self, widget):
"""Perform checkout function"""
command = {"table": "both", "command": common.get_template("check_out")}
command["command"]["data"]["book_uid"] = self.book_uid
command["command"]["data"]["user_uid"] = self.user["uid"]
self.pipe.send(command)
children = self.page0.get_children()
for each in children:
if each.get_label() == "Check Out Book":
button = each
try:
if (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Checked Out" in each.get_text())):
status = each
elif ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured")) or ("due back by" in each.get_text()) or ("Already Checked Out" in each.get_text())):
details = each
except AttributeError:
pass
response = self.pipe.recv()[1]
if isinstance(response, (int, float)):
due_date = time.ctime(response)
details.set_markup(f"Your book is due back by: {due_date}")
status.set_markup("Book Successfully Checked Out")
elif isinstance(response, dict):
if response["status"] == 0:
details.set_markup("An Error has occured")
status.set_markup("Waiting for Book...")
if response["status"] == 2:
status.set_markup("Book Found")
message = "Already Checked Out to "
if response["user"] == self.user["uid"]:
message = message + "You"
else:
message = message + "Someone Else"
details.set_markup(message)
self.page0.remove(button)
self.show_all()
def renew_scanner(self, widget):
"""Renewal book scanner"""
self.pipe.send("get_barcode")
children = self.page2.get_children()
element = []
for each in children:
# This is a bit of a hack but works
if "<class 'gi.overrides.Gtk.Label'>" == str(type(each)):
element.append(each)
for each in element:
if ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured")) or ("due back by" in each.get_text())):
details = each
elif (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Renewed" in each.get_text())):
status = each
input = self.pipe.recv()
if "status" in input[0]:
if input[0]["status"] == 0:
details.set_markup("An Error has occured")
elif input == []:
details.set_markup("Book Not Found")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Renew Book":
self.page2.remove(each)
break
elif "check_in_status" not in input[0]:
details.set_markup("Not a Book")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Renew Book":
self.page2.remove(each)
break
else:
self.book_uid = input[0]["uid"]
text = f"""
<b>uid</b>: {input[0]["uid"]}
<b>Name</b>: {input[0]["name"]}
<b>Status</b>: {input[0]["check_in_status"]["status"]}"""
details.set_markup(text)
status.set_markup("<b>Book Found</b>")
# See if button exists. If not, add button to send checkout command
exists = False
for each in children:
if each.get_label() == "Renew Book":
exists = True
if not exists:
button = Gtk.Button.new_with_label("Renew Book")
button.connect("clicked", self.renew)
button = self._set_default_margins(button)
self.page2.attach(button, 2, 4, 1, 1)
self.page2.show_all()
def renew(self, widget):
"""Perform renew function"""
command = {"table": "both", "command": common.get_template("renew")}
command["command"]["data"]["book_uid"] = self.book_uid
command["command"]["data"]["user_uid"] = self.user["uid"]
self.pipe.send(command)
children = self.page2.get_children()
for each in children:
if each.get_label() == "Renew Book":
button = each
try:
if (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Renewed" in each.get_text())):
status = each
elif ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured")) or ("due back by" in each.get_text()) or ("Already Checked In" in each.get_text())):
details = each
except AttributeError:
pass
response = self.pipe.recv()[1]
if isinstance(response, (int, float)):
due_date = time.ctime(response)
details.set_markup(f"Your book is due back by: {due_date}")
status.set_markup("Book Successfully Renewed")
elif isinstance(response, dict):
if response["status"] == 0:
details.set_markup("An Error has occured")
status.set_markup("Waiting for Book...")
if response["status"] == 2:
status.set_markup("Book Found")
details.set_markup("Already Checked In")
self.page2.remove(button)
self.show_all()
def check_in_scanner(self, widget):
"""Check in scanner"""
self.pipe.send("get_barcode")
children = self.page1.get_children()
element = []
for each in children:
# This is a bit of a hack but works
if "<class 'gi.overrides.Gtk.Label'>" == str(type(each)):
element.append(each)
for each in element:
if ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured"))):
details = each
elif (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Checked Back In" in each.get_text())):
status = each
input = self.pipe.recv()
if "status" in input[0]:
if input[0]["status"] == 0:
details.set_markup("An Error has occured")
elif input == []:
details.set_markup("Book Not Found")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Check In Book":
self.page1.remove(each)
break
elif "check_in_status" not in input[0]:
details.set_markup("Not a Book")
# see if button exists. Delete if so
for each in children:
if each.get_label() == "Check In Book":
self.page1.remove(each)
break
else:
self.book_uid = input[0]["uid"]
text = f"""
<b>uid</b>: {input[0]["uid"]}
<b>Name</b>: {input[0]["name"]}
<b>Status</b>: {input[0]["check_in_status"]["status"]}"""
details.set_markup(text)
status.set_markup("<b>Book Found</b>")
# See if button exists. If not, add button to send checkout command
exists = False
for each in children:
if each.get_label() == "Check In Book":
exists = True
if not exists:
button = Gtk.Button.new_with_label("Check In Book")
button.connect("clicked", self.check_in)
button = self._set_default_margins(button)
self.page1.attach(button, 2, 4, 1, 1)
self.page1.show_all()
def check_in(self, widget):
"""Perform check in function"""
command = {"table": "both", "command": common.get_template("check_in")}
command["command"]["data"]["book_uid"] = self.book_uid
command["command"]["data"]["user_uid"] = self.user["uid"]
self.pipe.send(command)
children = self.page1.get_children()
for each in children:
if each.get_label() == "Check In Book":
button = each
try:
if (("Waiting for Book..." in each.get_text()) or ("Book Found" in each.get_text()) or ("Checked In" in each.get_text())):
status = each
elif ((each.get_text() == "----") or ("uid" in each.get_text()) or (each.get_text() in ("Book Not Found", "Not a Book", "An Error has occured")) or ("Already Checked In" in each.get_text())):
details = each
except AttributeError:
pass
response = self.pipe.recv()[1]
if response["status"] == 1:
details.set_markup("----")
status.set_markup("Book Successfully Checked Back In")
elif response["status"] == 0:
details.set_markup("An Error has occured")
status.set_markup("Waiting for Book...")
elif response["status"] == 2:
status.set_markup("Book Found")
details.set_markup("Already Checked In")
self.page1.remove(button)
self.show_all()
def _set_default_margins(self, widget):
"""Set default margin size"""
widget.set_margin_start(10)
widget.set_margin_end(10)
widget.set_margin_top(10)
widget.set_margin_bottom(10)
return widget
def clear_window(self):
"""Clear Window"""
children = self.grid.get_children()
for each0 in children:
self.grid.remove(each0)
def exit(self, button):
"""Exit dialog"""
self.clear_window()
self.keys = {"enter": self._exit,
"esc": self._exit}
label = Gtk.Label()
label.set_markup("""\n<b>Are you sure you want to exit?</b>
Exiting now will cause any unsaved data to be lost.""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 2, 1)
yes = Gtk.Button.new_with_label("Exit")
yes.connect("clicked", self._exit)
yes = self._set_default_margins(yes)
self.grid.attach(yes, 1, 2, 1, 1)
no = Gtk.Button.new_with_label("Return")
no.connect("clicked", self.main_menu)
no = self._set_default_margins(no)
self.grid.attach(no, 2, 2, 1, 1)
self.show_all()
def _exit(self, button):
"""Exit"""
Gtk.main_quit("delete-event")
self.destroy()
self.pipe.send("shut_down")
def main_menu(self, widget):
"""Main window"""
self.user = common.get_template("db_users")
self.clear_window()
self.keys = {"enter": self.on_login,
"esc": self.exit}
# This is our sign in window. You can't do anything without letting the app know who you are
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Awaiting User Login</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 3, 1)
text = """<b>Please enter your UID</b>"""
if widget == "invalid":
text = f"{text}\nInvalid UID. Try again."
label1 = Gtk.Label()
label1.set_markup(text)
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.grid.attach(label1, 2, 2, 1, 1)
uid = Gtk.Entry()
uid = self._set_default_margins(uid)
self.grid.attach(uid, 2, 3, 1, 1)
button1 = Gtk.Button.new_with_label("Login -->")
button1.connect("clicked", self.on_login)
button1 = self._set_default_margins(button1)
self.grid.attach(button1, 3, 4, 1, 1)
button2 = Gtk.Button.new_with_label("Exit")
button2.connect("clicked", self.exit)
button2 = self._set_default_margins(button2)
self.grid.attach(button2, 1, 4, 1, 1)
self.show_all()
def on_login(self, widget):
"""Login handler"""
children = self.grid.get_children()
uid = children[2].get_text()
del children
command = {"table": "users", "command": common.get_template("get")}
try:
uid = int(uid)
except ValueError:
pass
command["command"]["filter"]["compare"] = uid
command["command"]["filter"]["field"] = "uid"
self.pipe.send(command)
response = self.pipe.recv()
if response == []:
self.main_menu("invalid")
self.user = response[0]
self.reset("")
def on_logout(self, widget):
"""Logout handler"""
self.user = None
self.reset("")
def user_menu(self, widget):
"""Menu for Users"""
self.clear_window()
self.keys = {"enter": None,
"esc": self.on_logout}
stack = Gtk.Stack()
stack.add_titled(self.page0, "Check Out", "Check Out")
stack.add_titled(self.page1, "Check In", "Check In")
stack.add_titled(self.page2, "Renew", "Renew")
stack = self._set_default_margins(stack)
self.grid.attach(stack, 1, 2, 4, 1)
stack_switcher = Gtk.StackSwitcher()
stack_switcher.set_stack(stack)
stack_switcher = self._set_default_margins(stack_switcher)
stack_switcher.connect("event", self.keyboard_changer)
self.grid.attach(stack_switcher, 2, 1, 2, 1)
button1 = Gtk.Button.new_with_label("Log Out")
button1.connect("clicked", self.on_logout)
button1 = self._set_default_margins(button1)
self.grid.attach(button1, 3, 4, 1, 1)
self.show_all()
def admin_menu(self, widget):
"""Menu for Admin"""
self.clear_window()
self.keys = {"enter": None,
"esc": self.on_logout}
stack = Gtk.Stack()
stack.add_titled(self.page0, "Check Out", "Check Out")
stack.add_titled(self.page1, "Check In", "Check In")
stack.add_titled(self.page2, "Renew", "Renew")
stack.add_titled(self.page3, "User Admin", "User Admin")
stack.add_titled(self.page4, "Book Admin", "Book Admin")
stack = self._set_default_margins(stack)
self.grid.attach(stack, 1, 2, 4, 1)
stack_switcher = Gtk.StackSwitcher()
stack_switcher.set_stack(stack)
stack_switcher = self._set_default_margins(stack_switcher)
stack_switcher.connect("event", self.keyboard_changer)
self.grid.attach(stack_switcher, 2, 1, 2, 1)
button1 = Gtk.Button.new_with_label("Log Out")
button1.connect("clicked", self.on_logout)
button1 = self._set_default_margins(button1)
self.grid.attach(button1, 3, 4, 1, 1)
self.show_all()
def keyboard_changer(self, widget, other_widget):
"""Change keyboard keys depending on what is on screen"""
children = self.grid.get_children()
for each in children:
if str(type(each)) == "<class 'gi.repository.Gtk.Stack'>":
visible = each.get_visible_child_name()
break
if visible == "Check Out":
self.keys = {"enter": self.check_in_scanner,
"esc": self.on_logout}
elif visible == "Check Out":
self.keys = {"enter": self.check_out_scanner,
"esc": self.on_logout}
elif visible == "Renew":
self.keys = {"enter": self.renew_scanner,
"esc": self.on_logout}
elif visible == "User Admin":
self.keys = {"enter": None,
"esc": self.on_logout}
elif visible == "Book Admin":
self.keys = {"enter": None,
"esc": self.on_logout}
def add_book_ui(self, widget):
"""UI to add a book"""
self.clear_window()
# Set up to collect all necessary info to add a book to the database
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>Add A Book</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 5, 1)
name = Gtk.Entry()
name.set_placeholder_text("Book Name")
name = self._set_default_margins(name)
self.grid.attach(name, 1, 2, 1, 1)
uid = Gtk.Entry()
uid.set_placeholder_text("UID (Unique ID)")
uid = self._set_default_margins(uid)
self.grid.attach(uid, 1, 3, 1, 1)
button = Gtk.Button.new_with_label("Generate Random UID")
button.connect("clicked", self.gen_uid)
button = self._set_default_margins(button)
self.grid.attach(button, 2, 3, 1, 1)
date = Gtk.Entry()
date.set_placeholder_text("Publish Year")
date = self._set_default_margins(date)
self.grid.attach(date, 2, 2, 1, 1)
button1 = Gtk.Button.new_with_label("<--Back")
button1.connect("clicked", self.reset)
button1 = self._set_default_margins(button1)
self.grid.attach(button1, 1, 4, 1, 1)
button2 = Gtk.Button.new_with_label("Add Book")
button2.connect("clicked", self.add_book)
button2 = self._set_default_margins(button2)
self.grid.attach(button2, 2, 4, 1, 1)
self.show_all()
def add_book(self, widget):
"""Add book to DB"""
db_struct = common.get_template("db_books")
command = common.get_template("add")
children = self.grid.get_children()
for each in children:
if "<class 'gi.repository.Gtk.Entry'>" == str(type(each)):
if each.get_placeholder_text() == "UID (Unique ID)":
db_struct["uid"] = int(each.get_text())
elif each.get_placeholder_text() == "Book Name":
db_struct["name"] = each.get_text()
elif each.get_placeholder_text() == "Publish Year":
db_struct["published"] = int(each.get_text())
# we have retreived data from the UI. Generate remaining data
db_struct["check_in_status"] = {"status": "checked_in",
"possession": None,
"duration": 0,
"due_date": 0}
db_struct["check_out_history"] = []
# Generate command
command["data"] = db_struct
command = {"table": "book", "command": command}
self.pipe.send(command)
output = self.pipe.recv()
if output["status"] == 1:
self.add_book_success(db_struct["name"], db_struct["uid"])
else:
self.error()
def error(self):
"""Error Dialog"""
self.clear_window()
label = Gtk.Label()
label.set_markup("""<span size="x-large"><b>ERROR</b></span>""")
label.set_justify(Gtk.Justification.CENTER)
label = self._set_default_margins(label)
self.grid.attach(label, 1, 1, 5, 1)
label1 = Gtk.Label()
label1.set_markup("""<b>An error has occured.</b>""")
label1.set_justify(Gtk.Justification.CENTER)
label1 = self._set_default_margins(label1)
self.grid.attach(label1, 1, 2, 5, 1)