-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewcalhandler.go
160 lines (141 loc) · 4.03 KB
/
viewcalhandler.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
package dutycal
import (
"database/cassandra"
"html/template"
"io"
"log"
"net/http"
"strconv"
"time"
"ancient-solutions.com/ancientauth"
)
// ViewCalHandler is a handler for viewing lists of events on the
// calendar. Mostly used as an HTTP handler.
type ViewCalHandler struct {
am *authManager
db *cassandra.RetryCassandraClient
templates *template.Template
config *DutyCalConfig
location *time.Location
}
type calendarViewData struct {
Auth AuthDetails
Weekstart time.Time
WeekstartText string
WeekNumber int64
PreviousWeek int64
NextWeek int64
Days []string
Events [][]*Event
Unassigned []*Event
Mine []*Event
}
// NewViewCalHandler creates a new HTTP handler for viewing calendar entries.
// All flags will just be placed into the ViewCalHandler as they are.
func NewViewCalHandler(
db *cassandra.RetryCassandraClient, auth *ancientauth.Authenticator,
loc *time.Location, tmpl *template.Template,
conf *DutyCalConfig) *ViewCalHandler {
if db == nil {
log.Panic("db is nil")
}
if conf == nil {
log.Panic("conf is nil")
}
if tmpl == nil {
log.Panic("tmpl is nil")
}
if loc == nil {
log.Panic("loc is nil")
}
return &ViewCalHandler{
am: NewAuthManager(auth),
db: db,
templates: tmpl,
config: conf,
location: loc,
}
}
func (v *ViewCalHandler) ServeHTTP(
rw http.ResponseWriter, req *http.Request) {
var user string
var md calendarViewData
var ts time.Time
var week int64
var err error
var offset int
req.ParseForm()
if len(req.FormValue("week")) > 0 {
// The user entered a specific week number, so we need to build
// from that.
week, err = strconv.ParseInt(req.FormValue("week"), 10, 64)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
io.WriteString(rw, "Error parsing week input: "+
err.Error()+"\r\n")
log.Print("Error parsing week input: ", err)
return
}
} else {
// No week specified — we'll have to find out.
week = getWeekFromTimestamp(time.Now())
}
// Get the timestamp of the start of the week.
ts = time.Unix(0, 0)
ts = ts.Add(time.Duration(week)*24*7*time.Hour +
12*7*time.Hour).Truncate(24 * 7 * time.Hour)
_, offset = ts.Zone()
ts = ts.Add(time.Duration(-offset) * time.Second)
md.Weekstart = ts
md.WeekstartText = ts.Format("Mon 2 Jan 2006")
md.WeekNumber = week
md.PreviousWeek = week - 1
md.NextWeek = week + 1
md.Days = make([]string, 0)
md.Events = make([][]*Event, 0)
if md.PreviousWeek < 0 {
md.PreviousWeek = 0
}
for weekday := 0; weekday < 7; weekday++ {
var dayend time.Time = ts.Add(36 * time.Hour).Truncate(24 * time.Hour)
var events []*Event = make([]*Event, 0)
// We have to determine the offset again, since it may have changed
// due to DST or similar.
_, offset = ts.Zone()
dayend = dayend.Add(time.Duration(-offset) * time.Second)
events, err = FetchEventRange(v.db, v.config,
ts, dayend, -1, v.location, nil, false)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
io.WriteString(rw, "Error fetching events for "+ts.String()+
": "+err.Error()+"\r\n")
log.Print("Error fetching events for ", ts, ": ", err)
return
}
md.Events = append(md.Events, events)
md.Days = append(md.Days, ts.Format("Mon 2 Jan"))
ts = dayend
}
md.Unassigned, err = FetchEventRange(v.db, v.config, time.Now(),
time.Unix(0, 0), v.config.GetUpcomingEventsLookahead(), v.location,
&user, false)
if err != nil {
log.Print("Error fetching upcoming unassigned events: ", err)
}
v.am.GenAuthDetails(req, &md.Auth)
user = md.Auth.User
if len(user) > 0 {
md.Mine, err = FetchEventRange(v.db, v.config, time.Now(),
time.Unix(0, 0), v.config.GetUserEventsLookahead(), v.location,
&user, false)
if err != nil {
log.Print("Error fetching upcoming events for ", user, ": ", err)
}
}
err = v.templates.ExecuteTemplate(rw, "viewcalendar.html", &md)
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
io.WriteString(rw, "Error displaying calendar: "+err.Error()+"\r\n")
log.Print("Error displaying calendar: ", err)
}
}