def update_verse_location(location: str, new_location: str): # Check if verse exists verse_record: Verse = VerseRead.get_verse_record(location) # Check if new verse already exists new_verse_record: Verse = VerseRead.get_verse_record(new_location) # Check if location of the verse is updated if verse_record is not None and new_verse_record is None: # Update verse location if a new location is provided verse_record.location = new_location Conn.commit() elif new_verse_record is not None: raise DebugException(502) else: raise DebugException(503) return VerseRead.get_verse_record(new_location, jsonify=True)
def edit_link(location: str, word: str, level: int, delete: bool = False, jsonify=False): # Clean words only if it is not deleting if not delete: word = VerseWrite._clean(word, convert_to_american=True) # Verify weight level if (not isinstance(level, int)) or level > 3 or level < 1: print(isinstance(level, int)) raise DebugException(504) # Check if link already exists verse_record: Verse = VerseRead.get_verse_record(location) keyword_record: Keyword = VerseRead.get_keyword_record(word) if keyword_record is None: # If keyword does not exist, add it to db VerseWrite.add_keyword(word) keyword_record = VerseRead.get_keyword_record(word) link_record: VerseKeywordLink = VerseRead.get_link_record( verse_record, keyword_record) if link_record is not None: if delete: # Update link if it already exists link_record.keyword.appearance -= 1 link_record.keyword.total_votes -= link_record.votes link_record.verse.update_max_vote() Conn.session.delete(link_record) Conn.commit() return VerseRead.jsonify_link_record(link_record) else: # Update link if it already exists link_record.level = level Conn.commit() else: if delete: # Trying to delete non-existent link raise DebugException(503) else: # Add link if it doesnt exist verse_record.keyword_links.append( Conn.link_table(verse_record, keyword_record, level)) Conn.commit() return VerseRead.get_link_record(verse_record, keyword_record, jsonify)
def get_link_record(verse_record: Verse, word_record: Keyword = None, jsonify=False): if verse_record is None: raise DebugException(503) # Check if a keyword record is provided if word_record is None: # Return all keyword links for the verse all_link_records = Conn.session.query(Conn.link_table).filter( Conn.link_table.verse_id == verse_record.id).all() if jsonify: return [ VerseRead.jsonify_link_record(link_record) for link_record in all_link_records ] else: return all_link_records else: # Return only the link with the keyword link_record = Conn.session.query(Conn.link_table).filter( Conn.link_table.verse_id == verse_record.id).filter( Conn.link_table.keyword_id == word_record.id).first() if jsonify: return VerseRead.jsonify_link_record(link_record) else: return link_record
def add_verse(location: str, lvl1: [str] = [], lvl2: [str] = [], lvl3: [str] = []): # Check if verse already exists verse_record: Verse = VerseRead.get_verse_record(location) if verse_record is None: # Add the verse record if the verse does not exist yet and retrieve added record Conn.session.add(Conn.verse_table(location)) Conn.commit() verse_record = VerseRead.get_verse_record(location) else: # If verse already exists, raise an error raise DebugException(502) # Combine keywords into one list keyword_list = [ (word, 3) for word in VerseWrite._clean_list(lvl3, convert_to_american=True) ] keyword_list.extend([ (word, 2) for word in VerseWrite._clean_list(lvl2, convert_to_american=True) ]) keyword_list.extend([ (word, 1) for word in VerseWrite._clean_list(lvl1, convert_to_american=True) ]) # Update keyword links for keyword, level in keyword_list: # Check if keyword already exists in DB keyword_record = VerseRead.get_keyword_record(keyword) # Add keyword if it doesn't exist if keyword_record is None: VerseWrite.add_keyword(keyword) keyword_record = VerseRead.get_keyword_record(keyword) # Check if link already exists in DB link_record = VerseRead.get_link_record(verse_record, keyword_record) if link_record is None: # Add link if it doesn't exist Conn.session.add( Conn.link_table(verse_record, keyword_record, level)) Conn.commit() else: # Update link if it exists link_record.level = level Conn.commit() return VerseRead.get_verse_record(location, jsonify=True)
def search_verse(query_string: str, jsonify: bool = False): # Try and match the exact location matched_verse = VerseRead.get_verse_record(location=query_string) if matched_verse is None: # Try and match location that starts with query string matched_verse = Conn.session.query(Conn.verse_table).filter( Conn.verse_table.location.startswith(query_string)).first() if matched_verse is None: # Raise Exception if nothing is found raise DebugException(501) if jsonify: return VerseRead.jsonify_verse_record(matched_verse) else: return matched_verse
def remove_verse(cls, location: str): verse_record: Verse = VerseRead.get_verse_record(location) if verse_record is None: raise DebugException(503) else: # Remove all links and the verse link_records = VerseRead.get_link_record(verse_record, jsonify=False) for link_record in link_records: Conn.session.delete(link_record) Conn.commit() Conn.session.delete(verse_record) Conn.commit() return VerseRead.jsonify_verse_record(verse_record)
def parse_location(cls, full_location: str): try: full_location_split = full_location.split(" ") book_title = "" for part in full_location_split[:-1]: book_title += part + " " book_title = book_title.strip() full_verse_location_array = full_location_split[-1].split("-") if len(full_verse_location_array) == 2: # Has start and end start_chapter_text, start_verse_text = full_verse_location_array[ 0].split(":") end_chapter_text, end_verse_text = full_verse_location_array[ 1].split(":") start_chapter = cls.text_to_int(start_chapter_text) start_verse = cls.text_to_int(start_verse_text) end_chapter = cls.text_to_int(end_chapter_text) end_verse = cls.text_to_int(end_verse_text) return { "book": book_title, "start_chapter": start_chapter, "start_verse": start_verse, "end_chapter": end_chapter, "end_verse": end_verse } elif len(full_verse_location_array) == 1: # Only start verse chapter, verse = full_verse_location_array[0].split(":") return { "book": book_title, "start_chapter": chapter, "start_verse": verse, "end_chapter": None, "end_verse": None } else: DebugTools.logging.print_debug( "Unable to Parse Verse Location") except Exception: raise DebugException(507)
def get_text_with_version(cls, version: str, book: str, start_chapter_text, start_verse_text, end_chapter_text=None, end_verse_text=None): try: if version not in cls.bible_dict: raise DebugException(506) except Exception: raise DebugException(506) try: start_chapter = cls.text_to_int(start_chapter_text) start_verse = cls.text_to_int(start_verse_text) end_chapter = cls.text_to_int(end_chapter_text) end_verse = cls.text_to_int(end_verse_text) # Get dictionary object for the whole book book_text = cls.bible_dict[version][book]["content"] if end_chapter_text is not None: relevant_book_text = {} # Iterate over dictionary object to filter unused chapters for key, val in book_text.items(): if start_chapter < int(key) < end_chapter: # If it is between start and end chapters, add it in relevant_book_text[key] = val if int(key) == start_chapter: # If it is starting chapter, filter away unrelated verses relevant_verses = {} for verse, verse_content in val.items(): if int(verse) >= start_verse: relevant_verses[verse] = verse_content relevant_book_text[key] = relevant_verses if int(key) == end_chapter: # If it is ending chapter, filter away unrelated verses relevant_verses = {} for verse, verse_content in val.items(): if int(verse) <= end_verse: relevant_verses[verse] = verse_content relevant_book_text[key] = relevant_verses else: relevant_book_text = { start_chapter_text: { start_verse_text: book_text[str(start_chapter)][str(start_verse)] } } return relevant_book_text except Exception as e: DebugTools.logging.print_debug(version, book, start_chapter_text, start_verse_text, end_chapter_text, end_verse_text) raise DebugException(505)