diff --git a/app/crud/contents_crud.py b/app/crud/contents_crud.py index 9c307f7d..128ac6ff 100644 --- a/app/crud/contents_crud.py +++ b/app/crud/contents_crud.py @@ -11,7 +11,8 @@ from crud import utils #pylint: disable=import-error from crud.nlp_sw_crud import update_job #pylint: disable=import-error from schema import schemas_nlp #pylint: disable=import-error -from custom_exceptions import NotAvailableException, TypeException, AlreadyExistsException #pylint: disable=import-error +from custom_exceptions import NotAvailableException, TypeException, AlreadyExistsException,\ + UnprocessableException #pylint: disable=import-error ist_timezone = timezone("Asia/Kolkata") @@ -55,6 +56,48 @@ def get_commentaries(db_: Session,**kwargs): 'resource_content':resource_db_content} return response +def process_reference(db_:Session,item): #pylint: disable=too-many-branches + '''Calculate refstart and refend ids for a given reference''' + if item.reference: + ref = item.reference.__dict__ + if ref['book'] is not None: + if ref['chapter'] is not None: + if ref['verseNumber'] is not None: + ref_start = utils.create_decimal_ref_id( + db_, ref['book'], ref['chapter'], ref['verseNumber']) + else: + # Setting verseNumber to 000 if it's not present - chapter intro + ref_start = utils.create_decimal_ref_id(db_, ref['book'], ref['chapter'], 0) + ref['verseNumber'] = 0 + else: + # Setting chapter and verseNumber to 000 if it's both are not present - book intro + ref_start = utils.create_decimal_ref_id( + db_, ref['book'], ref['chapter'], ref['verseNumber']) + + if ref['bookEnd'] is not None: + if ref['chapter'] is not None: + if ref['verseEnd'] is not None: + ref_end = utils.create_decimal_ref_id( + db_, ref['bookEnd'], ref['chapterEnd'], ref['verseEnd']) + else: + # Setting verseEnd to 999 if it's not present + ref_end =utils.create_decimal_ref_id(db_, ref['bookEnd'], ref['chapterEnd'],999) + ref['verseEnd'] = 999 + else: + # Setting chapterEnd and verseEnd to 999 if it's both are not present - epilogue + if ref['verseEnd'] is None: + ref_start = utils.create_decimal_ref_id( + db_, ref['book'], 999, 999) + else: + raise UnprocessableException("verse will not exist without chapter") + else: + ref_end = ref_start + else: + ref = None + ref_end = None + ref_start = None + return ref, ref_start, ref_end + def upload_commentaries(db_: Session, resource_name, commentaries, job_id, user_id=None):#pylint: disable=too-many-locals,R1710 '''Adds rows to the commentary table specified by resource_name''' update_args = { @@ -80,32 +123,13 @@ def upload_commentaries(db_: Session, resource_name, commentaries, job_id, user_ db_content = [] db_content_out = [] for item in commentaries: - if item.reference: - ref = item.reference.__dict__ - if ref['verseNumber'] is not None: - ref_start = utils.create_decimal_ref_id( - db_,ref['book'],ref['chapter'],ref['verseNumber']) - else: - #setting verseNumber to 000 if its not present - ref_start = utils.create_decimal_ref_id(db_,ref['book'],ref['chapter'],0) - ref['verseNumber'] = 0 - if ref['bookEnd'] is not None: - if ref['chapterEnd'] is not None and ref['verseEnd'] is not None: - ref_end = utils.create_decimal_ref_id( - db_,ref['bookEnd'],ref['chapterEnd'],ref['verseEnd']) - else: - #setting verseEnd to 999 if its not present - ref_end = utils.create_decimal_ref_id(db_,ref['bookEnd'],ref['chapterEnd'],999) - ref['verseEnd'] = 999 - else: - ref_end = None - else: - ref = None - ref_end = None - ref_start = None + #getting reference and its ids + ref,ref_start,ref_end = process_reference( db_,item) + query = db_.query(model_cls) for row in query.all(): - exist_check = query.filter(model_cls.refStart == ref_start).first() + exist_check = query.filter(model_cls.refStart == ref_start, \ + model_cls.refEnd == ref_end).first() if exist_check: update_args["output"]= { "message": 'Already exist commentary with same values for reference range', @@ -422,29 +446,8 @@ def upload_parascripturals(db_: Session, resource_name, parascriptural, user_id= model_cls = db_models.dynamicTables[resource_name] db_content = [] for item in parascriptural: - if item.reference: - ref = item.reference.__dict__ - if ref['verseNumber'] is not None: - ref_start = utils.create_decimal_ref_id( - db_,ref['book'],ref['chapter'],ref['verseNumber']) - else: - #setting verseNumber to 000 if its not present - ref_start = utils.create_decimal_ref_id(db_,ref['book'],ref['chapter'],0) - ref['verseNumber'] = 0 - if ref['bookEnd'] is not None: - if ref['chapterEnd'] is not None and ref['verseEnd'] is not None: - ref_end = utils.create_decimal_ref_id( - db_,ref['bookEnd'],ref['chapterEnd'],ref['verseEnd']) - else: - #setting verseEnd to 999 if its not present - ref_end = utils.create_decimal_ref_id(db_,ref['bookEnd'],ref['chapterEnd'],999) - ref['verseEnd'] = 999 - else: - ref_end = None - else: - ref = None - ref_end = None - ref_start = None + #getting reference and its ids + ref,ref_start,ref_end = process_reference( db_,item) if item.content: item.content = utils.normalize_unicode(item.content.strip()) if item.description: @@ -611,29 +614,8 @@ def upload_audio_bible(db_: Session, resource_name, audiobibles, user_id=None):# model_cls = db_models.dynamicTables[resource_name] db_content = [] for item in audiobibles: - if item.reference: - ref = item.reference.__dict__ - if ref['verseNumber'] is not None: - ref_start = utils.create_decimal_ref_id( - db_,ref['book'],ref['chapter'],ref['verseNumber']) - else: - #setting verseNumber to 000 if its not present - ref_start = utils.create_decimal_ref_id(db_,ref['book'],ref['chapter'],0) - ref['verseNumber'] = 0 - if ref['bookEnd'] is not None: - if ref['chapterEnd'] is not None and ref['verseEnd'] is not None: - ref_end = utils.create_decimal_ref_id( - db_,ref['bookEnd'],ref['chapterEnd'],ref['verseEnd']) - else: - #setting verseEnd to 999 if its not present - ref_end = utils.create_decimal_ref_id(db_,ref['bookEnd'],ref['chapterEnd'],999) - ref['verseEnd'] = 999 - else: - ref_end = None - else: - ref = None - ref_end = None - ref_start = None + #getting reference and its ids + ref,ref_start,ref_end = process_reference( db_,item) if item.name: item.name = utils.normalize_unicode(item.name.strip()) if item.audioFormat: diff --git a/app/crud/utils.py b/app/crud/utils.py index ec1a63eb..6c52b654 100644 --- a/app/crud/utils.py +++ b/app/crud/utils.py @@ -241,12 +241,14 @@ def create_decimal_ref_id(db_:Session, bookcode, chapter, verse): db_models.BibleBook.bookCode == bookcode.lower()).first() book_id = book_content.bookId if book_id is not None: - if chapter is None and verse is None: - chapter = 0 - verse = 0 - if chapter is None and verse is not None: - raise UnprocessableException("verse will not exist without chapter") - if chapter is not None and verse is not None: - ref_id = (book_id * 100000) + (chapter * 1000) + verse - return ref_id + if chapter is None: + if verse is None: + chapter = 0 + verse = 0 + else: + raise UnprocessableException("verse will not exist without chapter") + if chapter is not None: + if verse is not None: + ref_id = (book_id * 100000) + (chapter * 1000) + verse + return ref_id raise ValueError("book_id, chapter, and verse must not be None.") diff --git a/app/test/test_commentaries.py b/app/test/test_commentaries.py index eb56670c..6aa7034e 100644 --- a/app/test/test_commentaries.py +++ b/app/test/test_commentaries.py @@ -21,9 +21,6 @@ def assert_positive_get(item): '''Check for the properties in the normal return object''' assert "book" in item['reference'] - assert "chapter" in item['reference'] - # assert "verseStart" in item # optional params get_job remove null fields - # assert "verseEnd" in item assert "commentary" in item assert "active" in item @@ -85,7 +82,8 @@ def test_post_default(): 'commentary':'the creation'}, {'reference': {"book":"gen", "chapter":1, "verseNumber":-1,"verseEnd":-1}, 'commentary':'Chapter Epilogue. God completes creation in 6 days.'}, - {'reference': {"book":"gen", "chapter":-1},'commentary':'book Epilogue.'} + {'reference': {"book":"gen", "chapter":-1},'commentary':'book Epilogue.'}, + {'reference': {"book":"mat"},'commentary':'book intro to Mathew'} ] response,resource_name = check_post(data) # resource_name = check_post(data)[0] @@ -608,6 +606,7 @@ def test_delete_default(): } response = client.delete(UNIT_URL+resource_name + "?commentary_id=" + str(commentary_id), headers=headers_va) assert response.status_code == 200 + print("del resp:",response.json()) assert response.json()['message'] ==\ f"Commentary id {commentary_id} deleted successfully" #Check commentray is deleted from table @@ -615,6 +614,7 @@ def test_delete_default(): assert commentary_response.status_code == 200 delete_response = client.get(UNIT_URL+resource_name+'?reference={"book":"gen", "chapter":1, \ "verseNumber":0,"bookEnd":"gen", "chapterEnd":1,"verseEnd":10}',headers=headers_auth) + print("#####get resp after del:",delete_response.json()) assert_not_available_content(delete_response) def test_delete_default_superadmin(): diff --git a/app/test/test_parascripturals.py b/app/test/test_parascripturals.py index 0d79bd62..27e20241 100644 --- a/app/test/test_parascripturals.py +++ b/app/test/test_parascripturals.py @@ -63,6 +63,8 @@ def test_post_default(): "reference": {"book":"MAT", "chapter":2, "verseNumber":3, \ "bookEnd":"JHN", "chapterEnd":5, "verseEnd":6 }, "link":"http://somewhere.com/something"}, + {'category':'Bible Stories', 'title':"chapter 1 of mathew", + "reference": {"book":"MAT"}} ] response,resource_name = check_post(data) assert response.status_code == 201