//列出所有的比赛 url:/admin/contest/list/type/<contest,exercise> func (this *ContestController) List(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Contest List") this.Init(w, r) args := this.ParseURL(r.URL.String()) Type := args["type"] qry := make(map[string]string) qry["type"] = Type contestModel := model.ContestModel{} contestList, err := contestModel.List(args) if err != nil { http.Error(w, err.Error(), 400) } this.Data["Contest"] = contestList this.Data["Title"] = "Admin - " + strings.Title(Type) + " List" this.Data["Is"+strings.Title(Type)] = true this.Data["IsList"] = true err = this.Execute(w, "view/admin/layout.tpl", "view/admin/contest_list.tpl") if err != nil { http.Error(w, "tpl error", 500) return } }
//更改contest状态 //@URL:/admin/contests/(\d+)/status/ @method:POST func (cc *AdminContest) Status(Cid string) { restweb.Logger.Debug("Admin Contest Status") cid, err := strconv.Atoi(Cid) if err != nil { cc.Error("args error", 400) return } contestModel := model.ContestModel{} one, _ := contestModel.Detail(cid) if one.Creator != cc.Uid { cc.Error("privilege error", 400) return } var status int switch one.Status { case config.StatusAvailable: status = config.StatusReverse default: status = config.StatusAvailable } err = contestModel.Status(cid, status) if err != nil { cc.Error(err.Error(), 500) return } cc.Redirect("/admin/contests", http.StatusFound) //重定向到竞赛列表页 }
func (this *Contest) InitContest(w http.ResponseWriter, r *http.Request) { this.Init(w, r) args := this.ParseURL(r.URL.String()) cid, err := strconv.Atoi(args["cid"]) if err != nil { http.Error(w, "args error", 400) return } this.Cid = cid contestModel := model.ContestModel{} this.ContestDetail, err = contestModel.Detail(cid) if err != nil { http.Error(w, err.Error(), 500) return } this.Index = make(map[int]int) for k, v := range this.ContestDetail.List { this.Index[v] = k } this.Data["Cid"] = strconv.Itoa(this.Cid) this.Data["Title"] = "Contest Detail " + strconv.Itoa(this.Cid) this.Data["Contest"] = this.ContestDetail.Title this.Data["IsContestDetail"] = true }
func (c *Contest) InitContest(Cid string) { c.Init() cid, err := strconv.Atoi(Cid) if err != nil { c.Error(err.Error(), 400) return } c.Cid = cid contestModel := model.ContestModel{} c.ContestDetail, err = contestModel.Detail(cid) if err != nil { c.Error(err.Error(), 500) return } c.Index = make(map[int]int) for k, v := range c.ContestDetail.List { c.Index[v] = k } c.Output["Cid"] = strconv.Itoa(c.Cid) c.Output["Title"] = "Contest Detail " + strconv.Itoa(c.Cid) c.Output["Contest"] = c.ContestDetail.Title c.Output["IsContestDetail"] = true c.Output["IsContest"] = true }
func (this *ContestController) List(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Contest List") this.Init(w, r) args := this.ParseURL(r.URL.String()) Type := args["type"] CModel := model.ContestModel{} conetestList, err := CModel.List(args) if err != nil { http.Error(w, err.Error(), 500) return } this.Data["Contest"] = conetestList this.Data["Time"] = this.GetTime() this.Data["Type"] = Type this.Data["Title"] = strings.Title(Type) + " List" this.Data["Is"+strings.Title(Type)] = true this.Data["Privilege"] = this.Privilege err = this.Execute(w, "view/layout.tpl", "view/contest_list.tpl") if err != nil { class.Logger.Debug(err) http.Error(w, "tpl error", 500) return } }
// 插入比赛 //@URL:/admin/contests/ @method:POST func (cc *AdminContest) Insert() { restweb.Logger.Debug("Admin Contest Insert") one := cc.contest() contestModel := model.ContestModel{} err := contestModel.Insert(one) if err != nil { cc.Error(err.Error(), 500) return } cc.Redirect("/admin/contests", http.StatusFound) //重定向到竞赛列表页 }
//列出所有的比赛 //@URL: /admin/contests/ @method:GET func (cc *AdminContest) List() { restweb.Logger.Debug("Contest List") qry := make(map[string]string) contestModel := model.ContestModel{} contestList, err := contestModel.List(qry) if err != nil { cc.Error(err.Error(), 400) } cc.Output["Contest"] = contestList cc.Output["Title"] = "Admin - Contest List" cc.Output["IsContest"] = true cc.Output["IsList"] = true cc.RenderTemplate("view/admin/layout.tpl", "view/admin/contest_list.tpl") }
//@URL: /contests @method: GET func (c *ContestController) Index() { restweb.Logger.Debug("Contest List") CModel := model.ContestModel{} conetestList, err := CModel.List(nil) if err != nil { c.Error(err.Error(), 500) return } c.Output["Contest"] = conetestList c.Output["Time"] = restweb.GetTime() c.Output["Title"] = "Contest List" c.Output["IsContest"] = true c.Output["Privilege"] = c.Privilege c.RenderTemplate("view/layout.tpl", "view/contest_list.tpl") }
//删除竞赛 //@URL: /admin/contests/(\d+)/delete/ @method:POST func (cc *AdminContest) Delete(Cid string) { restweb.Logger.Debug("Admin Contest Delete") cid, err := strconv.Atoi(Cid) if err != nil { cc.Error("args error", 400) return } contestModel := model.ContestModel{} old, _ := contestModel.Detail(cid) if old.Creator != cc.Uid { cc.Error("privilege error", 400) return } err = contestModel.Delete(cid) if err != nil { cc.Error(err.Error(), 400) return } cc.W.WriteHeader(200) }
//删除竞赛 url:/admin/contest/delete/,method:POST func (this *ContestController) Delete(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Admin Contest Delete") if r.Method != "POST" { this.Err400(w, r, "Error", "Error Method to Delete contest") return } this.Init(w, r) args := this.ParseURL(r.URL.String()) cid, err := strconv.Atoi(args["cid"]) if err != nil { http.Error(w, "args error", 400) return } contestModel := model.ContestModel{} err = contestModel.Delete(cid) if err != nil { http.Error(w, err.Error(), 400) return } w.WriteHeader(200) }
//更改contest状态 url:/admin/contest/status/ func (this *ContestController) Status(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Admin Contest Status") if r.Method != "POST" { this.Err400(w, r, "Error", "Error Method to Change contest status") return } this.Init(w, r) args := this.ParseURL(r.URL.String()) cid, err := strconv.Atoi(args["cid"]) if err != nil { http.Error(w, "args error", 400) return } contestModel := model.ContestModel{} one, err := contestModel.Detail(cid) Type := one.Type var status int switch one.Status { case config.StatusAvailable: status = config.StatusReverse default: status = config.StatusAvailable } err = contestModel.Status(cid, status) if err != nil { http.Error(w, err.Error(), 500) return } http.Redirect(w, r, "/admin/contest?list/type?"+strings.Title(Type), http.StatusFound) //重定向到竞赛列表页 }
// 更新竞赛 //@URL:/admin/contests/(\d+)/ @method:POST func (cc *AdminContest) Update(Cid string) { restweb.Logger.Debug("Admin Contest Update") cid, err := strconv.Atoi(Cid) if err != nil { cc.Error("args error", 400) return } contestModel := model.ContestModel{} old, _ := contestModel.Detail(cid) if old.Creator != cc.Uid { cc.Error("privilege error", 400) return } one := cc.contest() err = contestModel.Update(cid, one) if err != nil { cc.Error(err.Error(), 400) return } cc.Redirect("/admin/contests", http.StatusFound) }
// 插入比赛 url:/admin/contest?insert/type?<contest,exercise> func (this *ContestController) Insert(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Admin Contest Insert") if r.Method != "POST" { this.Err400(w, r, "Error", "Error Method to Insert contest") return } this.Init(w, r) args := this.ParseURL(r.URL.String()) Type := args["type"] one := model.Contest{} one.Title = r.FormValue("title") one.Type = Type year, err := strconv.Atoi(r.FormValue("startTimeYear")) month, err := strconv.Atoi(r.FormValue("startTimeMonth")) day, err := strconv.Atoi(r.FormValue("startTimeDay")) hour, err := strconv.Atoi(r.FormValue("startTimeHour")) min, err := strconv.Atoi(r.FormValue("startTimeMinute")) start := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.Local) one.Start = start.Unix() year, err = strconv.Atoi(r.FormValue("endTimeYear")) month, err = strconv.Atoi(r.FormValue("endTimeMonth")) day, err = strconv.Atoi(r.FormValue("endTimeDay")) hour, err = strconv.Atoi(r.FormValue("endTimeHour")) min, err = strconv.Atoi(r.FormValue("endTimeMinute")) end := time.Date(year, time.Month(month), day, hour, min, 0, 0, time.Local) one.End = end.Unix() if start.After(end) { http.Error(w, "args error", 400) return } switch r.FormValue("type") { case "public": one.Encrypt = config.EncryptPB case "private": one.Encrypt = config.EncryptPT argument := r.FormValue("userlist") var cr rune = 13 crStr := string(cr) argument = strings.Trim(argument, crStr) argument = strings.Trim(argument, "/r/n") argument = strings.Replace(argument, "/r/n", "", -1) argument = strings.Replace(argument, crStr, "/n", -1) one.Argument = argument case "password": one.Encrypt = config.EncryptPW one.Argument = r.FormValue("password") default: http.Error(w, "args error", 400) return } problemString := r.FormValue("problemList") problemString = strings.Trim(problemString, " ") problemString = strings.Trim(problemString, ";") problemList := strings.Split(problemString, ";") var list []int for _, v := range problemList { pid, err := strconv.Atoi(v) if err != nil { http.Error(w, "conv error", 400) return } list = append(list, pid) } one.List = list //problemList 建议检查下problem是否存在,存在的将其在普通列表中不可见 contestModel := model.ContestModel{} err = contestModel.Insert(one) if err != nil { http.Error(w, err.Error(), 500) return } http.Redirect(w, r, "/admin/contest?list/type?"+Type, http.StatusFound) //重定向到竞赛列表页 }
// 竞赛编辑页面,url:/admin/contest/edit/ func (this *ContestController) Edit(w http.ResponseWriter, r *http.Request) { class.Logger.Debug("Admin Contest Edit") this.Init(w, r) args := this.ParseURL(r.URL.String()) cid, err := strconv.Atoi(args["cid"]) if err != nil { http.Error(w, "args error", 400) return } var one struct { *model.Contest StartTimeYear int StartTimeMonth int StartTimeDay int StartTimeHour int StartTimeMinute int EndTimeYear int EndTimeMonth int EndTimeDay int EndTimeHour int EndTimeMinute int ProblemList string IsPublic bool IsPrivate bool IsPassword bool } contestModel := model.ContestModel{} one.Contest, err = contestModel.Detail(cid) if err != nil { http.Error(w, err.Error(), 400) return } start := time.Unix(one.Start, 0).Local() one.StartTimeYear = start.Year() one.StartTimeMonth = int(start.Month()) one.StartTimeDay = start.Day() one.StartTimeHour = start.Hour() one.StartTimeMinute = start.Minute() end := time.Unix(one.End, 0).Local() one.EndTimeYear = end.Year() one.EndTimeMonth = int(end.Month()) one.EndTimeDay = end.Day() one.EndTimeHour = end.Hour() one.EndTimeMinute = end.Minute() one.ProblemList = "" for _, v := range one.List { one.ProblemList += strconv.Itoa(v) + ";" } one.IsPublic = false one.IsPrivate = false one.IsPassword = false switch one.Encrypt { case config.EncryptPB: one.IsPublic = true case config.EncryptPT: one.IsPrivate = true case config.EncryptPW: one.IsPassword = true } this.Data["Detail"] = one Type := one.Type this.Data["Title"] = "Admin - " + strings.Title(Type) + " Edit" this.Data["Is"+strings.Title(Type)] = true this.Data["IsEdit"] = true err = this.Execute(w, "view/admin/layout.tpl", "view/admin/contest_edit.tpl") if err != nil { http.Error(w, "tpl error", 500) return } }
func requireContest(ctx *restweb.Context) bool { url := ctx.R.URL.Path if restweb.GetAction(url, 2) == "password" { return false } Cid := restweb.GetAction(url, 1) cid, err := strconv.Atoi(Cid) if err != nil { return true } contestModel := model.ContestModel{} ContestDetail, err := contestModel.Detail(cid) if err != nil { return true } prvs := "0" + ctx.GetSession("Privilege") prv, err := strconv.Atoi(prvs) if err != nil { restweb.Logger.Debug(err) return true } Uid := ctx.GetSession("Uid") restweb.Logger.Debug(Uid, prv) if prv < config.PrivilegeTC { if time.Now().Unix() < ContestDetail.Start || ContestDetail.Status == config.StatusReverse { info := "The contest has not started yet" if ContestDetail.Status == config.StatusReverse { info = "No such contest" } Err400(ctx.W, "Contest", info) return true } else if ContestDetail.Encrypt == config.EncryptPW { if Uid == "" { ctx.Redirect("/sess", http.StatusFound) } else if ctx.GetSession(Cid+"pass") != ContestDetail.Argument.(string) { ctx.Redirect("/contests/"+Cid+"/password", http.StatusFound) } else { return false } return true } else if ContestDetail.Encrypt == config.EncryptPT { if Uid == "" { ctx.Redirect("/sess", http.StatusFound) return true } else { userlist := strings.Split(ContestDetail.Argument.(string), "\r\n") flag := false for _, user := range userlist { if user == Uid { flag = true break } } if flag == false { Err400(ctx.W, "Contest", "Sorry, the contest is private and you are not granted to participate in the contest.") return true } } } } return false }
// 竞赛编辑页面, //@URL:/admin/contests/(\d+)/ @method:GET func (cc *AdminContest) Edit(Cid string) { restweb.Logger.Debug("Admin Contest Edit") cid, err := strconv.Atoi(Cid) if err != nil { cc.Error("args error", 400) return } var one struct { *model.Contest StartTimeYear int StartTimeMonth int StartTimeDay int StartTimeHour int StartTimeMinute int EndTimeYear int EndTimeMonth int EndTimeDay int EndTimeHour int EndTimeMinute int ProblemList string IsPublic bool IsPrivate bool IsPassword bool } contestModel := model.ContestModel{} one.Contest, err = contestModel.Detail(cid) if err != nil { cc.Error(err.Error(), 400) return } start := time.Unix(one.Start, 0).Local() one.StartTimeYear = start.Year() one.StartTimeMonth = int(start.Month()) one.StartTimeDay = start.Day() one.StartTimeHour = start.Hour() one.StartTimeMinute = start.Minute() end := time.Unix(one.End, 0).Local() one.EndTimeYear = end.Year() one.EndTimeMonth = int(end.Month()) one.EndTimeDay = end.Day() one.EndTimeHour = end.Hour() one.EndTimeMinute = end.Minute() one.ProblemList = "" for _, v := range one.List { one.ProblemList += strconv.Itoa(v) + ";" } one.IsPublic = false one.IsPrivate = false one.IsPassword = false switch one.Encrypt { case config.EncryptPB: one.IsPublic = true case config.EncryptPT: one.IsPrivate = true case config.EncryptPW: one.IsPassword = true } cc.Output["Detail"] = one cc.Output["Title"] = "Admin - " + "Contest" + " Edit" cc.Output["IsContest"] = true cc.Output["IsEdit"] = true cc.RenderTemplate("view/admin/layout.tpl", "view/admin/contest_edit.tpl") }