Skip to content

Commit

Permalink
Merge pull request #715 from shimilgithub/api-refactor
Browse files Browse the repository at this point in the history
Changing reference structure and related tests in commentaries
  • Loading branch information
shimilgithub authored Oct 3, 2023
2 parents c72df02 + 194c8ba commit 3e93ad2
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 409 deletions.
230 changes: 108 additions & 122 deletions app/crud/contents_crud.py

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions app/crud/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
40 changes: 22 additions & 18 deletions app/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,34 +121,38 @@ class BibleBook(Base): # pylint: disable=too-few-public-methods
bookName = Column('book_name', String)
bookCode = Column('book_code', String)

class Commentary(): # pylint: disable=too-few-public-methods
class Commentary(): # pylint: disable=too-few-public-methods # pylint: disable=unsubscriptable-object
'''Corresponds to the dynamically created commentary tables in vachan Db(postgres)'''
commentaryId = Column('commentary_id', Integer,
Sequence('commentary_id_seq', start=100001, increment=1), primary_key=True)
@declared_attr
def book_id(cls): # pylint: disable=E0213
'''For modelling the bookId field in derived classes'''
return Column('book_id', Integer, ForeignKey('bible_books_look_up.book_id'))
@declared_attr
def book(cls): # pylint: disable=E0213
'''For modelling the book field in derived classes'''
return relationship(BibleBook)
@hybrid_property
def reference(self):
'''To store reference information as JSONB'''
return self._reference

@reference.setter
def reference(self, value):
'''To set reference information from JSONB'''
self._reference = value

_reference = Column('reference', JSONB)

@hybrid_property
def ref_string(self):
'''To compose surrogate id'''
return f'{self.book.bookCode} {self.chapter}:{self.verseStart}-{self.verseEnd}'
return f'{self.reference["book"]} {self.reference["chapter"]}:\
{self.reference["verseNumber"]}-{self.reference["verseEnd"]}'

@ref_string.expression
def ref_string(cls): # pylint: disable=E0213
'''To compose surrogate id'''
return func.concat(BibleBook.bookCode," ",cls.chapter,":",cls.verseStart,"-",cls.verseEnd)
chapter = Column('chapter', Integer)
verseStart = Column('verse_start', Integer)
verseEnd = Column('verse_end', Integer)
def ref_string(cls):# pylint: disable=E0213
'''To compose surrogate id(SQL expression)'''
return func.concat(cls.reference['book'], " ", cls.reference['chapter'],\
":", cls.reference['verseNumber'], "-", cls.reference['verseEnd'])
refStart = Column('ref_start', Integer)
refEnd = Column('ref_end', Integer)
commentary = Column('commentary', String)
active = Column('active', Boolean)
# createdUser = Column('created_user', String)
__table_args__ = (
# UniqueConstraint('book_id', 'chapter', 'verse_start', 'verse_end'),
{'extend_existing': True}
)

Expand Down
18 changes: 9 additions & 9 deletions app/routers/content_apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,9 +663,11 @@ async def get_bible_verse(request: Request,
@get_auth_access_check_decorator
async def get_commentary(request: Request,
resource_name: schemas.TableNamePattern=Path(..., examples="en_BBC_1_commentary"),
book_code: schemas.BookCodePattern=Query(None, examples="1ki"),
chapter: int = Query(None, examples=10, ge=-1), verse: int = Query(None, examples=1, ge=-1),
last_verse: int = Query(None, examples=3, ge=-1), active: bool = True,
reference: str = Query(None,
examples='{"book": "mat", "chapter": 1, "verseNumber": 6}'),
search_word: str=Query(None, examples="customary") ,
commentary: str=Query(None, examples="It was customary at the time"),
active: bool = True,
skip: int = Query(0, ge=0), limit: int = Query(100, ge=0),
user_details =Depends(get_user_or_none), db_: Session = Depends(get_db)):
'''Fetches commentries under the specified resource.
Expand All @@ -679,12 +681,10 @@ async def get_commentary(request: Request,
* limit=n: limits the no. of items to be returned to n
* returns [] for not available content'''
log.info('In get_commentary')
log.debug('resource_name: %s, book_code: %s, chapter: %s, verse:%s,\
last_verse:%s, skip: %s, limit: %s',
resource_name, book_code, chapter, verse, last_verse, skip, limit)
return contents_crud.get_commentaries(db_, resource_name=resource_name,chapter=chapter,\
book_code=book_code,verse=verse, last_verse=last_verse,active=active,\
skip = skip, limit = limit)
log.debug('resource_name: %s, reference: %s, skip: %s, limit: %s, search_word: %s,\
commentary: %s', resource_name, reference, skip, limit,search_word,commentary)
return contents_crud.get_commentaries(db_, resource_name=resource_name,reference = reference,\
search_word = search_word, commentary=commentary, active=active, skip = skip, limit = limit)

@router.post('/v2/resources/commentaries/{resource_name}',
response_model=schema_content.CommentaryCreateResponse, response_model_exclude_none=True,
Expand Down
91 changes: 11 additions & 80 deletions app/schema/schema_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,48 +224,15 @@ class BookContentType(str, Enum):

class CommentaryCreate(BaseModel):
'''Response object for commentaries'''
bookCode : BookCodePattern
chapter: int
verseStart: int = None
verseEnd: int = None
reference: Reference = None
commentary: str
active: bool = True

@validator('verseStart', 'verseEnd')
def check_verses(cls, val, values): # pylint: disable=E0213
'''verse fields should be greater than or equal to -1'''
if 'chapter' in values and values['chapter'] in [-1, 0]:
if val not in [-1, 0, None]:
raise ValueError('verse fields should be 0, for book introductions and epilogues')
val = 0
if val is None:
raise ValueError('verse fields must have a value, '+
'except for book introduction and epilogue')
if val < -1:
raise ValueError('verse fields should be greater than or equal to -1')
return val

@validator('verseEnd')
def check_range(cls, val, values): # pylint: disable=E0213
'''verse start should be less than or equal to verse end'''
if 'verseStart' in values and val < values['verseStart']:
raise ValueError('verse start should be less than or equal to verse end')
return val

@validator('chapter')
def check_chapter(cls, val): # pylint: disable=E0213
'''chapter fields should be greater than or equal to -1'''
if val < -1:
raise ValueError('chapter field should be greater than or equal to -1')
return val
class Config:
'''display example value in API documentation'''
schema_extra = {
"example": {
"bookCode": "1ki",
"chapter": 10,
"verseStart": 1,
"verseEnd": 7,
"reference": {"book":"MRK", "chapter":11, "verseNumber":12,
"bookEnd":"LUK", "chapterEnd":14, "verseEnd":15 },
"commentary": "It was customary at the time ...",
"active": True
}
Expand All @@ -274,48 +241,17 @@ class Config:

class CommentaryEdit(BaseModel):
'''Response object for commentaries'''
bookCode : BookCodePattern
chapter: int
verseStart: int = None
verseEnd: int = None
commentaryId:int
reference: Reference = None
commentary: str = None
active: bool = None

@validator('verseStart', 'verseEnd')
def check_verses(cls, val, values): # pylint: disable=E0213
'''verse fields should be greater than or equal to -1'''
if 'chapter' in values and values['chapter'] in [-1, 0]:
if val not in [-1, 0, None]:
raise ValueError('verse fields should be 0, for book introductions and epilogues')
val = 0
if val is None:
raise ValueError('verse fields must have a value, '+
'except for book introduction and epilogue')
if val < -1:
raise ValueError('verse fields should be greater than or equal to -1')
return val

@validator('verseEnd')
def check_range(cls, val, values): # pylint: disable=E0213
'''verse start should be less than or equal to verse end'''
if 'verseStart' in values and val < values['verseStart']:
raise ValueError('verse start should be less than or equal to verse end')
return val

@validator('chapter')
def check_chapter(cls, val): # pylint: disable=E0213
'''chapter fields should be greater than or equal to -1'''
if val < -1:
raise ValueError('chapter field should be greater than or equal to -1')
return val
class Config:
'''display example value in API documentation'''
schema_extra = {
"example": {
"bookCode": "1ki",
"chapter": 10,
"verseStart": 1,
"verseEnd": 7,
"commentaryId":100000,
"reference": {"book":"MRK", "chapter":15, "verseNumber":10,
"bookEnd":"JHN", "chapterEnd":1, "verseEnd":9 },
"commentary": "One of the practices of that time was ...",
"active": False
}
Expand All @@ -324,10 +260,7 @@ class Config:
class CommentaryResponse(BaseModel):
'''Response object for commentaries'''
commentaryId: int
book : BibleBook
chapter: int
verseStart: int = None
verseEnd: int = None
reference: Reference = None
commentary: str
active: bool
class Config:
Expand All @@ -337,10 +270,8 @@ class Config:
schema_extra = {
"example": {
"commentaryId":100000,
"bookCode": "1ki",
"chapter": 10,
"verseStart": 1,
"verseEnd": 7,
"reference": {"book":"MRK", "chapter":11, "verseNumber":12,
"bookEnd":"LUK", "chapterEnd":14, "verseEnd":15 },
"commentary": "It was customary at the time ...",
"active": True
}
Expand Down
Loading

0 comments on commit 3e93ad2

Please sign in to comment.