func (c *Comments) Post() { if !c.IsAjax() { return } var v M.ValidMap var cm M.Comment var err error cr := tplComment{Msg: T("internal")} c.Data["json"] = &cr id, err := strconv.Atoi(c.Ctx.Input.Param(":id")) if err == nil { err = c.ParseForm(&cm) } if err != nil { beego.Error(err) return } i := c.GetSession("uid") if i == nil { cr.Msg = T("auth_is_off") return } cm.User = i.(int) //comment author cm.Cand = id //candidate Id cm.Comment = html.EscapeString(cm.Comment) v, err = DM.Insert(&cm, "User", "Cand", "Comment") if err != nil { beego.Error(err) return } if !v.HasErrors() { //if no has errors returns inserted data to client cr.Id = cm.Id cr.Msg = "Ok" cr.Comment = cm.Comment cr.SetAuthor(cm.User) cr.SetDate(cm.CreatedAt) return } M.ExpandFormErrors(&v, c.Data) if val, ok := c.Data["errComment"]; ok { cr.Msg = val.(string) //return error by field comment } }
// save concrete candidate rating func (c *Rat) Post() { if !c.IsAjax() { return } var err error var s string var v M.ValidMap var ok bool r := M.Rat{Active: true} err = c.ParseForm(&r) uid := c.GetSession("uid") if uid == nil { c.Data["json"] = RJson{T("auth_is_off"), ok} return } fr := M.Rat{} err = DM.Find(&fr, M.Sf{}, M.Where{And: M.W{"Cand": r.Cand, "User": uid.(int), "Crit": r.Crit}}) if err != nil && err != sql.ErrNoRows { c.Data["json"] = RJson{T("internal"), ok} beego.Warn(err) return } if fr.Id > 0 { c.Data["json"] = RJson{T("rat_already"), ok} return } r.User = uid.(int) if err == sql.ErrNoRows { v, err = DM.Insert(&r) } if err != nil { s = T("save_error") beego.Error(err) } if !v.HasErrors() { s = T("rat_added") ok = true } else { s = T("validation_error") } c.Data["json"] = RJson{s, ok} }
//add new user func (c *User) Post() { var v M.ValidMap var err error var msg string c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.TplNames = "user/form.tpl" d := M.User{} flash := beego.NewFlash() err = c.ParseForm(&d) if err != nil { beego.Error(err) flash.Notice(T("internal")) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("User.Index"), 302) return } d.GToken = sql.NullString{c.Ctx.Request.Form["gtoken"][0], true} d.Active = true if d.Id == 0 { v, err = DM.Insert(&d) msg = T("user_created") } else { v, err = DM.Update(&d) msg = T("user_updated", map[string]interface{}{"Login": d.Login}) c.Data["isEdit"] = true } if err != nil { beego.Error(err) } if !v.HasErrors() { flash.Notice(msg) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("User.Index"), 302) return } M.ExpandFormErrors(&v, c.Data) c.Data["user"] = d c.Data["roles"] = c.rolesList() }
// save criteria func (c *Crit) Post() { var v M.ValidMap var err error msg := T("crit_created") c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.TplNames = "crit/form.tpl" flash := beego.NewFlash() crit := M.Crit{} err = c.ParseForm(&crit) if crit.Id != 0 { msg = T("crit_updated", map[string]interface{}{"Title": crit.Title}) c.Data["isEdit"] = true } if err == nil { if crit.Id == 0 { v, err = DM.Insert(&crit, "Title", "Dep") } else { v, err = DM.Update(&crit, "Title", "Dep") } } if err != nil { beego.Error(err) } if !v.HasErrors() && err == nil { flash.Notice(msg) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("Crit.Index"), 302) return } var deps []M.Dep err = DM.FindAll(&M.Dep{}, &deps, M.Sf{"Id", "Title"}, M.Where{}) if err != nil { beego.Error(err) } c.Data["deps"] = deps c.Data["crit"] = crit M.ExpandFormErrors(&v, c.Data) }
//save concrete or new department func (c *Dep) Post() { var v M.ValidMap var err error var msg string c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.TplNames = "dep/form.tpl" d := M.Dep{} flash := beego.NewFlash() err = c.ParseForm(&d) if err != nil { beego.Error(err) flash.Notice(T("internal")) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("Dep.Index"), 302) return } if d.Id == 0 { v, err = DM.Insert(&d, "Title") msg = T("dep_created") } else { v, err = DM.Update(&d, "Title") msg = T("dep_updated", map[string]interface{}{"Title": d.Title}) c.Data["isEdit"] = true } if err != nil { beego.Error(err) } if !v.HasErrors() { flash.Notice(msg) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("Dep.Index"), 302) return } M.ExpandFormErrors(&v, c.Data) c.Data["dep"] = d }
//save account settings func (c *Prof) Post() { var v M.ValidMap var err error var msg string s := []string{"Name", "Email", "NotifyByMail", "NotifyByTelegram"} c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.TplNames = "prof/get.tpl" d := M.Profile{} err = c.ParseForm(&d) d.Id = c.GetSession("uid").(int) if err == nil && d.Password != "" { s = append(s, "Password") } if err == nil { v, err = DM.Update(&d, s...) } if err == nil && !v.HasErrors() { c.SetSession("name", d.Name) msg = T("account_saved") } if err != nil { msg = T("internal") beego.Error(err) } if msg != "" { flash := beego.NewFlash() flash.Notice(msg) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("Prof.Get"), 302) return } M.ExpandFormErrors(&v, c.Data) c.Data["Name"] = d.Name }
//check auth form func (c *Main) Post() { var v M.ValidMap c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.TplNames = "loginform.tpl" loginform := LoginForm{} c.ParseForm(&loginform) v.Valid(loginform) c.Data["login"] = loginform.Login M.ExpandFormErrors(&v, c.Data) if v.HasErrors() { return } c.Data["errLogin"] = T("login_error") id, err := M.CheckPass(loginform.Login, loginform.Password) if err != nil { beego.Warn(err) return } u := M.User{} err = DM.FindByPk(&u, id) if err != nil { beego.Error(err) return } if len(u.GToken.String) > 0 { c.SetSession("gitlabToken", u.GToken.String) } c.SetSession("uid", id) c.SetSession("role", u.Role) c.SetSession("name", u.Name) c.Redirect(beego.UrlFor("Cand.Index"), 302) }
//save concrete or new candidate func (c *Cand) Post() { upload_dir := beego.AppConfig.String("upload_dir") c.TplNames = "cand/add.tpl" flash := beego.NewFlash() var v M.ValidMap var err error d := M.Cand{} var deps []M.Dep if err := c.ParseForm(&d); err != nil { beego.Error(err) flash.Error(T("internal")) flash.Store(&c.Controller) return } d.Note = sql.NullString{c.Ctx.Request.Form["note"][0], true} d.Active = true c.Data["xsrfdata"] = template.HTML(c.XsrfFormHtml()) c.Data["cand"] = d if err := DM.FindAll(&M.Dep{}, &deps, M.Sf{}, M.Where{}); err != nil { beego.Error("Department find error: ", err) flash.Error(T("internal")) flash.Store(&c.Controller) return } c.Data["deps"] = deps var header *multipart.FileHeader _, header, err = c.GetFile("img") if err := os.MkdirAll(upload_dir+"img/", os.ModePerm); err != nil { beego.Error(err) } if err := os.MkdirAll(beego.AppConfig.String("tmp_dir"), os.ModePerm); err != nil { beego.Error(err) } d.Img = sql.NullString{beego.AppConfig.String("static_dir") + "img/noavatar-" + strconv.Itoa(utils.RandInt(0, 4)) + ".jpg", true} if err == nil { if !strings.Contains(header.Header["Content-Type"][0], "image") { flash.Error(T("filetype_bad")) flash.Store(&c.Controller) return } var filename = beego.AppConfig.String("tmp_dir") + header.Filename c.SaveToFile("img", filename) furl, fsname := utils.Resize(filename, header, 350, 0) if furl == "" { flash.Error(T("resize_bad")) flash.Store(&c.Controller) } if err := os.Rename(furl, upload_dir+"img/"+fsname); err != nil { beego.Error(err) flash.Error(T("resize_bad")) flash.Store(&c.Controller) return } d.Img = sql.NullString{upload_dir + "img/" + fsname, true} } var is_new bool if d.Id == 0 { is_new = true v, err = DM.Insert(&d) } else { v, err = DM.Update(&d) } if err != nil { beego.Error("Model insert error: ", err) flash.Error(T("internal")) flash.Store(&c.Controller) return } if v.HasErrors() { M.ExpandFormErrors(&v, c.Data) return } if is_new { tmpl := c.AddMail(d.Name, d.Id) c.SendMails(tmpl) c.SendTgNotifs(fmt.Sprintf("New Candidate Added: %s %s %s:%d/adm/candidates/%d", d.Name, d.LName, c.Ctx.Input.Site(), c.Ctx.Input.Port(), d.Id)) } flash.Notice(T("cand_created", map[string]interface{}{"Name": d.Name, "Lname": d.LName})) flash.Store(&c.Controller) c.Redirect(beego.UrlFor("Cand.Index"), 302) }