-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtknewspaper.py
More file actions
589 lines (485 loc) · 21.4 KB
/
Copy pathtknewspaper.py
File metadata and controls
589 lines (485 loc) · 21.4 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
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
__VERSION__ = "1.3"
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from tinydb import *
from simplecrypt import encrypt, decrypt
import datetime, os, getpass, shutil, online_handle
'''
Creates a Snapshot backup of the encrypted file in the "Snapshots" folder
This is so we can have incremental backups in case there is a file loss
or file corruption.
'''
def backup(file):
date = str(datetime.datetime.today()).replace(":", ".").split(" ")
if not os.path.exists("Snapshot"):
os.mkdir("Snapshot")
if not os.path.exists("Snapshot/{}".format(date[0])):
os.mkdir("Snapshot/{}".format(date[0]))
date = "__{} {}.json".format(date[0], date[1][:8])
path = "Snapshot/{}/{}".format(str(datetime.date.today()), file.split('.')[0] + date)
open(path, "a").close()
shutil.copy(file, path)
'''
Read the encrypted data by decrypting the data
then writing the plain text onto the original file
this will let TinyDB to read the files
'''
def readencrypted(password, filename):
with open(filename, 'rb') as input:
global plaintext
ciphertext = input.read()
plaintext = decrypt(password, ciphertext)
plaintext = plaintext.decode('utf8')
with open(filename, 'w') as file:
file.write(plaintext)
'''
Read the plaintext file and encrypt the text
after which we store the encrypted text back in to the file
This will keep the data secured when not in use.
'''
def writeencrypted(password, filename):
global plaintext
with open(filename, 'r') as text:
plaintext = text.read()
with open(filename, 'wb') as output:
ciphertext = encrypt(password, plaintext)
output.write(ciphertext)
online_handle.upload(filename)
'''
If the root (Window) is closing prompts the user
to wait a bit, as the program encrypts the files
after which you are prompted with success and the root is destroyed
'''
def onquit():
messagebox.showinfo("Info", "Stay put Saving data")
writeencrypted(password, "people.json")
writeencrypted(password, "newspaper.json")
messagebox.showinfo("Info","Saving Complete")
root.destroy()
global db, newsdb
FILES = ["people.json", "newspaper.json"]
password = getpass.getpass("Password: ")
for file in FILES:
online_handle.dowload(file)
if os.path.exists(file):
backup(file)
try:
readencrypted(password, file)
except:
db = TinyDB("people.json")
newsdb = TinyDB("newspaper.json")
else:
open(file, 'w+')
writeencrypted(password, file)
readencrypted(password, file)
db = TinyDB("people.json")
newsdb = TinyDB("newspaper.json")
class AppEntry(Frame):
def __init__(self, master=None):
super().__init__(master)
self.name = StringVar()
self.address = StringVar()
self.paperName = StringVar()
self.paperPrice = DoubleVar()
self.satPaperPrice = DoubleVar()
self.sunPaperPrice = DoubleVar()
self.deliveryprice = DoubleVar()
self.paidfor = IntVar()
self.paydays = IntVar()
self.paydays.set(0)
self.totalprice = 0.0
self.weektotalprice = 0.0
self.datedifference = 0.0
self.newsname = ""
self.comboname = ""
self.addresssearch = []
self.daynames = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
self.weeklist = []
for i in range(7):
self.weeklist.append(BooleanVar())
self.masterframe = Frame(self.master)
self.createwidgets()
root.focus_force()
'''
Clears all the values in the newspaper section Entries
'''
def clearnewsvar(self):
self.paperName.set("")
self.paperPrice.set("")
self.satPaperPrice.set("")
self.sunPaperPrice.set("")
'''
Reset the week list. This is mainly used in exception handling
and initialisation
'''
def weekreset(self):
for i in range(7):
self.weeklist[i].set(False)
'''
Clears the mainframe of the root so that we can redraw new widgets
this will allow the program to operate in a single screen.
'''
def clearwidgets(self, back=True):
self.masterframe.destroy()
self.masterframe = Frame(self.master)
self.masterframe.grid()
if back:
Button(self.masterframe, text="Back", command=self.createwidgets).grid(row=9)
'''
Create all the widgets for the main menu such as
submitting data viewing data and newspaper date.
'''
def createwidgets(self, clear=True):
if clear:
self.clearwidgets(back=False)
Label(self.masterframe, text="Newspapers V.{}".format(__VERSION__)).grid(row=0)
labelframe = LabelFrame(self.masterframe, text="Options")
Button(labelframe, text="Deliveries", command=self.datawidgets).grid(row=1, columnspan=2, sticky="n")
Button(labelframe, text="Newspapers", command=self.newspaperwidgets).grid(row=2, columnspan=2)
Button(labelframe, text="New Customers", command=self.editwidgets).grid(row=3, columnspan=2)
labelframe.grid(row=1, ipadx=5, ipady=5, padx=10, columnspan=2)
'''
Get the data that is searched for
Using the value received from the Combobox
retrieve all the necessary data and insert it to the appropriate fields
'''
def getdata(self, event):
data = newsdb.all()[self.combobox.current()]
self.paperName.set(data["Name"])
self.paperPrice.set(data["Normal Price"])
self.satPaperPrice.set(data["Saturday Price"])
self.sunPaperPrice.set(data["Sunday Price"])
'''
Push the data back to the json.
If the data is exist then replace using confirmation
otherwise simply enter new data
'''
def sendnewspaperdata(self):
if str(self.paperName.get().lower())not in self.readdata('Name', True, database=newsdb):
newsdb.insert({'Name': self.paperName.get(), 'Normal Price': self.paperPrice.get(),
'Saturday Price': self.satPaperPrice.get(), 'Sunday Price': self.sunPaperPrice.get()})
self.clearnewsvar()
self.newspaperwidgets()
else:
response = messagebox.askquestion('Data exists', "Data already exists, would you like it to be replaced ?")
if response == "yes":
newsdb.update({'Name': self.paperName.get(), 'Normal Price': self.paperPrice.get(),
'Saturday Price': self.satPaperPrice.get(), 'Sunday Price': self.sunPaperPrice.get()},
doc_ids=[self.getid(self.combobox)])
'''
If data already exist then delete that data using
confirmation otherwise state that there is nothing to
delete.
'''
def deletenewspaperdata(self):
if messagebox.askyesno("Confirmation", "Are you sure you want to delete '{}' data".format(newsdb.get(doc_id=self.getid(self.combobox))["Name"])):
newsdb.remove(doc_ids=[self.getid(self.combobox)])
self.clearnewsvar()
self.newspaperwidgets()
else:
pass
'''
Widgets for the newspaper window
all of the gui elements should be located here.
When an item is selected via combo box the "getdata" function will get called
When "Delete" button is pressed the deletenewspaperdata function will get called
when "Submit" button is pressed the sendnewspaperdata function will get called
'''
def newspaperwidgets(self, clear=True):
if clear:
self.clearwidgets()
labelframe = LabelFrame(self.masterframe, text="Data")
Label(labelframe, text='Paper Name: ').grid(row=0, column=2, sticky="w")
Entry(labelframe, textvariable=self.paperName).grid(row=0,column=3)
Label(labelframe, text="Price").grid(row=1, column=2, sticky="w")
Entry(labelframe, textvariable=self.paperPrice).grid(row=1, column=3)
Label(labelframe, text="Saturday Price").grid(row=2, column=2, sticky="w")
Entry(labelframe, textvariable=self.satPaperPrice).grid(row=2, column=3)
Label(labelframe, text="Sunday Price").grid(row=3, column=2, sticky="w")
Entry(labelframe, textvariable=self.sunPaperPrice).grid(row=3, column=3)
labelframe.grid(row=0, column=1, ipady=3)
labelframe = LabelFrame(self.masterframe, text="Buttons")
Button(labelframe, text="Submit", command=self.sendnewspaperdata).grid(row=4, column=3, sticky='e')
Button(labelframe, text="Delete",command=self.deletenewspaperdata).grid(row=4, column=4, sticky='w')
labelframe.grid(row=1,column=1, sticky="e")
labelframe = LabelFrame(self.masterframe, text="Newspaper")
self.combobox = Combobox(labelframe, values=self.readdata('Name', False, newsdb))
self.combobox.grid(row=0)
self.combobox.bind("<<ComboboxSelected>>", self.getdata)
labelframe.grid(row=0, padx=5, ipady=3, sticky="n")
'''
Creates all the widgets for the submit application.
Such as Name label and entry and so on...
'''
def editwidgets(self, clear=True):
if clear:
self.clearwidgets()
labelframe = LabelFrame(self.masterframe, text="Data")
Label(labelframe, text="Full Name:").grid(row=0)
Entry(labelframe, textvariable=self.name).grid(row=0, column=1)
Label(labelframe, text="Address:").grid(row=1)
Entry(labelframe, textvariable=self.address).grid(row=1, column=1)
Label(labelframe, text="Delivery:").grid(row=2)
Entry(labelframe, textvariable=self.deliveryprice).grid(row=2, column=1)
labelframe.grid(row=0)
labelframe = LabelFrame(self.masterframe, text="Button")
self.submit = Button(labelframe, text="Submit", command=self.submitbutton).grid(row=0, column=2, padx=5)
labelframe.grid(row=0, column=1, padx=3, sticky="n")
'''
Reads the desired Key of data in the database
and then returns all the values for that desired field
'''
def readdata(self, nameofdata, lower, database=db):
if lower == True:
return [str(data[nameofdata]).lower() for data in database.all()]
else:
return [data[nameofdata] for data in database.all()]
'''
Submits data after checking it and clears the entry fields.
The checks include checking if the data is already present and
if the entry fields are not empty
'''
def submitbutton(self):
if self.name.get() != '' and self.address.get() != '' and self.deliveryprice.get() != '':
if str(self.name.get()).lower() not in self.readdata('Name', True) or str(self.address.get()).lower() not in self.readdata('Address', True):
db.insert({"Name": self.name.get(), "Address": self.address.get(), "Delivery Charge": self.deliveryprice.get()})
self.editwidgets()
self.name.set('')
self.address.set('')
self.deliveryprice.set('')
else:
messagebox.showerror("Error", "Data already in the database")
'''
Creates the widgets for the deliveries.
Using Label frames to create a neat layout which should look
similar on all platforms.
'''
def datawidgets(self, clear=True):
if clear:
self.clearwidgets()
labelframe = LabelFrame(self.masterframe)
Label(labelframe, text="Paper").grid(row=0)
self.newscombo = Combobox(labelframe, values=self.readdata("Name", True, database=newsdb))
self.newscombo.bind("<<ComboboxSelected>>", self.setnewsname)
self.newscombo.set(self.newsname)
self.newscombo.grid(row=0, column=1, sticky="w")
Label(labelframe, text="Address").grid(row=1)
self.combobox = Combobox(labelframe, values=self.readdata("Address", False))
self.combobox.bind("<Key>", self.getaddress)
self.combobox.bind("<BackSpace>", self.addressremove)
self.combobox.bind("<<ComboboxSelected>>", self.updatedata)
self.combobox.set(self.comboname)
self.combobox.grid(row=1, column=1, sticky="w")
labelframe.grid(row=0, padx=5, ipadx=2, ipady=2)
labelframe = LabelFrame(self.masterframe, text="Total")
Label(labelframe, text="{:0.2f}".format(self.totalprice)).grid(row=0)
labelframe.grid(row=1, column=0, padx=10, sticky="w")
labelframe = LabelFrame(self.masterframe, text="Week price")
Label(labelframe, text="{:0.2f}".format(self.weektotalprice)).grid(row=0)
labelframe.grid(row=2, padx=10, sticky="w")
labelframe = LabelFrame(self.masterframe, text="Pay Due in")
Label(labelframe, text=self.datedifference).grid(row=0)
labelframe.grid(row=1, column=0)
labelframe = LabelFrame(self.masterframe, text="Buttons")
Button(labelframe, text="Submit", command=self.datasubmit).grid(row=0, column=7, sticky='s')
Button(labelframe, text="Delete", command=self.datadelete).grid(row=0, column=4, columnspan=3, sticky='se')
Button(labelframe, text="Pay", command=self.paybillwidgets).grid(row=0, column=8, columnspan=3)
labelframe.grid(row=1, column=1)
'''
Sets a global variable of a newspaper name which is selected.
'''
def setnewsname(self, event):
self.newsname = self.newscombo.get()
if self.combobox.get() != "":
self.updatedata(None)
'''
Gets the ID of the selected field by using regex and then
stores this value which can be accessed by any other function
that will need it. The variable stays the same until a new field
is selected
'''
def getid(self, box, database=newsdb):
return database.all()[box.current()].doc_id
'''
Alternative way of calling getid this is for bind event in tkinter
as they do require an addition argument so that you can call a function.
Aswell as needing to link the json files in the same window.
'''
def getidalt(self, event):
self.getid(self.combobox)
'''
Update the list by checking the json file and retrieving
data of the appropriate user
'''
def updateweeklist(self):
try:
for i in range(len(self.daynames)):
if not self.userdata[str(self.newsname+self.daynames[i])]:
self.weeklist[i].set(False)
elif KeyError:
self.weeklist[i].set(True)
except KeyError:
self.weekreset()
def addressremove(self, event):
self.combobox.set("")
self.addresssearch = []
def getaddress(self, event):
self.addresssearch.append(event.char)
keypress = "".join(self.addresssearch)
possible = []
for index, value in enumerate(self.readdata("Address", False)):
if keypress.lower() in value.lower():
possible.append([index, value])
if len(possible) >= 1:
self.combobox['values'] = [x[1] for x in possible]
self.combobox.set("")
self.combobox.set(possible[0][1])
self.updatedata(None)
'''
Update the checkbox for the appropriate user after
getting their id and corresponding week values
'''
def updatedata(self, event):
if event == None:
self.comboname = self.combobox.get()[1:]
self.comboname = self.combobox.get()
self.datedifference = str(self.getdate(outscope=True, key="PaidTill"))[:10]
self.userdata = db.get(doc_id=self.getid(self.combobox, database=db))
self.totalprice = self.calculateprice()
self.weektotalprice = self.calculateprice(week=True)
self.updateweeklist()
labelframe = LabelFrame(self.masterframe, text="Days")
for i in range(len(self.weeklist)):
Checkbutton(labelframe, text=i + 1, variable=self.weeklist[i], onvalue=True,
offvalue=False).grid(row=0, column=i, sticky='nw')
labelframe.grid(row=0, column=1, padx=10)
self.datawidgets(clear=False)
'''
Returns the delivery charge of the specific user.
'''
def deliverycharge(self):
return float(db.all()[self.combobox.current()]["Delivery Charge"])
'''
Calculates the Price that is either in credit or is due
for the currently selected user.
'''
def calculateprice(self, week=False):
try:
totalprice = 0
if week:
diff = 6
else:
diff = self.getdate(outscope=True, key="PaidTill") - datetime.datetime.today()
diff = diff.days
credit = diff >= 0
data = db.get(doc_id=self.getid(self.combobox, database=db))
newspapernames = self.readdata("Name", False, database=newsdb)
for i in range(0, int(diff + 1)):
if i != 0 and i % 6 == 0:
try:
totalprice += self.deliverycharge()
except:
print("Error getting 'delivery charge'")
diff = abs(diff)
if diff > 1:
for newsname in newspapernames:
for i in range(diff + 1):
dayname = self.daynames[(i + datetime.datetime.today().weekday()) % 7]
try:
if data[newsname.lower() + dayname]:
if dayname == "Saturday":
totalprice += float(newsdb.get(where("Name") == newsname)["Saturday Price"])
elif dayname == "Sunday":
totalprice += float(newsdb.get(where("Name") == newsname)["Sunday Price"])
else:
totalprice += float(newsdb.get(where("Name") == newsname)["Normal Price"])
except Exception:
pass
if credit:
totalprice = totalprice
else:
totalprice = -totalprice
else:
totalprice = 0
except Exception:
totalprice = 0
return float(totalprice)
'''
Gets a date from the json file. Depending if "outscope" is false or true
it will give you the paid on date or the paid till date
default value for outscope is false
'''
def getdate(self, outscope=False, key="PaidOn"):
if not outscope:
try:
return(datetime.datetime.strptime(self.paydata["PaidTill"], "%Y-%m-%d"))
except KeyError:
return None
else:
try:
return(datetime.datetime.strptime(db.get(doc_id=self.getid(self.combobox, database=db))[key], "%Y-%m-%d"))
except KeyError:
return None
'''
Generates the necissary widgets for the pay bill window
uses labelframe to order eveyrything in a neat order
'''
def paybillwidgets(self, clear=True):
self.paydata = db.get(doc_id=self.getid(self.combobox, database=db))
if clear:
self.clearwidgets()
labelframe = LabelFrame(self.masterframe, text="Data")
Label(labelframe, text="Pay for").grid(row=0, column=0)
Entry(labelframe, width=5, textvariable=self.paydays).grid(row=0, column=1)
Label(labelframe, text="Days").grid(row=0, column=2)
labelframe.grid(row=0, padx=5, pady=5)
labelframe = LabelFrame(self.masterframe, text="Button")
Button(labelframe, text="Submit", command=self.paybuttonsubmit).grid(row=0)
labelframe.grid(row=0, column=1)
'''
Calculates the date difference and returns date
in the following format: YEAR-MONTH-DAY
'''
def countdatediff(self, date):
paytill = str(date + datetime.timedelta(days=int(self.paydays.get())))
return datetime.datetime.strptime(paytill[:10], "%Y-%m-%d")
'''
Submit the date stamps to the appropriate user.
PaidOn key shows when the person has last payed
PaidTill key show when the next payment is due
if you pay before PaidTill expires you will add ontop of the paid till
if you are late then you will be paying back for the days missed.
'''
def paybuttonsubmit(self):
if not self.getdate():
date = datetime.date.today()
ndate = str(self.countdatediff(date))
db.update({"PaidOn": str(date)[:10], "PaidTill": ndate[:10]}, doc_ids=[self.paydata.doc_id])
else:
date = self.getdate()
ndate = str(self.countdatediff(date))
db.update({"PaidOn": str(date)[:10], "PaidTill": ndate[:10]}, doc_ids=[self.paydata.doc_id])
messagebox.showinfo("Success", "Data has been updated")
self.datawidgets()
self.updatedata(None)
'''
Submits the week data to the json file so that it can be stored
permanently for that user
'''
def datasubmit(self):
for i in range(len(self.weeklist)):
db.update({str(self.newsname+self.daynames[i]): self.weeklist[i].get()}, doc_ids=[self.userdata.doc_id])
messagebox.showinfo("Success", "Data has been updated")
'''
Deletes the selected data from the json file and after which
the window is updated to represent the changes
'''
def datadelete(self):
if messagebox.askyesno("Confirmation", "Are you sure you want to delete {} record ?".format(self.userdata["Address"])):
db.remove(doc_ids=[self.userdata.doc_id])
self.datawidgets(clear=False)
root = Tk()
app = AppEntry(master=root)
root.protocol("WM_DELETE_WINDOW", onquit)
app.mainloop()