Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Sep 15, 2024
1 parent 1721512 commit 9f58b51
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 45 deletions.
75 changes: 37 additions & 38 deletions fiduswriter/book/static/js/modules/books/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,41 @@ export class BookActions {
selectFolders: false
})
fileSelector.init()

dialog.dialogEl.querySelector("#input-docx-template").addEventListener("change", event => {
const file = event.target.files[0]
return postJson(
"/api/book/docx_template/save/",
{id: book.id, file}
).then(({status, json}) => {
if (status !== 200) {
return
}
book.docx_template = json.docx_template
const docxTemplateRow = document.getElementById("docx-template-row")
if (docxTemplateRow) {
docxTemplateRow.innerHTML = bookDOCXDataRowTemplate({book})
}
})
})

dialog.dialogEl.querySelector("#input-odt-template").addEventListener("change", event => {
const file = event.target.files[0]
return postJson(
"/api/book/odt_template/save/",
{id: book.id, file}
).then(({status, json}) => {
if (status !== 200) {
return
}
book.odt_template = json.odt_template
const odtTemplateRow = document.getElementById("odt-template-row")
if (odtTemplateRow) {
odtTemplateRow.innerHTML = bookODTDataRowTemplate({book})
}
})
})

}

// Handle tab link clicking
Expand Down Expand Up @@ -549,48 +584,12 @@ export class BookActions {
break
}
case findTarget(event, "#select-docx-template", el): {
const fileSelector = document.createElement("input")
fileSelector.type = "file"
fileSelector.accept = ".docx"
fileSelector.addEventListener("change", event => {
const file = event.target.files[0]
return postJson(
"/api/book/docx_template/save/",
{id: book.id, file}
).then(({status, json}) => {
if (status !== 200) {
return
}
book.docx_template = json.docx_template
const docxTemplateRow = document.getElementById("docx-template-row")
if (docxTemplateRow) {
docxTemplateRow.innerHTML = bookDOCXDataRowTemplate({book})
}
})
})
const fileSelector = document.querySelector("#input-docx-template")
fileSelector.click()
break
}
case findTarget(event, "#select-odt-template", el): {
const fileSelector = document.createElement("input")
fileSelector.type = "file"
fileSelector.accept = ".odt"
fileSelector.addEventListener("change", event => {
const file = event.target.files[0]
return postJson(
"/api/book/odt_template/save/",
{id: book.id, file}
).then(({status, json}) => {
if (status !== 200) {
return
}
book.odt_template = json.odt_template
const odtTemplateRow = document.getElementById("odt-template-row")
if (odtTemplateRow) {
odtTemplateRow.innerHTML = bookODTDataRowTemplate({book})
}
})
})
const fileSelector = document.querySelector("#input-odt-template")
fileSelector.click()
break
}
Expand Down
32 changes: 28 additions & 4 deletions fiduswriter/book/static/js/modules/books/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,18 @@ export const bulkMenuModel = () => ({
const ids = overview.getSelected()
ids.forEach(id => {
const book = overview.bookList.find(book => book.id === id)
exportDOCX(book, overview)
if (book.docx_template) {
exportDOCX(book, overview)
} else {
addAlert(
"error",
book.title +
": " +
gettext(
"This book does not have a DOCX template."
)
)
}
})
},
disabled: overview => !overview.getSelected().length
Expand All @@ -354,7 +365,18 @@ export const bulkMenuModel = () => ({
const ids = overview.getSelected()
ids.forEach(id => {
const book = overview.bookList.find(book => book.id === id)
exportODT(book, overview)
if (book.odt_template) {
exportODT(book, overview)
} else {
addAlert(
"error",
book.title +
": " +
gettext(
"This book does not have an ODT template."
)
)
}
})
},
disabled: overview => !overview.getSelected().length
Expand Down Expand Up @@ -422,7 +444,8 @@ export const exportMenuModel = () => ({
tooltip: gettext("Export book as DOCX."),
action: ({saveBook, book, overview}) => {
saveBook().then(() => exportDOCX(book, overview))
}
},
disabled: book => !book.docx_template
},

{
Expand All @@ -431,7 +454,8 @@ export const exportMenuModel = () => ({
tooltip: gettext("Export book as ODT."),
action: ({saveBook, book, overview}) => {
saveBook().then(() => exportODT(book, overview))
}
},
disabled: book => !book.odt_template
},
{
type: "action",
Expand Down
2 changes: 2 additions & 0 deletions fiduswriter/book/static/js/modules/books/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export const bookODTDataRowTemplate = ({book}) =>
title="${gettext("Select an ODT template file")}">
<span class="ui-button-text">${gettext("Select ODT template file")}</span>
</button>
<input type="file" id="input-odt-template" accept=".odt" class="fw-media-file-input">
${
book.odt_template ?
`<button type="button" class="ui-button ui-widget ui-state-default
Expand Down Expand Up @@ -410,6 +411,7 @@ export const bookDOCXDataRowTemplate = ({book}) =>
title="${gettext("Select an DOCX template file")}">
<span class="ui-button-text">${gettext("Select DOCX template file")}</span>
</button>
<input type="file" id="input-docx-template" accept=".docx" class="fw-media-file-input">
${
book.docx_template ?
`<button type="button" class="ui-button ui-widget ui-state-default
Expand Down
67 changes: 66 additions & 1 deletion fiduswriter/book/tests/test_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_books(self):
settings.PROJECT_PATH, "book/tests/uploads/image.png"
)
self.driver.find_element(
By.CSS_SELECTOR, "input.fw-media-file-input"
By.CSS_SELECTOR, "#editimage input.fw-media-file-input"
).send_keys(image_path)
self.driver.find_element(
By.XPATH,
Expand Down Expand Up @@ -251,6 +251,35 @@ def test_books(self):
self.login_user(self.user, self.driver, self.client)
self.driver.refresh()
time.sleep(1)
# Add DOCX template
self.driver.find_element(By.CSS_SELECTOR, ".book-title").click()
self.driver.find_element(
By.CSS_SELECTOR, 'a[href="#optionTab4"]'
).click()
docx_path = os.path.join(
settings.PROJECT_PATH, "book/tests/uploads/template.docx"
)
self.driver.find_element(
By.CSS_SELECTOR, "#input-docx-template"
).send_keys(docx_path)
time.sleep(1)

# Add ODT template
self.driver.find_element(
By.CSS_SELECTOR, 'a[href="#optionTab5"]'
).click()
odt_path = os.path.join(
settings.PROJECT_PATH, "book/tests/uploads/template.odt"
)
self.driver.find_element(
By.CSS_SELECTOR, "#input-odt-template"
).send_keys(odt_path)
time.sleep(1)
self.driver.find_element(
By.XPATH, '//*[normalize-space()="Submit"]'
).click()
time.sleep(1)

# Add access rights for user 2 (write) + 3 (read)
self.driver.find_element(
By.CSS_SELECTOR,
Expand Down Expand Up @@ -488,6 +517,40 @@ def test_books(self):
)
os.remove(os.path.join(self.download_dir, "my-book-extra.latex.zip"))

# DOCX
self.driver.refresh()
self.driver.find_element(
By.CSS_SELECTOR, "tr:nth-child(1) > td > label"
).click()
WebDriverWait(self.driver, self.wait_time).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".dt-bulk-dropdown"))
).click()
self.driver.find_element(
By.XPATH, '//*[normalize-space()="Export selected as DOCX"]'
).click()
time.sleep(3)
assert os.path.isfile(
os.path.join(self.download_dir, "my-book-extra.docx")
)
os.remove(os.path.join(self.download_dir, "my-book-extra.docx"))

# ODT
self.driver.refresh()
self.driver.find_element(
By.CSS_SELECTOR, "tr:nth-child(1) > td > label"
).click()
WebDriverWait(self.driver, self.wait_time).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".dt-bulk-dropdown"))
).click()
self.driver.find_element(
By.XPATH, '//*[normalize-space()="Export selected as ODT"]'
).click()
time.sleep(3)
assert os.path.isfile(
os.path.join(self.download_dir, "my-book-extra.odt")
)
os.remove(os.path.join(self.download_dir, "my-book-extra.odt"))

# Copy document
self.driver.refresh()
self.driver.find_element(
Expand Down Expand Up @@ -842,6 +905,8 @@ def test_sanity_check(self):
Keys.BACKSPACE
).send_keys(
Keys.BACKSPACE
).send_keys(
Keys.BACKSPACE
).perform()
cross_reference = self.driver.find_element(
By.CSS_SELECTOR, ".doc-body .cross-reference.missing-target"
Expand Down
4 changes: 2 additions & 2 deletions fiduswriter/book/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
re_path(
"^docx_template/save/$",
views.save_docx_template,
name="save_docx_template",
name="book_docx_template_save",
),
re_path(
"^odt_template/save/$",
views.save_odt_template,
name="save_odt_template",
name="book_odt_template_save",
),
]

0 comments on commit 9f58b51

Please sign in to comment.