forked from d101tm/tmstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakedistrictcreditreport.py
executable file
·136 lines (110 loc) · 3.96 KB
/
makedistrictcreditreport.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
#!/usr/bin/env python3
""" Build District Credit Report based on Google spreadsheet """
from datetime import datetime, timedelta
import gspread
import tmglobals
from tmutil import cleandate, normalize, getGoogleCredentials, getClubBlock
from simpleclub import Club
from datetime import datetime, timedelta
myglobals = tmglobals.tmglobals()
class Clubinfo:
def __init__(self, labels, row):
self.labels = labels
for (k, v) in zip(labels, row):
setattr(self, k, v)
def __repr__(self):
return ', '.join([f'{l, getattr(self, l)}' for l in labels])
class Winner:
def __init__(self, clubname):
self.clubname = clubname
if __name__ == "__main__":
import tmparms
# Establish parameters
parms = tmparms.tmparms()
parms.add_argument('--outfile', default='${workdir}/districtcredit.html')
parms.add_argument('--creditfile', default='')
parms.add_argument('--sheetname', default='D101_RecognitionCredits')
# Do global setup
myglobals.setup(parms)
curs = myglobals.curs
conn = myglobals.conn
# Get the spreadsheet
gc = gspread.authorize(getGoogleCredentials())
book = gc.open_by_url(parms.creditfile)
sheet = book.worksheet(parms.sheetname)
outfile = open(parms.outfile, 'w')
### We are dependent on the format of the spreadsheet
# Last updated value is in 'B1' - let's convert it to English text
asof = sheet.acell('B1', value_render_option='UNFORMATTED_VALUE').value
asof = datetime(1899, 12, 30) + timedelta(days=asof)
asof = asof.strftime("%B %-d, %Y")
# Bring in the dataTables resources
outfile.write(
"""
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#credits').DataTable({"paging":false});
} );
</script>
<style type="text/css">
th.rjust, td.rjust {text-align: right;}
</style>
""")
# Write the prefatory information
outfile.write(
f"""<p>The following table shows District Credit as of {asof}. You can sort
any column by clicking on it, or limit the table to lines containing a string.</p>
""")
# Write the table header
outfile.write(
"""
<table id="credits" class="compact stripe row-border">
<thead>
<tr>
<th>Area</th>
<th>Club Number</th>
<th>Club Name</th>
<th>Earned</th>
<th>Available</th>
</tr>
</thead>
<tbody>
""")
# Labels are in row 3
labels = sheet.row_values(3)
nlabels = [normalize(c) for c in labels]
# Find 'ID' (club number), 'TOTAL CREDITS' and 'BALANCE CREDITS'
# We'll pick up club name and alignment from the database
idcol = nlabels.index('id')
namecol = nlabels.index('name')
credcol = nlabels.index('totalcredits')
balancecol = nlabels.index('balancecredits')
areacol = nlabels.index('area')
clubs = Club.getActiveClubs(curs)
# Values begin in row 4
for row in sheet.get_all_values()[3:]:
clubnum = row[idcol]
clubname = row[namecol]
alignment = row[areacol]
if not clubnum:
continue
try:
club = clubs[clubnum]
alignment = club.division + club.area
clubname = club.clubname
except KeyError:
continue
earned = row[credcol]
available = row[balancecol]
outfile.write('<tr>\n')
outfile.write(f' <td>{alignment}</td>\n')
outfile.write(f' <td class="rjust">{clubnum}</td>\n')
outfile.write(f' <td>{clubname}</td>\n')
outfile.write(f' <td class="rjust">{earned}</td>\n')
outfile.write(f' <td class="rjust">{available}</td>\n')
outfile.write('</tr>\n')
outfile.write(' </tbody>\n')
outfile.write('</table>\n')