gadmin

package module
v0.0.0-...-214a850 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 23 Imported by: 0

README

gadmin

gadmin solves the boring problem of building an admin interface on top of an existing data model. With little effort, it lets you manage your web service’s data through a user-friendly interface.

How does it work?

The basic concept behind gadmin, is that it lets you build complicated interfaces by grouping individual views together in classes: Each web page you see on the frontend, represents a method on a class that has explicitly been added to the interface.

We write gadmin in Go, depend on net/http, template/html and gorm.

Example

  • Mmodel view
mv := gadmin.NewModelView(User{}).
    SetColumnList("type", "first_name", "last_name", "email", "ip_address", "currency", "timezone", "phone_number").
    SetColumnEditableList("first_name", "type", "currency", "timezone").
    SetColumnDescriptions(map[string]string{"first_name": "First Name"}).
    SetCanSetPageSize(true).
    SetPageSize(5).
    SetTablePrefixHtml(`<h4>Some Caution</h4>`)
admin.AddView(mv)
  • View

Flask-Admin

Inspired by Flask-Admin. Copy the template files from flask-admin(bootstrap4 only).

Status

Demo

Documentation

Overview

Package reformism provides several utility functions for native text/template

Index

Constants

This section is empty.

Variables

View Source
var (
	ContentTypeJson     = "application/json; charset=utf-8"
	ContentTypeUtf8Html = "text/html; charset=utf-8"
)
View Source
var Funcs = template.FuncMap{
	"arg":     witharg,
	"require": requireArg,
	"done":    done,
	"args":    args,
	"slice":   makeSlice,
	"map":     makeMap,
	"seq":     makeSequence,
	"append":  appendSlice,
	"split":   splitStr,
	"join":    joinStr,
	"only":    only,
	"delete":  deleteItem,
	"set":     mapSet,
	"default": defaultOf,
	"log":     logto,

	"add": func(is ...int) int {
		var a int = 0
		for _, b := range is {
			a += b
		}
		return a
	},
	"sub": func(a, b int) int { return a - b },
}

Funcs is a FuncMap which can be passed as argument of .Func of text/template

Functions

func Flash

func Flash(r *http.Request, data any, category ...string)

`Flash` Mock flask.flash `get_flashed_messages` as `FlashedFrom(r).GetMessages()`

func FlashedFrom

func FlashedFrom(r *http.Request) *_Flashed

func PatchFlashed

func PatchFlashed(r *http.Request) *http.Request

Inject Flashed into cloned Request

func ReplyJson

func ReplyJson(w http.ResponseWriter, status int, obj any)

Types

type Admin

type Admin struct {
	*Blueprint

	DB *gorm.DB
	// contains filtered or unexported fields
}

func NewAdmin

func NewAdmin(name string, db *gorm.DB) *Admin

func (*Admin) AddCategory

func (A *Admin) AddCategory(cate string)
func (A *Admin) AddLink(cate, name, path string)

func (*Admin) AddMenuItem

func (A *Admin) AddMenuItem(mi MenuItem)

func (*Admin) AddView

func (A *Admin) AddView(view View) View

func (*Admin) Run

func (A *Admin) Run()

func (*Admin) UrlFor

func (A *Admin) UrlFor(model, endpoint string, args ...any) (string, error)

Flask.url_for, `endpoint` like: admin.index model.create_view .create_view

type BaseView

type BaseView struct {
	*Blueprint
	// contains filtered or unexported fields
}

func NewView

func NewView(menu MenuItem) *BaseView

func (*BaseView) Expose

func (V *BaseView) Expose(path string, h http.HandlerFunc)

Expose "/test" create Blueprint{Endpoint: "test", Path: "/test"}

func (*BaseView) GetBlueprint

func (V *BaseView) GetBlueprint() *Blueprint

func (*BaseView) GetMenu

func (V *BaseView) GetMenu() *MenuItem

func (*BaseView) GetUrl

func (V *BaseView) GetUrl(ep string, q *Query, args ...any) string

TODO: move query into `ModelView`

func (*BaseView) IsAccessible

func (V *BaseView) IsAccessible() bool

func (*BaseView) IsVisible

func (V *BaseView) IsVisible() bool

func (*BaseView) Render

func (V *BaseView) Render(w http.ResponseWriter, r *http.Request, name string, funcs template.FuncMap, data map[string]any)

type Blueprint

type Blueprint struct {
	Endpoint string                // {foo}.index
	Path     string                // /foo
	Children map[string]*Blueprint // endpoint => *Blueprint
	Name     string                // Foo
	Handler  http.HandlerFunc
	Register RegisterFunc // Custom register to mux, serve static file

}

like flask.Blueprint

| Name | Endpoint | Path | |-------|----------------|------------| | Foo | foo | foo | | | .index | / | | | .action_view | /action | | | foo.index | foo/ | | Admin | admin | /admin | | | .index | / |

A blueprint is A model and dependent pages

func (*Blueprint) Add

func (B *Blueprint) Add(child *Blueprint)

like flask `Blueprint.Register`

func (*Blueprint) GetUrl

func (B *Blueprint) GetUrl(endpoint string, qs ...url.Values) (string, error)

func (*Blueprint) RegisterTo

func (B *Blueprint) RegisterTo(mux *http.ServeMux, path string)

Add `Blueprint` to `http.ServeMux`

type Menu []*MenuItem
func (M *Menu) Add(m *MenuItem)
type MenuItem struct {
	Category string // parent item Name
	Name     string
	Path     string

	Icon  string
	Class string

	IsActive     bool
	IsVisible    bool
	IsAccessible bool

	Children []*MenuItem
}

Tree liked structure

type ModelView

type ModelView struct {
	*BaseView
	// contains filtered or unexported fields
}

func NewModelView

func NewModelView(m any, category ...string) *ModelView

TODO: ensure m not ptr

func (*ModelView) Render

func (mv *ModelView) Render(w http.ResponseWriter, r *http.Request, name string, funcs template.FuncMap, data map[string]any)

func (*ModelView) SetCanCreate

func (mv *ModelView) SetCanCreate(v bool) *ModelView

Permissions Is model creation allowed

func (*ModelView) SetCanEdit

func (mv *ModelView) SetCanEdit(v bool) *ModelView

Is model editing allowed

func (*ModelView) SetCanExport

func (mv *ModelView) SetCanExport(v bool) *ModelView

func (*ModelView) SetCanSetPageSize

func (mv *ModelView) SetCanSetPageSize(v bool) *ModelView

func (*ModelView) SetColumnDescriptions

func (mv *ModelView) SetColumnDescriptions(m map[string]string) *ModelView

func (*ModelView) SetColumnEditableList

func (mv *ModelView) SetColumnEditableList(vs ...string) *ModelView

func (*ModelView) SetColumnList

func (mv *ModelView) SetColumnList(vs ...string) *ModelView

Collection of the model field names for the list view. If not set, will get them from the model.

func (*ModelView) SetPageSize

func (mv *ModelView) SetPageSize(v int) *ModelView

func (*ModelView) SetTablePrefixHtml

func (mv *ModelView) SetTablePrefixHtml(v string) *ModelView

type Pack

type Pack struct {
	Origin any
	Args   map[string]any
}

Pack represents packed arguments and original dot

type Query

type Query struct {
	// 0 based with omit
	Page     int `form:"page,omitempty"`
	PageSize int `form:"page_size,omitempty"`
	// column index: 0,1,... maybe `null.String` is better
	Sort string `form:"sort,omitempty"`
	// desc or asc, default is asc
	Desc   bool   `form:"desc,omitempty"`
	Search string `form:"search,omitempty"`
	// contains filtered or unexported fields
}

func (*Query) Get

func (q *Query) Get(arg string) string

type RegisterFunc

type RegisterFunc func(*http.ServeMux, string, *Blueprint)

type View

type View interface {
	// Add custom handler, eg: /admin/{model}/path
	Expose(path string, h http.HandlerFunc)

	// Generate URL for the endpoint.
	// In model view, return {model}/{action}
	GetUrl(ep string, q *Query, args ...any) string

	GetBlueprint() *Blueprint
	GetMenu() *MenuItem

	// Override this method if you want dynamically hide or show
	// administrative views from Flask-Admin menu structure
	IsVisible() bool

	// Override this method to add permission checks.
	IsAccessible() bool
	Render(http.ResponseWriter, *http.Request, string, template.FuncMap, map[string]any)
}

type XEditableWidget

type XEditableWidget struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL