From 83ab6ffb52d8c71935d410eaf93a15ae81b21912 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sat, 12 Jul 2014 13:46:38 -0500 Subject: [PATCH] Working on adding support for template file attachments --- models/attachment.go | 8 +++++ models/models.go | 1 + models/template.go | 35 +++++++++++-------- static/js/app/app.js | 9 +++++ static/js/app/controllers.js | 22 ++++++++++-- .../js/app/partials/modals/templateModal.html | 20 +++++++---- 6 files changed, 72 insertions(+), 23 deletions(-) create mode 100644 models/attachment.go diff --git a/models/attachment.go b/models/attachment.go new file mode 100644 index 00000000..49366eb8 --- /dev/null +++ b/models/attachment.go @@ -0,0 +1,8 @@ +package models + +type Attachment struct { + TemplateId string `json:"-"` + Content string `json:"content"` + Type string `json:"type"` + Name string `json:"name"` +} diff --git a/models/models.go b/models/models.go index 72b27a7d..10307cd3 100644 --- a/models/models.go +++ b/models/models.go @@ -61,6 +61,7 @@ func Setup() error { db.CreateTable(Group{}) db.CreateTable(GroupTarget{}) db.CreateTable(Template{}) + db.CreateTable(Attachment{}) db.CreateTable(SMTP{}) db.CreateTable(Event{}) db.CreateTable(Campaign{}) diff --git a/models/template.go b/models/template.go index 3d20512c..8017ed7c 100644 --- a/models/template.go +++ b/models/template.go @@ -6,13 +6,14 @@ import ( ) type Template struct { - Id int64 `json:"id"` - UserId int64 `json:"-"` - Name string `json:"name"` - Subject string `json:"subject"` - Text string `json:"text"` - HTML string `json:"html"` - ModifiedDate time.Time `json:"modified_date"` + Id int64 `json:"id"` + UserId int64 `json:"-"` + Name string `json:"name"` + Subject string `json:"subject"` + Text string `json:"text"` + HTML string `json:"html"` + ModifiedDate time.Time `json:"modified_date"` + Attachments []Attachment `json:"attachments"` } var ErrTemplateNameNotSpecified = errors.New("Template Name not specified") @@ -28,11 +29,6 @@ func (t *Template) Validate() error { return nil } -type UserTemplate struct { - UserId int64 `json:"-"` - TemplateId int64 `json:"-"` -} - // GetTemplates returns the templates owned by the given user. func GetTemplates(uid int64) ([]Template, error) { ts := []Template{} @@ -41,6 +37,13 @@ func GetTemplates(uid int64) ([]Template, error) { Logger.Println(err) return ts, err } + for i, _ := range ts { + err = db.Where("template_id=?", ts[i].Id).Find(&ts[i].Attachments).Error + if err != nil { + Logger.Println(err) + return ts, err + } + } return ts, err } @@ -52,6 +55,11 @@ func GetTemplate(id int64, uid int64) (Template, error) { Logger.Println(err) return t, err } + err = db.Where("template_id=?", t.Id).Find(&t.Attachments).Error + if err != nil { + Logger.Println(err) + return t, err + } return t, err } @@ -80,8 +88,7 @@ func PostTemplate(t *Template) error { // PutTemplate edits an existing template in the database. // Per the PUT Method RFC, it presumes all data for a template is provided. func PutTemplate(t *Template) error { - Logger.Println(t) - err := db.Debug().Where("id=?", t.Id).Save(t).Error + err := db.Where("id=?", t.Id).Save(t).Error if err != nil { Logger.Println(err) return err diff --git a/static/js/app/app.js b/static/js/app/app.js index 1ec9c8b0..23e391b8 100644 --- a/static/js/app/app.js +++ b/static/js/app/app.js @@ -35,6 +35,15 @@ app.config(function($routeProvider) { }) }); +app.config( [ + '$compileProvider', + function( $compileProvider ) + { + $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|data):/); + // Angular before v1.2 uses $compileProvider.urlSanitizationWhitelist(...) + } +]); + app.filter('cut', function() { return function(value, max, tail) { if (!value) return ''; diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js index 01631099..5ff20f3b 100644 --- a/static/js/app/controllers.js +++ b/static/js/app/controllers.js @@ -18,7 +18,7 @@ app.controller('DashboardCtrl', function($scope, $filter, $location, CampaignSer campaign.x = new Date(campaign.created_date) campaign.y = 0 angular.forEach(campaign.results, function(result, r_key) { - if (result.status == "Clicked Link") { + if (result.status == "Success") { campaign.y++; } }) @@ -600,6 +600,7 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable name: '', html: '', text: '', + files: [] }; } else { @@ -652,7 +653,24 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable } }) -var TemplateModalCtrl = function($scope, $modalInstance) { +var TemplateModalCtrl = function($scope, $upload, $modalInstance) { + var reader = new FileReader(); + $scope.onFileSelect = function($files) { + angular.forEach($files, function(file, key) { + reader.onload = function(e) { + $scope.template.files.push({ + name : file.name, + content : reader.result.split(",")[1], + type : file.type || "application/octet-stream" + }) + $scope.$apply(); + } + reader.onerror = function(e) { + console.log(e) + } + reader.readAsDataURL(file) + }) + } $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; diff --git a/static/js/app/partials/modals/templateModal.html b/static/js/app/partials/modals/templateModal.html index b3ae1cd8..a813fcbb 100644 --- a/static/js/app/partials/modals/templateModal.html +++ b/static/js/app/partials/modals/templateModal.html @@ -11,7 +11,7 @@
- +
@@ -30,12 +30,18 @@
- -
-
- -
-
+
+ Add Files (Coming Soon!) + + +
+ + + + +  {{file.name}} + {{file.type}} +