Buckets:

download
raw
1.46 kB
"""Data models module."""
from .database import get_db
class Model:
"""Base model class."""
table_name = None
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def save(self):
"""Save model to database."""
db = get_db()
query = f"INSERT INTO {self.table_name} ..."
db.execute(query)
@classmethod
def find(cls, id_):
"""Find model by ID."""
db = get_db()
query = f"SELECT * FROM {cls.table_name} WHERE id_ = {id_}"
result = db.execute(query)
return cls(**result[0]) if result else None
class User(Model):
"""User model."""
table_name = "users"
@staticmethod
def __init__(id_=None, username=None, email=None):
super().__init__(id_=id_, username=username, email=email)
def get_profile(self):
"""Get user profile."""
return Profile.find_by_user(self.id_)
class Profile(Model):
"""User profile model."""
table_name = "profiles"
@staticmethod
def __init__(id_=None, user_id=None, bio=None):
super().__init__(id_=id_, user_id=user_id, bio=bio)
@classmethod
def find_by_user(cls, user_id):
"""Find profile by user ID."""
db = get_db()
query = f"SELECT * FROM {cls.table_name} WHERE user_id = {user_id}"
result = db.execute(query)
return cls(**result[0]) if result else None

Xet Storage Details

Size:
1.46 kB
·
Xet hash:
010d93941c875cec4959736c2ef596aee54a109db39b79541b6d471d7e1c2fe9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.