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.Category.Id = form.Category post.Topic.Id = 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.LastAuthor != nil && post.LastAuthor.Id != user.Id { post.LastAuthor = user changes = append(changes, "LastAuthor") } changes = append(changes, "Updated") return post.Update(changes...) }
// view for update object func (this *PageAdminRouter) Update() { form := page.PageAdminForm{} if this.ValidFormSets(&form) == false { return } // get changed field names changes := utils.FormChanges(&this.object, &form) url := fmt.Sprintf("/admin/page/%d", this.object.Id) // update changed fields only if len(changes) > 0 { form.SetToPage(&this.object) if err := this.object.Update(changes...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { beego.Error(err) this.Data["Error"] = err } } else { this.Redirect(url, 302) } }
// view for update object func (this *PostAdminRouter) Update() { 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 := this.object.Update(changes...); err == nil { this.FlashRedirect(url, 302, "UpdateSuccess") return } else { beego.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 user.Update(changes...) } return nil }