- Main panel is now blank at the top level ("Select a folder or asset
from the tree") instead of showing a dashboard - browsing happens in
the tree, the panel only shows something once you click into it.
Removed the now-dead DashboardStats/Stats() machinery it depended on.
- Add/Edit Asset no longer takes over the main panel; it opens in
#assetModal instead; asset_form.html was trimmed down to just the form
(no more breadcrumb/panel wrapper) since the modal chrome provides the
title and footer buttons now. The dynamic item-type-fields and submit
listeners moved from #main-panel's delegation to the modal element's.
- Statuses split out of the General settings page into its own
/settings/statuses page and sub-nav entry, alongside General, Item
Types, and Users.
160 lines
4.3 KiB
Go
160 lines
4.3 KiB
Go
package web
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"inventree/internal/models"
|
|
)
|
|
|
|
type settingsPageData struct {
|
|
User *models.User
|
|
SiteName string
|
|
AssetPrefix string
|
|
AssetPadding string
|
|
Error string
|
|
}
|
|
|
|
func (a *App) handleSettingsPage(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
siteName, err := a.Settings.Get(ctx, "site_name", "inventree")
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
prefix, err := a.Settings.Get(ctx, "asset_id_prefix", "IT-")
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
padding, err := a.Settings.Get(ctx, "asset_id_padding", "6")
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
a.Render(w, http.StatusOK, "settings", settingsPageData{
|
|
User: currentUser(r), SiteName: siteName, AssetPrefix: prefix, AssetPadding: padding,
|
|
})
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
type statusesPageData struct {
|
|
User *models.User
|
|
Statuses []models.Status
|
|
}
|
|
|
|
func (a *App) handleSettingsStatusesPage(w http.ResponseWriter, r *http.Request) {
|
|
statusList, err := a.Statuses.List(r.Context())
|
|
if err != nil {
|
|
writeErr(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
a.Render(w, http.StatusOK, "settings_statuses", statusesPageData{User: currentUser(r), Statuses: statusList})
|
|
}
|
|
|
|
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/statuses", 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/statuses", 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/statuses", 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/statuses", http.StatusSeeOther)
|
|
}
|