Example #1
0
//New returns a pointer to a TimeConfig with its fields set according to arguments.
func New(date, on, off time.Time) (tc *TimeConfig) {
	tc = new(TimeConfig)
	tc.Date = util.NormalizeDate(date)
	tc.Off = util.NormalizeTime(off)
	tc.On = util.NormalizeTime(on)
	return
}
Example #2
0
//TimeOverrideSubmit handles saving of time overrides into Datastore.
func TimeOverrideSubmit(c util.Context) (err error) {
	date, err := time.Parse(dateFormat, c.R.FormValue("date"))
	if err != nil {
		return
	}

	on, err := time.Parse(timeFormat, c.R.FormValue("on"))
	if err != nil {
		return
	}

	off, err := time.Parse(timeFormat, c.R.FormValue("off"))
	if err != nil {
		return
	}
	tc := timeConfig.New(util.NormalizeDate(date), util.NormalizeTime(on), util.NormalizeTime(off))
	var k *datastore.Key
	if _, ok := c.Vars["id"]; ok {
		k, err = datastore.DecodeKey(c.Vars["id"])
		if err != nil {
			return
		}
	} else {
		k = nil
	}
	tc.SetKey(k)
	err = tc.Save(c)
	if err != nil {
		return
	}
	http.Redirect(c.W, c.R, "/admin/config", 303)
	return
}
Example #3
0
//Save saves a TimeConfig to Datastore.
//If its Key field is set, it will replace an existing record
//that has that key. If not, it will use datastore.NewIncompleteKey()
//to create a new key and set the field.
func (tc *TimeConfig) Save(c appengine.Context) (err error) {
	tc.Date = util.NormalizeDate(tc.Date)
	tc.Off = util.NormalizeTime(tc.Off)
	tc.On = util.NormalizeTime(tc.On)
	err = gaemodel.Save(c, tc)
	if err != nil {
		return
	}

	err = config.UpdateTimestamp(c)
	return
}
Example #4
0
//SaveConfig saves data provided into the Config record.
func (c *Config) Save(ctx appengine.Context) (err error) {
	if c.Key() == nil {
		ctx.Infof("Creating new config key")
		key := datastore.NewIncompleteKey(ctx, "Config", nil)
		c.SetKey(key)
	} else if err != nil {
		return
	}

	c.StandardOn = util.NormalizeTime(c.StandardOn)
	c.StandardOff = util.NormalizeTime(c.StandardOff)

	c.Timestamp = time.Now().Unix()

	_, err = datastore.Put(ctx, c.Key(), c)
	if err != nil {
		return fmt.Errorf("Error when putting: %v", err)
	}

	action.DeleteFor(c, ctx)
	return
}
Example #5
0
//SetConfig handles saving the new configuration to Datastore.
func SetConfig(c util.Context) (err error) {
	conf, err := config.Get(c)
	if err != nil {
		return
	}

	on, err := time.Parse(timeFormat, c.R.FormValue("standardOn"))
	if err != nil {
		return
	}
	off, err := time.Parse(timeFormat, c.R.FormValue("standardOff"))
	if err != nil {
		return
	}
	conf.OverrideState, err = strconv.Atoi(c.R.FormValue("overrideState"))
	if err != nil {
		return
	}

	conf.UpdateInterval, err = strconv.Atoi(c.R.FormValue("updateInterval"))
	if err != nil {
		return
	}

	if c.R.FormValue("weekends") == "true" {
		conf.Weekends = true
	}

	conf.StandardOn = util.NormalizeTime(on)
	conf.StandardOff = util.NormalizeTime(off)

	err = conf.Save(c)
	if err != nil {
		return
	}
	http.Redirect(c.W, c.R, "/admin/config", 303)
	return
}