Skip to content

Commit 9a720eb

Browse files
Remove required from F if there is a pending approvalwq
1 parent fb96935 commit 9a720eb

8 files changed

+166
-27
lines changed

approval_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package uadmin
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestApprovalStruct is to test Approval struct
8+
func TestApprovalStruct(t *testing.T) {
9+
app := Approval{
10+
ModelName: "test",
11+
ColumnName: "column",
12+
ModelPK: 100,
13+
}
14+
15+
if app.String() != "test.column 100" {
16+
t.Errorf("Approval.String didn't return valid value. Got(%s) Expected (%s)", app.String(), "test.column 100")
17+
}
18+
}

export.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func exportHandler(w http.ResponseWriter, r *http.Request, session *Session) {
223223
fileName := GenerateBase64(24)
224224
err = file.Save("./media/export/" + fileName + ".xlsx")
225225
if err != nil {
226-
fmt.Printf(err.Error())
226+
Trail(ERROR, "exportHandler unable to save file %s. %s", "./media/export/"+fileName+".xlsx", err)
227227
}
228228
http.Redirect(w, r, "/media/export/"+fileName+".xlsx", 303)
229229
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

form_handler_test.go

+104-17
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,14 @@ func TestFormHandler(t *testing.T) {
173173
Save(&m6)
174174

175175
m7 := TestApproval{
176-
Name: "Test",
177-
Start: now,
178-
End: &now,
179-
Count: 1,
180-
Price: 1.0,
176+
Name: "Test",
177+
Start: now,
178+
End: &now,
179+
Count: 1,
180+
Price: 1.0,
181+
List: testList(0),
182+
TestModelID: m5.ID,
183+
Active: true,
181184
}
182185
Save(&m7)
183186

@@ -671,23 +674,101 @@ func TestFormHandler(t *testing.T) {
671674
http.StatusSeeOther,
672675
s2,
673676
map[string][]string{
674-
"ID": {fmt.Sprint(m6.ID)},
675-
"Name": {"Test1"},
676-
"Start": {tomorrow.Format("2006-01-02 15:04")},
677-
"End": {tomorrow.Format("2006-01-02 15:04")},
678-
"Count": {"2"},
679-
"Price": {"2"},
680-
"save": {"continue"},
677+
"ID": {fmt.Sprint(m6.ID)},
678+
"Name": {"Test1"},
679+
"Start": {tomorrow.Format("2006-01-02 15:04")},
680+
"End": {""},
681+
"Count": {"2"},
682+
"Price": {"2"},
683+
"List": {"1"},
684+
"TestModelID": {""},
685+
"save": {"continue"},
681686
},
682687
[]attrExample{},
683688
[]Approval{
684-
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Name", OldValue: m7.Name, NewValue: "Test1", NewValueDescription: "Test1", ChangedBy: "u1"},
685-
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Start", OldValue: m7.Start.Format("2006-01-02 15:04:05-07:00"), NewValue: tomorrow.Format("2006-01-02 15:04:05-07:00"), NewValueDescription: tomorrow.Format("2006-01-02 15:04:05-07:00"), ChangedBy: "u1"},
686-
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "End", OldValue: m7.End.Format("2006-01-02 15:04:05-07:00"), NewValue: tomorrow.Format("2006-01-02 15:04:05-07:00"), NewValueDescription: tomorrow.Format("2006-01-02 15:04:05-07:00"), ChangedBy: "u1"},
687-
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Count", OldValue: fmt.Sprint(m7.Count), NewValue: "2", NewValueDescription: "2", ChangedBy: "u1"},
688-
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Price", OldValue: fmt.Sprint(m7.Price), NewValue: "2", NewValueDescription: "2", ChangedBy: "u1"},
689+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Name", OldValue: m7.Name, NewValue: "Test1", NewValueDescription: "Test1", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
690+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Start", OldValue: m7.Start.Format("2006-01-02 15:04:05-07:00"), NewValue: tomorrow.Format("2006-01-02 15:04:05-07:00"), NewValueDescription: tomorrow.Format("2006-01-02 15:04:05-07:00"), ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
691+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "End", OldValue: m7.End.Format("2006-01-02 15:04:05-07:00"), NewValue: "", NewValueDescription: "", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
692+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Count", OldValue: fmt.Sprint(m7.Count), NewValue: "2", NewValueDescription: "2", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
693+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Price", OldValue: fmt.Sprint(m7.Price), NewValue: "2", NewValueDescription: "2", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
694+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "List", OldValue: fmt.Sprint(m7.List), NewValue: "1", NewValueDescription: GetString(testList(1)), ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
695+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "TestModel", OldValue: fmt.Sprint(m7.TestModelID), NewValue: "0", NewValueDescription: "", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
696+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Active", OldValue: fmt.Sprint(m7.Active), NewValue: "false", NewValueDescription: "false", ChangedBy: "u1", ApprovalAction: ApprovalAction(1)},
689697
},
690698
},
699+
//20
700+
{
701+
httptest.NewRequest("GET", fmt.Sprintf("/testapproval/%d", m7.ID), nil),
702+
http.StatusOK,
703+
s2,
704+
map[string][]string{},
705+
[]attrExample{
706+
{"input", "name", "Name", "value", "Test1", -1, "", true},
707+
{"input", "name", "Start", "value", tomorrow.Format("2006-01-02 15:04:05"), -1, "", true},
708+
{"input", "name", "End", "value", "", -1, "", true},
709+
{"input", "name", "Count", "value", "2", -1, "", true},
710+
{"input", "name", "Price", "value", "2", -1, "", true},
711+
{"select", "name", "List", "name", "List", -1, "", true},
712+
{"option", "value", "", "selected", "", 5, "", false},
713+
{"option", "value", "1", "selected", "", 5, "", true},
714+
{"select", "name", "TestModelID", "name", "TestModelID", -1, "", true},
715+
{"option", "value", "", "selected", "", 8, "", true},
716+
{"option", "value", "1", "selected", "", 8, "", false},
717+
{"input", "name", "Active", "checked", "", -1, "", false},
718+
},
719+
[]Approval{},
720+
},
721+
//21
722+
{
723+
httptest.NewRequest("POST", fmt.Sprintf("/testapproval/%d", m7.ID), nil),
724+
http.StatusSeeOther,
725+
s2,
726+
map[string][]string{
727+
"ID": {fmt.Sprint(m6.ID)},
728+
"Name": {"Test2"},
729+
"Start": {now.Format("2006-01-02 15:04")},
730+
"End": {now.Format("2006-01-02 15:04")},
731+
"Count": {"3"},
732+
"Price": {"3"},
733+
"List": {""},
734+
"TestModelID": {fmt.Sprint(m7.ID)},
735+
"Active": {"on"},
736+
"save": {"continue"},
737+
},
738+
[]attrExample{},
739+
[]Approval{
740+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Name", OldValue: "Test1", NewValue: "Test2", NewValueDescription: "Test2", ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
741+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Start", OldValue: tomorrow.Format("2006-01-02 15:04:05-07:00"), NewValue: now.Format("2006-01-02 15:04:05-07:00"), NewValueDescription: now.Format("2006-01-02 15:04:05-07:00"), ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
742+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "End", OldValue: "", NewValue: now.Format("2006-01-02 15:04:05-07:00"), NewValueDescription: now.Format("2006-01-02 15:04:05-07:00"), ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
743+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Count", OldValue: "2", NewValue: "3", NewValueDescription: "3", ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
744+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Price", OldValue: "2", NewValue: "3", NewValueDescription: "3", ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
745+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "List", OldValue: "1", NewValue: "0", NewValueDescription: "0", ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
746+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "TestModel", OldValue: "0", NewValue: fmt.Sprint(m7.ID), NewValueDescription: GetString(m7), ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
747+
{ModelName: "testapproval", ModelPK: m6.ID, ColumnName: "Active", OldValue: "false", NewValue: "true", NewValueDescription: "true", ChangedBy: "u1", ApprovalAction: ApprovalAction(2)},
748+
},
749+
},
750+
//22
751+
{
752+
httptest.NewRequest("GET", fmt.Sprintf("/testapproval/%d", m7.ID), nil),
753+
http.StatusOK,
754+
s2,
755+
map[string][]string{},
756+
[]attrExample{
757+
{"input", "name", "Name", "value", "Test1", -1, "", true},
758+
{"input", "name", "Start", "value", tomorrow.Format("2006-01-02 15:04:05"), -1, "", true},
759+
{"input", "name", "End", "value", "", -1, "", true},
760+
{"input", "name", "Count", "value", "2", -1, "", true},
761+
{"input", "name", "Price", "value", "2", -1, "", true},
762+
{"select", "name", "List", "name", "List", -1, "", true},
763+
{"option", "value", "", "selected", "", 5, "", false},
764+
{"option", "value", "1", "selected", "", 5, "", true},
765+
{"select", "name", "TestModelID", "name", "TestModelID", -1, "", true},
766+
{"option", "value", "", "selected", "", 8, "", true},
767+
{"option", "value", "1", "selected", "", 8, "", false},
768+
{"input", "name", "Active", "checked", "", -1, "", false},
769+
},
770+
[]Approval{},
771+
},
691772
}
692773

693774
for i, e := range examples {
@@ -756,6 +837,12 @@ func TestFormHandler(t *testing.T) {
756837
if tempApp.ID == 0 {
757838
t.Errorf("formHandler didn't create the correct approval for example %d for values. %#v", i, app)
758839
}
840+
841+
if app.ApprovalAction != ApprovalAction(0) {
842+
tempApp.ApprovalAction = app.ApprovalAction
843+
tempApp.UpdatedBy = "admin"
844+
tempApp.Save()
845+
}
759846
}
760847
}
761848

get_form_data.go

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func getFormData(a interface{}, r *http.Request, session *Session, s *ModelSchem
107107
f.ApprovalBy = lastA.ApprovalBy
108108
f.ApprovalID = lastA.ID
109109
f.OldValue = lastA.OldValue
110+
111+
// Remove required if the field has a pending approval
112+
f.Required = f.Required && (f.ApprovalAction != ApprovalAction(0))
110113
}
111114
}
112115

grouppermission_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package uadmin
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestGroupPermission is for testing GroupPermission struct
8+
func TestGroupPermission(t *testing.T) {
9+
gp := GroupPermission{}
10+
gp.ID = 100
11+
if gp.String() != "100" {
12+
t.Errorf("GroupPermission.String didn't return a valid value. Expected (%s) got (%s).", "100", gp.String())
13+
}
14+
}

language_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package uadmin
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// TestLanguage is for testing Language struct
8+
func TestLanguage(t *testing.T) {
9+
lang := Language{
10+
Code: "ts",
11+
}
12+
if lang.String() != "ts" {
13+
t.Errorf("Language.String didn't return a valid value. Expected (%s) got (%s).", "ts", lang.String())
14+
}
15+
}

register.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package uadmin
22

33
import (
4-
"fmt"
54
"io/ioutil"
65
"net/http"
76
"os"
@@ -10,7 +9,6 @@ import (
109

1110
"github.com/jinzhu/gorm"
1211
"github.com/jinzhu/inflection"
13-
"github.com/uadmin/uadmin/colors"
1412
"github.com/uadmin/uadmin/helper"
1513
)
1614

@@ -249,11 +247,11 @@ func RegisterInlines(model interface{}, fk map[string]string) {
249247
fkMap[strings.ToLower(t.Name())] = gorm.ToColumnName(v)
250248
// Check if the field name is in the struct
251249
if t.Kind() != reflect.Struct {
252-
fmt.Printf("%sUnable to register inline for (%s) inline %s.%s. Please pass a struct as key.\n", colors.Error, reflect.TypeOf(model).Name(), t.Name(), v)
250+
Trail(ERROR, "Unable to register inline for (%s) inline %s.%s. Please pass a struct as key.", reflect.TypeOf(model).Name(), t.Name(), v)
253251
continue
254252
}
255253
if _, ok := t.FieldByName(v); !ok {
256-
fmt.Printf("%sUnable to register inline for (%s) inline %s.%s. Field name is not in struct.\n", colors.Error, reflect.TypeOf(model).Name(), t.Name(), v)
254+
Trail(ERROR, "Unable to register inline for (%s) inline %s.%s. Field name is not in struct.", reflect.TypeOf(model).Name(), t.Name(), v)
257255
continue
258256
}
259257
inlineList = append(inlineList, kmodel.Interface())

server_test.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ type TestModelB struct {
4242

4343
type TestApproval struct {
4444
Model
45-
Name string `uadmin:"approval"`
46-
Start time.Time `uadmin:"approval"`
47-
End *time.Time `uadmin:"approval"`
48-
Count int `uadmin:"approval"`
49-
Price float64 `uadmin:"approval"`
45+
Name string `uadmin:"approval"`
46+
Start time.Time `uadmin:"approval"`
47+
End *time.Time `uadmin:"approval"`
48+
Count int `uadmin:"approval"`
49+
Price float64 `uadmin:"approval"`
50+
List testList `uadmin:"approval"`
51+
TestModel TestModelA `uadmin:"approval"`
52+
TestModelID uint
53+
Active bool `uadmin:"approval"`
5054
}
5155

5256
// Method__List__Form is a method to test method based properties for models

0 commit comments

Comments
 (0)