forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_handler.go
122 lines (108 loc) · 2.65 KB
/
list_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package uadmin
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
// listHandler !
func listHandler(w http.ResponseWriter, r *http.Request, session *Session) {
r.ParseMultipartForm(32 << 20)
type Context struct {
User string
Pagination int
Data *listData
Schema ModelSchema
IsUpdated bool
Demo bool
CanAdd bool
CanDelete bool
HasAccess bool
SiteName string
Language Language
RootURL string
HasCategorical bool
}
c := Context{}
c.RootURL = RootURL
c.SiteName = SiteName
c.Language = getLanguage(r)
c.User = session.User.Username
user := session.User
// Creat the template
t := template.New("").Funcs(template.FuncMap{
"Tf": Tf,
})
t, err := t.ParseFiles("./templates/uadmin/" + Theme + "/list.html")
if err != nil {
log.Println("ERROR: listHandler.ParseFiles", err.Error())
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
ModelName := r.URL.Path
up := user.HasAccess(ModelName)
if user.UserGroupID != 0 {
Get(&user.UserGroup, "id = ?", user.UserGroupID)
}
gp := user.UserGroup.HasAccess(ModelName)
if up.ID != 0 && !user.Admin {
if !up.Read {
page404Handler(w, r, session)
return
}
c.HasAccess = true
c.CanAdd = up.Add
c.CanDelete = up.Delete
} else if gp.ID != 0 && !user.Admin {
if !gp.Read {
page404Handler(w, r, session)
return
}
c.HasAccess = true
c.CanAdd = gp.Add
c.CanDelete = gp.Delete
} else if !user.Admin {
page404Handler(w, r, session)
return
}
if user.Admin {
c.HasAccess = true
c.CanAdd = true
c.CanDelete = true
}
if r.Method == cPOST {
if r.FormValue("delete") == "delete" {
processDelete(ModelName, w, r, session, &user)
c.IsUpdated = true
http.Redirect(w, r, fmt.Sprint(RootURL+r.URL.Path), 303)
}
}
// Initialize the schema
m, ok := NewModel(ModelName, false)
// Return 404 if it is an unknown model
if !ok {
page404Handler(w, r, session)
return
}
// Get the schema for the model
c.Schema, _ = getSchema(m.Interface())
for i := range c.Schema.Fields {
if c.Schema.Fields[i].CategoricalFilter {
c.HasCategorical = true
}
}
// func (*ModelSchema, *User) (string, []interface{})
query := ""
args := []interface{}{}
if c.Schema.ListModifier != nil {
query, args = c.Schema.ListModifier(&c.Schema, &user)
}
c.Data = getListData(m.Interface(), PageLength, r, session, query, args...)
c.Pagination = paginationHandler(c.Data.Count, PageLength)
err = t.ExecuteTemplate(w, "list.html", c)
// lock.Unlock()
if err != nil {
log.Println("ERROR: listHandler.ExecuteTemplate", err.Error())
}
}