+ added ical generation for the timetable to timetable.go
Signed-off-by: Wasabi1092 <xavier.tang109@gmail.com>
---
src/server/timetable.go | 63 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 63 insertions(+)
diff --git a/src/server/timetable.go b/src/server/timetable.go
index f44aa12..0652a11 100644
--- a/src/server/timetable.go
+++ b/src/server/timetable.go
@@ -365,3 +365,66 @@ func TimetableHTML(user site.User) (timetableData, error) {
return data, nil
}
+
+// Export the user calendar as a .ics file
+
+func TimetableIcal(user site.User, start, end time.Time, w http.ResponseWriter) error {
+ var err error
+ iCalString := ""
+
+ school, ok := schools[user.School]
+ if !ok {
+ w.WriteHeader(500)
+ return errors.New("unsupported platform", nil)
+ }
+ lessons, err := school.Lessons(user, start, end)
+
+ if err != nil {
+ w.WriteHeader(500)
+ return errors.New("failed to get lessons", err)
+ }
+ //build the start of the string
+ iCalString += "BEGIN:VCALENDAR\n"
+ iCalString += "VERSION:2.0\n"
+ iCalString += "PRODID:taco/calendar\n"
+ iCalString += "CALSCALE:GREGORIAN\n"
+ iCalString += "METHOD:PUBLISH\n"
+ for _, lesson := range lessons {
+ uuid, err := GenerateUUID()
+ if err != nil {
+ w.WriteHeader(500)
+ return errors.New("failed to generate UUID", err)
+ }
+
+ iCalString += "BEGIN:VEVENT\n"
+ iCalString += string(uuid[:]) + "\n"
+ iCalString += "DTSTAMP:" + time.Now().Format("20060102T150405Z") + "\n"
+ iCalString += "DTSTART:" + lesson.Start.Format("20060102T150405Z") + "\n"
+ iCalString += "DTEND:" + lesson.End.Format("20060102T150405Z") + "\n"
+ iCalString += "SUMMARY:" + lesson.Class + "\n"
+ iCalString += "DESCRIPTION:" + lesson.Teacher + "\n"
+ iCalString += "LOCATION:" + lesson.Room + "\n"
+ iCalString += "END:VEVENT\n"
+ }
+ iCalString += "END:VCALENDAR\n"
+
+ _, err = io.WriteString(w, iCalString);
+ if err!=nil{
+ w.WriteHeader(500)
+ return errors.New("Failed to write calendar file", err)
+ }
+ return nil
+}
+
+func GenerateUUID() (u *UUID, err error){
+ // generates a version 4 UUID and returns the byte string
+ u = new(UUID)
+ _, err = rand.Read(u[:])
+ if err != nil {
+ return nil, errors.New("failed to generate UUID", err)
+ }
+ u[8] = (u[8] | 0x40) & 0x7F
+ u[6] = (u[6] & 0xF) | (4 << 4)
+
+ return u, nil
+}
--
2.39.3 (Apple Git-145)
This is one of the best patches I've reviewed so far! However, there
are some minor issues that need fixing.
You need to make sure your local code compiles before you commit. This
patch unfortunately does not compile. The most notable issue is that
UUID is undefined, and based on GenerateUUID I'm not sure what data
structure UUID should be. Preferably it should simply be a string, not
some other structure.