class User(BaseModel, UserMixin): __tablename__ = "user" name = db.Column(db.String(128), nullable=True) email = db.Column(db.String(256), nullable=False, unique=True) password = db.Column(db.String(256), nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=datetime.now) last_logged_at = db.Column(db.DateTime, nullable=False, default=datetime.now) @classmethod def get(cls, user_id: int): try: return db.session.query(User).get(user_id) except NoResultFound: return None @classmethod def get_by_email(cls, email: str): return db.session.query(User).filter(User.email == email).first() @classmethod def check_exists(cls, email: str) -> bool: return bool(db.session.query(User).filter(User.email == email).first()) @classmethod def get_or_none(cls, email: str, password_hash: str): user = (db.session.query(User).filter( User.email == email, User.password == password_hash).first()) return user
class Category(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) def __init__(self, name): self.name = name def __repr__(self): return '<Category %r>' % self.name
class UserSeed(BaseModel): __tablename__ = "user_seeds" user_id = db.Column(db.Integer(), db.ForeignKey("user.id"), nullable=True) user = db.relationship("User") seed = db.Column(db.Integer()) @classmethod def get_or_create(cls, user) -> int: if user.is_authenticated: try: user_seed = (db.session.query(UserSeed).filter( UserSeed.user == user).one()) except NoResultFound: user_seed = cls(user=user, seed=rand()) db.session.add(user_seed) db.session.commit() return user_seed.seed else: try: default_seed = (db.session.query(UserSeed).filter( UserSeed.user == None).one()) except NoResultFound: default_seed = cls(seed=rand()) db.session.add(default_seed) db.session.commit() return default_seed.seed @classmethod def get_by_user(cls, user) -> int: return cls.get_or_create(user) @classmethod def create_new_seed(cls, user) -> bool: if user.is_authenticated: user_seed = db.session.query(UserSeed).filter( UserSeed.user == user).one() user_seed.seed = rand() db.session.commit() return True else: default_seed = (db.session.query(UserSeed).filter( UserSeed.user == None).one()) default_seed.seed = rand() db.session.commit() return True return False
class Category(BaseModel): __tablename__ = "category" name = db.Column(db.String(128), unique=True, nullable=False) image = db.Column(db.String(1024), nullable=True) background = db.Column(db.String(1024), nullable=True) def __repr__(self): return f'<Category> "{self.name}"' @classmethod def get_or_create(cls, category_name: str): try: category = ( db.session.query(Category).filter(Category.name == category_name).one() ) return category except: category = Category(name=category_name) db.session.add(category) db.session.commit() return category @classmethod def all(cls) -> list: categories = db.session.query(Category).all() return categories @property def count_words(self) -> int: count_words = ( db.session.query(CategoryCards) .filter(CategoryCards.category_id == self.id) .count() ) return count_words def get_first_card_id(self) -> int: category_card = ( db.session.query(CategoryCards) .filter(CategoryCards.category_id == self.id) .order_by(CategoryCards.category_id) .first() ) return category_card.card_id
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) body = db.Column(db.Text) pub_date = db.Column(db.DateTime) category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', backref=db.backref('posts', lazy='dynamic')) def __init__(self, title, body, category, pub_date=None): self.title = title self.body = body if pub_date is None: pub_date = datetime.utcnow() self.pub_date = pub_date self.category = category def __repr__(self): return '<Post %r>' % self.title
class CardStats(BaseModel): __tablename__ = "card_stats" card_id = db.Column(db.Integer, db.ForeignKey("card.id"), nullable=False) card = db.relationship("Card") user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) user = db.relationship("User") vote = db.Column(db.Integer, nullable=False) created_at = db.Column( db.DateTime(), default=datetime.now, index=True, nullable=False ) @classmethod def add(cls, card, user, vote) -> bool: result = False try: card_stat = cls(card=card, user=user, vote=vote) db.session.add(card_stat) db.session.commit() return True except: db.session.rollback() return False @classmethod def get_stats(cls, card_id, user_id: int = None) -> tuple: positive_votes = cls.get_positive_votes(card_id) negative_votes = cls.get_negative_votes(card_id) return positive_votes, negative_votes @classmethod def get_positive_votes(cls, card_id: str) -> int: return ( db.session.query(cls).filter(cls.card_id == card_id, cls.vote == 1).count() ) @classmethod def get_negative_votes(cls, card_id: str) -> int: return ( db.session.query(cls).filter(cls.card_id == card_id, cls.vote == -1).count() )
class BaseModel(db.Model): __abstract__ = True __table_args__ = {"sqlite_autoincrement": True} # remove if postgres id = db.Column(db.Integer, primary_key=True)
class Card(BaseModel): __tablename__ = "card" original_word = db.Column(db.String(128), unique=True, nullable=False) translated_word = db.Column(db.String(128), nullable=False) active = db.Column(db.Boolean(), default=True) created_at = db.Column(db.DateTime(), default=datetime.now) def vote_yes(self, user_id): return CardStats.get_positive_votes(self.id) def vote_no(self, user_id): return CardStats.get_negative_votes(self.id) def __repr__(self) -> str: return f'<Card>: "{self.original_word}, {self.translated_word}"' @classmethod def list_add(cls, words: list, category_name: str) -> bool: custom_translate = None if len(words) < 1: raise Exception("Length of list with words must me more than 1.") category = Category.get_or_create(category_name) for word in words: if ":" in word: word, translated_word = word.split(":")[0], word.split(":")[1] else: translated_word = translate(word) sleep(0.5) card = db.session.query(Card).filter(Card.original_word == word).first() if card is None: try: card = Card(original_word=word, translated_word=translated_word) db.session.add(card) db.session.commit() category_and_card = CategoryCards( category_id=category.id, card_id=card.id ) db.session.add(category_and_card) db.session.commit() except Exception as e: db.session.rollback() print(f"Not added, {card}, {e}") else: category_and_card = ( db.session.query(CategoryCards) .filter( CategoryCards.card_id == card.id, CategoryCards.category_id == category.id, ) .first() ) if category_and_card is None: category_and_card = CategoryCards( category_id=category.id, card_id=card.id ) db.session.add(category_and_card) db.session.commit() return True def get_related_categories(self) -> list: related_categories = list() category_and_card = ( db.session.query(CategoryCards) .filter(CategoryCards.card_id == self.id) .all() ) for elem in category_and_card: category = db.session.query(Category).get(elem.category_id) related_categories.append(category.name) related_categories = ", ".join(related_categories) return related_categories
class CategoryCards(BaseModel): __tablename__ = "categories_cards" category_id = db.Column(db.Integer, db.ForeignKey("category.id")) # category = db.relationship('Category') card_id = db.Column(db.Integer, db.ForeignKey("card.id"))