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
//use command sed repleace SRCFILE to real source file
func genDocFile(path string) error {
	out, err := util.RunOutput("sed", "s/SRCFILE/"+path+"/g", "Seedfile")
	if err != nil {
		return err
	}
	_, err = util.WriteFile("Dockerfile", out)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 3
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("/")
}
Esempio n. 4
0
File: code.go Progetto: jango2015/OJ
func (c *Code) Submit(code string, problemId int64, lang string) revel.Result {
	source := &models.Source{}
	path := source.GenPath()
	source.CreatedAt = time.Now()
	source.Status = models.UnHandled
	source.ProblemId = problemId
	//get user id
	username := c.Session[USERNAME]
	u := models.GetCurrentUser(username)
	if u != nil {
		source.UserId = u.Id
	} else {
		c.Flash.Error("wrong user")
		return c.Redirect(routes.Code.Answer(problemId))
	}
	switch lang {
	case "c":
		_, err := util.WriteFile(path+"/tmp.c", []byte(code))
		if err != nil {
			log.Println(err)
		}
		source.Lang = models.C
	case "cpp":
		util.WriteFile(path+"/tmp.cpp", []byte(code))
		source.Lang = models.CPP
	case "go":
		util.WriteFile(path+"/tmp.go", []byte(code))
		source.Lang = models.Go
	default:
		c.Flash.Error("wrong lang %s\n", lang)
		return c.Redirect(routes.Code.Answer(problemId))
	}
	_, err := engine.Insert(source)
	if err != nil {
		c.Flash.Error(err.Error())
		return c.Redirect(routes.Code.Answer(problemId))
	}
	c.Flash.Success(username + " 提交成功")
	return c.Redirect("/code/status")
}