示例#1
0
func registerForOneDay(w http.ResponseWriter, r *http.Request) *webapp.Error {
	if r.Method != "POST" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		fmt.Fprintf(w, "Method not allowed")
		return nil
	}
	c := appengine.NewContext(r)
	user, class, err := classAndUser(w, r)
	if err != nil {
		return err
	}
	token, ok := checkToken(c, user.ID, r.URL.Path, r.FormValue(auth.TokenFieldName))
	if !ok {
		return webapp.UnauthorizedError(fmt.Errorf("Invalid auth token"))
	}
	date, dateErr := parseLocalDate(r.FormValue("date"))
	if dateErr != nil {
		return invalidData(w, "Invalid date; please use mm/dd/yyyy format")
	}
	// TODO(rwsims): The date here should really be the end time of the
	// class on the given day.
	student := students.NewDropIn(user, class, date)
	switch err := student.Add(c, time.Now()); err {
	case nil:
		break
	case students.ErrClassIsFull:
		if err := classFullPage.Execute(w, class); err != nil {
			return webapp.InternalError(err)
		}
	default:
		return webapp.InternalError(fmt.Errorf("failed to write student: %s", err))
	}
	token.Delete(c)
	http.Redirect(w, r, "/", http.StatusSeeOther)
	return nil
}
示例#2
0
func registerPaperStudent(w http.ResponseWriter, r *http.Request) *webapp.Error {
	if r.Method != "POST" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		fmt.Fprintf(w, "Method not allowed")
		return nil
	}
	c := appengine.NewContext(r)
	user, class, werr := classAndUser(w, r)
	if werr != nil {
		return werr
	}
	token, ok := checkToken(c, user.ID, r.URL.Path, r.FormValue(auth.TokenFieldName))
	if !ok {
		return webapp.UnauthorizedError(fmt.Errorf("Invalid auth token"))
	}
	fields, err := webapp.ParseRequiredValues(r, "firstname", "lastname", "email", "type")
	if err != nil {
		return missingFields(w)
	}
	acct, err := account.WithEmail(c, fields["email"])
	switch err {
	case nil:
		// Register with existing account
		break
	case account.ErrUserNotFound:
		// Need to create a paper account for this registration. This account will not be stored.
		info := account.Info{
			FirstName: fields["firstname"],
			LastName:  fields["lastname"],
			Email:     fields["email"],
		}
		if phone := fields["phone"]; phone != "" {
			info.Phone = phone
		}
		acct = account.Paper(info, class.ID)
		break
	default:
		return webapp.InternalError(fmt.Errorf("failed to look up account for %q: %s", fields["email"], err))
	}
	var student *students.Student
	if fields["type"] == "dropin" {
		// TODO(rwsims): The date here should really be the end time of the
		// class on the given day.
		date, err := parseLocalDate(r.FormValue("date"))
		if err != nil {
			return invalidData(w, "Invalid date; please use mm/dd/yyyy format")
		}
		student = students.NewDropIn(acct, class, date)
	} else {
		student = students.New(acct, class)
	}
	switch err := student.Add(c, time.Now()); err {
	case nil:
		break
	case students.ErrClassIsFull:
		if err := classFullPage.Execute(w, class); err != nil {
			return webapp.InternalError(err)
		}
	default:
		return webapp.InternalError(fmt.Errorf("failed to write student: %s", err))
	}
	token.Delete(c)
	http.Redirect(w, r, fmt.Sprintf("/roster?class=%d", class.ID), http.StatusSeeOther)
	return nil
}