示例#1
0
func session(w http.ResponseWriter, r *http.Request) *webapp.Error {
	idString := r.FormValue("id")
	if idString == "" {
		return missingFields(w)
	}
	id, err := strconv.ParseInt(idString, 10, 64)
	if err != nil {
		return invalidData(w, fmt.Sprintf("Couldn't parse %q as ID", idString))
	}
	c := appengine.NewContext(r)
	session, err := classes.SessionWithID(c, id)
	switch err {
	case nil:
		break
	case classes.ErrSessionNotFound:
		return invalidData(w, fmt.Sprintf("No such session"))
	default:
		return webapp.InternalError(fmt.Errorf("failed to find session %d: %s", id, err))
	}
	classList := session.Classes(c)
	sort.Sort(classes.ClassesByStartTime(classList))
	teachers := classes.TeachersByClass(c, classList)
	data := map[string]interface{}{
		"Session":     session,
		"Classes":     classes.GroupedByDay(classList),
		"DaysInOrder": daysInOrder,
		"Teachers":    teachers,
	}
	if err := sessionPage.Execute(w, data); err != nil {
		return webapp.InternalError(err)
	}
	return nil
}
示例#2
0
func addClass(w http.ResponseWriter, r *http.Request) *webapp.Error {
	idString := r.FormValue("session")
	if idString == "" {
		return missingFields(w)
	}
	id, err := strconv.ParseInt(idString, 10, 64)
	if err != nil {
		return invalidData(w, fmt.Sprintf("Invalid session ID"))
	}
	c := appengine.NewContext(r)
	session, err := classes.SessionWithID(c, id)
	switch err {
	case nil:
		break
	case classes.ErrSessionNotFound:
		return invalidData(w, "No such session.")
	default:
		return webapp.InternalError(fmt.Errorf("failed to look up session %d: %s", id, err))
	}
	staffAccount, ok := staffContext(r)
	if !ok {
		return webapp.UnauthorizedError(fmt.Errorf("only staff may add classes"))
	}
	if r.Method == "POST" {
		token, err := auth.TokenForRequest(c, staffAccount.ID, r.URL.Path)
		if err != nil {
			return webapp.UnauthorizedError(fmt.Errorf("didn't find an auth token"))
		}
		if !token.IsValid(r.FormValue(auth.TokenFieldName), time.Now()) {
			return webapp.UnauthorizedError(fmt.Errorf("invalid auth token"))
		}
		fields, err := webapp.ParseRequiredValues(r, "name", "description", "maxstudents", "dayofweek", "starttime", "length", "dropinonly")
		if err != nil {
			return missingFields(w)
		}
		weekday, err := parseWeekday(fields["dayofweek"])
		if err != nil {
			return invalidData(w, "Invalid weekday")
		}
		maxStudents, err := strconv.ParseInt(fields["maxstudents"], 10, 32)
		if err != nil || maxStudents <= 0 {
			return invalidData(w, "Invalid student capacity")
		}
		length, err := parseMinutes(fields["length"])
		if err != nil {
			return invalidData(w, "Invalid length")
		}
		start, err := parseLocalTime(fields["starttime"])
		if err != nil {
			return invalidData(w, "Invalid start time; please use HH:MMpm format (e.g., 3:04pm)")
		}
		class := &classes.Class{
			Title:           fields["name"],
			LongDescription: []byte(fields["description"]),
			Weekday:         weekday,
			DropInOnly:      fields["dropinonly"] == "yes",
			Capacity:        int32(maxStudents),
			Length:          length,
			StartTime:       start,
			Session:         session.ID,
		}
		if email := r.FormValue("teacher"); email != "" {
			teacher, err := classes.TeacherWithEmail(c, email)
			if err != nil {
				return invalidData(w, "Invalid teacher selected")
			}
			class.Teacher = teacher.Key(c)
		}
		if err := class.Insert(c); err != nil {
			return webapp.InternalError(fmt.Errorf("failed to add class: %s", err))
		}
		c.Infof("class ID: %d", class.ID)
		token.Delete(c)
		http.Redirect(w, r, "/staff", http.StatusSeeOther)
		return nil
	}
	token, err := auth.NewToken(staffAccount.ID, r.URL.Path, time.Now())
	if err != nil {
		return webapp.InternalError(err)
	}
	if err := token.Store(c); err != nil {
		return webapp.InternalError(err)
	}
	data := map[string]interface{}{
		"Token":       token.Encode(),
		"Session":     session,
		"Teachers":    classes.Teachers(c),
		"DaysInOrder": daysInOrder,
	}
	if err := addClassPage.Execute(w, data); err != nil {
		return webapp.InternalError(err)
	}
	return nil
}