func (this *BulletinAdminEdit) Post() { form := bulletin.BulletinAdminForm{Id: int(this.object.Id)} if this.ValidFormSets(&form) == false { return } // get changed field names changes := utils.FormChanges(&this.object, &form) url := fmt.Sprintf("/admin/bulletin/%d", this.object.Id) // update changed fields only if len(changes) > 0 { form.SetToBulletin(&this.object) if err := models.UpdateById(this.object.Id, this.object, models.Obj2Table(changes)...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { log.Error(err) this.Data["Error"] = err } } else { this.Redirect(url, 302) } }
func (form *PostForm) UpdatePost(post *models.Post, user *models.User) error { changes := utils.FormChanges(post, form) if len(changes) == 0 { return nil } utils.SetFormValues(form, post) post.CategoryId = form.Category post.TopicId = form.Topic for _, c := range changes { if c == "Content" { post.ContentCache = utils.RenderMarkdown(form.Content) changes = append(changes, "ContentCache") } } // update last edit author if post.LastAuthorId != user.Id { post.LastAuthorId = user.Id changes = append(changes, "LastAuthor") } changes = append(changes, "Updated") return models.UpdateById(post.Id, post, models.Obj2Table(changes)...) }
// view for update object func (this *PostAdminEdit) Post() { form := this.GetForm(false) if this.ValidFormSets(&form) == false { return } // get changed field names changes := utils.FormChanges(&this.object, &form) url := fmt.Sprintf("/admin/post/%d", this.object.Id) // update changed fields only if len(changes) > 0 { //fix the bug of category not updated changes = append(changes, "Category") form.SetToPost(&this.object) if err := models.UpdateById(this.object.Id, this.object, models.Obj2Table(changes)...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { log.Error(err) this.Data["Error"] = err } } else { this.Redirect(url, 302) } }
func (form *ProfileForm) SaveUserProfile(user *models.User) error { // set md5 value if the value is an email if strings.IndexRune(form.GrEmail, '@') != -1 { form.GrEmail = utils.EncodeMd5(form.GrEmail) } changes := utils.FormChanges(user, form) if len(changes) > 0 { // if email changed then need re-active if user.Email != form.Email { user.IsActive = false changes = append(changes, "IsActive") } utils.SetFormValues(form, user) return models.UpdateById(user.Id, user, changes...) } return nil }