Esempio n. 1
0
// POST /problem/edit
func (p *Problem) PostEdit(problem models.Problem,
	inputTest, outputTest []byte) revel.Result {
	defer func() {
		delete(p.Session, ID)
	}()
	if inputTest != nil {
		problem.InputTestPath = path.Dir(problem.InputTestPath) +
			"/inputTest"
		_, err := util.WriteFile(problem.InputTestPath, inputTest)
		if err != nil {
			log.Println(err)
		}

	}
	if outputTest != nil {
		problem.OutputTestPath = path.Dir(problem.OutputTestPath) +
			"/outputTest"
		_, err := util.WriteFile(problem.OutputTestPath, outputTest)
		if err != nil {
			log.Println(err)
		}
	}
	id, err := strconv.ParseInt(p.Session[ID], 10, 64)
	if err != nil {
		p.Flash.Error("id error")
		log.Println(err)
		return p.Redirect("/")
	}
	_, err = engine.Id(id).Update(problem)
	if err != nil {
		log.Println(err)
	}
	return p.Redirect("/")
}
Esempio n. 2
0
// POST /problem/new
func (p *Problem) PostNew(problem models.Problem,
	inputTest, outputTest []byte) revel.Result {

	problem.Validate(p.Validation, inputTest, outputTest)
	if p.Validation.HasErrors() {
		p.Validation.Keep()
		//p.FlashParams()
		return p.Redirect("/")
	}
	if IsAdmin(p.Session[USERNAME]) {
		problem.IsValid = true
	} else {
		//if user is not admin,checked
		//the problem effectiveness manually by administrators
		has, id := models.GetUserId(p.Session[USERNAME])
		if has {
			problem.PosterId = id
		}
		problem.IsValid = false
	}
	_, err := util.WriteFile(problem.InputTestPath, inputTest)
	if err != nil {
		p.Flash.Error(err.Error())
		log.Println(err)
		return p.Redirect(routes.Notice.Crash())
	}
	_, err = util.WriteFile(problem.OutputTestPath, outputTest)
	if err != nil {
		p.Flash.Error(err.Error())
		log.Println(err)
		return p.Redirect(routes.Notice.Crash())
	}
	_, err = engine.Insert(&problem)
	if err != nil {
		p.Flash.Error("insert error")
		log.Println(err)
		return p.Redirect(routes.Notice.Crash())
	}
	p.Flash.Success("post success!")
	return p.Redirect("/")
}