Compare commits

..

No commits in common. "81927e9017deb911d5d52461317bfad8a0b14406" and "04e66d7888f4567e004e4a6cf76765e8451c6a2d" have entirely different histories.

3 changed files with 21 additions and 43 deletions

View File

@ -36,19 +36,18 @@
</form>
</header>
<form method=post action="container?id={{ .Container.Id }}">
{{- $description := or .NewDescription .Container.Description }}
<textarea name=description rows="{{ max 5 (lines $description) }}"
<textarea name=description
rows="{{ max 5 (lines .Container.Description) }}"
placeholder="Popis obalu nebo jeho obsahu">
{{- $description -}}
{{- .Container.Description -}}
</textarea>
<footer>
<div>
<label for=series>Řada:</label>
<select name=series id=series>
{{- $preselect := or .NewSeries .Container.Series }}
{{- range $prefix, $desc := .AllSeries }}
<option value="{{ $prefix }}"
{{ if eq $prefix $preselect }}selected{{ end -}}
{{ if eq $prefix $.Container.Series }}selected{{ end -}}
>{{ $prefix }} &mdash; {{ $desc }}</option>
{{- end }}
</select>
@ -56,7 +55,7 @@
<div>
<label for=parent>Nadobal:</label>
<input type=text name=parent id=parent
value="{{ or .NewParent .Container.Parent }}">
value="{{ .Container.Parent }}">
</div>
<input type=submit value="Uložit">
</footer>
@ -70,27 +69,21 @@
<h2>Nový obal</h2>
</header>
<form method=post action="container">
{{- $description := or .NewDescription "" }}
<textarea name=description rows="{{ max 5 (lines $description) }}"
placeholder="Popis obalu nebo jeho obsahu">
{{- $description -}}
</textarea>
<textarea name=description rows=5
placeholder="Popis obalu nebo jeho obsahu"></textarea>
<footer>
<div>
<label for=series>Řada:</label>
<select name=series id=series>
{{- $preselect := or .NewSeries "" }}
{{- range $prefix, $desc := .AllSeries }}
<option value="{{ $prefix }}"
{{ if eq $prefix $preselect }}selected{{ end -}}
>{{ $prefix }} &mdash; {{ $desc }}</option>
{{- end }}
</select>
</div>
<div>
<label for=parent>Nadobal:</label>
<input type=text name=parent id=parent
value="{{ or .NewParent "" }}">
<input type=text name=parent id=parent value="">
</div>
<input type=submit value="Uložit">
</footer>

View File

@ -205,13 +205,6 @@ func dbContainerCreate(c *Container) error {
}
func dbContainerUpdate(c *Container, updated Container) error {
if _, ok := indexSeries[updated.Series]; !ok {
return errNoSuchSeries
}
if updated.Parent != "" && indexContainer[updated.Parent] == nil {
return errNoSuchContainer
}
newID := updated.Id()
if updated.Series != c.Series && len(c.Children()) > 0 {
return errCannotChangeSeriesNotEmpty

View File

@ -116,13 +116,14 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
var err error
if r.Method == http.MethodPost {
if err = handleContainerPost(r); err == nil {
redirect := r.URL.EscapedPath()
redirect := "container"
if shownId != "" {
redirect += "?id=" + url.QueryEscape(shownId)
}
http.Redirect(w, r, redirect, http.StatusSeeOther)
return
}
// TODO: Use the last data as a prefill.
} else if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
@ -133,6 +134,14 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
allSeries[s.Prefix] = s.Description
}
var container *Container
children := indexChildren[""]
if c, ok := indexContainer[ContainerId(shownId)]; ok {
children = c.Children()
container = c
}
params := struct {
Error error
ErrorNoSuchSeries bool
@ -143,9 +152,6 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
ErrorWouldContainItself bool
ErrorContainerInUse bool
Container *Container
NewDescription *string
NewSeries string
NewParent *string
Children []*Container
AllSeries map[string]string
}{
@ -157,24 +163,10 @@ func handleContainer(w http.ResponseWriter, r *http.Request) {
ErrorCannotChangeNumber: err == errCannotChangeNumber,
ErrorWouldContainItself: err == errWouldContainItself,
ErrorContainerInUse: err == errContainerInUse,
Children: indexChildren[""],
Container: container,
Children: children,
AllSeries: allSeries,
}
if c, ok := indexContainer[ContainerId(shownId)]; ok {
params.Children = c.Children()
params.Container = c
}
if description, ok := r.Form["description"]; ok {
params.NewDescription = &description[0]
}
if series, ok := r.Form["series"]; ok {
// It seems impossible to dereference strings in text/template so that
// `eq` can be used, and we don't actually need a null value here.
params.NewSeries = series[0]
}
if parent, ok := r.Form["parent"]; ok {
params.NewParent = &parent[0]
}
executeTemplate("container.tmpl", w, &params)
}
@ -206,7 +198,7 @@ func handleSeries(w http.ResponseWriter, r *http.Request) {
var err error
if r.Method == http.MethodPost {
if err = handleSeriesPost(r); err == nil {
http.Redirect(w, r, r.URL.EscapedPath(), http.StatusSeeOther)
http.Redirect(w, r, "series", http.StatusSeeOther)
return
}
// XXX: This is rather ugly.