Coverage for app / models / reading_status.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-12-06 04:49 +0000

1""" 

2This module defines the ReadingStatus and ReadingStatusEnum classes for tracking the  

3reading status of users for specific books. It is built using SQLAlchemy and includes  

4relationships with User and Book models, and supports statuses like "up_next" and "read". 

5""" 

6from typing import TYPE_CHECKING 

7from enum import Enum as PyEnum 

8 

9from sqlalchemy import ForeignKey, Enum 

10from sqlalchemy.orm import Mapped, mapped_column, relationship, declared_attr 

11 

12from app import db 

13 

14if TYPE_CHECKING: 

15 from app.models.book import Book 

16 from app.security.models import User 

17 

18 

19class ReadingStatusEnum(PyEnum): 

20 """ 

21 Defines the ReadingStatusEnum class, representing statuses for reading materials. 

22  

23 This enumeration provides distinct statuses for tracking reading progress, such as books  

24 that are "up next" or "read". The enum values are stored as strings. 

25 """ 

26 up_next = "up_next" # pylint: disable=invalid-name 

27 read = "read" # pylint: disable=invalid-name 

28 

29 

30# pylint: disable=too-few-public-methods 

31class ReadingStatus(db.Model): 

32 """ 

33 Represents the reading status of a user for a specific book. 

34 

35 This model links users with books and tracks their reading status using predefined 

36 statuses such as "up_next" and "read". Relationships with the User and Book models  

37 are also defined for joining and querying related data. 

38 """ 

39 __tablename__ = "reading_status" 

40 id: Mapped[int] = mapped_column(primary_key=True) 

41 user_id: Mapped[int] = mapped_column(ForeignKey("user.id")) 

42 book_id: Mapped[int] = mapped_column(ForeignKey("books.id")) 

43 status: Mapped[ReadingStatusEnum] = mapped_column(Enum(ReadingStatusEnum)) 

44 book: Mapped["Book"] = relationship(back_populates="reading_statuses", lazy="joined") 

45 

46 @declared_attr 

47 def user(self) -> Mapped["User"]: 

48 """ 

49 Provides a declared attribute `user` representing a relationship to the `User` model. 

50 The relationship is configured with lazy loading set to "joined" and allows back 

51 population through the `reading_statuses` field of the `User` model. 

52 

53 :return: A SQLAlchemy relationship to the associated `User` model 

54 """ 

55 return relationship("User", back_populates="reading_statuses", lazy="joined")