Statuses import, an icon picker, folder-scoped CSV import, and site name
- Statuses can now be bulk-imported/updated via CSV (Name, Color columns; matches and updates existing statuses by name). New internal/statuses/csv.go, tested. - Icon fields (folder create/rename, item type form) are now a searchable dropdown of the 50 most common Bootstrap Icons instead of a free-text box, via a small reusable .icon-picker widget in app.js/app.css. - CSV asset import is now folder-scoped when triggered from the tree: right-click a folder (or its background) > Import CSV... imports every row directly into that folder, so the CSV needs no Folder column at all, and the option no longer appears at the top level. The original global, Folder-path-aware import (with auto-create-missing-folders) moved to a new admin-only Settings > Import / Export page, alongside the new Statuses import and format examples for both. assets.Import gained ImportOptions.TargetFolderID to drive the folder-scoped mode. - Site Name (Settings > General) now actually renders in the navbar brand and <title>, via a per-App "siteName" template func bound in loadTemplates. The brand is plain text now, not a link to /inventory.
This commit is contained in:
+1
-1
@@ -58,7 +58,7 @@ func NewApp(db *sql.DB, log *slog.Logger, secureCookies bool) (*App, error) {
|
||||
SecureCookies: secureCookies,
|
||||
}
|
||||
|
||||
tmpl, err := loadTemplates()
|
||||
tmpl, err := loadTemplates(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -30,6 +30,11 @@ func (a *App) handleExportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// handleImportCSV backs the folder-scoped "Import CSV..." context-menu
|
||||
// action: every row imports directly into the given folder_id, so the CSV
|
||||
// needs no Folder column at all. It's only ever invoked from within a
|
||||
// specific folder (never at the top level) and responds with JSON since
|
||||
// it's called via fetch from the tree/panel context menu.
|
||||
func (a *App) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
@@ -43,7 +48,10 @@ func (a *App) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
createMissing := r.FormValue("create_missing_folders") == "1"
|
||||
opts := assets.ImportOptions{}
|
||||
if folderID, err := formOptionalID(r, "folder_id"); err == nil && folderID != nil {
|
||||
opts.TargetFolderID = folderID
|
||||
}
|
||||
|
||||
itemTypes, err := a.ItemTypes.List(ctx, true)
|
||||
if err != nil {
|
||||
@@ -61,7 +69,7 @@ func (a *App) handleImportCSV(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.Assets.Import(ctx, file, assets.ImportOptions{CreateMissingFolders: createMissing}, itemTypes, statusList, userList)
|
||||
result, err := a.Assets.Import(ctx, file, opts, itemTypes, statusList, userList)
|
||||
if err != nil {
|
||||
jsonErr(w, http.StatusBadRequest, err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"inventree/internal/assets"
|
||||
"inventree/internal/models"
|
||||
)
|
||||
|
||||
type settingsImportPageData struct {
|
||||
User *models.User
|
||||
Kind string
|
||||
Imported int
|
||||
Updated int
|
||||
Errs int
|
||||
Error string
|
||||
}
|
||||
|
||||
// handleSettingsImportPage backs the admin "Import / Export" settings page:
|
||||
// global CSV import for assets (with a Folder column, optionally
|
||||
// auto-creating missing folders) and for statuses, plus format examples.
|
||||
// This is distinct from the folder-scoped "Import CSV..." context-menu
|
||||
// action in the tree, which never needs a Folder column.
|
||||
func (a *App) handleSettingsImportPage(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
a.Render(w, http.StatusOK, "settings_import", settingsImportPageData{
|
||||
User: currentUser(r),
|
||||
Kind: q.Get("kind"),
|
||||
Imported: queryInt(r, "imported", 0),
|
||||
Updated: queryInt(r, "updated", 0),
|
||||
Errs: queryInt(r, "errs", 0),
|
||||
Error: q.Get("error"),
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) handleSettingsImportAssets(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
file, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
createMissing := r.FormValue("create_missing_folders") == "1"
|
||||
|
||||
itemTypes, err := a.ItemTypes.List(ctx, true)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
statusList, err := a.Statuses.List(ctx)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
userList, err := a.Users.List(ctx)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.Assets.Import(ctx, file, assets.ImportOptions{CreateMissingFolders: createMissing}, itemTypes, statusList, userList)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=assets&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, fmt.Sprintf("/settings/import?kind=assets&imported=%d&updated=%d&errs=%d", result.Created, result.Updated, len(result.Errors)), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (a *App) handleSettingsImportStatuses(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=statuses&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
file, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=statuses&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
result, err := a.Statuses.Import(r.Context(), file)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/settings/import?kind=statuses&error="+url.QueryEscape(err.Error()), http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, fmt.Sprintf("/settings/import?kind=statuses&imported=%d&updated=%d&errs=%d", result.Created, result.Updated, len(result.Errors)), http.StatusSeeOther)
|
||||
}
|
||||
@@ -25,6 +25,20 @@ func queryOptionalID(r *http.Request, name string) (*int64, error) {
|
||||
return &id, nil
|
||||
}
|
||||
|
||||
// formOptionalID parses a POST form/multipart-form field (already parsed via
|
||||
// ParseForm/ParseMultipartForm) as *int64; empty or missing returns nil.
|
||||
func formOptionalID(r *http.Request, name string) (*int64, error) {
|
||||
v := r.FormValue(name)
|
||||
if v == "" {
|
||||
return nil, nil
|
||||
}
|
||||
id, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &id, nil
|
||||
}
|
||||
|
||||
func queryInt64(r *http.Request, name string) int64 {
|
||||
v := r.URL.Query().Get(name)
|
||||
if v == "" {
|
||||
|
||||
+17
-3
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
@@ -94,8 +95,21 @@ type templateSet struct {
|
||||
// each page can define a block named "content" without colliding with the
|
||||
// others (html/template templates sharing one *template.Template must have
|
||||
// unique names, but isolated clones may reuse the same name freely).
|
||||
func loadTemplates() (*templateSet, error) {
|
||||
shared := template.New("root").Funcs(templateFuncs)
|
||||
func loadTemplates(a *App) (*templateSet, error) {
|
||||
// siteName is bound per-App (rather than living in the package-level
|
||||
// templateFuncs) since it needs a live DB read through a.Settings; it's
|
||||
// used by layout.html for the <title> and navbar brand text.
|
||||
appFuncs := template.FuncMap{
|
||||
"siteName": func() string {
|
||||
name, err := a.Settings.Get(context.Background(), "site_name", "inventree")
|
||||
if err != nil || name == "" {
|
||||
return "inventree"
|
||||
}
|
||||
return name
|
||||
},
|
||||
}
|
||||
|
||||
shared := template.New("root").Funcs(templateFuncs).Funcs(appFuncs)
|
||||
|
||||
shared, err := shared.ParseFS(content.TemplatesFS, "templates/layout.html")
|
||||
if err != nil {
|
||||
@@ -124,7 +138,7 @@ func loadTemplates() (*templateSet, error) {
|
||||
pages[name] = clone
|
||||
}
|
||||
|
||||
login := template.New("login-root").Funcs(templateFuncs)
|
||||
login := template.New("login-root").Funcs(templateFuncs).Funcs(appFuncs)
|
||||
login, err = login.ParseFS(content.TemplatesFS, "templates/login.html")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse login: %w", err)
|
||||
|
||||
@@ -80,6 +80,10 @@ func (a *App) Router() http.Handler {
|
||||
mux.HandleFunc("POST /settings/item-types/{id}/fields/{fieldID}", a.requireAdmin(a.handleFieldUpdate))
|
||||
mux.HandleFunc("POST /settings/item-types/{id}/fields/{fieldID}/delete", a.requireAdmin(a.handleFieldDelete))
|
||||
|
||||
mux.HandleFunc("GET /settings/import", a.requireAdmin(a.handleSettingsImportPage))
|
||||
mux.HandleFunc("POST /settings/import/assets", a.requireAdmin(a.handleSettingsImportAssets))
|
||||
mux.HandleFunc("POST /settings/import/statuses", a.requireAdmin(a.handleSettingsImportStatuses))
|
||||
|
||||
mux.HandleFunc("GET /settings/users", a.requireAdmin(a.handleUsersList))
|
||||
mux.HandleFunc("GET /settings/users/new", a.requireAdmin(a.handleUserNewForm))
|
||||
mux.HandleFunc("POST /settings/users", a.requireAdmin(a.handleUserCreate))
|
||||
|
||||
Reference in New Issue
Block a user