-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
76 lines (60 loc) · 3.17 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations
import datetime
import logging
import uuid
from enum import Enum
from sqlalchemy import UUID, DateTime
from sqlalchemy import Enum as DbEnum
from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.orm import Mapped, mapped_column, relationship
from rating_api.settings import get_settings
from .base import BaseDbModel
settings = get_settings()
logger = logging.getLogger(__name__)
class ReviewStatus(str, Enum):
APPROVED: str = "approved"
PENDING: str = "pending"
DISMISSED: str = "dismissed"
class Lecturer(BaseDbModel):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
first_name: Mapped[str] = mapped_column(String, nullable=False)
last_name: Mapped[str] = mapped_column(String, nullable=False)
middle_name: Mapped[str] = mapped_column(String, nullable=False)
avatar_link: Mapped[str] = mapped_column(String, nullable=True)
timetable_id: Mapped[int] = mapped_column(Integer, unique=True, nullable=False)
comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer")
@hybrid_method
def search_by_name(self, query: str) -> bool:
response = true
query = query.split(' ')
for q in query:
response = and_(
response, or_(self.first_name.contains(q), self.middle_name.contains(q), self.last_name.contains(q))
)
return response
@hybrid_method
def search_by_subject(self, query: str) -> bool:
query = query.lower()
response = true
if query != "":
response = and_(Comment.review_status == ReviewStatus.APPROVED, func.lower(Comment.subject) == query)
return response
class Comment(BaseDbModel):
uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
user_id: Mapped[int] = mapped_column(Integer, nullable=True)
create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
subject: Mapped[str] = mapped_column(String, nullable=False)
text: Mapped[str] = mapped_column(String, nullable=True)
mark_kindness: Mapped[int] = mapped_column(Integer, nullable=False)
mark_freebie: Mapped[int] = mapped_column(Integer, nullable=False)
mark_clarity: Mapped[int] = mapped_column(Integer, nullable=False)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments")
review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False)
class LecturerUserComment(BaseDbModel):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)
update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False)