-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
48 lines (30 loc) · 1.15 KB
/
database.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
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import configs
engine = create_engine(configs.db_url)
session = sessionmaker(bind=engine)()
base = declarative_base()
class Article(base):
__tablename__ = 'article'
link = Column(String, primary_key=True, nullable=False, unique=True)
title = Column(String, nullable=False)
article = Column(String, nullable=False)
date = Column(String, nullable=False)
class User(base):
__tablename__ = 'user'
telegram_id = Column(Integer, primary_key=True, unique=True)
def init_db():
base.metadata.create_all(engine)
def add_user(telegram_id):
user = User(telegram_id=telegram_id)
session.add(user)
session.commit()
def get_users():
return session.query(User.telegram_id).all()
def remove_user(telegram_id):
session.query(User).filter(User.telegram_id == telegram_id).delete()
session.commit()
def check_if_user_exists(telegram_id):
exists = session.query(session.query(User).filter(User.telegram_id == telegram_id).exists()).scalar()
return exists