A small IT asset inventory system built around a nested folder tree, matching the project brief: Go + html/template + Bootstrap 5 + vanilla JS, SQLite storage (WAL, FKs, migrations), embedded templates/static assets, single compiled binary. - AJAX-driven inventory tree: lazy-loaded folder expansion with per-user persisted expand state, drag-and-drop move for folders/assets, safe delete-with-relocate-or-cascade confirmation, search with breadcrumbs. - Assets: CRUD, duplicate, move, auto-generated IT-000001-style IDs, CSV import (with optional folder auto-create) and export. - Admin-defined item types with custom fields (text/long_text/number/date/ boolean/dropdown/url/ip_address). - Configurable statuses, session-based auth (bcrypt, secure cookies), admin/user roles, user management. - CLI: serve, version, database migrate, database backup; /healthz; graceful shutdown; Dockerfile/docker-compose/systemd unit; Go test suite.
157 lines
4.1 KiB
Go
157 lines
4.1 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"inventree/internal/models"
|
|
)
|
|
|
|
type settingsPageData struct {
|
|
User *models.User
|
|
Statuses []models.Status
|
|
SiteName string
|
|
AssetPrefix string
|
|
AssetPadding string
|
|
Error string
|
|
}
|
|
|
|
func (a *App) settingsData(r *http.Request) (settingsPageData, error) {
|
|
ctx := r.Context()
|
|
statusList, err := a.Statuses.List(ctx)
|
|
if err != nil {
|
|
return settingsPageData{}, err
|
|
}
|
|
siteName, err := a.Settings.Get(ctx, "site_name", "inventree")
|
|
if err != nil {
|
|
return settingsPageData{}, err
|
|
}
|
|
prefix, err := a.Settings.Get(ctx, "asset_id_prefix", "IT-")
|
|
if err != nil {
|
|
return settingsPageData{}, err
|
|
}
|
|
padding, err := a.Settings.Get(ctx, "asset_id_padding", "6")
|
|
if err != nil {
|
|
return settingsPageData{}, err
|
|
}
|
|
return settingsPageData{
|
|
User: currentUser(r), Statuses: statusList, SiteName: siteName, AssetPrefix: prefix, AssetPadding: padding,
|
|
}, nil
|
|
}
|
|
|
|
func (a *App) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
|
|
data, err := a.settingsData(r)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
a.Render(w, http.StatusOK, "settings", data)
|
|
}
|
|
|
|
func (a *App) handleSettingsGeneral(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
ctx := r.Context()
|
|
_ = a.Settings.Set(ctx, "site_name", r.FormValue("site_name"))
|
|
_ = a.Settings.Set(ctx, "asset_id_prefix", r.FormValue("asset_id_prefix"))
|
|
if _, err := strconv.Atoi(r.FormValue("asset_id_padding")); err == nil {
|
|
_ = a.Settings.Set(ctx, "asset_id_padding", r.FormValue("asset_id_padding"))
|
|
}
|
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
|
}
|
|
|
|
func (a *App) handleStatusCreate(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
st := &models.Status{Name: r.FormValue("name"), Color: r.FormValue("color")}
|
|
if err := a.Statuses.Create(r.Context(), st); err != nil {
|
|
a.Log.Warn("create status failed", "error", err)
|
|
}
|
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
|
}
|
|
|
|
func (a *App) handleStatusUpdate(w http.ResponseWriter, r *http.Request) {
|
|
id, err := pathID(r, "id")
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
st, err := a.Statuses.Get(r.Context(), id)
|
|
if err != nil {
|
|
writeErr(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
st.Name = r.FormValue("name")
|
|
st.Color = r.FormValue("color")
|
|
if err := a.Statuses.Update(r.Context(), st); err != nil {
|
|
a.Log.Warn("update status failed", "error", err)
|
|
}
|
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
|
}
|
|
|
|
func (a *App) handleStatusDelete(w http.ResponseWriter, r *http.Request) {
|
|
id, err := pathID(r, "id")
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if err := a.Statuses.Delete(r.Context(), id); err != nil {
|
|
a.Log.Warn("delete status failed", "error", err)
|
|
}
|
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
|
}
|
|
|
|
func (a *App) handleStatusReorder(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
id, err := pathID(r, "id")
|
|
if err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if err := r.ParseForm(); err != nil {
|
|
writeErr(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
dir := r.FormValue("dir")
|
|
|
|
list, err := a.Statuses.List(ctx)
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
idx := -1
|
|
for i, st := range list {
|
|
if st.ID == id {
|
|
idx = i
|
|
break
|
|
}
|
|
}
|
|
if idx >= 0 {
|
|
swapWith := -1
|
|
if dir == "up" && idx > 0 {
|
|
swapWith = idx - 1
|
|
} else if dir == "down" && idx < len(list)-1 {
|
|
swapWith = idx + 1
|
|
}
|
|
if swapWith >= 0 {
|
|
ids := make([]int64, len(list))
|
|
for i, st := range list {
|
|
ids[i] = st.ID
|
|
}
|
|
ids[idx], ids[swapWith] = ids[swapWith], ids[idx]
|
|
if err := a.Statuses.Reorder(ctx, ids); err != nil {
|
|
a.Log.Warn("reorder statuses failed", "error", err)
|
|
}
|
|
}
|
|
}
|
|
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
|
}
|