-
Notifications
You must be signed in to change notification settings - Fork 0
/
councilmember.go
221 lines (197 loc) · 5.16 KB
/
councilmember.go
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
package main
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/jehiah/legislator/db"
)
func (l LegislationList) Statuses() []Status {
d := make(map[string]int)
for _, ll := range l {
d[ll.StatusName] += 1
}
var o []Status
for n, c := range d {
o = append(o, Status{Name: n, Count: c, Percent: (float64(c) / float64(len(l))) * 100})
}
sortSeq := []string{
// exhaustive from MatterStatuses
"Adopted",
"Approved",
"Companion Pending Approval by Council",
"Coupled on Call-Up Vote",
"Defeated",
"Deferred",
"Disapproved",
"Discharged from Committee",
"Failed",
"Filed",
"General Orders Calendar",
"Hearing Transcripts ",
"Local Laws",
"Press Conference Filed",
"Press Conference Scheduled",
"Received, Ordered, Printed and Filed",
"Reported from Committee and Introduced",
"Reported from Committee",
"Returned Unsigned by Mayor",
"Special Event Filed",
"Special Event Scheduled",
"Town Hall Meeting Filed",
"Town Hall Meeting Scheduled",
"Introduced",
"Committee",
"Laid Over in Committee",
"Filed (End of Session)",
"Vetoed",
"Withdrawn",
"Enacted (Charter Referendum)",
"Enacted (Mayor's Desk for Signature)",
"Enacted",
}
sortLookup := make(map[string]int, len(sortSeq))
for i, s := range sortSeq {
sortLookup[s] = i
}
sort.Slice(o, func(i, j int) bool { return sortLookup[o[i].Name] < sortLookup[o[j].Name] })
// TODO: sort by sequence not by string
return o
}
type Status struct {
Name string
Count int
Percent float64
}
func (s Status) Description() string {
switch s.Name {
case "Committee":
return "in Committee"
}
return s.Name
}
func (s Status) CSSClass() string {
return "status-" + strings.ToLower(strings.Fields(s.Name)[0])
}
// Councilmember returns the list of councilmembers at /councilmembers/$name
//
// Redirects from /councilmembers/$district -> /councilmembers/$name
func (a *App) Councilmember(w http.ResponseWriter, r *http.Request) {
councilmember := r.PathValue("councilmember")
// log.Printf("Councilmember %q", councilmember)
if i, _ := strconv.Atoi(councilmember); i > 0 && i <= 51 {
var metadata []PersonMetadata
err := a.getJSONFile(r.Context(), "build/people_metadata.json", &metadata)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
for _, m := range metadata {
if m.District == i {
a.addExpireHeaders(w, time.Hour*24*7)
http.Redirect(w, r, "/councilmembers/"+m.Slug, 301)
return
}
}
}
if matched, err := regexp.MatchString("^[a-z-]+$", councilmember); err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
} else if !matched {
log.Printf("council member %q not found", councilmember)
http.Error(w, "Not Found", 404)
return
}
t := newTemplate(a.templateFS, "councilmember.html")
var people []db.Person
err := a.getJSONFile(r.Context(), "build/people_all.json", &people)
if err != nil {
if err == storage.ErrObjectNotExist || os.IsNotExist(err) {
a.addExpireHeaders(w, time.Minute*5)
http.Error(w, "Not Found", 404)
return
}
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
var person Person
for _, p := range people {
if p.Slug == councilmember {
person = Person{Person: p}
break
}
}
if person.Person.Slug == "" {
log.Printf("council member %q not found", councilmember)
a.addExpireHeaders(w, time.Minute*5)
http.Error(w, "Not Found", 404)
return
}
var metadata []PersonMetadata
err = a.getJSONFile(r.Context(), "build/people_metadata.json", &metadata)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
for _, m := range metadata {
if m.ID == person.Person.ID {
person.PersonMetadata = m
}
}
cacheTTL := time.Minute * 15
if person.End.Before(time.Now()) {
cacheTTL = time.Hour
}
type Page struct {
Page string
Person Person
LastSync LastSync
Legislation LegislationList
PrimarySponsor LegislationList
SecondarySponsor LegislationList
CurrentSession Session
}
body := Page{
Page: "councilmembers",
Person: person,
CurrentSession: CurrentSession,
}
if person.IsActive {
// TODO: some files may be cached from previous sessions
err = a.getJSONFile(r.Context(), fmt.Sprintf("build/legislation_%s.json", person.Person.Slug), &body.Legislation)
if err != nil {
// not found is ok; it means they are likely not active in current session (yet?)
if err != storage.ErrObjectNotExist {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
}
body.PrimarySponsor = body.Legislation.FilterPrimarySponsor(person.ID())
body.SecondarySponsor = body.Legislation.FilterSecondarySponsor(person.ID())
}
err = a.getJSONFile(r.Context(), "build/last_sync.json", &body.LastSync)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
w.Header().Set("content-type", "text/html")
a.addExpireHeaders(w, cacheTTL)
err = t.ExecuteTemplate(w, "councilmember.html", body)
if err != nil {
log.Print(err)
http.Error(w, "Internal Server Error", 500)
return
}
}