-
Notifications
You must be signed in to change notification settings - Fork 60
/
export.go
222 lines (196 loc) · 5.97 KB
/
export.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package uadmin
import (
"fmt"
"net/http"
"os"
"reflect"
"strings"
"time"
"github.com/tealeg/xlsx"
)
func getFilter(r *http.Request, session *Session) (interface{}, []interface{}) {
queryList := []string{}
args := []interface{}{}
for k, v := range r.URL.Query() {
if k == "m" || k == "o" || k == "p" {
continue
}
if k == "q" {
continue
}
user := session.User
if len(v) > 0 {
v[0] = strings.Replace(v[0], "{username}", user.Username, -1)
v[0] = strings.Replace(v[0], "{me}", user.Username, -1)
v[0] = strings.Replace(v[0], "{userid}", fmt.Sprint(user.ID), -1)
}
queryParts := strings.Split(k, "__")
query := "`" + queryParts[0] + "`"
if len(queryParts) > 1 {
if queryParts[1] == "lt" {
// Less than
query += " < ?"
}
if queryParts[1] == "lte" {
// Less than or equal to
query += " <= ?"
}
if queryParts[1] == "gt" {
// Greater than
query += " > ?"
}
if queryParts[1] == "gte" {
// Greater than or equal to
query += " >= ?"
}
if queryParts[1] == "in" {
// Greater than or equal to
query += " IN (?)"
}
if queryParts[1] == "contains" {
// Greater than or equal to
query += " LIKE ?"
}
} else {
query += " = ?"
}
if len(queryParts) > 1 && queryParts[1] == "in" {
args = append(args, strings.Split(v[0], ","))
} else if len(queryParts) > 1 && queryParts[1] == "contains" {
args = append(args, "%"+v[0]+"%")
} else {
args = append(args, v[0])
}
queryList = append(queryList, query)
}
return strings.Join(queryList, " AND "), args
}
// exportHandler handles http request for exporting data
func exportHandler(w http.ResponseWriter, r *http.Request, session *Session) {
//http://hostname/admin/export/?m=orders&date__gte=2016-02-01&date__lte=2016-03-01
var err error
// TODO: Call ListSchemaModifier of the schema and use the modified one
modelName := r.URL.Query().Get("m")
schema, ok := getSchema(modelName)
if !ok {
page404Handler(w, r, session)
return
}
a, ok := NewModelArray(modelName, false)
if !ok {
page404Handler(w, r, session)
return
}
m, _ := NewModel(modelName, false)
query, args := getFilter(r, session)
ap, ok := m.Interface().(adminPager)
if ok {
err = ap.AdminPage("id", true, 0, -1, a.Addr().Interface(), query, args...)
} else {
err = AdminPage("id", true, 0, -1, a.Addr().Interface(), query, args...)
}
if err != nil {
page404Handler(w, r, session)
return
}
var file *xlsx.File
var sheet *xlsx.Sheet
var row *xlsx.Row
var cell *xlsx.Cell
file = xlsx.NewFile()
sheet, err = file.AddSheet("Sheet1")
if err != nil {
page404Handler(w, r, session)
Trail(ERROR, "Error in exportHandler, unable to add sheet. %s", err)
return
}
t := reflect.TypeOf(m.Interface())
// Header
row = sheet.AddRow()
headerStyle := xlsx.NewStyle()
headerStyle.Font.Bold = true
headerStyle.Font.Size = 10
headerStyle.Font.Name = "Arial"
headerStyle.ApplyFont = true
for i := 0; i < m.NumField(); i++ {
if !schema.FieldByName(t.Field(i).Name).ListDisplay || m.Field(i).Type().Name() == "Model" || (m.Field(i).Type().Kind() == reflect.Uint && strings.HasSuffix(t.Field(i).Name, "ID")) {
continue
}
cell = row.AddCell()
cell.SetStyle(headerStyle)
cell.Value = getDisplayName(t.Field(i).Name)
}
// Body (Data)
now := time.Now()
for i := 0; i < a.Len(); i++ {
preloaded := false
row = sheet.AddRow()
colIndex := -1
for c := 0; c < m.NumField(); c++ {
if !schema.FieldByName(t.Field(c).Name).ListDisplay || m.Field(c).Type().Name() == "Model" || (m.Field(c).Type().Kind() == reflect.Uint && strings.HasSuffix(t.Field(c).Name, "ID")) {
continue
}
colIndex++
// Add a new cell
cell = row.AddCell()
// Get data based on data type
if t.Field(c).Type.Kind() == reflect.Float64 {
cell.SetFloat(a.Index(i).Field(c).Float())
//cell.Value = fmt.Sprintf("%.2f", a.Index(i).Field(c).Float())
} else if t.Field(c).Type == reflect.TypeOf(&now) {
dt, ok := a.Index(i).Field(c).Interface().(*time.Time)
if ok && dt != nil {
//cell.Value = dt.Format("2006-01-02 15:04:05")
cell.SetDateTime(*dt)
cell.NumFmt = "YYYY-MM-DD HH:MM AM/PM"
sheet.Col(colIndex).Width = 13.4
}
} else if t.Field(c).Type == reflect.TypeOf(now) {
dt, ok := a.Index(i).Field(c).Interface().(time.Time)
if ok {
//cell.Value = dt.Format("2006-01-02 15:04:05")
cell.SetDateTime(dt)
cell.NumFmt = "YYYY-MM-DD HH:MM AM/PM"
sheet.Col(colIndex).Width = 13.4
}
} else if t.Field(c).Type.Kind() == reflect.Struct || (t.Field(c).Type.Kind() == reflect.Ptr && t.Field(c).Type.Elem().Kind() == reflect.Struct) {
if !preloaded {
Preload(a.Index(i).Addr().Interface())
}
cell.Value = GetString(a.Index(i).Field(c).Interface())
} else if t.Field(c).Type.Kind() == reflect.Int {
if t.Field(c).Type == reflect.TypeOf(0) {
cell.SetInt(int(a.Index(i).Field(c).Int()))
// cell.Value = fmt.Sprint(a.Index(i).Field(c).Int())
} else {
value := a.Index(i).Field(c).Int()
for mIndex := 0; mIndex < t.Field(c).Type.NumMethod(); mIndex++ {
rValue := a.Index(i).Field(c).Method(mIndex).Call([]reflect.Value{})[0].Int()
if rValue == value {
cell.Value = getDisplayName(t.Field(c).Type.Method(mIndex).Name)
break
}
}
}
} else if t.Field(c).Type.Kind() == reflect.Bool {
cell.SetBool(a.Index(i).Field(c).Bool())
// cell.Value = fmt.Sprint(a.Index(i).Field(c).Bool())
} else {
cell.Value = fmt.Sprint(a.Index(i).Field(c).Interface())
}
}
}
exportRoot := "./media/export/"
if _, err = os.Stat(exportRoot); os.IsNotExist(err) {
os.MkdirAll(exportRoot, 0700)
os.Create(exportRoot + "index.html")
}
fileName := GenerateBase64(24)
err = file.Save("./media/export/" + fileName + ".xlsx")
if err != nil {
fmt.Printf(err.Error())
}
http.Redirect(w, r, "/media/export/"+fileName+".xlsx", 303)
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
// Check if the file exists
}