本文整理汇总了Python中flaskbb.forum.models.Topic类的典型用法代码示例。如果您正苦于以下问题:Python Topic类的具体用法?Python Topic怎么用?Python Topic使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Topic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
def save(self, user, forum):
topic = Topic(title=self.title.data)
post = Post(content=self.content.data)
if self.track_topic.data:
user.track_topic(topic)
return topic.save(user=user, forum=forum, post=post)
示例2: test_topic_merge_other_forum
def test_topic_merge_other_forum(topic):
"""You cannot merge a topic with a topic from another forum."""
forum_other = Forum(title="Test Forum 2", category_id=1)
forum_other.save()
topic_other = Topic(title="Test Topic 2")
post_other = Post(content="Test Content 2")
topic_other.save(user=topic.user, forum=forum_other, post=post_other)
assert not topic.merge(topic_other)
示例3: save
def save(self, user, forum):
topic = Topic(title=self.title.data, content=self.content.data)
if self.track_topic.data:
user.track_topic(topic)
else:
user.untrack_topic(topic)
current_app.pluggy.hook.flaskbb_form_topic_save(form=self, topic=topic)
return topic.save(user=user, forum=forum)
示例4: create_test_data
def create_test_data():
"""
Creates 5 users, 2 categories and 2 forums in each category. It also opens
a new topic topic in each forum with a post.
"""
create_default_groups()
create_default_settings()
# create 5 users
for u in range(1, 6):
username = "test%s" % u
email = "test%[email protected]" % u
user = User(username=username, password="test", email=email)
user.primary_group_id = u
user.save()
user1 = User.query.filter_by(id=1).first()
user2 = User.query.filter_by(id=2).first()
# create 2 categories
for i in range(1, 3):
category_title = "Test Category %s" % i
category = Category(title=category_title,
description="Test Description")
category.save()
# create 2 forums in each category
for j in range(1, 3):
if i == 2:
j += 2
forum_title = "Test Forum %s %s" % (j, i)
forum = Forum(title=forum_title, description="Test Description",
category_id=i)
forum.save()
# create a topic
topic = Topic()
post = Post()
topic.title = "Test Title %s" % j
post.content = "Test Content"
topic.save(post=post, user=user1, forum=forum)
# create a second post in the forum
post = Post()
post.content = "Test Post"
post.save(user=user2, topic=topic)
示例5: test_topic_merge
def test_topic_merge(topic):
"""Tests the topic merge method."""
topic_other = Topic(title="Test Topic Merge")
post = Post(content="Test Content Merge")
topic_other.save(post=post, user=topic.user, forum=topic.forum)
# Save the last_post_id in another variable because topic_other will be
# overwritten later
last_post_other = topic_other.last_post_id
assert topic_other.merge(topic)
# I just want to be sure that the topic is deleted
topic_other = Topic.query.filter_by(id=topic_other.id).first()
assert topic_other is None
assert topic.post_count == 2
assert topic.last_post_id == last_post_other
示例6: test_topic_save
def test_topic_save(forum, normal_user):
post = Post(content="Test Content")
topic = Topic(title="Test Title")
assert forum.last_post_id is None
assert forum.post_count == 0
assert forum.topic_count == 0
topic.save(forum=forum, post=post, user=normal_user)
assert topic.title == "Test Title"
# The first post in the topic is also the last post
assert topic.first_post_id == post.id
assert topic.last_post_id == post.id
assert forum.last_post_id == post.id
assert forum.post_count == 1
assert forum.topic_count == 1
示例7: create_test_data
def create_test_data():
create_default_groups()
# create 5 users
for u in range(1, 6):
username = "test%s" % u
email = "test%[email protected]" % u
user = User(username=username, password="test", email=email)
user.primary_group_id = u
user.save()
# create a category
category = Forum(is_category=True, title="Test Category",
description="Test Description")
category.save()
# create 2 forums in the category
for i in range(1, 3):
forum_title = "Test Forum %s " % i
forum = Forum(title=forum_title, description="Test Description",
parent_id=category.id)
forum.save()
# Create a subforum
subforum_title = "Test Subforum %s " % i
subforum = Forum(title=subforum_title,
description="Test Description", parent_id=forum.id)
subforum.save()
user = User.query.filter_by(id=1).first()
# create 1 topic in each forum
for i in range(2, 6): # Forum ids are not sequential because categories.
forum = Forum.query.filter_by(id=i).first()
topic = Topic()
post = Post()
topic.title = "Test Title %s" % (i-1)
post.content = "Test Content"
topic.save(user=user, forum=forum, post=post)
示例8: create_welcome_forum
def create_welcome_forum():
"""
This will create the `welcome forum` that nearly every
forum software has after the installation process is finished
"""
if User.query.count() < 1:
raise "You need to create the admin user first!"
user = User.query.filter_by(id=1).first()
category = Category(title="My Category", position=1)
category.save()
forum = Forum(title="Welcome", description="Your first forum",
category_id=category.id)
forum.save()
topic = Topic(title="Welcome!")
post = Post(content="Have fun with your new FlaskBB Forum!")
topic.save(user=user, forum=forum, post=post)
示例9: create_welcome_forum
def create_welcome_forum():
"""This will create the `welcome forum` with a welcome topic.
Returns True if it's created successfully.
"""
if User.query.count() < 1:
return False
user = User.query.filter_by(id=1).first()
category = Category(title="My Category", position=1)
category.save()
forum = Forum(title="Welcome", description="Your first forum",
category_id=category.id)
forum.save()
topic = Topic(title="Welcome!")
post = Post(content="Have fun with your new FlaskBB Forum!")
topic.save(user=user, forum=forum, post=post)
return True
示例10: test_topic_save
def test_topic_save(forum, user):
"""Test the save topic method with creating and editing a topic."""
post = Post(content="Test Content")
topic = Topic(title="Test Title")
assert forum.last_post_id is None
assert forum.post_count == 0
assert forum.topic_count == 0
topic.save(forum=forum, post=post, user=user)
assert topic.title == "Test Title"
topic.title = "Test Edit Title"
topic.save()
assert topic.title == "Test Edit Title"
# The first post in the topic is also the last post
assert topic.first_post_id == post.id
assert topic.last_post_id == post.id
assert forum.last_post_id == post.id
assert forum.post_count == 1
assert forum.topic_count == 1
示例11: insert_mass_data
def insert_mass_data(topics=100, posts=100):
"""Creates a few topics in the first forum and each topic has
a few posts. WARNING: This might take very long!
Returns the count of created topics and posts.
:param topics: The amount of topics in the forum.
:param posts: The number of posts in each topic.
"""
user1 = User.query.filter_by(id=1).first()
user2 = User.query.filter_by(id=2).first()
forum = Forum.query.filter_by(id=1).first()
created_posts = 0
created_topics = 0
if not (user1 or user2 or forum):
return False
# create 1000 topics
for i in range(1, topics + 1):
# create a topic
topic = Topic()
post = Post()
topic.title = "Test Title %s" % i
post.content = "Test Content"
topic.save(post=post, user=user1, forum=forum)
created_topics += 1
# create 100 posts in each topic
for j in range(1, posts + 1):
post = Post()
post.content = "Test Post"
post.save(user=user2, topic=topic)
created_posts += 1
return created_topics, created_posts
示例12: insert_mass_data
def insert_mass_data():
"""
Creates 100 topics in the first forum and each topic has 100 posts.
"""
user1 = User.query.filter_by(id=1).first()
user2 = User.query.filter_by(id=2).first()
forum = Forum.query.filter_by(id=1).first()
# create 1000 topics
for i in range(1, 101):
# create a topic
topic = Topic()
post = Post()
topic.title = "Test Title %s" % i
post.content = "Test Content"
topic.save(post=post, user=user1, forum=forum)
# create 100 posts in each topic
for j in range(1, 100):
post = Post()
post.content = "Test Post"
post.save(user=user2, topic=topic)
示例13: post
def post(self, topic_id, slug=None):
topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
form = self.form()
if not form:
flash(_("Cannot post reply"), "warning")
return redirect("forum.view_topic", topic_id=topic_id, slug=slug)
elif form.validate_on_submit():
post = form.save(real(current_user), topic)
return redirect(url_for("forum.view_post", post_id=post.id))
else:
for e in form.errors.get("content", []):
flash(e, "danger")
return redirect(
url_for("forum.view_topic", topic_id=topic_id, slug=slug)
)
示例14: get
def get(self, topic_id, slug=None):
page = request.args.get("page", 1, type=int)
# Fetch some information about the topic
topic = Topic.get_topic(topic_id=topic_id, user=real(current_user))
# Count the topic views
topic.views += 1
topic.save()
# Update the topicsread status if the user hasn't read it
forumsread = None
if current_user.is_authenticated:
forumsread = ForumsRead.query.filter_by(
user_id=current_user.id,
forum_id=topic.forum_id).first()
topic.update_read(real(current_user), topic.forum, forumsread)
# fetch the posts in the topic
posts = Post.query.outerjoin(
User, Post.user_id == User.id
).filter(
Post.topic_id == topic.id
).add_entity(
User
).order_by(
Post.id.asc()
).paginate(page, flaskbb_config["POSTS_PER_PAGE"], False)
# Abort if there are no posts on this page
if len(posts.items) == 0:
abort(404)
return render_template(
"forum/topic.html",
topic=topic,
posts=posts,
last_seen=time_diff(),
form=self.form()
)
示例15: view_topic
def view_topic(topic_id, slug=None):
page = request.args.get('page', 1, type=int)
# Fetch some information about the topic
topic = Topic.get_topic(topic_id=topic_id, user=current_user)
# Count the topic views
topic.views += 1
topic.save()
# fetch the posts in the topic
posts = Post.query.\
outerjoin(User, Post.user_id == User.id).\
filter(Post.topic_id == topic.id).\
add_entity(User).\
order_by(Post.id.asc()).\
paginate(page, flaskbb_config['POSTS_PER_PAGE'], False)
# Abort if there are no posts on this page
if len(posts.items) == 0:
abort(404)
# Update the topicsread status if the user hasn't read it
forumsread = None
if current_user.is_authenticated:
forumsread = ForumsRead.query.\
filter_by(user_id=current_user.id,
forum_id=topic.forum.id).first()
topic.update_read(current_user, topic.forum, forumsread)
form = None
if Permission(CanPostReply):
form = QuickreplyForm()
if form.validate_on_submit():
post = form.save(current_user, topic)
return view_post(post.id)
return render_template("forum/topic.html", topic=topic, posts=posts,
last_seen=time_diff(), form=form)