views

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2023 License: BSD-1-Clause Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TAG_DEFAULT_DELIMITER_KEYVALUE = ";"
	TAG_DEFAULT_DELIMITER_KEY      = "="
	TAG_DEFAULT_DELIMITER_VALUE    = ","
)

Functions

This section is empty.

Types

type BaseFormView

type BaseFormView[T interfaces.Saver] struct {
	// The action of the form
	//
	// This will be used in mainly error messages.
	//
	// Example: "create", "view", "update"
	Action string

	// A fallback url for when an error occurs, and the form is submitted.
	BackURL func(r *request.Request) string
	// The success url for when the form is submitted.
	PostRedirect func(r *request.Request, data T) string
	// Extra function to run for authentication.
	//
	// This will be run before the form is rendered.
	ExtraAuth func(r *request.Request) error
	// The required permissions needed to perform an action on the model.
	NeedsAuth bool
	// Needs admin permissions to perform an action on the model.
	NeedsAdmin bool
	// Whether a superuser can perform the action.
	SuperUserCanPerform bool
	// Function to run before rendering a template.
	BeforeRender func(r *request.Request, data T, fieldMap *orderedmap.Map[string, *FormField])
	// Function to run on submission, when overridden; the default save method will not be called.
	OnSubmit func(r *request.Request, data T) error
	// The form data for when the form is submitted.
	//
	// This will only get filled on the POST method!
	FormData map[string]FormData
	// MaxMemory is the maximum memory used when parsing the files of a request.
	MaxMemory int64 // defaults to 32MB
	// Override the get and post methods.
	GET  func(r *request.Request) error
	POST func(r *request.Request, data T) error
	// Template to pass the template variables to.
	Template string
	// Function to get a template.
	GetTemplate func(string) (*template.Template, string, error)
	// The instance of the model passed in.
	//
	// This will be automatically filled in with the model passed in.
	//
	// Do not edit this before the POST method is called!
	Instance T
	// The function to get the instance of the model passed in.
	//
	// This will set the instance to the result of the function.
	GetInstance func(r *request.Request) (T, error)

	// Function to run after the form is submitted and the view specified has been called.
	//
	// This will only get called on the POST method!
	AfterSubmit func(r *request.Request, data T)

	// The tag to use to fetch attributes from struct fields.
	//
	// Defaults to "fields".
	FormTag string

	// Javascript to include in the template.
	//
	// You must include the <script> tags.
	Scripts map[string]template.HTML
	// contains filtered or unexported fields
}

A FormView is a view that renders a form and handles the submission of that form.

It will fill in the values of the form based on the model passed in.

The default post method will call the Save method on the model passed in.

Default tag for the formfields is "fields", this can be changed by setting the FormTag field.

func (*BaseFormView[T]) Save

func (c *BaseFormView[T]) Save(r *request.Request) error

func (*BaseFormView[T]) Serve

func (c *BaseFormView[T]) Serve(r *request.Request)

func (*BaseFormView[T]) WithFields

func (c *BaseFormView[T]) WithFields(fields ...string) error

The fields to use with the form

If the field cannot be found on the struct, we will check the instance's struct methods.

This method must implement one of the following signatures:

func() interface{}
func() (interface{}, tags.TagMap)
func() (interface{}, tags.TagMap, error)

type BaseView

type BaseView[MODEL any] struct {
	// The template to use
	Template string

	// Function to get a template with
	GetTemplate func(string) (*template.Template, string, error)

	// The URL to redirect to after a failed action.
	BackURL func(r *request.Request, model MODEL) string

	// The URL to redirect to after a successful action.
	SuccessURL func(r *request.Request, model MODEL) string

	// The permissions required to perform an action on the model
	//
	// If there is a permission registered, the user must be authenticated!
	RequiredPerms []string

	// NeedsAuth specifies whether the user needs to be authenticated to view the page.
	NeedsAuth bool

	// Needs admin permissions to perform an action on the model.
	NeedsAdmin bool

	// Whether a superuser can perform the action.
	SuperUserCanPerform bool

	// The action to perform on the model.
	//
	// This is used to display the correct message to the user.
	//
	// For example: "delete", "update" or "view"
	Action string

	GetQuerySet func(r *request.Request) (MODEL, error)

	// Overrides for the get and post functions.
	Get  func(r *request.Request, m MODEL)
	Post func(r *request.Request, m MODEL)

	// Extra func to run on the data.
	//
	// This can be used for validation, returning an error will make the request error.
	//
	// With the error message.
	Extra func(r *request.Request, model MODEL) error

	// Extra auth func to run on the data.
	//
	// This can be used for validation, returning an error will make the request error.
	//
	// With the error message.
	ExtraAuth func(r *request.Request, model MODEL) error
}

func (*BaseView[T]) Serve

func (v *BaseView[T]) Serve(r *request.Request)

type CRUDView

type CRUDView[T CrudModel[T]] struct {
	// DeleteView
	Delete *DeleteView[T]

	// UpdateView
	Update *UpdateView[T]

	// CreateView
	Create *CreateView[T]

	// ListView
	List *ListView[T]

	// DetailView
	Detail *DetailView[T]
}

func (*CRUDView[T]) URLs

func (c *CRUDView[T]) URLs() router.Registrar

type CreateView

type CreateView[T interfaces.Saver] struct {
	BaseFormView[T]
	Fields []string
}

func (*CreateView[T]) ServeHTTP

func (c *CreateView[T]) ServeHTTP(r *request.Request)

type CrudModel

type CrudModel[T any] interface {
	// Reader
	interfaces.Deleter
	interfaces.Saver
	interfaces.Lister[T]
}

type DeleteView

type DeleteView[model interfaces.Deleter] struct {
	BaseView[model]
}

func (*DeleteView[model]) ServeHTTP

func (d *DeleteView[model]) ServeHTTP(r *request.Request)

type DetailView

type DetailView[T any] struct {
	BaseView[T]

	DeleteURL func(r *request.Request, model T) string
	EditURL   func(r *request.Request, model T) string
}

func (*DetailView[T]) ServeHTTP

func (d *DetailView[T]) ServeHTTP(r *request.Request)

type FormData

type FormData struct {
	Values []string
	Files  []interfaces.File
}

type FormField

type FormField struct {
	Field interfaces.FormField
	Tags  tags.TagMap

	Label string
	// contains filtered or unexported fields
}

type FormGroup

type FormGroup struct {
	Label interfaces.Element
	Input interfaces.Element
}

type ListView

type ListView[T interfaces.Lister[T]] struct {
	BaseView[[]T]

	// Fallback number of items per page (When an error occurs for example)
	//
	// This is also the default.
	//
	// This is used when the user doesn't specify the number of items per page.
	FallbackPerPage int

	// Maximum number of items per page.
	//
	// This is used when the user specifies a number of items per page that is
	// greater than this value.
	MaxPerPage int

	GetQuerySet func(r *request.Request) ([]T, error)
}

An implementation of a generic listview.

This view is used for displaying a list of items.

The following template variables are set by this view:

current_page (int)
total_pages (int)
total_items (int)
items_per_page (int)
has_next_page (bool)
has_previous_page (bool)
next_page (int)
previous_page (int)
items (slice of T)

func (*ListView[T]) ServeHTTP

func (v *ListView[T]) ServeHTTP(r *request.Request)

type UpdateView

type UpdateView[T interfaces.Saver] struct {
	BaseFormView[T]
	Fields []string

	// GetQuerySet has priority over GetInstance!
	GetQuerySet func(r *request.Request) (T, error)
}

func (*UpdateView[T]) ServeHTTP

func (c *UpdateView[T]) ServeHTTP(r *request.Request)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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