func (this *ProblemController) Insert(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Admin Problem Insert") if r.Method != "POST" { this.Err400(w, r, "Error", "Error Method to Insert problem") return } this.Init(w, r) if this.Privilege != config.PrivilegeAD { this.Err400(w, r, "Warning", "Error Privilege to Insert problem") return } one := model.Problem{} one.Title = r.FormValue("title") time, err := strconv.Atoi(r.FormValue("time")) if err != nil { http.Error(w, "The value 'Time' is neither too short nor too large", 400) return } one.Time = time memory, err := strconv.Atoi(r.FormValue("memory")) if err != nil { http.Error(w, "The value 'Memory' is neither too short nor too large", 400) return } one.Memory = memory if special := r.FormValue("special"); special == "" { one.Special = 0 } else { one.Special = 1 } in := r.FormValue("in") out := r.FormValue("out") one.Description = template.HTML(r.FormValue("description")) one.Input = template.HTML(r.FormValue("input")) one.Output = template.HTML(r.FormValue("output")) one.In = in one.Out = out one.Source = r.FormValue("source") one.Hint = r.FormValue("hint") problemModel := model.ProblemModel{} pid, err := problemModel.Insert(one) if err != nil { http.Error(w, err.Error(), 500) return } createfile(config.Datapath+strconv.Itoa(pid), "sample.in", in) createfile(config.Datapath+strconv.Itoa(pid), "sample.out", out) http.Redirect(w, r, "/admin/problem?list", http.StatusFound) }
//@URL: /admin/problems/ @method: POST func (pc *AdminProblem) Insert() { restweb.Logger.Debug("Admin Problem Insert") one := pc.problem() problemModel := model.ProblemModel{} pid, err := problemModel.Insert(one) if err != nil { pc.Error(err.Error(), 500) return } restweb.Logger.Debug(config.Datapath + strconv.Itoa(pid)) createfile(config.Datapath+strconv.Itoa(pid), "sample.in", one.In) createfile(config.Datapath+strconv.Itoa(pid), "sample.out", one.Out) pc.Redirect("/problems/"+strconv.Itoa(pid), http.StatusFound) }
//@URL: /admin/problems/importor/ @method: POST func (pc *AdminProblem) Import() { pc.R.ParseMultipartForm(32 << 20) fhs := pc.R.MultipartForm.File["fps.xml"] file, err := fhs[0].Open() if err != nil { restweb.Logger.Debug(err) return } defer file.Close() content, err := ioutil.ReadAll(file) if err != nil { restweb.Logger.Debug(err) return } contentStr := string(content) problem := model.Problem{} protype := reflect.TypeOf(problem) proValue := reflect.ValueOf(&problem).Elem() restweb.Logger.Debug(protype.NumField()) for i, lenth := 0, protype.NumField(); i < lenth; i++ { tag := protype.Field(i).Tag.Get("xml") restweb.Logger.Debug(i, tag) if tag == "" { continue } matchStr := "<" + tag + `><!\[CDATA\[(?ms:(.*?))\]\]></` + tag + ">" tagRx := regexp.MustCompile(matchStr) tagString := tagRx.FindAllStringSubmatch(contentStr, -1) restweb.Logger.Debug(tag) if len(tagString) > 0 { switch tag { case "time_limit", "memory_limit": limit, err := strconv.Atoi(tagString[0][1]) if err != nil { restweb.Logger.Debug(err) limit = 1 } proValue.Field(i).Set(reflect.ValueOf(limit)) case "description", "input", "output": proValue.Field(i).SetString(tagString[0][1]) default: proValue.Field(i).SetString(tagString[0][1]) } } } problem.ROJ = "ZJGSU" proModel := model.ProblemModel{} pid, err := proModel.Insert(problem) if err != nil { restweb.Logger.Debug(err) return } // 建立测试数据文件 createfile(config.Datapath+strconv.Itoa(pid), "sample.in", problem.In) createfile(config.Datapath+strconv.Itoa(pid), "sample.out", problem.Out) flag, flagJ := true, -1 for _, tag := range []string{"test_input", "test_output"} { // restweb.Logger.Debug(tag) matchStr := "<" + tag + `><!\[CDATA\[(?ms:(.*?))\]\]></` + tag + ">" tagRx := regexp.MustCompile(matchStr) tagString := tagRx.FindAllStringSubmatch(contentStr, -1) // restweb.Logger.Debug(tagString) if flag { flag = false caselenth := 0 for matchLen, j := len(tagString), 0; j < matchLen; j++ { if len(tagString[j][1]) > caselenth { caselenth = len(tagString[j][1]) flagJ = j } } } if flagJ >= 0 && flagJ < len(tagString) { // restweb.Logger.Debug(tagString[flagJ][1]) filename := strings.Replace(tag, "_", ".", 1) filename = strings.Replace(filename, "put", "", -1) createfile(config.Datapath+strconv.Itoa(pid), filename, tagString[flagJ][1]) } } pc.Redirect("/admin/problems", http.StatusFound) }