-
Notifications
You must be signed in to change notification settings - Fork 1
/
loginclasses.py
954 lines (804 loc) · 33.5 KB
/
loginclasses.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
#! /usr/bin/python
#coding:utf-8
import sys, os
import locale
import datetime
import curses
import re
import sqlite3 as sqlite
locale.setlocale(locale.LC_ALL, '')
#code = locale.getpreferredencoding()
class Window:
'''Base class for windows.'''
def __init__(self, fieldname, parentScreen):
self.parentHeight, self.parentWidth = parentScreen.getmaxyx() # Base window dimensions.
self.parentYpos, self.parentXpos = parentScreen.getbegyx() # Top right corner of window.
class WarningWindow(Window):
'''This is a Warrning window:
It pops-up if some of the fields are empty or
some other error ocours.'''
def __init__(self, fieldname, parentScreen):
curses.curs_set(0)
curses.cbreak()
curses.noecho()
fieldLen = len(fieldname)
# height, width = screen.getmaxyx()
# ypos, xpos = screen.getbegyx()
self.height = 7
self.width = 44
self.ypos = ((self.parentHeight - self.height) / 2) + self.parentYpos
self.xpos = ((self.parentWidth - self.width) / 2) + self.parentXpos
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED)
self.win.keypad(1)
self.win.attron(curses.color_pair(1))
self.win.bkgd(' ', curses.color_pair(1))
strHeader = ' Warning! '
strOK = ' OK '
self.win.box()
self.win.addstr(1, (self.width - 11) / 2,
strHeader, curses.A_REVERSE | curses.A_BOLD)
self.win.addstr(5, (self.width - 4) / 2,
strOK, curses.A_REVERSE)
self.win.attroff(curses.color_pair(1))
self.win.refresh()
def getch(self):
while True:
key = self.win.getch()
if key == ord('\n'): # Since curses.KEY_ENTER don't work, this will
break;
def addstr(self, y, x, string):
self.win.addstr(y, x, string)
self.win.refresh()
def flushinp(self):
self.win.flushinp()
def refresh(self):
self.win.refresh()
def destroy(self):
self.win.touchwin()
self.win.refresh()
del self.win
class MenuWindow(Window):
'''This is Menu Window for:
choosing between Login or Register page'''
def __init__(self, parentScreen):
# Make it green
#curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.noecho()
curses.curs_set(0)
curses.raw()
#stdscrDim = stdscr.getmaxyx()
self.height = 9
self.width = 50
self.ypos = (self.parentHeight - self.height) / 2
self.xpos = (self.parentWidth - self.width) / 2
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
#self.win.attron(curses.color_pair(1))
self.win.keypad(1)
strHeader = ' Welcome to My Application '
strExplain = 'Press key in [] to enter:'
strLogin = '[L] - Login'
strRegester = '[R] - Registration'
strQuit = '[CTRL-X] - Exit'
self.win.box()
self.win.addstr(1, (self.width-len(strHeader))/2,
strHeader, curses.A_REVERSE)
self.win.addstr(3, (self.width-len(strExplain))/2,
strExplain, curses.A_BOLD)
self.win.addstr(5, 5, strLogin, curses.A_NORMAL)
self.win.addstr(6, 5, strRegester, curses.A_NORMAL)
self.win.addstr(7, 5, strQuit, curses.A_NORMAL)
self.win.refresh()
def putwin(self, filename): # Not implemented yet!
self.win.putwin(filename)
def get_choice(self):
while True:
choice = self.win.getch()
if choice == ord('l'):
break
elif choice == ord('r'):
break
else:
kname = curses.keyname(choice)
if kname == '^X':
return unicode(kname)
return choice
def destroy(self):
self.win.touchwin()
self.win.refresh()
del self.win
class LoginForm(Window):
def __init__(self, stdscr):
'''Login window'''
# This part is for cursor positions.
self.STARTPOS = 14
self.ENDPOS = 15
self.maxInput = self.STARTPOS + self.ENDPOS
curses.curs_set(1)
curses.noraw()
#stdscrDim = stdscr.getmaxyx()
self.height = 10
self.width = 50
self.ypos = (Window.height - self.height) / 2
self.xpos = (Window.width - self.width) / 2
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
self.win.keypad(1)
strHeader = ' Welcome to Login Form '
strUserName = 'User name:'
strPassword = 'Password:'
self.win.box()
self.win.addstr(1, (self.width - len(strHeader)) / 2,
strHeader, curses.A_REVERSE)
self.win.addstr(3, 2,
strUserName, curses.A_NORMAL)
self.win.addstr(5, 2,
strPassword, curses.A_NORMAL)
# Buttons
self.win.addstr(8, 2, ' [CTRL-B] - Back ', curses.A_REVERSE)
self.win.addstr(8, 30, ' [CTRL-L] - Login ', curses.A_REVERSE)
self.win.refresh()
stdscr.refresh() # If terminal window is resized, refresh the parent.
# Wrapping functions
def getch(self, flag, end):
'''
flag -> If True=We are in password field.
end -> Did we finished filling the form? True=Yes.
This is needed after last field is populated.
'''
container = ''
y, x = self.win.getyx()
movingX = x
rightX = self.STARTPOS
while True:
if end:
curses.raw()
curses.noecho()
self.win.keypad(1)
curses.curs_set(0)
ch = self.win.getch()
kname = curses.keyname(ch)
if kname == '^L':
return kname
elif not end:
curses.raw()
curses.noecho()
self.win.keypad(1)
ch = self.win.getch()
kname = curses.keyname(ch)
if ch == 2: # If user hits CTRL-B
container = ''
return 2
elif kname == '^L':
return kname
elif ch == curses.KEY_BACKSPACE: # Backspace
if rightX > self.STARTPOS: # If cursor is bigger then start position.
container = container[:-1] # Delete the last character.
y, x = self.win.getyx()
x -= 1 # --+
rightX -= 1 # |--> Keep track of pointers
movingX -= 1 # --+
self.win.addch(y, x, ' ') # Remove last char fom screen.
self.win.move(y, x) # Move back the cursor
self.win.refresh()
else:
self.warning()
elif ch == 9: # TAB ('^I') for Gnome-terminal
return ch
elif ch == curses.KEY_BTAB: # SHIFT+TAB for Gnome-terminal
return ch
elif ch == curses.KEY_HOME:
self.win.move(y, self.STARTPOS)
movingX = self.STARTPOS
elif ch == curses.KEY_END:
self.win.move(y, self.STARTPOS + len(container))
movingX = self.STARTPOS + len(container)
x = self.STARTPOS + len(container)
elif ch == 10: # If user hits the enter,
if container != '': # if we have something in container and
return container
else:
return 10 # or we don't.
elif ch == curses.KEY_LEFT:
y, x = self.win.getyx()
movingX = x # probably movingX has to be a static variable
if movingX > self.STARTPOS:
movingX -= 1 # Tracking the X position
self.win.move(y, movingX)
else:
self.win.move(y, self.STARTPOS)
self.warning()
elif ch == curses.KEY_RIGHT:
y, x = self.win.getyx()
movingX = x
if movingX < self.maxInput and movingX < rightX:
movingX += 1 # Tracking the X position
self.win.move(y, movingX)
else:
if movingX == self.maxInput:
self.win.move(y, self.maxInput)
curses.beep()
curses.flash()
elif movingX == rightX:
self.win.move(y, rightX)
self.warning()
elif ch in range(65, 91) or \
ch in range(97, 123) or \
ch in range(49, 58) or \
ch == ord('_'):
curses.noraw()
if not flag and movingX == rightX: # If we arn't in the password
if rightX < self.maxInput: # max right position
self.win.addch(ch) # field and no arrow key used yet.
container += chr(ch)
movingX += 1
rightX += 1
else:
self.warning()
# Creating illusion of input on screen...
elif not flag and movingX < rightX: # If we moved with arrow keys
if rightX < self.maxInput: # max right position
containerLeft = container[:movingX-self.STARTPOS]
containerRight = container[movingX-self.STARTPOS:]
container = containerLeft
container += chr(ch)
container += containerRight
movingX += 1
rightX += 1
self.win.addstr(y, self.STARTPOS, container)
self.win.move(y, movingX)
self.win.refresh()
else:
self.warning()
elif flag: # If we are in the password field
if rightX < self.maxInput: # max right position
curses.echo()
container += chr(ch)
self.win.addch(curses.ACS_DIAMOND)
movingX += 1
rightX += 1
else:
self.warning()
def getstr(self):
return self.win.getstr()
def getmaxyx(self):
return self.win.getmaxyx()
def getbegyx(self):
return self.win.getbegyx()
def move(self, y, x):
self.win.move(y, x)
def refresh(self):
self.win.refresh()
def touchwin(self):
self.win.touchwin()
def clrtoeol(self):
self.win.clrtoeol()
def box(self):
self.win.box()
def warning(self):
curses.beep()
curses.flash()
def destroy(self):
self.win.touchwin() # I use the above touchwin()
self.win.refresh()
del self.win
class RegisterForm:
def __init__(self, stdscr):
'''Registration window'''
# This part is for cursor positions.
self.STARTPOS = 21
self.ENDPOS = 30
self.maxInput = self.STARTPOS + self.ENDPOS
curses.curs_set(1)
curses.noraw()
stdscrDim = stdscr.getmaxyx()
self.height = 22
self.width = 55
self.ypos = (stdscrDim[0] - self.height) / 2
self.xpos = (stdscrDim[1] - self.width) / 2
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
self.win.keypad(1)
strHeader = ' Welcome to Registration Form '
strGender = 'Gender:'
strFirstName = 'First name:'
strLastName = 'Last name:'
strUserName = 'User name:'
strEmail = 'E-mail:'
strPassword = 'Password:'
strConfirmPassword = 'Confirm password:'
strDateOfBirth = 'Date of birth:'
self.win.box()
self.win.addstr(1, (self.width-len(strHeader))/2,
strHeader, curses.A_REVERSE)
self.win.addstr(3, 2,
strGender, curses.A_NORMAL)
self.win.addstr(5, 2,
strFirstName, curses.A_NORMAL)
self.win.addstr(7, 2,
strLastName, curses.A_NORMAL)
self.win.addstr(9, 2,
strUserName, curses.A_NORMAL)
self.win.addstr(11, 2,
strEmail, curses.A_NORMAL)
self.win.addstr(13, 2,
strPassword, curses.A_NORMAL)
self.win.addstr(15, 2,
strConfirmPassword, curses.A_NORMAL)
self.win.addstr(17, 2,
strDateOfBirth, curses.A_NORMAL)
self.win.refresh()
# Buttons
self.win.addstr(20, 2, ' [CTRL-B] - Back ', curses.A_REVERSE)
self.win.addstr(20, 32, ' [CTRL-R] - Register ', curses.A_REVERSE)
self.win.refresh()
stdscr.refresh() # If terminal window is resized, refresh the parent.
# Wrapping functions
def box(self):
self.win.box()
def getch(self, flag, end):
container = ''
y, x = self.win.getyx()
movingX = x
rightX = self.STARTPOS
while True:
if end:
curses.raw()
curses.noecho()
self.win.keypad(1)
curses.curs_set(0)
ch = self.win.getch()
kname = curses.keyname(ch)
if kname == '^R':
return kname
elif not end:
curses.raw()
curses.noecho()
self.win.keypad(1)
ch = self.win.getch()
kname = curses.keyname(ch)
if ch == 2: # If user hits CTRL-B
return 2
elif kname == '^R':
return '^R'
elif ch == curses.KEY_BACKSPACE: # Backspace
if rightX > self.STARTPOS: # If cursor is bigger then start position.
container = container[:-1] # Delete the last character.
y, x = self.win.getyx()
x -= 1 # --+
rightX -= 1 # |--> Keep track of pointers
movingX -= 1 # --+
self.win.addch(y, x, ' ') # Remove last char from screen.
self.win.move(y, x) # Move back the cursor
self.win.refresh()
else:
self.warning()
elif ch == 127: # DEL
pass
elif ch == 9: # TAB ('^I') for Gnome-terminal
return ch
elif ch == curses.KEY_BTAB: # SHIFT+TAB for Gnome-terminal
return ch
elif ch == curses.KEY_HOME:
self.win.move(y, self.STARTPOS)
movingX = self.STARTPOS
elif ch == curses.KEY_END:
x = self.STARTPOS + len(container)
self.win.move(y, self.STARTPOS + len(container))
movingX = x
rightX = x
elif ch == 10: # If user hits the enter,
if container != '': # if we have something in container and
return container
else:
return 10 # or we don't.
elif ch == curses.KEY_LEFT:
y, x = self.win.getyx()
movingX = x # probably movingX has to be a static variable
if movingX > self.STARTPOS:
movingX -= 1 # Tracking the X position
self.win.move(y, movingX)
else:
self.win.move(y, self.STARTPOS)
self.warning()
elif ch == curses.KEY_RIGHT:
y, x = self.win.getyx()
movingX = x
if movingX < self.maxInput and movingX < rightX:
movingX += 1 # Tracking the X position
self.win.move(y, movingX)
else:
if movingX == self.maxInput:
self.win.move(y, self.maxInput)
curses.beep()
curses.flash()
elif movingX == rightX:
self.win.move(y, rightX)
self.warning()
elif ch in range(65, 91) or \
ch in range(97, 123) or \
ch in range(48, 58) or \
ch == ord('_') or \
ch == ord('-') or \
ch == ord('.') or \
ch == ord('@'):
curses.noraw()
if not flag and movingX == rightX: # If we aren't in the password
if rightX < self.maxInput: # max right position
self.win.addch(ch) # field and no arrow key used yet.
container += chr(ch)
movingX += 1
rightX += 1
else:
self.warning()
elif not flag and movingX < rightX: # If we moved with arrow keys
if rightX < self.maxInput: # max right position
containerLeft = container[:movingX-self.STARTPOS]
containerRight = container[movingX-self.STARTPOS:]
container = containerLeft
container += chr(ch)
container += containerRight
movingX += 1
rightX += 1
self.win.addstr(y, self.STARTPOS, container)
self.win.move(y, movingX)
self.win.refresh()
else:
self.warning()
elif flag: # If we are in the password field
if rightX < self.maxInput: # max right position
curses.echo()
container += chr(ch)
self.win.addch(curses.ACS_DIAMOND)
movingX += 1
rightX += 1
else:
self.warning()
def getstr(self):
return self.win.getstr()
def getmaxyx(self):
return self.win.getmaxyx()
def getbegyx(self):
return self.win.getbegyx()
def move(self, y, x):
self.win.move(y, x)
def clrtoeol(self):
self.win.clrtoeol()
def refresh(self):
self.win.refresh()
def touchwin(self):
self.win.touchwin()
def warning(self):
curses.beep()
curses.flash()
def destroy(self):
self.touchwin() # I use the above touchwin()
self.win.refresh()
del self.win
class LoggedUserWindow:
def __init__(self, stdscr):
# self.CURSPOS = ((6, 4), (7, 4), (8, 4), (11, 36))
# self.STARTPOS and self.ENDPOS is only for last input field.
self.STARTPOS = 40
self.ENDPOS = 25
self.maxInput = self.STARTPOS + self.ENDPOS
curses.curs_set(1)
curses.noraw()
curses.cbreak()
stdscrDim = stdscr.getmaxyx()
self.height = 20
self.width = 70
self.ypos = (stdscrDim[0] - self.height) / 2
self.xpos = (stdscrDim[1] - self.width) / 2
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
self.win.bkgd(' ', curses.color_pair(4))
strHeader1 = ' You Are Logged as a User '
strHeader2 = 'Choose from options below:'
strListAll = 'Mark with SPACE wich table to search.'
#strKJI = u'[ ] Kiemelt jelent\u030b\u006Fs\u0301\u0065g\u030B\u0075 int\u0301\u0065zm\u0301\u0065nyek'
strKJI = '[ ] Kiemelt jelentőségű intézmények'
strEM = '[ ] e-magyar'
strKJII = '[ ] Kiemelt jelentőségű ifjúsági intézmények'
strSearch = 'Search in marked tables for subject:'
strList = '[CTRL-L] List all from selected categories'
strBack = '[CTRL-B] Back to main window'
strExit = '[CTRL-X] - Exit'
self.win.attron(curses.color_pair(2))
self.win.box()
self.win.addstr(1, (self.width - len(strHeader1)) / 2,
strHeader1, curses.A_REVERSE | curses.A_BOLD)
self.win.addstr(2, (self.width - len(strHeader2)) / 2,
strHeader2, curses.A_NORMAL)
self.win.addstr(4, 3, strListAll, curses.A_UNDERLINE)
self.win.attroff(curses.color_pair(2))
self.win.refresh()
self.win.attron(curses.color_pair(3))
self.win.addstr(6, 3, strKJI, curses.A_NORMAL)
self.win.addstr(7, 3, strEM, curses.A_NORMAL)
self.win.addstr(8, 3, strKJII, curses.A_NORMAL)
self.win.addstr(11, 3, strSearch, curses.A_NORMAL)
self.win.attroff(curses.color_pair(3))
self.win.refresh()
self.win.attron(curses.color_pair(4))
self.win.addstr(16, 3, strList, curses.A_NORMAL)
self.win.addstr(17, 3, strBack, curses.A_NORMAL)
self.win.addstr(18, 3, strExit, curses.A_NORMAL)
self.win.attroff(curses.color_pair(4))
self.win.refresh()
stdscr.refresh() # If terminal window is resized, refresh the parent.
self.win.move(6, 4)
def getch(self):
def add_next_byte():
'''Local function:
for checking next byte if the input was
different then ascii char set.'''
c = self.win.getch()
if 128 <= ch <= 191:
return ch
else:
raise UnicodeError
# Is the position X-ed (checked)?
posy1 = False
posy2 = False
posy3 = False
# Invoking needed variables for cursor position
movingX = self.STARTPOS
rightX = self.STARTPOS
container = ''
while True:
curses.raw()
self.win.keypad(1)
ch = self.win.getch()
kname = curses.keyname(ch)
y, x = self.win.getyx()
if kname == '^X':
return '^X'
elif ch == curses.KEY_UP:
y, x = self.win.getyx()
if y == 6:
if not posy1 and not posy2 and not posy3:
self.win.move(8, 4)
else:
self.win.move(11, 40)
elif y == 8:
self.win.move(7, 4)
elif y == 7:
self.win.move(6, 4)
elif y == 11:
self.win.move(8, 4)
elif ch == curses.KEY_DOWN:
y, x = self.win.getyx()
if y == 6:
self.win.move(7, 4)
elif y == 7:
self.win.move(8, 4)
elif y == 8:
if not posy1 and not posy2 and not posy3:
self.win.move(6, 4)
else:
self.win.move(11, 40)
elif y == 11:
self.win.move(6, 4)
elif ch == curses.KEY_LEFT:
y, x = self.win.getyx()
if y == 11:
movingX = x # probably movingX has to be a static variable
if movingX > self.STARTPOS:
movingX -= 1 # Tracking the X position
self.win.move(y, movingX)
else:
self.win.move(y, self.STARTPOS)
self.warning()
elif ch == curses.KEY_RIGHT:
y, x = self.win.getyx()
if y == 11:
movingX = x
if movingX < self.maxInput and movingX < rightX:
movingX += 1 # Tracking the X position
self.win.move(y, movingX)
else:
if movingX == self.maxInput:
self.win.move(y, self.maxInput)
curses.beep()
curses.flash()
elif movingX == rightX:
self.win.move(y, rightX)
self.warning()
elif ch == curses.KEY_BACKSPACE:
y, x = self.win.getyx()
if y == 11:
if rightX > self.STARTPOS:
container = container[:-1]
x -= 1
rightX -= 1
movingX -= 1
self.win.addch(y, x, ' ')
self.win.move(y, x)
self.win.refresh()
else:
self.warning()
elif ch == 127: # DEL
pass
elif ch == 10:
pass
elif ch == 9: # TAB ('^I') for Gnome-terminal
pass
elif ch == 2:
return 2
elif ch == curses.KEY_HOME:
if y == 11:
self.win.move(y, self.STARTPOS)
movingX = self.STARTPOS
elif ch == curses.KEY_END:
if y == 11:
x = self.STARTPOS + len(container)
self.win.move(y, x)
movingX = x
rightX = x
elif ch == 32: # Space character
cursorPosition = self.win.getyx()
if cursorPosition[0] != 11: # if y is different then 11.
curses.noraw()
if cursorPosition == (6, 4):
if not posy1:
self.win.addch('X')
self.win.move(6, 4)
self.win.refresh()
posy1 = True
elif posy1:
self.win.addch(' ')
self.win.move(6, 4)
self.win.refresh()
posy1 = False
elif cursorPosition == (7, 4):
if not posy2:
self.win.addch('X')
self.win.move(7, 4)
self.win.refresh()
posy2 = True
elif posy2:
self.win.addch(' ')
self.win.move(7, 4)
self.win.refresh()
posy2 = False
elif cursorPosition == (8, 4):
if not posy3:
self.win.addch('X')
self.win.move(8, 4)
self.win.refresh()
posy3 = True
elif posy3:
self.win.addch(' ')
self.win.move(8, 4)
self.win.refresh()
posy3 = False
elif y == 11 and \
(ch in range(48, 58) or \
ch in range(65, 91) or \
ch in range(97, 123) or \
ch in range(127, 245) or \
ch == ord('_') or \
ch == ord('-') or \
ch == ord('.') or \
ch == ord('@')):
curses.noraw()
if movingX == rightX:
if rightX < self.maxInput:
if ch <= 127: # 1 byte
container += chr(ch)
elif 194 <= ch <= 223: # 2 byte
container += chr(ch)
container += chr(add_next_byte())
elif 224 <= ch <= 239: # 3 byte
container += chr(ch)
container += chr(add_next_byte())
container += chr(add_next_byte())
elif 240 <= ch <= 244: # 4 byte
container += chr(ch)
container += chr(add_next_byte())
container += chr(add_next_byte())
container += chr(add_next_byte())
self.win.addch(ch)
movingX += 1
rightX += 1
self.win.move(y, rightX)
else:
self.warning()
elif movingX < rightX:
if rightX < self.maxInput:
containerLeft = container[:movingX-self.STARTPOS]
containerRight = container[movingX-self.STARTPOS:]
container = containerLeft
container += chr(ch)
container += containerRight
movingX += 1
rightX += 1
self.win.addstr(y, self.STARTPOS, container)
self.win.move(y, movingX)
self.win.refresh()
else:
self.warning
else:
pass
def touchwin(self):
self.win.touchwin()
def warning(self):
curses.beep()
curses.flash()
def destroy(self):
self.touchwin() # I use the above touchwin()
self.win.refresh()
del self.win
class LoggedAdminWindow:
def __init__(self, stdscr):
curses.curs_set(1)
curses.noraw()
stdscrDim = stdscr.getmaxyx()
self.height = 20
self.width = 50
self.ypos = (stdscrDim[0] - self.height) / 2
self.xpos = (stdscrDim[1] - self.width) / 2
self.win = curses.newwin(self.height, self.width, self.ypos, self.xpos)
curses.raw()
self.win.keypad(1)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
strHeader1 = ' You Are Logged as Administrator '
strHeader2 = 'Choose from options below:'
strCreateUser = '[1] Create User (Go to register form)'
strDeleteUser = '[2] Delete User'
strAddTable = '[3] Add Category'
strAddContact = '[4] Add Contact'
strEditContact = '[5] Edit Contact'
strDelContact = '[6] Delete Contact'
strListByCategory = '[7] List Contacts by Category'
strListContact = '[8] List All Contacts'
strAdd = '[CTRL-B] Back to main menu (Logout)'
self.win.bkgd(' ', curses.color_pair(3))
self.win.attron(curses.color_pair(2))
self.win.addstr(1, (self.width-len(strHeader1)) / 2,
strHeader1, curses.A_BOLD)
self.win.addstr(2, (self.width-len(strHeader2)) / 2,
strHeader2, curses.A_NORMAL)
self.win.addstr(4, 3, strCreateUser, curses.A_NORMAL)
self.win.addstr(5, 3, strDeleteUser, curses.A_NORMAL)
self.win.addstr(6, 3, strAddTable, curses.A_NORMAL)
self.win.addstr(7, 3, strAddContact, curses.A_NORMAL)
self.win.addstr(8, 3, strEditContact, curses.A_NORMAL)
self.win.addstr(9, 3, strDelContact, curses.A_NORMAL)
self.win.addstr(10, 3, strListByCategory, curses.A_NORMAL)
self.win.addstr(11, 3, strListContact, curses.A_NORMAL)
self.win.addstr(18, 3, strAdd, curses.A_NORMAL)
self.win.box()
self.win.attroff(curses.color_pair(2))
self.win.refresh()
def getch(self):
self.win.getch()
def touchwin(self):
self.win.touchwin()
def warning(self):
curses.beep()
curses.flash()
def destroy(self):
self.touchwin() # I use the above touchwin()
self.win.refresh()
del self.win
class User:
'''
Construct the user data.
'''
def __init__(self):
self.gender = ['', 'Gender']
self.firstname = ['', 'Firstname']
self.lastname = ['', 'Lastname']
self.username = ['', 'Username']
self.email = ['', 'Email']
self.password = ['', 'Password']
self.confirmPassword = ['', 'Confirm password']
self.oldPassword = ['', 'oldPassword']
self.dob = ['', 'Date of Birth']
self.regDate = ['', 'regDate']
self.lastLogDate = ['', 'lastLogDate']
if __name__ == '__main__':
main()