-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
67 lines (47 loc) · 2.36 KB
/
models.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
import datetime as dt
import sqlalchemy as sql
import passlib.hash as hash
import database as database # Database Configuration
import uuid
class User(database.Base):
__tablename__ = "users"
id = sql.Column(sql.Integer, primary_key=True, index=True)
email = sql.Column(sql.String, unique=True, index=True)
first_name = sql.Column(sql.String)
last_name = sql.Column(sql.String)
hashed_password = sql.Column(sql.String)
date_created = sql.Column(sql.DateTime, default=dt.datetime.utcnow)
profile_picture = sql.Column(sql.String)
# Relationship with Conversation (one user can have multiple conversations)
conversations = sql.orm.relationship("Conversation", back_populates="user")
def verify_password(self, password: str):
return hash.bcrypt.verify(password, self.hashed_password)
def __repr__(self) -> str:
return f"User Record {self.first_name} {self.last_name}"
class Conversation(database.Base):
__tablename__ = "conversations"
id = sql.Column(sql.String, primary_key=True)
title = sql.Column(sql.String, nullable=False, unique=False) # Title of the conversation
# Relationship with User (many conversations belong to one user)
user_id = sql.Column(sql.Integer, sql.ForeignKey("users.id"), nullable=False)
user = sql.orm.relationship("User", back_populates="conversations")
date_created = sql.Column(sql.DateTime, default=dt.datetime.utcnow)
messages = sql.orm.relationship("Message", back_populates="conversation")
def __repr__(self) -> str:
return f"Conversation: {self.title}"
@classmethod
def generate_uuid(cls):
generated_uid = str(uuid.uuid4())
return generated_uid
class Message(database.Base):
__tablename__ = "messages"
id = sql.Column(sql.Integer, primary_key=True, index=True)
text_content = sql.Column(sql.String) # Content of the message
# Relationship with User (each message has an author)
author_id = sql.Column(sql.Integer, sql.ForeignKey("users.id"), nullable=True)
author = sql.orm.relationship("User")
is_bot_message = sql.Column(sql.Boolean, default=False)
conversation_id = sql.Column(sql.Integer, sql.ForeignKey("conversations.id"), )
conversation = sql.orm.relationship("Conversation", back_populates="messages")
def __repr__(self) -> str:
return f"Message: {self.text}"