forked from kaybhutani/Certify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
155 lines (123 loc) · 5.56 KB
/
generate.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
import pandas as pd
from PIL import Image, ImageFont, ImageDraw
import sys
from hashids import Hashids
from datetime import datetime, date
import time
import os
import pymongo
import shutil
import config
class Create:
#defining variables
def __init__(self, template, spreadsheet, values, font, certify, ts):
self.template = template
self.spreadsheet = spreadsheet
self.values = values
self.font = font
self.certify = certify
self.ts = ts
def resizeFontValues(self):
img = Image.open(self.template).convert('RGB')
width, height = img.size
heightNew = 500
ratio = height/heightNew
for coor in self.values:
self.values[coor] = [int((self.values[coor][0])*ratio), int((self.values[coor][1])*ratio) ]
self.font["size"]=int((self.font["size"])*ratio)
if self.certify["verify"] == True:
self.certify["coordinates"]=[ int((self.certify["coordinates"][0])*ratio),int((self.certify["coordinates"][1])*ratio) ]
def generate(self):
try:
data=pd.read_csv(self.spreadsheet)
except:
data=pd.read_excel(self.spreadsheet)
data.fillna("")
#updating data
print(self.values, self.font, self.certify)
self.resizeFontValues()
print(self.values, self.font, self.certify)
#getting max no. of rows
count = max(list(data.count()))
#update data with certify code if user checked it
datanew = self.addUnique(data,count)
#create directory for files
path = './static/temp/download/download_' + str(self.ts)
os.mkdir(path)
os.mkdir(path + '/images')
#generating certificate
self.imageProcessing(datanew,count)
#remove folder after creating zip
shutil.rmtree(path)
def imageProcessing(self,data,count):
for i in range(0,count):
#opening image and canvas
img = Image.open(self.template).convert('RGB')
#opening canvas
draw = ImageDraw.Draw(img)
#k for iterating through values{} index
for j,k in zip(self.values,range(0,len(self.values))):
fontSize = self.font['size']
if(j=='Certify'):
fontSize=int(fontSize/1.5)
#get font for that element
fontName = ImageFont.truetype(self.font['name'], fontSize)
# center allign text
w, h = draw.textsize(data[j][i])
#draw on image
draw.text([self.values[j][0]-(w/2),self.values[j][1]], data[j][i],font=fontName, fill=self.font['color'])
#save image separately
#also save in database --> verified folder if verification is checked
if(self.certify['verify']):
img.save('./static/verified/images/' + data["Certify"][i] +'.jpg')
#save for downloading
folder = './static/temp/download/download_' + str(self.ts)
img.save( folder + '/images/' + str(i) +'.jpg')
#save dataframe as spreadsheet
data.to_csv(folder + '/data.csv', header=False, index=False)
shutil.make_archive(folder , 'zip', folder)
#generate unique id based on timestamp and salt
def unique(self):
#generating hashid, specifying salt and alphabets available
hashids = Hashids(salt = "certify to create certificates", alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvyxyz1234567890")
#generating unique id based on time stamp before and after decimal
uniqueId = hashids.encode(int(datetime.today().timestamp()))[-3:] + hashids.encode(int(str(datetime.today().timestamp()).split('.')[1]))
return uniqueId
#add unique id to data frame if certify verification is True
def addUnique(self,data,count):
if(self.certify["verify"]==True):
uIds=[]
#creating dataframe copy
data = data.copy()
#generate unique id for all values
for i in range(0,count):
uIds.append(self.unique())
#append column to dataframe
data['Certify'] = uIds
#creating empty list for database data
listData = []
#iterating through unique id array and 0 to count in same loop
for i,n in zip(uIds,range(0,count)):
#adding unique id to dictionary
dictData = {}
#making dict for values corresponding to certify code
dictData[i] = {}
#iterating through values
for j in self.values:
#adding value and its data
dictData[i][j] = data[j][n]
dictData[i]["Generated on"] = date.today().strftime("%d/%m/%Y")
listData.append(dictData)
self.addToDatabase(listData)
#adding certify to value
self.values["Certify"]=self.certify["coordinates"]
#return new data frame
return data
def addToDatabase(self,listData):
client = pymongo.MongoClient(config.MONGODB_URI, connectTimeoutMS=30000)
#database certify
database = client.certify
#collection userData in Certify
collection = database["userData"]
#inserting full list as many documents
collection.insert_many(listData)