-
Notifications
You must be signed in to change notification settings - Fork 0
/
fms.py
192 lines (149 loc) · 5.26 KB
/
fms.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
import eel
from os import environ, path, listdir
from tempfile import gettempdir
from psutil import net_connections
from docxtpl import DocxTemplate, RichText
from docx2pdf import convert
from shutil import copyfile
from json import load, dump
from re import match
eel.init('src')
@eel.expose
def generate(data):
try:
domaine = data['metier']['domaine'].strip()
nbp = len(data['parcours'])
if nbp==5: flimit = 288
else: flimit = 403
# Try to make a space between aspet&contrainte
if len(data['metier']['aspectPositif']) < 45:
data['metier']['aspectPositif'] += '\n'
context = {
'nom' : RichText(data['profil']['nom'], font='Arial', bold=True,\
size = 22 if len(data['profil']['nom'])>50 else 24),
'poste' : data['profil']['poste'],
'biographie' : RichText(data['profil']['biographie'], font='Arial', \
size = 22 if len(data['profil']['biographie'])>399 else 24),
'competenceQualite' : RichText(data['metier']['competenceQualite'], font='Arial', color='#000000', \
size = 22 if len(data['metier']['competenceQualite'])>185 else 24),
'accessMetier' : RichText(data['metier']['accessMetier'], font='Arial', color='#000000',\
size = 22 if len(data['metier']['accessMetier'])>160 else 24),
'aspectPositif' : RichText(data['metier']['aspectPositif'], font='Arial', color='#000000',\
size = 22 if len(data['metier']['aspectPositif'])>72 else 24),
'contrainte' : RichText(data['metier']['contrainte'], font='Arial', color='#000000',\
size = 22 if len(data['metier']['contrainte'])>72 else 24),
'domaine' : domaine,
'formation' : RichText(data['etudes']['formation'], font='Arial', color='#000000',\
size = 22 if (len(data['etudes']['formation'])>flimit) else 24),
'etablissement' : RichText(data['etudes']['etablissement'], font='Arial', color='#000000',\
size = 28 if len(data['etudes']['etablissement'])>32 else 32),
'insertionProfessionnel' : RichText(data['etudes']['insertionProfessionnel'], font='Arial', color='#000000',\
size = 22 if len(data['etudes']['insertionProfessionnel'])>111 else 24)
}
for i in range(nbp):
if i == nbp-1: color = '#ffffff'
else: color = '#000000'
context[f'p{i}'] = RichText(data['parcours'][i][0], font='Arial', color=color,\
size = 22 if len(data['parcours'][i][0])>11 else 24)
context[f'v{i}'] = RichText(data[ 'parcours'][i][1], font='Arial', color=color, \
size = 22 if len(data['parcours'][i][1])>34 else 24)
col = None
for kdom, vdom in verifJSON().items():
if kdom == domaine:
col = vdom
break
# Si aucune correspondance
if not col: return
# Get the template
doc = DocxTemplate(f"template/Trame-vierge{nbp}-{col}.docx")
# Change image on template if image set
if data['profil']['profilImage'] != '':
data['profil']['profilImage'] = forceJPG(data['profil']['profilImage'])
doc.replace_pic('test.jpg', data['profil']['profilImage'])
# Generate template
doc.render(context)
name = "FicheMetier_" + data['profil']['poste'].replace(' ', '_')
# Save the doc generated
doc.save(f"{gettempdir()}\\{name}.docx")
# Convert to pdf
convert(f"{gettempdir()}\\{name}.docx")
return f"{gettempdir()}\\{name}.pdf"
except Exception as err:
print(err)
return
@eel.expose
def getAllcolor():
allcolor = []
if path.isdir("template"):
tempNames = listdir("template/")
for templ in tempNames:
if match(r'^Trame-vierge[3-5]{1}-[a-zA-Z_]{1,}.docx$', templ):
# decomposer pour avoir que la couleur
allcolor.append(templ.split("-")[-1].split(".")[0])
# suppression de doublons
return list(set(allcolor))
@eel.expose
def addDomaine(dom, col):
try:
# ouvre le fichier de domaine personalisé
fdom = open("_domaine.json", "r")
# charger les configurations
jDom = load(fdom)
except Exception as err:
print(err)
# On crée un nouveau apres erreur
fdom = open("_domaine.json", "w")
jDom = {"new_domaine": {dom: col}}
else:
fdom.close()
fdom = open("_domaine.json", "w")
if jDom.get("new_domaine"):
# on ajoute dans l'existante
jDom["new_domaine"][dom] = col
else: jDom = {"new_domaine": {dom: col}}
finally:
dump(jDom, fdom)
fdom.close()
return True
@eel.expose()
def getAllDomaine():
return list(verifJSON().keys())
def forceJPG(img):
if not img.endswith('.jpg'):
filename = img.split('\\')[-1]
newIm = f"{gettempdir()}\\{filename}.jpg"
copyfile(img, newIm)
return newIm
return img
def verifJSON():
domaine = {
'Santé' : 'bleu',
'Informatique' : 'bleu',
'Commerce et administration': 'bleu',
'Agronomie' : 'vert',
'Science humaine et Communication' : 'rouge',
'Tourisme' : 'orange',
'Industrie et BT' : 'violet',
"Justice et force de l'ordre" : 'jaune'
}
if path.isfile("_domaine.json"):
with open("_domaine.json", 'r', encoding='Utf-8') as fDom:
try:
jDom = load(fDom)
if jDom.get('new_domaine'):
for k,v in jDom.get('new_domaine').items():
domaine[k] = v
except Exception as err: print(err)
return domaine
def viewPort(port):
# Verification d'un PORT
for proc in net_connections():
if proc.laddr.port == port: return True
def main():
PORT = 1333
while viewPort(PORT):
PORT += 1
environ['fmsPORT'] = str(PORT)
eel.start(mode='custom', cmdline_args=['node_modules/electron/dist/electron.exe', '.'], port=PORT)
if __name__ == '__main__':
main()