-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathdb.py
More file actions
executable file
·170 lines (139 loc) · 5.31 KB
/
db.py
File metadata and controls
executable file
·170 lines (139 loc) · 5.31 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# **************************************************************************
# Copyright © 2016 jianglin
# File Name: db.py
# Author: jianglin
# Email: mail@honmaple.com
# Created: 2016-12-15 20:52:07 (CST)
# Last Update: Monday 2022-12-12 15:22:48 (CST)
# By:
# Description:
# **************************************************************************
from datetime import datetime
from flask_login import current_user
from flask_maple.models import ModelMixin
from forums.extension import db
from forums.count import Count
from forums.jinja import safe_clean, markdown
topic_follower = db.Table(
'topic_follower',
db.Column('topic_id', db.Integer, db.ForeignKey('topics.id')),
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')))
class Topic(db.Model, ModelMixin):
__tablename__ = 'topics'
__searchable__ = ['title', 'content']
CONTENT_TYPE_TEXT = '0'
CONTENT_TYPE_MARKDOWN = '1'
CONTENT_TYPE_ORGMODE = '2'
CONTENT_TYPE = (('0', 'text'), ('1', 'markdown'), ('2', 'org-mode'))
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(81), nullable=False)
content = db.Column(db.Text, nullable=False)
content_type = db.Column(
db.String(10), nullable=False, default=CONTENT_TYPE_MARKDOWN)
created_at = db.Column(
db.DateTime, default=datetime.utcnow(), nullable=False)
updated_at = db.Column(
db.DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())
is_good = db.Column(db.Boolean, default=False)
is_top = db.Column(db.Boolean, default=False)
author_id = db.Column(db.Integer,
db.ForeignKey('user.id', ondelete="CASCADE"))
author = db.relationship(
"User",
backref=db.backref(
'topics', cascade='all,delete-orphan', lazy='dynamic'),
lazy='joined')
board_id = db.Column(db.Integer,
db.ForeignKey('boards.id', ondelete="CASCADE"))
board = db.relationship(
"Board",
backref=db.backref(
'topics', cascade='all,delete-orphan', lazy='dynamic'),
lazy='joined')
followers = db.relationship(
"User",
secondary=topic_follower,
backref=db.backref('following_topics', lazy='dynamic'),
lazy='dynamic')
__mapper_args__ = {"order_by": created_at.desc()}
def is_followed(self, user=None):
if user is None:
user = current_user
return db.session.query(topic_follower).filter(
topic_follower.c.topic_id == self.id,
topic_follower.c.follower_id == user.id).exists()
def is_collected(self, user=None):
if user is None:
user = current_user
return self.collects.filter_by(author_id=user.id).exists()
@property
def text(self):
if self.content_type == Topic.CONTENT_TYPE_TEXT:
return safe_clean(self.content)
elif self.content_type == Topic.CONTENT_TYPE_MARKDOWN:
return markdown(self.content, False)
return self.content
@property
def newest_reply(self):
return self.replies.order_by('-id').first()
@property
def reply_count(self):
return Count.topic_reply_count(self.id)
@reply_count.setter
def reply_count(self, value):
return Count.topic_reply_count(self.id, value)
@property
def read_count(self):
return Count.topic_read_count(self.id)
@read_count.setter
def read_count(self, value):
return Count.topic_read_count(self.id, value)
def __str__(self):
return self.title
def __repr__(self):
return "<Topic %r>" % self.title
reply_liker = db.Table(
'reply_liker',
db.Column('reply_id', db.Integer, db.ForeignKey('replies.id')),
db.Column('liker_id', db.Integer, db.ForeignKey('user.id')))
class Reply(db.Model, ModelMixin):
__tablename__ = 'replies'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.Text, nullable=False)
created_at = db.Column(
db.DateTime, default=datetime.utcnow(), nullable=False)
updated_at = db.Column(
db.DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow())
topic_id = db.Column(db.Integer,
db.ForeignKey('topics.id', ondelete="CASCADE"))
topic = db.relationship(
Topic,
backref=db.backref(
'replies', cascade='all,delete-orphan', lazy='dynamic'),
lazy='joined')
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
author = db.relationship(
"User", backref=db.backref('replies', lazy='dynamic'), lazy='joined')
likers = db.relationship(
"User",
secondary=reply_liker,
backref=db.backref('like_replies', lazy='dynamic'),
lazy='dynamic')
def is_liked(self, user=None):
if user is None:
user = current_user
if not user.is_authenticated:
return False
return self.likers.filter_by(id=user.id).exists()
@property
def liker_count(self):
return Count.reply_liker_count(self.id)
@liker_count.setter
def liker_count(self, value):
return Count.reply_liker_count(self.id, value)
def __str__(self):
return self.content[:10]
def __repr__(self):
return "<Topic %r>" % self.content[:10]