Adding logic to handle getting the template for a campaign

Added SMTP Model
Added better flash support in controllers.js
Added SMTP Options accordion in campaign modal
pull/24/head
Jordan 2014-06-03 13:27:20 -05:00
parent af44dbb07c
commit db24496fb0
6 changed files with 131 additions and 25 deletions

View File

@ -1,9 +1,10 @@
package models
import (
"database/sql"
"fmt"
"time"
"github.com/jinzhu/gorm"
)
//Campaign is a struct representing a created campaign
@ -13,10 +14,24 @@ type Campaign struct {
Name string `json:"name" sql:"not null"`
CreatedDate time.Time `json:"created_date"`
CompletedDate time.Time `json:"completed_date"`
TemplateId int64 `json:"-"`
Template Template `json:"template"` //This may change
Status string `json:"status"`
Results []Result `json:"results,omitempty"`
Groups []Group `json:"groups,omitempty"`
SMTP SMTP `json:"options,omitempty"`
}
func (c *Campaign) Validate() (string, bool) {
switch {
case c.Name == "":
return "Must specify campaign name", false
case len(c.Groups) == 0:
return "No groups specified", false
case c.Template.Name == "":
return "No template specified", false
}
return "", true
}
type Result struct {
@ -58,7 +73,7 @@ func PostCampaign(c *Campaign, uid int64) error {
// Check to make sure all the groups already exist
for i, g := range c.Groups {
c.Groups[i], err = GetGroupByName(g.Name, uid)
if err == sql.ErrNoRows {
if err == gorm.RecordNotFound {
Logger.Printf("Error - Group %s does not exist", g.Name)
return err
} else if err != nil {
@ -66,6 +81,17 @@ func PostCampaign(c *Campaign, uid int64) error {
return err
}
}
// Check to make sure the template exists
t, err := GetTemplateByName(c.Template.Name, uid)
if err == gorm.RecordNotFound {
Logger.Printf("Error - Template %s does not exist", t.Name)
return err
} else if err != nil {
Logger.Println(err)
return err
}
c.TemplateId = t.Id
// Insert into the DB
err = db.Save(c).Error
if err != nil {
@ -109,13 +135,3 @@ func DeleteCampaign(id int64) error {
}
return err
}
func ValidateCampaign(c *Campaign) (string, bool) {
if c.Name == "" {
return "Must specify campaign name", false
}
if len(c.Groups) == 0 {
return "No groups specified", false
}
return "", true
}

View File

@ -56,6 +56,7 @@ func Setup() error {
db.CreateTable(Group{})
db.CreateTable(GroupTarget{})
db.CreateTable(Template{})
db.CreateTable(SMTP{})
db.CreateTable(Campaign{})
//Create the default user
init_user := User{

26
models/smtp.go Normal file
View File

@ -0,0 +1,26 @@
package models
type SMTP struct {
SMTPId int64 `json:"-"`
CampaignId int64 `json:"-"`
Hostname string `json:"hostname"`
Port int `json:"port"`
UseAuth bool `json:"use_auth"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty" sql:"-"`
FromAddress string `json:"from_address"`
}
func (s *SMTP) Validate() (string, bool) {
switch {
case s.UseAuth == false && (s.Username == "" && s.Password == ""):
return "Auth requested, but username or password blank", false
case s.FromAddress == "":
return "No from address specified", false
case s.Hostname == "":
return "No hostname specified", false
case s.Port == 0:
return "No port specified", false
}
return "", true
}

View File

@ -98,6 +98,7 @@ app.controller('CampaignCtrl', function($scope, $modal, CampaignService, GroupSe
deleteCampaign.$delete({
id: deleteCampaign.id
}, function() {
$scope.successFlash("Campaign deleted successfully")
$scope.mainTableParams.reload();
});
}
@ -157,6 +158,23 @@ app.controller('CampaignResultsCtrl', function($scope, CampaignService, GroupSer
});
app.controller('GroupCtrl', function($scope, $modal, GroupService, ngTableParams) {
$scope.errorFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "danger",
"message": message,
"icon": "fa-exclamation-circle"
})
}
$scope.successFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "success",
"message": message,
"icon": "fa-check-circle"
})
}
$scope.mainTableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
@ -254,9 +272,31 @@ var GroupModalCtrl = function($scope, $modalInstance) {
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.ok = function(group) {
$modalInstance.dismiss('')
$scope.saveGroup(group)
};
}
app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTableParams) {
$scope.errorFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "danger",
"message": message,
"icon": "fa-exclamation-circle"
})
}
$scope.successFlash = function(message) {
$scope.flashes = [];
$scope.flashes.push({
"type": "success",
"message": message,
"icon": "fa-check-circle"
})
}
$scope.mainTableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
@ -322,7 +362,12 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable
var deleteTemplate = new TemplateService(template);
deleteTemplate.$delete({
id: deleteTemplate.id
}, function() {
}, function(response) {
if (response.success) {
$scope.successFlash(response.message)
} else {
$scope.errorFlash(response.message)
}
$scope.mainTableParams.reload();
});
}
@ -367,10 +412,12 @@ app.controller('SettingsCtrl', function($scope, $http, $window) {
'Content-Type': 'application/x-www-form-urlencoded'
} // set the headers so angular passing info as form data (not request payload)
})
.success(function(data) {
$scope.user.api_key = data;
$window.user.api_key = data;
$scope.successFlash("API Key Successfully Reset")
.success(function(response) {
if (response.success) {
$scope.user.api_key = response.data;
$window.user.api_key = response.data;
$scope.successFlash(response.message)
}
})
}
$scope.save_settings = function(){

View File

@ -9,14 +9,30 @@
<input type="text" class="form-control" ng-model="campaign.name" id="name" placeholder="Campaign name" autofocus>
<br />
<label class="control-label" for="template">Template:</label>
<input type="text" class="form-control" placeholder="Template Name" id="template" typeahead="template.name for template in templates | filter:{name:$viewValue}" typeahead-editable="false" ng-model="template.name" />
<input type="text" class="form-control" placeholder="Template Name" id="template" typeahead="template.name for template in templates | filter:{name:$viewValue}" typeahead-editable="false" ng-model="campaign.template.name" />
<br />
<accordian>
<accordion-group>
<accordion-heading>
SMTP Options <i class="pull-right glyphicon" ng-class="{'glyphicon-chevron-down': isOpen, 'glyphicon-chevron-right': !isOpen}"></i>
</accordion-heading>
<label class="control-label" for="from">From:</label>
<input type="text" class="form-control" placeholder="First Last <test@example.com>" id="form">
<br />
<label class="control-label" for="smtp_server">SMTP Server:</label>
<input type="text" class="form-control" placeholder="host:port" id="smtp_server">
<label class="control-label" for="smtp_server">Hostname:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.hostname" placeholder="host:port" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Port:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.port" placeholder="host:port" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Username:</label>
<input type="text" class="form-control" ng-model="campaign.smtp.username" placeholder="host:port" id="smtp_server">
<br />
<label class="control-label" for="smtp_server">Password:</label>
<input type="password" class="form-control" ng-model="campaign.smtp.password" id="smtp_server">
<br />
</accordion-group>
</accordion>
<label class="control-label" for="users">Groups:</label>
<form ng-submit="addGroup(group)">
<div class="input-group">

View File

@ -38,6 +38,6 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="saveGroup(group)" data-dismiss="modal">Save Group</button>
<button type="button" class="btn btn-primary" ng-click="ok(group)" data-dismiss="modal">Save Group</button>
</div>
</div>