• Stars
    star
    898
  • Rank 50,853 (Top 2 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Qor Admin - Instantly create a beautiful, cross platform, configurable Admin Interface and API for managing your data in minutes.

QOR Admin

Instantly create a beautiful, cross platform, configurable Admin Interface and API for managing your data in minutes.

GoDoc Build Status

For security issues, please send us an email to [email protected] and give us time to respond BEFORE posting as an issue or reporting on public forums.

Features

  • Generate Admin Interface for managing data
  • RESTFul JSON API
  • Association handling
  • Search and filtering
  • Actions/Batch Actions
  • Authentication and Authorization
  • Extendability

Quick Start

package main

import (
  "fmt"
  "net/http"
  "github.com/jinzhu/gorm"
  _ "github.com/mattn/go-sqlite3"
  "github.com/qor/admin"
)

// Create a GORM-backend model
type User struct {
  gorm.Model
  Name string
}

// Create another GORM-backend model
type Product struct {
  gorm.Model
  Name        string
  Description string
}

func main() {
  DB, _ := gorm.Open("sqlite3", "demo.db")
  DB.AutoMigrate(&User{}, &Product{})

  // Initialize
  Admin := admin.New(&admin.AdminConfig{DB: DB})

  // Allow to use Admin to manage User, Product
  Admin.AddResource(&User{})
  Admin.AddResource(&Product{})

  // initialize an HTTP request multiplexer
  mux := http.NewServeMux()

  // Mount admin interface to mux
  Admin.MountTo("/admin", mux)

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

go run main.go and visit localhost:9000/admin to see the result!

How to use remoteSelector with publish2.version integrated record

For has many relationship

Suppose we have 2 models Factory and Item. Factory has many Items.

In the struct, you need add a field resource.CompositePrimaryKeyField to the "many" side, which is Item here.

type Factory struct {
	gorm.Model
	Name string

	publish2.Version
	Items       []Item `gorm:"many2many:factory_items;association_autoupdate:false"`
	ItemsSorter sorting.SortableCollection
}

type Item struct {
	gorm.Model
	Name string
	publish2.Version

	// github.com/qor/qor/resource
	resource.CompositePrimaryKeyField // Required
}

Then define a remote resource selector. You need configure the ID meta like below to make it support composite primary key, this is mandatory.

func generateRemoteItemSelector(adm *admin.Admin) (res *admin.Resource) {
	res = adm.AddResource(&Item{}, &admin.Config{Name: "ItemSelector"})
	res.IndexAttrs("ID", "Name")

	// Required. Convert single ID into composite primary key
	res.Meta(&admin.Meta{
	Name: "ID",
	Valuer: func(value interface{}, ctx *qor.Context) interface{} {
		if r, ok := value.(*Item); ok {
			// github.com/qor/qor/resource
			return resource.GenCompositePrimaryKey(r.ID, r.GetVersionName())
		}
		return ""
	},
	})

	return res
}

Last, use it in the Factory resource.

itemSelector := generateRemoteItemSelector(adm)
factoryRes.Meta(&admin.Meta{
	Name: "Items",
	Config: &admin.SelectManyConfig{
	RemoteDataResource: itemSelector,
	},
})

For has one relationship

Suppose we have 2 models. Factory and Manager. Factory has one Manager.

First, In the struct, you need add a field resource.CompositePrimaryKeyField to the "one" side, which is Manager here.

type Factory struct {
	gorm.Model
	Name string
	publish2.Version

	ManagerID          uint
	ManagerVersionName string // Required. in "xxxVersionName" format.
	Manager            Manager
}

type Manager struct {
	gorm.Model
	Name string
	publish2.Version

	// github.com/qor/qor/resource
	resource.CompositePrimaryKeyField // Required
}

Then define a remote resource selector. You need configure the ID meta like below to make it support composite primary key, this is mandatory.

func generateRemoteManagerSelector(adm *admin.Admin) (res *admin.Resource) {
	res = adm.AddResource(&Manager{}, &admin.Config{Name: "ManagerSelector"})
	res.IndexAttrs("ID", "Name")

	// Required. Convert single ID into composite primary key
	res.Meta(&admin.Meta{
		Name: "ID",
		Valuer: func(value interface{}, ctx *qor.Context) interface{} {
			if r, ok := value.(*Manager); ok {
				// github.com/qor/qor/resource
				return resource.GenCompositePrimaryKey(r.ID, r.GetVersionName())
			}
			return ""
		},
	})

	return res
}

Last, use it in the Factory resource.
```go
managerSelector := generateRemoteManagerSelector(adm)
factoryRes.Meta(&admin.Meta{
	Name: "Manager",
	Config: &admin.SelectOneConfig{
		RemoteDataResource: managerSelector,
	},
})

If you need to overwrite Collection. you have to pass composite primary key as the first element of the returning array instead of ID.

factoryRes.Meta(&admin.Meta{
  Name: "Items",
  Config: &admin.SelectManyConfig{
	Collection: func(value interface{}, ctx *qor.Context) (results [][]string) {
		if c, ok := value.(*Factory); ok {
		var items []Item
		ctx.GetDB().Model(c).Related(&items, "Items")

		for _, p := range items {
		// The first element must be the composite primary key instead of ID
		results = append(results, []string{resource.GenCompositePrimaryKey(p.ID, p.GetVersionName()), p.Name})
		}
		}
		return
	},
	RemoteDataResource: itemSelector,
  },
})

To support assign associations when creating a new version

If you want to assign associations when creating a new version of object immediately. You need to define a function called AssignVersionName to the versioned struct with pointer receiver which should contains the generating new version name's logic and assign the new version name to the object. e.g.

func (fac *Factory) AssignVersionName(db *gorm.DB) {
	var count int
	name := time.Now().Format("2006-01-02")
	if err := db.Model(&CollectionWithVersion{}).Where("id = ? AND version_name like ?", fac.ID, name+"%").Count(&count).Error; err != nil {
    panic(err)
  }
	fac.VersionName = fmt.Sprintf("%s-v%v", name, count+1)
}

Live DEMO

Documentation

https://doc.getqor.com/admin

To print all registered routes

// adm is a QOR admin instance
adm.GetRouter().PrintRoutes()

ViewPath Note

QOR was developed before go mod was introduced. So it still support go path while finding its template files. The priority is

  1. check vendor, if not found
  2. check $GOPATH/pkg/mod/github.com/qor/[email protected]/views. the version would be detected automatically by your go.mod file, if still not found
  3. load view path from $GOPATH/src/github.com/qor/admin/views

So if you want to use the template under the pkg/mod, make sure $GOPATH/src/github.com/qor/admin is absent.

License

Released under the MIT License.

More Repositories

1

qor

QOR is a set of libraries written in Go that abstracts common features needed for business applications, CMSs, and E-commerce systems.
Go
5,198
star
2

qor-example

An example application showcasing the QOR SDK
Go
1,241
star
3

auth

Golang Authentication solution
Go
677
star
4

transition

Transition is a Golang state machine implementation
Go
425
star
5

roles

Roles is an authorization library for Golang
Go
140
star
6

validations

Validations is a GORM extension, used to validate models when creating, updating
Go
128
star
7

i18n

I18n is a golang implementation, provides internationalization support for your application, with different backends support
Go
105
star
8

worker

Worker run jobs in background at scheduled time
Go
61
star
9

media

Media add uploading files to cloud or other destinations with support for image cropping and resizing features to any structs
JavaScript
60
star
10

oss

QOR OSS provides common interface to operate files in cloud storage/filesystem
Go
57
star
11

amazon-pay-sdk-go

Amazon Pay Go SDK
Go
31
star
12

mailer

Mail solution
Go
23
star
13

audited

Audited is used to log last UpdatedBy and CreatedBy for your models
Go
21
star
14

auth_themes

Auth Themes
Go
20
star
15

media_library

Abandoned, use https://github.com/qor/media instead
Go
20
star
16

bindatafs

Compile QOR templates into binary with go-bindata
Go
19
star
17

l10n

L10n make your resources(models) be able to localize into different locales
Go
18
star
18

exchange

QOR exchange provides conversion (import/export) functionality for any Qor.Resource to CSV, Excel file
Go
17
star
19

gomerchant

Stripe, Paygent, and Amazon Pay Adaptors
Go
17
star
20

render

Render Templates
Go
16
star
21

seo

SEO module for QOR3
Go
14
star
22

sorting

Sorting: adds sorting and reordering abilities to your models.
JavaScript
12
star
23

publish2

Version Control with Schedule
Go
11
star
24

widget

Qor Widget - Define some customizable, shareable HTML widgets for different pages
Go
11
star
25

session

Session management
Go
8
star
26

filebox

Filebox could be used to provide access permission control for files, directories
Go
8
star
27

doc

QOR3 documentation
CSS
6
star
28

publish

Publish allow user update a resource but do not show the change in website until it is get "published" for GORM-backend models
Go
6
star
29

activity

Qor Activity: add Comment and Track data/state changes to any Qor Resource support to admin interface
Go
6
star
30

notification

QOR Notification
Go
6
star
31

slug

Slug is an extension for qor.
JavaScript
6
star
32

responder

Responder: Respond differently according to request's accepted mime type
Go
6
star
33

location

Qor Location - Make your struct support pick up location from google map in Qor Admin
JavaScript
5
star
34

assetfs

AssetFileSystem
Go
5
star
35

middlewares

Middlewares Management
Go
5
star
36

serializable_meta

Serializable meta
Go
4
star
37

action_bar

Action Bar in QOR3
Go
4
star
38

cache

Cache Store
Go
4
star
39

redirect_back

A Golang HTTP Handler that redirect back to last URL saved in session
Go
4
star
40

page_builder

Page Builder
JavaScript
3
star
41

metas

Meta Types for Admin
JavaScript
3
star
42

log

Qor Logger
Go
3
star
43

help

Help for QOR ADMIN
JavaScript
3
star
44

banner_editor

Banner Editor in QOR3
JavaScript
2
star
45

application

Application
Go
2
star
46

qor-example-cases

Learn QOR3 by examples
Go
2
star
47

wildcard_router

WildcardRouter handles dynamic routes
Go
2
star
48

variations

Variations
JavaScript
2
star
49

app

App Generator - WIP
Swift
1
star