Skip to content

Commit

Permalink
Misc: card stack, type, img_from_url, rename refs on table rename (sa…
Browse files Browse the repository at this point in the history
…ltcorn#710)

* stack cards in user admin

* fix typo

* img_from_url

* change refs when table is renamed

* cmp bool coerced for sqlite

* not coerced in non sqlite part
  • Loading branch information
glutamate authored Apr 21, 2021
1 parent 23013c1 commit 8bbc964
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 51 deletions.
5 changes: 5 additions & 0 deletions packages/saltcorn-data/base-plugin/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
button,
textarea,
span,
img,
text_attr,
} = require("@saltcorn/markup/tags");
const { contract, is } = require("contractis");
Expand Down Expand Up @@ -92,6 +93,10 @@ const string = {
isEdit: false,
run: (s) => a({ href: text(s || "") }, text_attr(s || "")),
},
img_from_url: {
isEdit: false,
run: (s, req, attrs) => img({ src: text(s || ""), style: "width:100%" }),
},
as_header: { isEdit: false, run: (s) => h3(text_attr(s || "")) },
edit: {
isEdit: true,
Expand Down
12 changes: 8 additions & 4 deletions packages/saltcorn-data/models/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,14 +358,18 @@ class Table {
const client = await db.getClient();
await client.query(`BEGIN`);
try {

//2. rename table
//rename table
await db.query(
`alter table ${schemaPrefix}"${sqlsanitize(
this.name
)}" rename to "${sqlsanitize(new_name)}";`
);
//3. rename history
//change refs
await db.query(
`update ${schemaPrefix}_sc_fields set reftable_name=$1 where reftable_name=$2`,
[sqlsanitize(new_name), sqlsanitize(this.name)]
);
//rename history
if (this.versioned)
await db.query(
`alter table ${schemaPrefix}"${sqlsanitize(
Expand Down Expand Up @@ -396,7 +400,7 @@ class Table {
} else if (!new_table.versioned && existing.versioned) {
await new_table.drop_history_table();
}
Object.assign(this, new_table_rec )
Object.assign(this, new_table_rec);
}

async get_history(id) {
Expand Down
59 changes: 42 additions & 17 deletions packages/saltcorn-data/tests/table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,7 @@ describe("Table get data", () => {
});
expect(nrows).toStrictEqual(1);
});
it("should get joined rows with arbitrary fieldnames", async () => {
const patients = await Table.findOne({ name: "patients" });
const michaels = await patients.getJoinedRows({
where: { name: "Michael Douglas" },
joinFields: {
pages: { ref: "favbook", target: "pages" },
author: { ref: "favbook", target: "author" },
},
});
expect(michaels.length).toStrictEqual(1);
expect(michaels[0].pages).toBe(728);
expect(michaels[0].author).toBe("Leo Tolstoy");
});

it("should get joined rows with limit and order", async () => {
const patients = await Table.findOne({ name: "patients" });
const all = await patients.getJoinedRows({
Expand Down Expand Up @@ -320,14 +308,51 @@ describe("Table get data", () => {
type: "Bool",
required: true,
});
const table1 = await Table.create("refsunsure");
await Field.create({
table:table1,
label: "also_tall",
type: "Bool",
required: true,
});
await Field.create({
table:table1,
label: "theref",
type: "Key to notsurename",
required: true,
});
const id = await table.insertRow({tall:false})
await table1.insertRow({also_tall:true, theref:id})
const joinFields= {reftall: { ref: 'theref', target: 'tall' }}
const rows = await table1.getJoinedRows({joinFields})
expect(rows[0].theref).toBe(id)
expect(!!rows[0].reftall).toBe(false) //for sqlite
if (!db.isSQLite) {
await table.rename("isthisbetter");
const table1 = await Table.findOne({ name: "isthisbetter" });
table1.versioned = true;
await table1.update(table1);
await table1.rename("thisisthebestname");
const table3 = await Table.findOne({ name: "refsunsure" });
const rows1 = await table3.getJoinedRows({joinFields})
expect(rows1[0].theref).toBe(id)
expect(rows1[0].reftall).toBe(false)
const table2 = await Table.findOne({ name: "isthisbetter" });
expect(!!table2).toBe(true)
table2.versioned = true;
await table2.update(table2);
await table2.rename("thisisthebestname");
}
});
it("should get joined rows with arbitrary fieldnames", async () => {
const patients = await Table.findOne({ name: "patients" });
const michaels = await patients.getJoinedRows({
where: { name: "Michael Douglas" },
joinFields: {
pages: { ref: "favbook", target: "pages" },
author: { ref: "favbook", target: "author" },
},
});
expect(michaels.length).toStrictEqual(1);
expect(michaels[0].pages).toBe(728);
expect(michaels[0].author).toBe("Leo Tolstoy");
});
});

describe("relations", () => {
Expand Down
58 changes: 30 additions & 28 deletions packages/server/auth/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ const user_settings_form = (req) =>
"min_role_upload",
"email_mask",
"allow_forgot",
"custom_http_headers"
"custom_http_headers",
],
action: "/useradmin/settings",
});
Expand Down Expand Up @@ -507,33 +507,35 @@ router.get(
req,
active_sub: "Users",
sub2_page: user.email,
contents: [
{
type: "card",
title: req.__("Edit user %s", user.email),
contents: renderForm(form, req.csrfToken()),
},
{
type: "card",
title: req.__("API token"),
contents: [
div(
user.api_token
? span({ class: "mr-1" }, "API token for this user: ") +
code(user.api_token)
: req.__("No API token issued")
),
div(
{ class: "mt-4" },
post_btn(
`/useradmin/gen-api-token/${user.id}`,
user.api_token ? "Reset" : "Generate",
req.csrfToken()
)
),
],
},
],
contents: {
above: [
{
type: "card",
title: req.__("Edit user %s", user.email),
contents: renderForm(form, req.csrfToken()),
},
{
type: "card",
title: req.__("API token"),
contents: [
div(
user.api_token
? span({ class: "mr-1" }, "API token for this user: ") +
code(user.api_token)
: req.__("No API token issued")
),
div(
{ class: "mt-4" },
post_btn(
`/useradmin/gen-api-token/${user.id}`,
user.api_token ? "Reset" : "Generate",
req.csrfToken()
)
),
],
},
],
},
});
})
);
Expand Down
2 changes: 1 addition & 1 deletion packages/server/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@
"Showing matches in table %s.": "Showing matches in table %s.",
"Edit trigger": "Edit trigger",
"Show on page": "Show on page",
"Requests to render this view directly will instead show the chosen page, if any. The chosewn page should embed this view. Use this to decorate the view with additional elements.": "Requests to render this view directly will instead show the chosen page, if any. The chosewn page should embed this view. Use this to decorate the view with additional elements.",
"Requests to render this view directly will instead show the chosen page, if any. The chosen page should embed this view. Use this to decorate the view with additional elements.": "Requests to render this view directly will instead show the chosen page, if any. The chosen page should embed this view. Use this to decorate the view with additional elements.",
"Hide pagination": "Hide pagination",
"True": "True",
"False": "False",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/routes/viewedit.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const viewForm = (req, tableOptions, roles, pages, values) => {
name: "default_render_page",
label: req.__("Show on page"),
sublabel: req.__(
"Requests to render this view directly will instead show the chosen page, if any. The chosewn page should embed this view. Use this to decorate the view with additional elements."
"Requests to render this view directly will instead show the chosen page, if any. The chosen page should embed this view. Use this to decorate the view with additional elements."
),
input_type: "select",
options: [
Expand Down

0 comments on commit 8bbc964

Please sign in to comment.