Beispiel #1
0
func init() {
	var err error
	var db gorm.DB

	dbConfig := config.Config.DB
	if config.Config.DB.Adapter == "mysql" {
		db, err = gorm.Open("mysql", fmt.Sprintf("%v:%v@/%v?parseTime=True&loc=Local", dbConfig.User, dbConfig.Password, dbConfig.Name))
	} else if config.Config.DB.Adapter == "postgres" {
		db, err = gorm.Open("postgres", fmt.Sprintf("user=%v password=%v dbname=%v sslmode=disable", dbConfig.User, dbConfig.Password, dbConfig.Name))
	} else {
		panic(errors.New("not supported database adapter"))
	}

	if err == nil {
		DB = &db
		Publish = publish.New(DB)
		config.Config.I18n = i18n.New(database.New(DB))

		l10n.RegisterCallbacks(DB)
		sorting.RegisterCallbacks(DB)
		validations.RegisterCallbacks(DB)
	} else {
		panic(err)
	}
}
Beispiel #2
0
func main() {
	roles.Register("admin", func(req *http.Request, cu qor.CurrentUser) bool {
		return true
	})
	mux := http.NewServeMux()
	adm := admin.New(&qor.Config{DB: &db.DB})

	adm.SetAuth(&Auth{})
	page := adm.AddResource(&db.Page{}, &admin.Config{Menu: []string{"Cms"}})
	// adm.AddResource(&db.Product{}, &admin.Config{Menu: []string{"Cms"}})
	page.Meta(&admin.Meta{
		Name:     "Section1",
		Type:     "rich_editor",
		Resource: adm.AddResource(&admin.AssetManager{}, &admin.Config{Invisible: true}),
	})

	page.Meta(&admin.Meta{Name: "Url", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		url := resource.(*db.Page).Url
		url = fmt.Sprintf(`<a href="%s/page%s">%s</a>`, config.CmsHost, url, url)
		return template.HTML(url)
	}})

	page.NewAttrs("-ProductCodes")
	page.EditAttrs("-ProductCodes")
	page.IndexAttrs("-ProductCodes")
	I18nBackend := database.New(&db.DB)
	// config.I18n = i18n.New(I18nBackend)
	adm.AddResource(i18n.New(I18nBackend), &admin.Config{Menu: []string{"系统设置"}, Invisible: true})

	adm.MountTo("/admin", mux)
	// frontend routes
	router := gin.Default()
	router.LoadHTMLGlob("templates/*")
	bindPages(router)
	router.StaticFS("/system/", http.Dir("public/system"))
	// books
	pageRoutes := router.Group("/page")
	{
		// listing
		// pageRoutes.GET("", controllers.ListBooksHandler)
		// pageRoutes.GET("/", controllers.ListBooksHandler)
		pageRoutes.GET("/:url", controllers.ViewPageHandler)
	}
	mux.Handle("/", router)

	log.Println("Starting Server at 9000.")
	err := http.ListenAndServe(":9000", mux)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Beispiel #3
0
func main() {
	roles.Register("admin", func(req *http.Request, cu qor.CurrentUser) bool {
		return true
	})

	adm := admin.New(&qor.Config{DB: &db.DB})

	adm.SetAuth(&Auth{})

	noUpdatePermission := roles.Deny(roles.Update, "admin")

	reportItem := adm.AddResource(&db.ReportItem{}, &admin.Config{Menu: []string{"查询"},
		Permission: roles.Deny(roles.Update, "admin").Deny(roles.Delete, "admin").Deny(roles.Create, "admin"),
	})
	reportItem.IndexAttrs("WhoHasThemName", "ClientName", "DeviceName", "DeviceCode", "Count")
	reportItem.Scope(&admin.Scope{
		Default: true,
		Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
			return db.Where("count > 0").Order("updated_at DESC")
		},
	})

	reportItem.Scope(&admin.Scope{
		Name:  "employee-take-outs",
		Label: "Employee Take Outs",
		Group: "设备状态",
		Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
			return db.Where("who_has_them_type = ?", "Employee")
		},
	})
	reportItem.Scope(&admin.Scope{
		Name:  "to-check-company",
		Label: "To Check Company",
		Group: "设备状态",
		Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
			return db.Where("who_has_them_type = ?", "DeviceCheckCompany")
		},
	})
	reportItem.Scope(&admin.Scope{
		Name:  "in-warehouse",
		Label: "In Warehouse",
		Group: "设备状态",
		Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
			return db.Where("who_has_them_type = ?", "Warehouse")
		},
	})

	reportItem.Meta(&admin.Meta{
		Name: "WhoHasThemName",
		Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
			ri := resource.(*db.ReportItem)
			name := ri.WhoHasThemName
			color := "black"
			switch ri.WhoHasThemType {
			case "Employee":
				color = "red"
			case "DeviceCheckCompany":
				color = "blue"
			}

			if color != "black" {
				name = fmt.Sprintf(`<strong style="color:%s">%s</string>`, color, name)
			}

			return template.HTML(name)
		},
	})

	cdIn := adm.AddResource(&db.ClientDeviceIn{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})

	cdIn.Meta(&admin.Meta{Name: "WarehouseID", Type: "select_one", Collection: db.WarehouseCollection})
	cdIn.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	// cdIn.Scope(&admin.Scope{
	// 	Default: true,
	// 	Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
	// 		return db.Preload("Device").Preload("Client")
	// 	},
	// })
	cdIn.IndexAttrs("ClientName", "DeviceName", "WarehouseName", "Quantity", "ByWhomName", "Date")
	cdIn.NewAttrs("ClientName", "DeviceName", "WarehouseID", "Quantity", "ByWhomID", "Date")
	cdIn.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ClientDeviceIn).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	cdOut := adm.AddResource(&db.ClientDeviceOut{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})

	cdOut.Meta(&admin.Meta{Name: "ReportItemID", Type: "select_one", Collection: db.CurrentClientDeviceCollection})
	cdOut.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	cdOut.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ClientDeviceOut).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	// cdOut.Scope(&admin.Scope{
	// 	Default: true,
	// 	Handle: func(db *gorm.DB, ctx *qor.Context) *gorm.DB {
	// 		return db.Preload("Device").Preload("Client")
	// 	},
	// })
	cdOut.IndexAttrs("ClientName", "DeviceName", "Quantity", "WarehouseName", "ByWhomName", "Date")
	cdOut.NewAttrs("ReportItemID", "ByWhomID", "Date")

	deviceOut := adm.AddResource(&db.DeviceOut{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})

	deviceOut.IndexAttrs("ToWhomName", "FromWarehouseName", "DeviceName", "Quantity", "ByWhomName", "Date")
	deviceOut.NewAttrs("FromReportItemID", "Quantity", "ToWhomID", "ByWhomID", "Date")
	deviceOut.Meta(&admin.Meta{Name: "FromReportItemID", Type: "select_one", Collection: db.CurrentWarehouseDeviceCollection})
	deviceOut.Meta(&admin.Meta{Name: "ToWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	deviceOut.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	deviceOut.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.DeviceOut).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	deviceIn := adm.AddResource(&db.DeviceIn{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})
	deviceIn.IndexAttrs("FromWhomName", "ToWarehouseName", "DeviceName", "Quantity", "ByWhomName", "Date")
	deviceIn.NewAttrs("FromReportItemID", "Quantity", "ToWarehouseID", "ByWhomID", "Date")
	deviceIn.Meta(&admin.Meta{Name: "FromReportItemID", Type: "select_one", Collection: db.CurrentEmployeeDeviceCollection})
	deviceIn.Meta(&admin.Meta{Name: "ToWarehouseID", Type: "select_one", Collection: db.WarehouseCollection})
	deviceIn.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	deviceIn.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.DeviceIn).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	deviceCheckOut := adm.AddResource(&db.ClientDeviceCheckOut{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})
	deviceCheckOut.IndexAttrs("FromWarehouseName", "ToDeviceCheckCompanyName", "DeviceName", "ClientName", "Quantity", "ByWhomName", "Date")
	deviceCheckOut.NewAttrs("FromReportItemID", "Quantity", "ToDeviceCheckCompanyID", "ByWhomID", "Date")
	deviceCheckOut.Meta(&admin.Meta{Name: "FromReportItemID", Type: "select_one", Collection: db.CurrentClientDeviceCollection})
	deviceCheckOut.Meta(&admin.Meta{Name: "ToDeviceCheckCompanyID", Type: "select_one", Collection: db.DeviceCheckCompanyCollection})
	deviceCheckOut.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	deviceCheckOut.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ClientDeviceCheckOut).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	deviceCheckIn := adm.AddResource(&db.ClientDeviceCheckIn{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})

	deviceCheckIn.IndexAttrs("FromDeviceCheckCompanyName", "ToWarehouseName", "DeviceName", "Quantity", "ByWhomName", "Date")
	deviceCheckIn.NewAttrs("FromReportItemID", "Quantity", "ToWarehouseID", "ByWhomID", "Date")
	deviceCheckIn.Meta(&admin.Meta{Name: "FromReportItemID", Type: "select_one", Collection: db.CurrentDeviceCheckCollection})
	deviceCheckIn.Meta(&admin.Meta{Name: "ToWarehouseID", Type: "select_one", Collection: db.WarehouseCollection})
	deviceCheckIn.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	deviceCheckIn.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ClientDeviceCheckIn).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	consumableOut := adm.AddResource(&db.ConsumableOut{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})
	consumableOut.IndexAttrs("DeviceName", "Quantity", "ToWhomName", "WarehouseName", "ByWhomName", "Date")
	consumableOut.NewAttrs("ReportItemID", "Quantity", "ToWhomID", "ByWhomID", "Date")
	consumableOut.Meta(&admin.Meta{Name: "ReportItemID", Type: "select_one", Collection: db.CurrentConsumableCollection})
	consumableOut.Meta(&admin.Meta{Name: "ToWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	consumableOut.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	consumableOut.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ConsumableOut).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	consumableIn := adm.AddResource(&db.ConsumableIn{}, &admin.Config{
		Menu:       []string{"日常操作"},
		Permission: noUpdatePermission,
	})

	consumableIn.IndexAttrs("DeviceName", "Quantity", "WarehouseName", "ByWhomName", "Date")
	consumableIn.NewAttrs("ReportItemID", "Quantity", "ToWhomID", "ByWhomID", "Date")
	consumableIn.Meta(&admin.Meta{Name: "ReportItemID", Type: "select_one", Collection: db.CurrentConsumableCollection})
	consumableIn.Meta(&admin.Meta{Name: "ByWhomID", Type: "select_one", Collection: db.EmployeeCollection})
	consumableIn.Meta(&admin.Meta{Name: "Date", Valuer: func(resource interface{}, ctx *qor.Context) interface{} {
		date := resource.(*db.ConsumableIn).Date
		if date.IsZero() {
			date = time.Now()
		}
		return date
	}})

	device := adm.AddResource(&db.Device{}, &admin.Config{Menu: []string{"数据维护"}})
	device.Meta(&admin.Meta{Name: "CategoryID", Type: "select_one", Collection: db.DeviceCategories})
	device.Meta(&admin.Meta{Name: "WarehouseID", Type: "select_one", Collection: db.WarehouseCollection})
	device.Meta(&admin.Meta{Name: "Note", Type: "rich_editor"})
	device.EditAttrs("Name", "Code", "TotalQuantity", "TypeSize", "UnitName", "MakerName", "MakeDate", "FromSource", "Note")
	device.NewAttrs("Name", "Code", "CategoryID", "TotalQuantity", "WarehouseID", "TypeSize", "UnitName", "MakerName", "MakeDate", "FromSource", "Note")
	device.IndexAttrs("Name", "Code", "CategoryName", "TypeSize", "MakerName", "TotalQuantity")

	employee := adm.AddResource(&db.Employee{}, &admin.Config{Menu: []string{"数据维护"}})
	employee.IndexAttrs("Name", "Title", "Mobile")

	warehouse := adm.AddResource(&db.Warehouse{}, &admin.Config{Menu: []string{"数据维护"}})
	warehouse.Meta(&admin.Meta{Name: "Name", Type: "string"})
	warehouse.Meta(&admin.Meta{Name: "Address", Type: "string"})
	warehouse.EditAttrs("Name", "Address")
	warehouse.NewAttrs(warehouse.EditAttrs())
	warehouse.IndexAttrs("Name", "Address")

	deviceCheckCompany := adm.AddResource(&db.DeviceCheckCompany{}, &admin.Config{Menu: []string{"数据维护"}})
	deviceCheckCompany.Meta(&admin.Meta{Name: "Name", Type: "string"})
	deviceCheckCompany.Meta(&admin.Meta{Name: "Address", Type: "string"})
	deviceCheckCompany.EditAttrs("Name", "Address")
	deviceCheckCompany.NewAttrs(warehouse.EditAttrs())
	deviceCheckCompany.IndexAttrs("Name", "Address")

	I18nBackend := database.New(&db.DB)
	// config.I18n = i18n.New(I18nBackend)
	adm.AddResource(i18n.New(I18nBackend), &admin.Config{Menu: []string{"系统设置"}, Invisible: true})

	adm.MountTo("/admin", http.DefaultServeMux)
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/admin/report_items", 302)
	})

	log.Println("Starting Server at 9000.")
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Beispiel #4
0
func init() {
	// setting up QOR admin
	Admin = admin.New(&qor.Config{DB: Pub.DraftDB()})
	Admin.AddResource(Pub)
	Admin.SetAuth(&Auth{})

	I18n = i18n.New(database.New(StagingDB))
	Admin.AddResource(I18n)

	roles.Register("admin", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "admin" {
			return true
		}

		return false
	})

	roles.Register("user", func(req *http.Request, currentUser qor.CurrentUser) bool {
		if currentUser == nil {
			return false
		}

		if currentUser.(*User).Role == "user" {
			return true
		}

		return false
	})

	user := Admin.AddResource(&User{})

	user.Meta(&admin.Meta{
		Name:  "UserRole",
		Label: "Role",
		//Type:  "select_one",
		Collection: func(resource interface{}, context *qor.Context) (results [][]string) {
			return [][]string{
				{"admin", "admin"},
				{"user", "user"},
			}
		},
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			if value == nil {
				return value
			}
			user := value.(*User)
			log.Println("user", user.Role)
			return user.Role
		},
	})

	user.IndexAttrs("ID", "Name", "Role")
	user.NewAttrs("Name", "UserRole")
	user.EditAttrs("Name", "UserRole")

	author := Admin.AddResource(&Author{})

	author.IndexAttrs("ID", "Name")
	author.SearchAttrs("ID", "Name")

	book := Admin.AddResource(&Book{})

	book.Meta(&admin.Meta{
		Name:  "FormattedDate",
		Label: "Release Date",
		Valuer: func(value interface{}, context *qor.Context) interface{} {
			book := value.(*Book)
			return book.ReleaseDate.Format("Jan 2, 2006")
		},
	})

	book.Meta(&admin.Meta{
		Name: "Synopsis",
		Type: "rich_editor",
	})

	// which fields should be displayed in the books list on admin
	book.IndexAttrs("ID", "Title", "Authors", "FormattedDate", "Price")
	// which fields should be editable in the book esit interface
	book.NewAttrs("Title", "Authors", "Synopsis", "ReleaseDate", "Price", "CoverImage")
	book.EditAttrs("Title", "Authors", "Synopsis", "ReleaseDate", "Price", "CoverImage")
	// which fields should be searched when using the item search on the list view
	book.SearchAttrs("ID", "Title")
}
Beispiel #5
0
func main() {
	Admin := admin.New(&qor.Config{DB: Publish.DraftDB()})
	Admin.SetAuth(&Auth{})

	Admin.AddMenu(&admin.Menu{Name: "Dashboard", Link: "/admin"})

	creditcard := Admin.AddResource(&CreditCard{}, &admin.Config{Invisible: true})
	creditcard.Meta(&admin.Meta{Name: "issuer", Type: "select_one", Collection: []string{"VISA", "MasterCard", "UnionPay", "JCB", "American Express", "Diners Club"}})

	user := Admin.AddResource(&User{}, &admin.Config{Menu: []string{"Users Management"}})

	user.Scope(&admin.Scope{Name: "active", Group: "Status", Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB {
		return db.Where("state = ?", "Active")
	}})
	user.Scope(&admin.Scope{Name: "inactive", Group: "Status", Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB {
		return db.Where("state = ?", "Inactive")
	}})
	user.Scope(&admin.Scope{Name: "enabled", Handle: func(db *gorm.DB, context *qor.Context) *gorm.DB { return db }})

	user.Meta(&admin.Meta{Name: "CreditCard", Resource: creditcard})
	user.Meta(&admin.Meta{Name: "fullname", Alias: "name"})

	user.Meta(&admin.Meta{Name: "State", Type: "select_one", Collection: []string{"Active", "Inactive"}})
	user.Meta(&admin.Meta{Name: "Role", Type: "select_one"})

	user.Meta(&admin.Meta{Name: "Languages", Type: "select_many"})
	Asset := Admin.AddResource(&admin.AssetManager{})
	user.Meta(&admin.Meta{Name: "description", Type: "rich_editor", Resource: Asset})

	Admin.AddResource(&Language{}, &admin.Config{Name: "Locales", Menu: []string{"Products Management"}})

	product := Admin.AddResource(&Product{}, &admin.Config{Menu: []string{"Products Management"}})
	product.SearchAttrs("Name", "Description")
	product.Meta(&admin.Meta{Name: "Collection", Type: "select_one"})

	Admin.AddResource(&Collection{}, &admin.Config{Menu: []string{"Products Management"}})
	Admin.AddResource(&Order{}, &admin.Config{Menu: []string{"Orders Management"}})

	I18n := i18n.New(database.New(Publish.DraftDB()))
	Admin.AddResource(I18n)

	Admin.AddResource(Publish)

	mux := http.NewServeMux()
	Admin.MountTo("/admin", mux)
	mux.Handle("/system/", http.FileServer(http.Dir("public")))
	mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		var user User

		if request.Method == "POST" {
			request.ParseForm()
			if !DB.First(&user, "name = ?", request.Form.Get("username")).RecordNotFound() {
				cookie := http.Cookie{Name: "userid", Value: fmt.Sprintf("%v", user.ID), Expires: time.Now().AddDate(1, 0, 0)}
				http.SetCookie(writer, &cookie)
				writer.Write([]byte("<html><body>logged as `" + user.Name + "`, go <a href='/admin'>admin</a></body></html>"))
			} else {
				http.Redirect(writer, request, "/login?failed_to_login", 301)
			}
		} else if userid, err := request.Cookie("userid"); err == nil {
			if !DB.First(&user, "id = ?", userid.Value).RecordNotFound() {
				writer.Write([]byte("<html><body>already logged as `" + user.Name + "`, go <a href='/admin'>admin</a></body></html>"))
			} else {
				http.Redirect(writer, request, "/logout", http.StatusSeeOther)
			}
		} else {
			writer.Write([]byte(`<html><form action="/login" method="POST"><input name="username" value="" placeholder="username"><input type=submit value="Login"></form></html>`))
		}
	})

	mux.HandleFunc("/logout", func(writer http.ResponseWriter, request *http.Request) {
		cookie := http.Cookie{Name: "userid", MaxAge: -1}
		http.SetCookie(writer, &cookie)
		http.Redirect(writer, request, "/login?logged_out", http.StatusSeeOther)
	})

	fmt.Println("listening on :9000")
	http.ListenAndServe(":9000", mux)
}