This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
303 lines (251 loc) · 10.4 KB
/
main.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
#!/usr/bin/env python3
import csv
import json
import os
import re
import sys
from datetime import datetime, timedelta
import requests
from dateutil import relativedelta
from PIL import Image, ImageDraw, ImageFont
class promotion:
def __init__(self, milpacID, rank, date):
'''
Handle promotion processing.
Inputs:
milpacID (int): Milpac ID of trooper being promoted.
rank (str): Rank being promoted, short version (Ex: SGT).
date (str): Date trooper is being promoted, in DD-MMM-YYYY format. (Ex: 25-Jan-2020)
'''
self.milpacID = milpacID
self.rank = rank
self.date = date
with open("config.json") as file:
self.config = json.load(file)
ranks = [i["short"] for i in self.config["ranks"]]
if not rank in ranks:
sys.exit(f"{rank} is not a correct rank. Options are:\n{ranks}\nTerminating Program.")
if not re.findall(r"\d{2}-\w{3}-\d{4}", date):
sys.exit(f"{date} is an improper date format.\nCorrect example: 04-Jan-2020\nTerminating Program.")
with open("APIKey.txt") as file:
self.APIKey = file.readline().replace("\n", "")
self.rawTroopers = requests.get(
f"https://api.7cav.us/v1/users/active",
headers={"Authorization": f"Bearer {self.APIKey}"}
).json()["data"]["users"]
# Trooper's basic information
self.trooper = [i for i in self.rawTroopers if i["milpac_id"] == milpacID][0]
self.userID = self.trooper["user_id"] # Trooper's forum ID
# Config info about requested rank.
self.requestedRank = [i for i in self.config["ranks"] if i["short"] == rank][0]
def push(self):
# EXECUTES EVERYTHING!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TIG = self.checkTIG()
if TIG != True:
print(TIG)
return 0
if self.checkNCOA() == False:
print("Trooper does not meet NCOA requirements.")
return 0
self.promoCitation()
self.ncoCitation()
print("Approver: "+self.getApprover())
def checkTIG(self):
'''
Checks Time In Grade (TIG) Requirement. One month is 30 days.
Output:
True (bool), if TIG met.
If not met, dict with following:
RequiredTIG (int): Required TIG, in months.
TIG (int): Current TIG, in months.
Eligible (str): Date eligible for requested promo.
'''
lastPromo = datetime.strptime(self.trooper["promotion_date"], "%Y-%m-%d %H:%M:%S")
currentPromo = datetime.strptime(self.date, "%d-%b-%Y")
reqTIG = self.requestedRank["RequiredTIG"]
TIG = relativedelta.relativedelta(currentPromo, lastPromo).months
# TIG = (currentPromo - lastPromo).month
if reqTIG == 0: # Hadle no TIG Requirement
return True
elif TIG >= reqTIG: # Requirement met.
return True
else: # Requirement not met.
return {
"RequiredTIG": reqTIG,
"CurrentTIG": TIG,
"Eligible": str(lastPromo + (reqTIG * timedelta(days=30)))
}
def checkNCOA(self):
# Checks for NCOA eligibility.
# If NCOA check not requried, return a Null value.
if self.requestedRank["CheckNCOA"] == False:
return None
serviceRecord = requests.get(
f"https://api.7cav.us/v1/user/{self.userID}/records",
headers={"Authorization": f"Bearer {self.APIKey}"}
).json()["data"]
p2, p1, old = False, False, False
for s in serviceRecord:
entry = s["details"].lower()
if any(e in entry for e in ["ncoa warrior leadership course", "ncoa-wlc"]): # Check for any NCOA graduation.
if "phase ii" in entry: # Check for phase 2.
p2 = True
elif "phase i" in entry: # Check for phase 1.
p1 = True
else: # If phase 1 and 2 not found. Assume old system.
old = True
if old == True:
return True
elif p1 == True and p2 == True:
return True
else:
return False
def getApprover(self):
# Gets promotion approver.
rankApprover = self.requestedRank["Approver"]
regex = re.findall(r"(\w)/(\d-7)", self.trooper["primary_position"])
if regex == False:
return None
else:
company = "/".join(regex[0])
battalion = regex[0][1]
def findByBillet(billet):
user = [i for i in self.rawTroopers if i["primary_position"] == billet][0]
realName = user["real_name"]
forumName = user["username"]
return f"{realName} | @{forumName}"
if rankApprover == False:
return "N/A"
elif rankApprover == "RTC":
return "Recruit Training Command"
elif rankApprover == "S1":
return "S1 Department"
elif rankApprover == "Company":
return findByBillet(f"Commander {company}")
elif rankApprover == "Battalion":
return findByBillet(f"Battalion Commander {battalion}")
elif rankApprover == "COS":
return findByBillet("Chief of Staff")
elif rankApprover == "GOA":
return findByBillet("Regimental Commander")
def folderName(self):
# Get name of trooper's citation folder.
name = self.trooper["real_name"].split(" ")
return "-".join([name[-1]] + name[:-1]).lower()
def ordinalIndicator(self, num):
'''
Take number and output string w/ ordinal indicator attached
Inputs:
num (int): Number to be formatted
Output (str): number with ordinal indicator (Ex: 17th)
'''
if int(num) in range(11, 20): # If a 'teen' number. (11, 12, 13, ...)
return f"{int(num)}th"
elif str(num)[-1] == "1": # If number ends in "1"
return f"{int(num)}st"
elif str(num)[-1] == "2": # If number ends in "2"
return f"{int(num)}nd"
elif str(num)[-1] == "3": # If number ends in "3"
return f"{int(num)}rd"
else:
return f"{int(num)}th"
def promoCitation(self):
# Make promotion citation.
rankShort = self.requestedRank["short"]
img = Image.open(f"templates/{rankShort}.jpeg")
draw = ImageDraw.Draw(img)
def writeText(confName, text):
nonlocal img, draw
c = self.requestedRank["citation"][confName]
x, y = c["pos"]
font = ImageFont.truetype(c["fontName"], c["fontSize"])
w, h = draw.textsize(text, font=font)
draw.text(
(x-(w/2), y-(h/2)),
text,
(0,0,0),
font=font
)
# Write name
writeText("name", self.trooper["real_name"].upper())
# Handle dateText formating
dt = datetime.strptime(self.date, "%d-%b-%Y")
dateReplace = [
["[d]", self.ordinalIndicator(dt.strftime("%d"))],
["[D]", self.ordinalIndicator(dt.strftime("%d")).upper()],
["[m]", dt.strftime("%B")],
["[M]", dt.strftime("%B").upper()],
["[y]", dt.strftime("%Y")]
]
dateText = self.requestedRank["citation"]["date"]["dateText"]
for r in dateReplace:
dateText = dateText.replace(r[0], r[1])
# Write date
writeText("date", dateText)
# Save the file
try:
os.makedirs(f"generatedCitations/{self.folderName()}")
except:
pass
fileName = f"generatedCitations/{self.folderName()}/"+self.requestedRank["paygrade"]+"-"+self.requestedRank["short"]+"-"+dt.strftime("%y%m%d")+".jpeg"
img.save(fileName)
print(f"{fileName} Generated")
def ncoCitation(self):
# Make NCO ribbon citation. if Required
# If NCO ribbon not required, terminate function.
if self.requestedRank["NCORibbon"] == False:
return False
rawAwards = requests.get(
f"https://api.7cav.us/v1/user/{self.userID}/awards",
headers={"Authorization": f"Bearer {self.APIKey}"}
).json()["data"]
fullRank = self.requestedRank["long"]
# If the trooper already has NCO ribbon, temrinate function.
if any(i for i in rawAwards if f"{fullRank} Promotion" in i["details"]):
return False
# Make NCO Ribbon citation.
rankShort = self.requestedRank["short"]
img = Image.open(f"templates/NCO-{rankShort}.jpeg")
draw = ImageDraw.Draw(img)
def writeText(confName, text):
nonlocal img, draw
c = self.config["NCORibbon"]["citation"][confName]
x, y = c["pos"]
font = ImageFont.truetype(c["fontName"], c["fontSize"])
w, h = draw.textsize(text, font=font)
draw.text(
(x-(w/2), y-(h/2)),
text,
(0,0,0),
font=font
)
# Write name
citationName = self.requestedRank["long"]+" "+self.trooper["real_name"]
writeText("name", citationName.upper())
# Handle dateText formating
dt = datetime.strptime(self.date, "%d-%b-%Y")
dateReplace = [
["[d]", self.ordinalIndicator(dt.strftime("%d"))],
["[D]", self.ordinalIndicator(dt.strftime("%d")).upper()],
["[m]", dt.strftime("%B")],
["[M]", dt.strftime("%B").upper()],
["[y]", dt.strftime("%Y")]
]
dateText = self.config["NCORibbon"]["citation"]["date"]["dateText"]
for r in dateReplace:
dateText = dateText.replace(r[0], r[1])
# Write date
writeText("date", dateText)
# Save the file
fileName = f"generatedCitations/{self.folderName()}/NCO-"+self.requestedRank["short"]+"-"+dt.strftime("%y%m%d")+".jpeg"
img.save(fileName)
print(f"{fileName} Generated")
def forumPost(self):
# Generate forum post
pass
if __name__ == "__main__":
promotion(
milpacID=int(input("Milpac ID: ")),
rank=input("Rank (short): ").upper(),
date=input("Date (dd-mmm-yyyy): ")
).push()