123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- # -*- coding: utf-8 -*-
- """
- flaskbb.forum.models
- ~~~~~~~~~~~~~~~~~~~~
- It provides the models for the forum
- :copyright: (c) 2013 by the FlaskBB Team.
- :license: BSD, see LICENSE for more details.
- """
- from datetime import datetime
- from flaskbb.extensions import db
- from flaskbb.helpers import DenormalizedText
- from helpers import get_forum_ids
- class Post(db.Model):
- __tablename__ = "posts"
- id = db.Column(db.Integer, primary_key=True)
- topic_id = db.Column(db.Integer, db.ForeignKey("topics.id", use_alter=True,
- name="fk_topic_id",
- ondelete="CASCADE"))
- user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
- content = db.Column(db.Text)
- date_created = db.Column(db.DateTime, default=datetime.utcnow())
- date_modified = db.Column(db.DateTime)
- def save(self, user=None, topic=None):
- # update/edit the post
- if self.id:
- db.session.add(self)
- db.session.commit()
- return self
- # Adding a new post
- if user and topic:
- self.user_id = user.id
- self.topic_id = topic.id
- self.date_created = datetime.utcnow()
- # This needs to be done before I update the last_post_id.
- db.session.add(self)
- db.session.commit()
- # Now lets update the last post id
- # TODO: Invalidate relevant caches.
- #topic.last_post_id = self.id
- #topic.forum.last_post_id = self.id
- # Update the post counts
- # TODO: Invalidate relevant caches.
- #user.post_count += 1
- #topic.post_count += 1
- #topic.forum.post_count += 1
- # And commit it!
- db.session.add(topic)
- db.session.commit()
- return self
- def delete(self):
- # This will delete the whole topic
- if self.topic.first_post_id == self.id:
- self.topic.delete()
- return self
- # Delete the last post
- if self.topic.last_post.id == self.id:
- # Now the second last post will be the last post
- # TODO: Invalidate relevant caches
- #self.topic.last_post_id = self.topic.second_last_post
- #self.topic.forum.last_post_id = self.topic.second_last_post
- db.session.commit()
- # Update the post counts
- # TODO: Invalidate relevant caches
- #self.user.post_count -= 1
- #self.topic.post_count -= 1
- #self.topic.forum.post_count -= 1
- # Is there a better way to do this?
- db.session.delete(self)
- db.session.commit()
- return self
- class Topic(db.Model):
- __tablename__ = "topics"
- id = db.Column(db.Integer, primary_key=True)
- forum_id = db.Column(db.Integer, db.ForeignKey("forums.id", use_alter=True,
- name="fk_forum_id"))
- title = db.Column(db.String)
- user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
- date_created = db.Column(db.DateTime, default=datetime.utcnow())
- locked = db.Column(db.Boolean, default=False)
- important = db.Column(db.Boolean, default=False)
- views = db.Column(db.Integer, default=0)
- # One-to-many
- posts = db.relationship("Post", backref="topic", lazy="joined",
- primaryjoin="Post.topic_id == Topic.id",
- cascade="all, delete-orphan", post_update=True)
- def __init__(self, title=None):
- if title:
- self.title = title
- @property
- def post_count(self):
- """
- Returns the amount of posts within the current topic.
- """
- # TODO: Cache
- return Post.query.\
- filter(Post.topic_id == self.id).\
- count()
- @property
- def first_post(self):
- """
- Returns the first post within the current topic.
- """
- # TODO: Cache this method.
- return Post.query.\
- filter(Post.topic_id == self.id).\
- order_by(Post.date_created.asc()).\
- first()
- @property
- def last_post(self):
- """
- Returns the latest post within the current topic.
- """
- # TODO: Cache this method.
- return Post.query.\
- filter(Post.topic_id == self.id).\
- order_by(Post.date_created.desc()).\
- first()
- @property
- def second_last_post(self):
- """
- Returns the second last post.
- """
- return self.posts[-2].id
- def save(self, user=None, forum=None, post=None):
- # Updates the topic - Because the thread title (by intention)
- # isn't change able, so we are just going to update the post content
- if self.id:
- db.session.add(self)
- db.session.commit()
- return self
- # Set the forum and user id
- self.forum_id = forum.id
- self.user_id = user.id
- # Update the topic count
- forum.topic_count += 1
- # Insert and commit the topic
- db.session.add(self)
- db.session.commit()
- # Create the topic post
- post.save(user, self)
- # Update the first post id
- # TODO: Invalidate first_post cache
- #self.first_post_id = post.id
- db.session.commit()
- return self
- def delete(self, users=None):
- # TODO: Invalidate forum last post caches
- #topic = Topic.query.filter_by(forum_id=self.forum_id).\
- # order_by(Topic.last_post_id.desc())
- #
- #if topic and topic[0].id == self.id:
- # try:
- # self.forum.last_post_id = topic[1].last_post_id
- #Catch an IndexError when you delete the last topic in the forum
- #except IndexError:
- # self.forum.last_post_id = 0
- # These things needs to be stored in a variable before they are deleted
- forum = self.forum
- # Delete the topic
- db.session.delete(self)
- db.session.commit()
- # Update the post counts
- # TODO: Invalidate relevant caches
- #if users:
- # If someone knows a better method for this,
- # feel free to improve it :)
- #for user in users:
- # user.post_count = Post.query.filter_by(user_id=user.id).count()
- # db.session.commit()
- #forum.topic_count = Topic.query.filter_by(
- # forum_id=self.forum_id).count()
- #
- #forum.post_count = Post.query.filter(
- # Post.topic_id == Topic.id,
- # Topic.forum_id == self.forum_id).count()
- db.session.commit()
- return self
- class Forum(db.Model):
- __tablename__ = "forums"
- id = db.Column(db.Integer, primary_key=True)
- title = db.Column(db.String)
- description = db.Column(db.String)
- position = db.Column(db.Integer, default=0)
- is_category = db.Column(db.Boolean, default=False)
- parent_id = db.Column(db.Integer, db.ForeignKey("forums.id"))
- # One-to-many
- topics = db.relationship("Topic", backref="forum", lazy="joined")
- children = db.relationship("Forum", backref=db.backref("parent", remote_side=[id]))
- moderators = db.Column(DenormalizedText) # TODO: No forum_moderators column?
- @property
- def post_count(self, include_children=True):
- """
- Returns the amount of posts within the current forum or it's children.
- Children can be excluded by setting the second parameter to 'false'.
- """
- # TODO: Cache
- if include_children:
- return Post.query.\
- filter(Post.topic_id == Topic.id). \
- filter(Topic.forum_id.in_(get_forum_ids(self))). \
- count()
- else:
- return Post.query.\
- filter(Post.topic_id == Topic.id).\
- filter(Topic.forum_id == self.id).\
- count()
- @property
- def topic_count(self, include_children=True):
- """
- Returns the amount of topics within the current forum or it's children.
- Children can be excluded by setting the second parameter to 'false'.
- """
- # TODO: Cache
- if include_children:
- return Topic.query.\
- filter(Topic.forum_id.in_(get_forum_ids(self))). \
- count()
- else:
- return Topic.query.\
- filter(Topic.forum_id == self.id).\
- count()
- @property
- def last_post(self, include_children=True):
- """
- Returns the latest post within the current forum or it's children.
- Children can be excluded by setting the second parameter to 'false'.
- """
- # TODO: Cache this method.
- if include_children:
- return Post.query.\
- filter(Post.topic_id == Topic.id). \
- filter(Topic.forum_id.in_(get_forum_ids(self))). \
- order_by(Post.date_created.desc()). \
- first()
- else:
- return Post.query.\
- filter(Post.topic_id == Topic.id).\
- filter(Topic.forum_id == self.id).\
- order_by(Post.date_created.desc()).\
- first()
- def add_moderator(self, user_id):
- self.moderators.add(user_id)
- def remove_moderator(self, user_id):
- self.moderators.remove(user_id)
- def save(self):
- db.session.add(self)
- db.session.commit()
- return self
- def delete(self):
- db.session.delete(self)
- db.session.commit()
- return self
- @classmethod
- def get_categories(cls):
- return cls.query.filter(cls.is_category)
- def get_breadcrumbs(self):
- breadcrumbs = []
- parent = self.parent
- while parent is not None:
- breadcrumbs.append(parent)
- parent = parent.parent
- breadcrumbs.reverse()
- return breadcrumbs
- """
- A topic can be tracked by many users
- and a user can track many topics.. so it's a many-to-many relationship
- """
- topictracker = db.Table('topictracker',
- db.Column('user_id', db.Integer(), db.ForeignKey('users.id')),
- db.Column('topic_id', db.Integer(), db.ForeignKey('topics.id')))
- class Tracking(db.Model):
- """
- This model tracks the unread/read posts
- Note: This functionality isn't implemented yet, but this will be the next
- feature after the TopicTracker
- """
- __tablename__ = "tracking"
- id = db.Column(db.Integer, primary_key=True)
- user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
- topic_id = db.Column(db.Integer, db.ForeignKey("topics.id"))
- last_read = db.Column(db.DateTime, default=datetime.utcnow())
- def save(self):
- db.session.add(self)
- db.session.commit()
- return self
- def delete(self):
- db.session.delete(self)
- db.session.commit()
- return self
|