Add the Active field to the campsite type’s edit form and function

In the new form this field is hidden and always active, because it makes
no sense to add an inactive campsite type.
This commit is contained in:
jordi fita mas 2023-08-14 11:43:58 +02:00
parent 1e1797c1b4
commit 208952b964
10 changed files with 219 additions and 168 deletions

View File

@ -8,18 +8,19 @@ begin;
set search_path to camper, public;
create or replace function edit_campsite_type(slug uuid, name text, description text) returns uuid as
create or replace function edit_campsite_type(slug uuid, name text, description text, active boolean) returns uuid as
$$
update campsite_type
set name = edit_campsite_type.name
, description = xmlparse(content edit_campsite_type.description)
, active = edit_campsite_type.active
where slug = edit_campsite_type.slug
returning slug;
$$
language sql
;
revoke execute on function edit_campsite_type(uuid, text, text) from public;
grant execute on function edit_campsite_type(uuid, text, text) to admin;
revoke execute on function edit_campsite_type(uuid, text, text, boolean) from public;
grant execute on function edit_campsite_type(uuid, text, text, boolean) to admin;
commit;

View File

@ -81,12 +81,13 @@ func serveTypeIndex(w http.ResponseWriter, r *http.Request, user *auth.User, com
}
type typeEntry struct {
Slug string
Name string
Slug string
Name string
Active bool
}
func collectTypeEntries(ctx context.Context, company *auth.Company, conn *database.Conn) ([]*typeEntry, error) {
rows, err := conn.Query(ctx, "select slug, name from campsite_type where company_id = $1", company.ID)
rows, err := conn.Query(ctx, "select slug, name, active from campsite_type where company_id = $1", company.ID)
if err != nil {
return nil, err
}
@ -95,7 +96,7 @@ func collectTypeEntries(ctx context.Context, company *auth.Company, conn *databa
var types []*typeEntry
for rows.Next() {
entry := &typeEntry{}
if err = rows.Scan(&entry.Slug, &entry.Name); err != nil {
if err = rows.Scan(&entry.Slug, &entry.Name, &entry.Active); err != nil {
return nil, err
}
types = append(types, entry)
@ -149,18 +150,23 @@ func editType(w http.ResponseWriter, r *http.Request, user *auth.User, company *
f.MustRender(w, r, user, company)
return
}
conn.MustExec(r.Context(), "select edit_campsite_type($1, $2, $3)", f.Slug, f.Name, f.Description)
conn.MustExec(r.Context(), "select edit_campsite_type($1, $2, $3, $4)", f.Slug, f.Name, f.Description, f.Active)
httplib.Redirect(w, r, "/admin/campsites/types", http.StatusSeeOther)
}
type typeForm struct {
Slug string
Active *form.Checkbox
Name *form.Input
Description *form.Input
}
func newTypeForm() *typeForm {
return &typeForm{
Active: &form.Checkbox{
Name: "active",
Checked: true,
},
Name: &form.Input{
Name: "name",
},
@ -172,14 +178,15 @@ func newTypeForm() *typeForm {
func (f *typeForm) FillFromDatabase(ctx context.Context, conn *database.Conn, slug string) error {
f.Slug = slug
row := conn.QueryRow(ctx, "select name, description from campsite_type where slug = $1", slug)
return row.Scan(&f.Name.Val, &f.Description.Val)
row := conn.QueryRow(ctx, "select name, description, active from campsite_type where slug = $1", slug)
return row.Scan(&f.Name.Val, &f.Description.Val, &f.Active.Checked)
}
func (f *typeForm) Parse(r *http.Request) error {
if err := r.ParseForm(); err != nil {
return err
}
f.Active.FillValue(r)
f.Name.FillValue(r)
f.Description.FillValue(r)
return nil
@ -192,5 +199,5 @@ func (f *typeForm) Valid(l *locale.Locale) bool {
}
func (f *typeForm) MustRender(w http.ResponseWriter, r *http.Request, user *auth.User, company *auth.Company) {
template.MustRenderAdmin(w, r, user, company, "campsite/type/new.gohtml", f)
template.MustRenderAdmin(w, r, user, company, "campsite/type/form.gohtml", f)
}

29
pkg/form/checkbox.go Normal file
View File

@ -0,0 +1,29 @@
/*
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
* SPDX-License-Identifier: AGPL-3.0-only
*/
package form
import (
"database/sql/driver"
"net/http"
)
type Checkbox struct {
Name string
Checked bool
Error error
}
func (checkbox *Checkbox) setError(err error) {
checkbox.Error = err
}
func (checkbox *Checkbox) FillValue(r *http.Request) {
checkbox.Checked = len(r.Form[checkbox.Name]) > 0
}
func (checkbox *Checkbox) Value() (driver.Value, error) {
return checkbox.Checked, nil
}

136
po/ca.po
View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: camper\n"
"Report-Msgid-Bugs-To: jordi@tandem.blog\n"
"POT-Creation-Date: 2023-08-08 20:04+0200\n"
"POT-Creation-Date: 2023-08-14 11:39+0200\n"
"PO-Revision-Date: 2023-07-22 23:45+0200\n"
"Last-Translator: jordi fita mas <jordi@tandem.blog>\n"
"Language-Team: Catalan <ca@dodds.net>\n"
@ -35,83 +35,45 @@ msgstr "Salta al contingut principal"
msgid "Singular Lodges"
msgstr "Allotjaments singulars"
#: web/templates/admin/page/form.gohtml:16
#: web/templates/admin/page/form.gohtml:33
msgctxt "title"
msgid "Edit Page"
msgstr "Edició de pàgina"
#: web/templates/admin/page/form.gohtml:18
#: web/templates/admin/page/form.gohtml:35
msgctxt "title"
msgid "New Page"
msgstr "Nova pàgina"
#: web/templates/admin/page/form.gohtml:42
msgctxt "input"
msgid "Title"
msgstr "Títol"
#: web/templates/admin/page/form.gohtml:50
msgctxt "input"
msgid "Content"
msgstr "Contingut"
#: web/templates/admin/page/form.gohtml:59
#: web/templates/admin/campsite/type/new.gohtml:59
msgctxt "action"
msgid "Update"
msgstr "Actualitza"
#: web/templates/admin/page/form.gohtml:61
#: web/templates/admin/campsite/type/new.gohtml:61
msgctxt "action"
msgid "Add"
msgstr "Afegeix"
#: web/templates/admin/page/index.gohtml:6
#: web/templates/admin/page/index.gohtml:12
msgctxt "title"
msgid "Pages"
msgstr "Pàgines"
#: web/templates/admin/page/index.gohtml:11
msgctxt "action"
msgid "Add Page"
msgstr "Afegeix pàgina"
#: web/templates/admin/page/index.gohtml:17
msgctxt "header"
msgid "Title"
msgstr "Títol"
#: web/templates/admin/page/index.gohtml:29
msgid "No pages added yet."
msgstr "No sha afegit cap pàgina encara."
#: web/templates/admin/campsite/type/new.gohtml:16
#: web/templates/admin/campsite/type/new.gohtml:33
#: web/templates/admin/campsite/type/form.gohtml:8
#: web/templates/admin/campsite/type/form.gohtml:25
msgctxt "title"
msgid "Edit Campsite Type"
msgstr "Edició del tipus dallotjament"
#: web/templates/admin/campsite/type/new.gohtml:18
#: web/templates/admin/campsite/type/new.gohtml:35
#: web/templates/admin/campsite/type/form.gohtml:10
#: web/templates/admin/campsite/type/form.gohtml:27
msgctxt "title"
msgid "New Campsite Type"
msgstr "Nou tipus dallotjament"
#: web/templates/admin/campsite/type/new.gohtml:42
#: web/templates/admin/campsite/type/form.gohtml:37
#: web/templates/admin/campsite/type/index.gohtml:18
msgctxt "campsite type"
msgid "Active"
msgstr "Actiu"
#: web/templates/admin/campsite/type/form.gohtml:46
#: web/templates/admin/profile.gohtml:26
msgctxt "input"
msgid "Name"
msgstr "Nom"
#: web/templates/admin/campsite/type/new.gohtml:50
#: web/templates/admin/campsite/type/form.gohtml:54
msgctxt "input"
msgid "Description"
msgstr "Descripció"
#: web/templates/admin/campsite/type/form.gohtml:63
msgctxt "action"
msgid "Update"
msgstr "Actualitza"
#: web/templates/admin/campsite/type/form.gohtml:65
msgctxt "action"
msgid "Add"
msgstr "Afegeix"
#: web/templates/admin/campsite/type/index.gohtml:6
#: web/templates/admin/campsite/type/index.gohtml:12
msgctxt "title"
@ -128,7 +90,15 @@ msgctxt "header"
msgid "Name"
msgstr "Nom"
#: web/templates/admin/campsite/type/index.gohtml:29
#: web/templates/admin/campsite/type/index.gohtml:25
msgid "Yes"
msgstr "Sí"
#: web/templates/admin/campsite/type/index.gohtml:25
msgid "No"
msgstr "No"
#: web/templates/admin/campsite/type/index.gohtml:31
msgid "No campsite types added yet."
msgstr "No sha afegit cap tipus dallotjament encara."
@ -199,10 +169,6 @@ msgctxt "action"
msgid "Logout"
msgstr "Surt"
#: pkg/page/admin.go:157
msgid "Title can not be empty."
msgstr "No podeu deixar el títol en blanc."
#: pkg/app/login.go:56 pkg/app/user.go:246
msgid "Email can not be empty."
msgstr "No podeu deixar el correu-e en blanc."
@ -224,7 +190,7 @@ msgctxt "language option"
msgid "Automatic"
msgstr "Automàtic"
#: pkg/app/user.go:249 pkg/campsite/types/admin.go:190
#: pkg/app/user.go:249 pkg/campsite/types/admin.go:197
msgid "Name can not be empty."
msgstr "No podeu deixar el nom en blanc."
@ -240,10 +206,44 @@ msgstr "Lidioma escollit no és vàlid."
msgid "File must be a valid PNG or JPEG image."
msgstr "El fitxer has de ser una imatge PNG o JPEG vàlida."
#: pkg/app/admin.go:40
#: pkg/app/admin.go:37
msgid "Access forbidden"
msgstr "Accés prohibit"
#: pkg/auth/user.go:40
msgid "Cross-site request forgery detected."
msgstr "Sha detectat un intent de falsificació de petició a llocs creuats."
#~ msgctxt "title"
#~ msgid "Edit Page"
#~ msgstr "Edició de pàgina"
#~ msgctxt "title"
#~ msgid "New Page"
#~ msgstr "Nova pàgina"
#~ msgctxt "input"
#~ msgid "Title"
#~ msgstr "Títol"
#~ msgctxt "input"
#~ msgid "Content"
#~ msgstr "Contingut"
#~ msgctxt "title"
#~ msgid "Pages"
#~ msgstr "Pàgines"
#~ msgctxt "action"
#~ msgid "Add Page"
#~ msgstr "Afegeix pàgina"
#~ msgctxt "header"
#~ msgid "Title"
#~ msgstr "Títol"
#~ msgid "No pages added yet."
#~ msgstr "No sha afegit cap pàgina encara."
#~ msgid "Title can not be empty."
#~ msgstr "No podeu deixar el títol en blanc."

136
po/es.po
View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: camper\n"
"Report-Msgid-Bugs-To: jordi@tandem.blog\n"
"POT-Creation-Date: 2023-08-08 20:04+0200\n"
"POT-Creation-Date: 2023-08-14 11:39+0200\n"
"PO-Revision-Date: 2023-07-22 23:46+0200\n"
"Last-Translator: jordi fita mas <jordi@tandem.blog>\n"
"Language-Team: Spanish <es@tp.org.es>\n"
@ -35,83 +35,45 @@ msgstr "Saltar al contenido principal"
msgid "Singular Lodges"
msgstr "Alojamientos singulares"
#: web/templates/admin/page/form.gohtml:16
#: web/templates/admin/page/form.gohtml:33
msgctxt "title"
msgid "Edit Page"
msgstr "Edición de página"
#: web/templates/admin/page/form.gohtml:18
#: web/templates/admin/page/form.gohtml:35
msgctxt "title"
msgid "New Page"
msgstr "Nueva página"
#: web/templates/admin/page/form.gohtml:42
msgctxt "input"
msgid "Title"
msgstr "Título"
#: web/templates/admin/page/form.gohtml:50
msgctxt "input"
msgid "Content"
msgstr "Contenido"
#: web/templates/admin/page/form.gohtml:59
#: web/templates/admin/campsite/type/new.gohtml:59
msgctxt "action"
msgid "Update"
msgstr "Actualitzar"
#: web/templates/admin/page/form.gohtml:61
#: web/templates/admin/campsite/type/new.gohtml:61
msgctxt "action"
msgid "Add"
msgstr "Añadir"
#: web/templates/admin/page/index.gohtml:6
#: web/templates/admin/page/index.gohtml:12
msgctxt "title"
msgid "Pages"
msgstr "Páginas"
#: web/templates/admin/page/index.gohtml:11
msgctxt "action"
msgid "Add Page"
msgstr "Añadir página"
#: web/templates/admin/page/index.gohtml:17
msgctxt "header"
msgid "Title"
msgstr "Título"
#: web/templates/admin/page/index.gohtml:29
msgid "No pages added yet."
msgstr "No se ha añadido ninguna página todavía."
#: web/templates/admin/campsite/type/new.gohtml:16
#: web/templates/admin/campsite/type/new.gohtml:33
#: web/templates/admin/campsite/type/form.gohtml:8
#: web/templates/admin/campsite/type/form.gohtml:25
msgctxt "title"
msgid "Edit Campsite Type"
msgstr "Edición del tipo de alojamientos"
#: web/templates/admin/campsite/type/new.gohtml:18
#: web/templates/admin/campsite/type/new.gohtml:35
#: web/templates/admin/campsite/type/form.gohtml:10
#: web/templates/admin/campsite/type/form.gohtml:27
msgctxt "title"
msgid "New Campsite Type"
msgstr "Nuevo tipo de alojamiento"
#: web/templates/admin/campsite/type/new.gohtml:42
#: web/templates/admin/campsite/type/form.gohtml:37
#: web/templates/admin/campsite/type/index.gohtml:18
msgctxt "campsite type"
msgid "Active"
msgstr "Activo"
#: web/templates/admin/campsite/type/form.gohtml:46
#: web/templates/admin/profile.gohtml:26
msgctxt "input"
msgid "Name"
msgstr "Nombre"
#: web/templates/admin/campsite/type/new.gohtml:50
#: web/templates/admin/campsite/type/form.gohtml:54
msgctxt "input"
msgid "Description"
msgstr "Descripción"
#: web/templates/admin/campsite/type/form.gohtml:63
msgctxt "action"
msgid "Update"
msgstr "Actualitzar"
#: web/templates/admin/campsite/type/form.gohtml:65
msgctxt "action"
msgid "Add"
msgstr "Añadir"
#: web/templates/admin/campsite/type/index.gohtml:6
#: web/templates/admin/campsite/type/index.gohtml:12
msgctxt "title"
@ -128,7 +90,15 @@ msgctxt "header"
msgid "Name"
msgstr "Nombre"
#: web/templates/admin/campsite/type/index.gohtml:29
#: web/templates/admin/campsite/type/index.gohtml:25
msgid "Yes"
msgstr "Sí"
#: web/templates/admin/campsite/type/index.gohtml:25
msgid "No"
msgstr "No"
#: web/templates/admin/campsite/type/index.gohtml:31
msgid "No campsite types added yet."
msgstr "No se ha añadido ningún tipo de alojamiento todavía."
@ -199,10 +169,6 @@ msgctxt "action"
msgid "Logout"
msgstr "Salir"
#: pkg/page/admin.go:157
msgid "Title can not be empty."
msgstr "No podéis dejar el título en blanco."
#: pkg/app/login.go:56 pkg/app/user.go:246
msgid "Email can not be empty."
msgstr "No podéis dejar el correo-e en blanco."
@ -224,7 +190,7 @@ msgctxt "language option"
msgid "Automatic"
msgstr "Automático"
#: pkg/app/user.go:249 pkg/campsite/types/admin.go:190
#: pkg/app/user.go:249 pkg/campsite/types/admin.go:197
msgid "Name can not be empty."
msgstr "No podéis dejar el nombre en blanco."
@ -240,10 +206,44 @@ msgstr "El idioma escogido no es válido."
msgid "File must be a valid PNG or JPEG image."
msgstr "El archivo tiene que ser una imagen PNG o JPEG válida."
#: pkg/app/admin.go:40
#: pkg/app/admin.go:37
msgid "Access forbidden"
msgstr "Acceso prohibido"
#: pkg/auth/user.go:40
msgid "Cross-site request forgery detected."
msgstr "Se ha detectado un intento de falsificación de petición en sitios cruzados."
#~ msgctxt "title"
#~ msgid "Edit Page"
#~ msgstr "Edición de página"
#~ msgctxt "title"
#~ msgid "New Page"
#~ msgstr "Nueva página"
#~ msgctxt "input"
#~ msgid "Title"
#~ msgstr "Título"
#~ msgctxt "input"
#~ msgid "Content"
#~ msgstr "Contenido"
#~ msgctxt "title"
#~ msgid "Pages"
#~ msgstr "Páginas"
#~ msgctxt "action"
#~ msgid "Add Page"
#~ msgstr "Añadir página"
#~ msgctxt "header"
#~ msgid "Title"
#~ msgstr "Título"
#~ msgid "No pages added yet."
#~ msgstr "No se ha añadido ninguna página todavía."
#~ msgid "Title can not be empty."
#~ msgstr "No podéis dejar el título en blanco."

View File

@ -2,6 +2,6 @@
begin;
drop function if exists camper.edit_campsite_type(uuid, text, text);
drop function if exists camper.edit_campsite_type(uuid, text, text, boolean);
commit;

View File

@ -9,15 +9,15 @@ set search_path to camper, public;
select plan(12);
select has_function('camper', 'edit_campsite_type', array ['uuid', 'text', 'text']);
select function_lang_is('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'sql');
select function_returns('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'uuid');
select isnt_definer('camper', 'edit_campsite_type', array ['uuid', 'text', 'text']);
select volatility_is('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'volatile');
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'guest', array[]::text[]);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'employee', array[]::text[]);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text'], 'authenticator', array[]::text[]);
select has_function('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean']);
select function_lang_is('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'sql');
select function_returns('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'uuid');
select isnt_definer('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean']);
select volatility_is('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'volatile');
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'guest', array[]::text[]);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'employee', array[]::text[]);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'edit_campsite_type', array ['uuid', 'text', 'text', 'boolean'], 'authenticator', array[]::text[]);
set client_min_messages to warning;
truncate campsite_type cascade;
@ -29,25 +29,25 @@ insert into company (company_id, business_name, vatin, trade_name, phone, email,
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 'ca')
;
insert into campsite_type (company_id, slug, name, description)
values (1, '87452b88-b48f-48d3-bb6c-0296de64164e', 'Type A', '<p>A</p>')
, (1, '9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type B', '<p>B</p>')
insert into campsite_type (company_id, slug, name, description, active)
values (1, '87452b88-b48f-48d3-bb6c-0296de64164e', 'Type A', '<p>A</p>', true)
, (1, '9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type B', '<p>B</p>', false)
;
select lives_ok(
$$ select edit_campsite_type('87452b88-b48f-48d3-bb6c-0296de64164e', 'Type 1', '<p>1</p>') $$,
'Should be ablo to edit the first type'
$$ select edit_campsite_type('87452b88-b48f-48d3-bb6c-0296de64164e', 'Type 1', '<p>1</p>', false) $$,
'Should be able to edit the first type'
);
select lives_ok(
$$ select edit_campsite_type('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type 2', '<p>2</p>') $$,
'Should be ablo to edit the second type'
$$ select edit_campsite_type('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type 2', '<p>2</p>', true) $$,
'Should be able to edit the second type'
);
select bag_eq(
$$ select slug::text, name, description::text from campsite_type $$,
$$ values ('87452b88-b48f-48d3-bb6c-0296de64164e', 'Type 1', '<p>1</p>')
, ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type 2', '<p>2</p>')
$$ select slug::text, name, description::text, active from campsite_type $$,
$$ values ('87452b88-b48f-48d3-bb6c-0296de64164e', 'Type 1', '<p>1</p>', false)
, ('9b6370f7-f941-46f2-bc6e-de455675bd0a', 'Type 2', '<p>2</p>', true)
$$,
'Should have updated all campsite types.'
);

View File

@ -2,6 +2,6 @@
begin;
select has_function_privilege('camper.edit_campsite_type(uuid, text, text)', 'execute');
select has_function_privilege('camper.edit_campsite_type(uuid, text, text, boolean)', 'execute');
rollback;

View File

@ -29,6 +29,18 @@
</h2>
{{ CSRFInput }}
<fieldset>
{{ if .Slug }}
{{ with .Active -}}
<label>
<input type="checkbox" name="{{ .Name }}" {{ if .Checked}}checked{{ end }}
{{ template "error-attrs" . }}>
{{( pgettext "Active" "campsite type" )}}<br>
</label>
{{ template "error-message" . }}
{{- end }}
{{ else }}
<input type="hidden" name="{{ .Active.Name }}" value="true">
{{ end }}
{{ with .Name -}}
<label>
{{( pgettext "Name" "input")}}<br>

View File

@ -15,12 +15,14 @@
<thead>
<tr>
<th scope="col">{{( pgettext "Name" "header" )}}</th>
<th scope="col">{{( pgettext "Active" "campsite type" )}}</th>
</tr>
</thead>
<tbody>
{{ range .Types -}}
<tr>
<td><a href="/admin/campsites/types/{{ .Slug }}">{{ .Name }}</a></td>
<td>{{ if .Active }}{{( gettext "Yes" )}}{{ else }}{{( gettext "No" )}}{{ end }}</td>
</tr>
{{- end }}
</tbody>