-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
142 lines (121 loc) · 6.51 KB
/
app.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
import datetime
import csv
import PySimpleGUI as sg
from makepdf import HTML2PDF, cert
from sendemail import *
import os
defaultNamelist = os.getcwd()
defaultCert = os.getcwd()
defaultOutput = os.getcwd()
defaultEmail = '[email protected]'
defaultPercentage = 0
version = '1.0.0'
sender = '[email protected]' # Change this according to your requirements
mailserver = 'SMTP.YOURDOMAIN.COM' # Change this according to your requirements
def scrLog(text):
currentDT = datetime.datetime.now()
print( '['+str(currentDT)+'] ' + text)
layout = [
[sg.Text('Put in the following details:')],
[sg.Text('Name List', size=(16, 1)), sg.InputText(str(defaultNamelist),key='list'), sg.FileBrowse()],
[sg.Text('Certificate', size=(16, 1)), sg.InputText(str(defaultCert), key='cert'), sg.FileBrowse()],
[sg.Text('Output Directory', size=(16, 1)), sg.InputText(str(defaultOutput),key='out'), sg.FolderBrowse()],
[sg.Radio('Generate Certificate', "RADIO1", size=(20, 1) ,default=True), sg.Radio('Generate Certificate and Send Email', "RADIO1")],
[sg.Text('Bcc Email', size=(16, 1)), sg.InputText(str(defaultEmail), key='email')],
[sg.Text('Email Subject', size=(16, 1)), sg.InputText(str('Training Certificate'), key='Subject')],
[sg.Text('Mesage Body', size=(16, 1)), sg.Multiline(str('Attached is your Certificate for attending our workshop. Thank you!'), size=(45, 8), key='Body')],
[sg.Text(' ', size=(16, 1))],
[sg.Text('Status :', size=(16, 1))],
[sg.ProgressBar(100, orientation='h', size=(42, 20), key='progressbar'), sg.Text(str(defaultPercentage)+' %', key='percentageNumber')],
[sg.Output(size=(72, 10))],
[sg.Text(' ', size=(16, 1))],
[sg.Submit('Start'), sg.Cancel('Quit'), sg.Button('About')]
]
window = sg.Window(
title='Certificate Generator',
disable_close=True,
icon='certificate.ico'
).Layout(layout)
progress_bar = window.FindElement('progressbar')
while True:
(event, values) = window.Read()
if event == 'EXIT' or event == 'Quit' or event is None:
break # exit button clicked
if event == 'Start':
emailCounter = 0
pdfCounter = 0
errorEmailCounter = 0
errorPdfCounter = 0
nameList = values['list']
certTemplate = values['cert']
outputDir = values['out']
certOnly = values[0]
sendEmail = values[1]
cc = values['email']
subject = values['Subject']
msgbody = values['Body']
if (msgbody != 'Do not reply to this email. This is a system generated message!'):
msgbody = values['Body']+'\nDo not reply to this email. This is a system generated message!'
scrLog('opening ' +nameList)
try :
row_count = sum(1 for row in csv.reader( open(nameList) ) )
except:
scrLog('Error opening ' +nameList)
try:
with open(nameList, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
filename = row['name'] + '_' +row['date'] + '_' + row['course']
filename = filename.replace(" ", "")+".pdf"
recipients = row['email']
scrLog('Creating certificate '+filename)
result = cert(row['name'], row['course'], row['date'],certTemplate, filename, row['location'], row['serialnumber'], outputDir)
if result == 'Success':
scrLog('[success] '+ filename + ' was created')
pdfCounter += 1
if sendEmail == True:
scrLog('Sending '+filename+' to '+recipients)
emailed = email(sender,recipients,subject,filename,msgbody,mailserver,outputDir, cc)
if emailed == 'sent':
scrLog('[success] '+'Notification Sent for ' + filename + ' to ' + recipients)
emailCounter += 1
else:
scrLog('Error Sending certificate ('+ filename +') to '+recipients)
errorEmailCounter +=1
elif result == 'Error':
scrLog('[Error] '+ filename + ' file not created')
errorPdfCounter += 1
if sendEmail == True:
percentage = (((emailCounter + pdfCounter + errorPdfCounter + errorEmailCounter))/((row_count-1)*2))*100
else:
percentage = (((pdfCounter + errorPdfCounter))/(row_count-1))*100
progress_bar.UpdateBar(percentage)
values['percentageNumber'] = str(int(percentage)) + ' %'
window.FindElement('percentageNumber').Update(values['percentageNumber'])
window.FindElement('list').Update(values['list'])
window.FindElement('cert').Update(values['cert'])
window.FindElement('out').Update(values['out'])
window.FindElement('email').Update(values['email'])
window.FindElement('Body').Update(values['Body'])
window.FindElement('Subject').Update(values['Subject'])
except:
scrLog('Error opening File!')
window.FindElement('list').Update(values['list'])
window.FindElement('cert').Update(values['cert'])
window.FindElement('out').Update(values['out'])
window.FindElement('email').Update(values['email'])
window.FindElement('Body').Update(values['Body'])
window.FindElement('Subject').Update(values['Subject'])
scrLog('Email :'+ str(emailCounter) +' = Sent. '+ str(errorEmailCounter) +' = Error sending')
scrLog('PDF : '+ str(pdfCounter) +' = Created. '+ str(errorPdfCounter) +' = Error Creation')
scrLog('Done!')
elif event == 'Quit':
scrLog('Quit...\nBye!')
elif event == 'About':
window.FindElement('list').Update(values['list'])
window.FindElement('cert').Update(values['cert'])
window.FindElement('out').Update(values['out'])
window.FindElement('email').Update(values['email'])
window.FindElement('Body').Update(values['Body'])
window.FindElement('Subject').Update(values['Subject'])
sg.Popup('Ron Bulaon\nRonWork.com\[email protected]\n\nVersion '+version, icon='certificate.ico', title='About')