forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
354 lines (324 loc) · 11.3 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package uadmin
import (
"fmt"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"time"
// import upportd image formats to allow exporting
// images to excel
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/xuri/excelize/v2"
)
func getFilter(r *http.Request, session *Session, schema *ModelSchema) (interface{}, []interface{}) {
queryList := []string{}
args := []interface{}{}
var dateRe = regexp.MustCompile(`^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$`)
for k, v := range r.URL.Query() {
if k == "m" || k == "o" || k == "p" || k == "return_url" {
continue
}
if len(v) > 0 {
// Unescape '{' and '}'
v[0], _ = url.QueryUnescape(v[0])
// Replace placeholders
v[0] = strings.Replace(v[0], "{USERNAME}", session.User.Username, -1)
v[0] = strings.Replace(v[0], "{USERID}", fmt.Sprint(session.User.ID), -1)
v[0] = strings.Replace(v[0], "{NOW}", time.Now().Format("2006-01-02 15:04:05"), -1)
}
if k == "q" {
// Code for search
searchQuery := []string{}
for i := range schema.Fields {
f := schema.Fields[i]
if f.Searchable {
for _, term := range strings.Split(v[0], " ") {
searchQuery = append(searchQuery, fmt.Sprintf("%s "+getLike(false)+" ?", GetDB().Config.NamingStrategy.ColumnName("", schema.Fields[i].Name)))
args = append(args, "%"+term+"%")
}
}
}
if len(searchQuery) > 0 {
queryList = append(queryList, fmt.Sprintf("(%s)", strings.Join(searchQuery, " OR ")))
}
continue
}
queryParts := strings.Split(k, "__")
if SQLInjection(r, queryParts[0], "") {
continue
}
query := columnEnclosure() + queryParts[0] + columnEnclosure()
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" {
// IN
query += " IN (?)"
}
if queryParts[1] == "contains" {
// Contains
query += " " + getLike(true) + " ?"
}
if queryParts[1] == "icontains" {
// Contains
query += " " + getLike(false) + " ?"
}
} 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 {
// Format dates
dateType := false
var columnName string
for i := range schema.Fields {
columnName = GetDB().Config.NamingStrategy.ColumnName("", schema.Fields[i].Name)
if columnName == queryParts[0] {
if schema.Fields[i].Type == cDATE {
dateType = true
break
}
}
}
if dateType && v[0] == "" {
query = queryParts[0] + " IS NULL"
//args = append(args, nil)
} else if dateRe.MatchString(v[0]) {
d, _ := time.Parse("2006-01-02", v[0])
d = time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, getTZ())
args = append(args, d)
} 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 {
pageErrorHandler(w, r, session)
return
}
a, ok := NewModelArray(modelName, false)
if !ok {
pageErrorHandler(w, r, session)
return
}
m, _ := NewModel(modelName, false)
query, args := getFilter(r, session, &schema)
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 {
pageErrorHandler(w, r, session)
return
}
file := excelize.NewFile()
t := reflect.TypeOf(m.Interface())
sheetName := "Sheet1"
// 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)
}
*/
var colName string
var colIndex = 0
dateFormat := "yyyy-mm-dd HH:MM:SS"
var preloaded bool
headerStyle, _ := file.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 7},
{Type: "top", Color: "000000", Style: 7},
{Type: "bottom", Color: "000000", Style: 7},
{Type: "right", Color: "000000", Style: 7},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"cccccc"},
Pattern: 1,
},
Font: &excelize.Font{Bold: true},
})
bodyStyle, _ := file.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 7},
{Type: "top", Color: "000000", Style: 7},
{Type: "bottom", Color: "000000", Style: 7},
{Type: "right", Color: "000000", Style: 7},
},
Alignment: &excelize.Alignment{WrapText: true, Vertical: "top"},
})
dateStyle, _ := file.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 7},
{Type: "top", Color: "000000", Style: 7},
{Type: "bottom", Color: "000000", Style: 7},
{Type: "right", Color: "000000", Style: 7},
},
CustomNumFmt: &dateFormat,
})
codeStyle, _ := file.NewStyle(&excelize.Style{
Border: []excelize.Border{
{Type: "left", Color: "000000", Style: 7},
{Type: "top", Color: "000000", Style: 7},
{Type: "bottom", Color: "000000", Style: 7},
{Type: "right", Color: "000000", Style: 7},
},
Font: &excelize.Font{Family: "mono"},
Alignment: &excelize.Alignment{WrapText: true, Vertical: "top"},
})
// Add header
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")) || schema.FieldByName(t.Field(i).Name).Type == cLINK {
continue
}
colIndex++
colName, _ = excelize.ColumnNumberToName(colIndex)
file.SetCellValue(sheetName, colName+"1", schema.FieldByName(t.Field(i).Name).DisplayName)
file.SetCellStyle(sheetName, colName+"1", colName+"1", headerStyle)
file.SetColWidth(sheetName, colName, colName, 1.3*float64(len(schema.FieldByName(t.Field(i).Name).DisplayName)))
}
// Add body data
for i := 0; i < a.Len(); i++ {
colIndex = 0
preloaded = false
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")) || schema.FieldByName(t.Field(c).Name).Type == cLINK {
continue
}
colIndex++
colName, _ = excelize.ColumnNumberToName(colIndex)
cellName := fmt.Sprintf(colName+"%d", i+2)
// Determine the data type
if schema.FieldByName(t.Field(c).Name).Type == cDATE {
// Process Date/Time
var cDate time.Time
if t.Field(c).Type.Kind() == reflect.Ptr {
if a.Index(i).Field(c).IsNil() {
continue
}
cDate = a.Index(i).Field(c).Elem().Interface().(time.Time)
} else {
cDate = a.Index(i).Field(c).Interface().(time.Time)
}
startDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Now().Location())
file.SetCellValue(sheetName, cellName, cDate.Sub(startDate).Hours()/24)
file.SetColWidth(sheetName, colName, colName, 20)
file.SetCellStyle(sheetName, cellName, cellName, dateStyle)
cWidth, _ := file.GetColWidth(sheetName, colName)
if cWidth < 20 {
file.SetColWidth(sheetName, colName, colName, 20)
}
} else if t.Field(c).Type.Kind() == reflect.Struct || (t.Field(c).Type.Kind() == reflect.Ptr && t.Field(c).Type.Elem().Kind() == reflect.Struct) {
// Process forign keys
if !preloaded {
Preload(a.Index(i).Addr().Interface())
}
file.SetCellValue(sheetName, cellName, GetString(a.Index(i).Field(c).Interface()))
file.SetCellStyle(sheetName, cellName, cellName, bodyStyle)
excelAdjustWidthHight(file, sheetName, colName, cellName, i+2, GetString(a.Index(i).Field(c).Interface()))
} else if t.Field(c).Type.Kind() == reflect.Int && t.Field(c).Type != reflect.TypeOf(0) {
// Process static list type
value := a.Index(i).Field(c).Interface()
file.SetCellValue(sheetName, cellName, GetString(value))
file.SetCellStyle(sheetName, cellName, cellName, bodyStyle)
excelAdjustWidthHight(file, sheetName, colName, cellName, i+2, GetString(value))
} else if schema.FieldByName(t.Field(c).Name).Type == cIMAGE || schema.FieldByName(t.Field(c).Name).Type == cIMAGE_MINIO {
// Process images
if a.Index(i).Field(c).String() == "" {
continue
}
file.SetRowHeight(sheetName, i+2, 100)
file.SetColWidth(sheetName, colName, colName, 25)
file.AddPicture(sheetName, cellName, a.Index(i).Field(c).String()[1:], `{"autofit": true, "print_obj": true, "lock_aspect_ratio": true, "locked": false, "positioning": "oneCell", "x_scale":5.0, "y_scale":5.0}`)
file.SetCellStyle(sheetName, cellName, cellName, bodyStyle)
} else if schema.FieldByName(t.Field(c).Name).Type == cCODE {
file.SetCellValue(sheetName, cellName, a.Index(i).Field(c).Interface())
file.SetCellStyle(sheetName, cellName, cellName, codeStyle)
excelAdjustWidthHight(file, sheetName, colName, cellName, i+2, fmt.Sprint(a.Index(i).Field(c).Interface()))
} else {
// All other data
file.SetCellValue(sheetName, cellName, a.Index(i).Field(c).Interface())
file.SetCellStyle(sheetName, cellName, cellName, bodyStyle)
excelAdjustWidthHight(file, sheetName, colName, cellName, i+2, 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)
for _, err = os.Stat("./media/export/" + fileName + ".xlsx"); os.IsExist(err); {
fileName = GenerateBase64(24)
}
err = file.SaveAs("./media/export/" + fileName + ".xlsx")
if err != nil {
Trail(ERROR, "exportHandler unable to save file %s. %s", "./media/export/"+fileName+".xlsx", err)
}
http.Redirect(w, r, "/media/export/"+fileName+".xlsx", http.StatusSeeOther)
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
}
func excelAdjustWidthHight(file *excelize.File, sheetName, colName, cellName string, rowIndex int, content string) {
cWidth, _ := file.GetColWidth(sheetName, colName)
sWidth := float64(len(content))
if sWidth > 40 {
cHight, _ := file.GetRowHeight(sheetName, rowIndex)
sHight := sWidth / 2
if sHight > 100 {
sHight = 100
}
if cHight < sHight {
file.SetRowHeight(sheetName, rowIndex, sHight)
}
sWidth = 40
}
if cWidth < sWidth {
file.SetColWidth(sheetName, colName, colName, sWidth)
}
}