Fix Verse/Chapter blank after saving Church Bible Verse

Fixes #78
This commit is contained in:
meichthys 2025-11-10 04:33:19 +00:00
parent c105f4972a
commit c828d10b43
2 changed files with 48 additions and 19 deletions

View File

@ -2,22 +2,52 @@
// For license information, please see license.txt
frappe.ui.form.on('Church Bible Verse', {
// Dynamically set select values for chapter and verse fields
// Alternatively, we could store the number of chapters and verses for each book/chatper
after_save: function(frm) {
// If document was renamed, reload to new URL
const expected_name = `${frm.doc.book} ${frm.doc.chapter}:${frm.doc.verse}`;
if (frm.doc.name !== expected_name) {
frappe.set_route('Form', frm.doctype, expected_name);
}
},
refresh: async function(frm) {
// Set chapter options when form loads
if (frm.doc.book) {
try {
let chapters = await get_chapter_count(frm.doc.book);
let options = Array.from({ length: chapters }, (_, i) => i + 1).join('\n');
frm.set_df_property('chapter', 'options', options);
} catch (e) {
frm.set_df_property('chapter', 'options', Array.from({ length: 150}, (_, i) => i + 1).join('\n'));
}
}
// Set verse options when form loads
if (frm.doc.book && frm.doc.chapter) {
try {
let verses = await get_verse_count(frm.doc.book, frm.doc.chapter);
let options = Array.from({ length: verses }, (_, i) => i + 1).join('\n');
frm.set_df_property('verse', 'options', options);
} catch (e) {
frm.set_df_property('verse', 'options', Array.from({ length: 176 }, (_, i) => i + 1).join('\n'));
}
}
},
book: async function(frm) {
// Set options for chapter field
try{
let chapters = await get_chapter_count(frm.doc.book)
// Set options for chapter field when book changes
try {
let chapters = await get_chapter_count(frm.doc.book);
let options = Array.from({ length: chapters }, (_, i) => i + 1).join('\n');
frm.set_df_property('chapter', 'options', options);
} catch (e) {
frm.set_df_property('chapter', 'options', Array.from({ length: 150}, (_, i) => i + 1).join('\n'));
frm.set_df_property('chapter', 'options', Array.from({ length: 150}, (_, i) => i + 1).join('\n'));
}
},
chapter: async function(frm) {
// Set options for verse field
// Set options for verse field when chapter changes
try {
let verses = await get_verse_count(frm.doc.book, frm.doc.chapter)
let verses = await get_verse_count(frm.doc.book, frm.doc.chapter);
let options = Array.from({ length: verses }, (_, i) => i + 1).join('\n');
frm.set_df_property('verse', 'options', options);
} catch (e) {
@ -26,8 +56,8 @@ frappe.ui.form.on('Church Bible Verse', {
}
});
async function get_chapter_count(book) {
// Get the number of chapters in a book
const book_doc = await frappe.db.get_doc("Church Bible Book", book);
let book_abbreviation = book_doc.abbreviation
const url = `https://bible-api.com/data/kjv/${book_abbreviation}`;
@ -44,6 +74,7 @@ async function get_chapter_count(book) {
}
async function get_verse_count(book, chapter) {
// Get the number of verses in a chapter of a book
const book_doc = await frappe.db.get_doc("Church Bible Book", book);
let book_abbreviation = book_doc.abbreviation
const url = `https://bible-api.com/data/kjv/${book_abbreviation}/${chapter}`;

View File

@ -9,18 +9,16 @@ class ChurchBibleVerse(Document):
pass
def autoname(self):
name = self.get_name()
if not frappe.db.exists("Church Bible Verse", self.name):
self.name = name
return
else:
if self.name != self.get_name():
frappe.rename_doc("Church Bible Verse", self.name, name)
self.name = self.get_name()
def get_name(self):
"""Constructs the document name"""
return f"{self.book} {self.chapter}:{self.verse}"
def on_update(self):
# Rename document when updating
self.autoname()
def before_save(self):
if not self.is_new():
new_name = self.get_name()
if self.name != new_name:
# Rename the document before saving
frappe.rename_doc("Church Bible Verse", self.name, new_name, force=True)
self.name = new_name