-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
124 lines (99 loc) · 3.75 KB
/
model.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from datetime import datetime
from sqlalchemy import Column, ForeignKey, Integer, String, Text, DateTime, UniqueConstraint
from sqlalchemy.orm import relationship
from base import Base, Session
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
telegram_id = Column(Integer, index=True, unique=True)
name = Column(String(250))
def __init__(self, telegram_id, name):
self.telegram_id = telegram_id
self.name = name
@staticmethod
def find(telegram_id):
results = Session().query(User).filter_by(telegram_id=telegram_id).first()
return results
@staticmethod
def insert(telegram_id, name):
user = User(telegram_id, name)
Session().add(user)
return user
@staticmethod
def find_create(telegram_id, name):
user = User.find(telegram_id)
if not user:
user = User.insert(telegram_id, name)
if user.name != name:
user.name = name
return user
class Channel(Base):
__tablename__ = 'channel'
id = Column(Integer, primary_key=True)
name = Column(String(250))
telegram_id = Column(Integer, index=True, unique=True)
def __init__(self, telegram_id, name):
self.telegram_id = telegram_id
self.name = name
@staticmethod
def insert(telegram_id, name):
channel = Channel(telegram_id, name)
Session().add(channel)
return channel
@staticmethod
def find_create(telegram_id, name):
channel = Channel.find(telegram_id)
if not channel:
channel = Channel.insert(telegram_id, name)
return channel
@staticmethod
def find(telegram_id):
return Session().query(Channel).filter_by(telegram_id=telegram_id).first()
class Definition(Base):
__tablename__ = 'definition'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('user.id'), nullable=False)
# create 1 to many relationship
user = relationship('User', backref='user')
channel_id = Column(Integer, ForeignKey('channel.id'), index=True)
channel = relationship('Channel', backref='channel')
term = Column(String(250), nullable=False)
content = Column(Text, nullable=False)
created = Column(DateTime, nullable=False)
updated = Column(DateTime, onupdate=datetime.now)
__table_args__ = (UniqueConstraint('channel_id', 'term', name='channel_term_uc'),)
def __init__(self, term, content, user, channel):
self.user = user
self.channel = channel
self.term = term
self.content = content
self.created = datetime.now()
@staticmethod
def find_term(channel_telegram_id, term):
return Session().query(Definition).join(Channel)\
.filter(Channel.telegram_id == channel_telegram_id)\
.filter(Definition.term == term)\
.first()
@staticmethod
def find_all(channel_telegram_id):
return Session().query(Definition).join(Channel) \
.filter(Channel.telegram_id == channel_telegram_id) \
.order_by(Definition.term) \
.all()
@staticmethod
def insert(user, channel, term, term_content):
definition = Definition(term, term_content, user, channel)
Session().add(definition)
return definition
@staticmethod
def insert_update(user, channel, term, term_content):
definition = Definition.find_term(channel.telegram_id, term)
if not definition:
Definition.insert(user, channel, term, term_content)
else:
definition.content = term_content
definition.user = user
@staticmethod
def delete(definition):
if definition:
Session().delete(definition)