class Message(db.Model, Base): __tablename__ = 'message' id = db.Column(db.Integer, primary_key=True) from_user_id = db.Column(db.Integer, db.ForeignKey('user.id')) to_user_id = db.Column(db.Integer, db.ForeignKey('user.id')) to_player_id = db.Column(db.Integer, db.ForeignKey('player.id')) sent_at = db.Column(db.DateTime, default=datetime.utcnow) seen_at = db.Column(db.DateTime) notified_at = db.Column(db.DateTime) body = db.Column(db.Text()) body_html = db.Column(db.Text()) user_ip = db.Column(db.String(15)) deleted = db.Column(db.Boolean, default=False) from_user = db.relationship('User', foreign_keys='Message.from_user_id', backref=db.backref('messages_sent')) to_user = db.relationship('User', foreign_keys='Message.to_user_id', backref=db.backref('messages_received')) to_player = db.relationship('Player', foreign_keys='Message.to_player_id', backref=db.backref('messages_received')) def save(self, commit=True): from standardweb.lib import forums self.body_html = forums.convert_bbcode(self.body) for pat, path in forums.emoticon_map: self.body_html = pat.sub(path, self.body_html) return super(Message, self).save(commit) def to_dict(self): result = { 'id': self.id, 'sent_at': self.sent_at.replace(tzinfo=pytz.UTC).isoformat(), 'seen_at': self.seen_at.replace( tzinfo=pytz.UTC).isoformat() if self.seen_at else None, 'from_user': self.from_user.to_dict(), 'body_html': self.body_html } if self.to_user: result['to_user'] = self.to_user.to_dict() return result
class ForumPost(db.Model, Base): __tablename__ = 'forum_post' id = db.Column(db.Integer, primary_key=True) topic_id = db.Column(db.Integer, db.ForeignKey('forum_topic.id')) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) created = db.Column(db.DateTime, default=datetime.utcnow) updated = db.Column(db.DateTime, default=None) updated_by_id = db.Column(db.Integer, db.ForeignKey('user.id')) body = db.Column(db.Text()) body_html = db.Column(db.Text()) user_ip = db.Column(db.String(15)) score = db.Column(db.Numeric(), default=0) deleted = db.Column(db.Boolean, default=False) topic = db.relationship('ForumTopic', foreign_keys='ForumPost.topic_id') user = db.relationship('User', foreign_keys='ForumPost.user_id') updated_by = db.relationship('User', foreign_keys='ForumPost.updated_by_id') @property def url(self): return url_for('forum_post', post_id=self.id) @property def is_bad(self): return self.score < app.config['BAD_POST_THRESHOLD'] @property def grouped_votes(self): from standardweb.lib import forums return forums.grouped_votes(self.votes) def get_body_html(self, highlight=None): if highlight: return re.sub(r'(%s)' % re.escape(highlight), r'<span class="search-match">\1</span>', self.body_html, flags=re.IGNORECASE) return self.body_html def save(self, commit=True): from standardweb.lib import forums self.body_html = forums.convert_bbcode(self.body) for pat, path in forums.emoticon_map: self.body_html = pat.sub(path, self.body_html) return super(ForumPost, self).save(commit)
class ForumProfile(db.Model, Base): __tablename__ = 'forum_profile' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) post_count = db.Column(db.Integer, default=0) signature = db.Column(db.Text()) signature_html = db.Column(db.Text()) user = db.relationship('User', backref=db.backref('forum_profile', uselist=False)) @property def last_post(self): post = ForumPost.query.filter( ForumPost.deleted == False, ForumPost.user_id == self.user_id).order_by( ForumPost.created.desc()).limit(1).first() return post
class ForumBan(db.Model, Base): __tablename__ = 'forum_ban' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) ban_start = db.Column(db.DateTime, default=datetime.utcnow) reason = db.Column(db.Text()) by_user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', foreign_keys='ForumBan.user_id', backref=db.backref('forum_ban', uselist=False)) by_user = db.relationship('User', foreign_keys='ForumBan.by_user_id')
class ForumAttachment(db.Model, Base): __tablename__ = 'forum_attachment' id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey('forum_post.id')) size = db.Column(db.Integer()) content_type = db.Column(db.String(255)) path = db.Column(db.String(255)) name = db.Column(db.Text()) hash = db.Column(db.String(40)) post = db.relationship('ForumPost', backref=db.backref('attachments')) @classmethod def create_attachment(cls, post_id, image, commit=True): try: content_type = image.headers.get('Content-Type') image_content = image.content size = len(image_content) path = str(post_id) attachment = cls(post_id=post_id, size=size, content_type=content_type, path=path, name=image.filename) attachment.save(commit=commit) with open(attachment.file_path, 'w') as f: f.write(image_content) return attachment except: return None def save(self, commit=True): import hashlib self.hash = hashlib.sha1(self.path + app.config['SECRET_KEY']).hexdigest() return super(ForumAttachment, self).save(commit) @property def url(self): return url_for('forum_attachment', hash=self.hash) @property def file_path(self): return os.path.join(app.root_path, 'attachments', self.path)
class ForumPostTracking(db.Model, Base): __tablename__ = 'forum_posttracking' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) topics = db.Column(db.Text(), default=None) last_read = db.Column(db.DateTime, default=None) user = db.relationship('User', backref=db.backref('posttracking', uselist=False)) def get_topics(self): try: return json.loads(self.topics) if self.topics else None except ValueError: return None def set_topics(self, topics): self.topics = json.dumps(topics)
class Forum(db.Model, Base): __tablename__ = 'forum' id = db.Column(db.Integer, primary_key=True) category_id = db.Column(db.Integer, db.ForeignKey('forum_category.id')) name = db.Column(db.String(80)) position = db.Column(db.Integer, default=0) description = db.Column(db.Text()) updated = db.Column(db.DateTime, default=datetime.utcnow) post_count = db.Column(db.Integer, default=0) topic_count = db.Column(db.Integer, default=0) last_post_id = db.Column(db.Integer, db.ForeignKey('forum_post.id')) locked = db.Column(db.Boolean) category = db.relationship('ForumCategory') topics = db.relationship('ForumTopic') last_post = db.relationship('ForumPost') @property def url(self): return url_for('forum', forum_id=self.id)