Bug fixes on PUT /api/template

Hotfix for buffer issue in worker.go.. going to find out the root cause and fix it properly
pull/24/head
Jordan 2014-08-07 05:48:52 -05:00
parent 86dca67a5a
commit 33b9ec2196
5 changed files with 28 additions and 13 deletions

View File

@ -220,6 +220,7 @@ func API_Templates_Id(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(vars["id"], 0, 64) id, _ := strconv.ParseInt(vars["id"], 0, 64)
t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64)) t, err := models.GetTemplate(id, ctx.Get(r, "user_id").(int64))
if checkError(err, w, "Template not found", http.StatusNotFound) { if checkError(err, w, "Template not found", http.StatusNotFound) {
Logger.Println(err)
return return
} }
switch { switch {

View File

@ -141,7 +141,7 @@ func PostCampaign(c *Campaign, uid int64) error {
for _, g := range c.Groups { for _, g := range c.Groups {
// Insert a result for each target in the group // Insert a result for each target in the group
for _, t := range g.Targets { for _, t := range g.Targets {
r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId} r := Result{Email: t.Email, Status: STATUS_UNKNOWN, CampaignId: c.Id, UserId: c.UserId, FirstName: t.FirstName, LastName: t.LastName}
r.GenerateId() r.GenerateId()
err = db.Save(&r).Error err = db.Save(&r).Error
if err != nil { if err != nil {

View File

@ -14,6 +14,8 @@ type Result struct {
UserId int64 `json:"-"` UserId int64 `json:"-"`
RId string `json:"id"` RId string `json:"id"`
Email string `json:"email"` Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Status string `json:"status" sql:"not null"` Status string `json:"status" sql:"not null"`
} }

View File

@ -3,6 +3,8 @@ package models
import ( import (
"errors" "errors"
"time" "time"
"github.com/jinzhu/gorm"
) )
type Template struct { type Template struct {
@ -39,10 +41,13 @@ func GetTemplates(uid int64) ([]Template, error) {
} }
for i, _ := range ts { for i, _ := range ts {
err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error
if err != nil { if err != nil && err != gorm.RecordNotFound {
Logger.Println(err) Logger.Println(err)
return ts, err return ts, err
} }
if err == gorm.RecordNotFound {
err = nil
}
} }
return ts, err return ts, err
} }
@ -56,10 +61,13 @@ func GetTemplate(id int64, uid int64) (Template, error) {
return t, err return t, err
} }
err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error
if err != nil { if err != nil && err != gorm.RecordNotFound {
Logger.Println(err) Logger.Println(err)
return t, err return t, err
} }
if err == gorm.RecordNotFound {
err = nil
}
return t, err return t, err
} }
@ -98,11 +106,14 @@ func PostTemplate(t *Template) error {
// Per the PUT Method RFC, it presumes all data for a template is provided. // Per the PUT Method RFC, it presumes all data for a template is provided.
func PutTemplate(t *Template) error { func PutTemplate(t *Template) error {
// Delete all attachments, and replace with new ones // Delete all attachments, and replace with new ones
err := db.Where("template_id=?", t.Id).Delete(&Attachment{}).Error err := db.Debug().Where("template_id=?", t.Id).Delete(&Attachment{}).Error
if err != nil { if err != nil && err != gorm.RecordNotFound {
Logger.Println(err) Logger.Println(err)
return err return err
} }
if err == gorm.RecordNotFound {
err = nil
}
for i, _ := range t.Attachments { for i, _ := range t.Attachments {
t.Attachments[i].TemplateId = t.Id t.Attachments[i].TemplateId = t.Id
err := db.Save(&t.Attachments[i]).Error err := db.Save(&t.Attachments[i]).Error
@ -111,7 +122,7 @@ func PutTemplate(t *Template) error {
return err return err
} }
} }
err = db.Where("id=?", t.Id).Save(t).Error err = db.Debug().Where("id=?", t.Id).Save(t).Error
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
return err return err

View File

@ -49,27 +49,28 @@ func processCampaign(c *models.Campaign) {
} }
for _, t := range c.Results { for _, t := range c.Results {
// Parse the templates // Parse the templates
var buff bytes.Buffer var html_buff bytes.Buffer
var text_buff bytes.Buffer
tmpl, err := template.New("html_template").Parse(c.Template.HTML) tmpl, err := template.New("html_template").Parse(c.Template.HTML)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
err = tmpl.Execute(&buff, t) err = tmpl.Execute(&html_buff, t)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
e.HTML = buff.Bytes() e.HTML = html_buff.Bytes()
buff.Reset() //buff.Reset()
tmpl, err = template.New("text_template").Parse(c.Template.Text) tmpl, err = template.New("text_template").Parse(c.Template.Text)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
err = tmpl.Execute(&buff, t) err = tmpl.Execute(&text_buff, t)
if err != nil { if err != nil {
Logger.Println(err) Logger.Println(err)
} }
e.Text = buff.Bytes() e.Text = text_buff.Bytes()
buff.Reset() //buff.Reset()
Logger.Println("Creating email using template") Logger.Println("Creating email using template")
e.To = []string{t.Email} e.To = []string{t.Email}
err = e.Send(c.SMTP.Host, auth) err = e.Send(c.SMTP.Host, auth)