本文整理汇总了Python中biostar.apps.posts.models.Post.save方法的典型用法代码示例。如果您正苦于以下问题:Python Post.save方法的具体用法?Python Post.save怎么用?Python Post.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类biostar.apps.posts.models.Post
的用法示例。
在下文中一共展示了Post.save方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_post
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def create_post(b, author, root=None, parent=None, tag_val=''):
from biostar.apps.posts.models import Post
title = b.subj
body = b.body
if not parent:
title = title.strip()
title = ' '.join(title.splitlines())
title = ' '.join(title.split())
title = title.title()
post = Post(title=title, type=Post.QUESTION, content=body, tag_val=tag_val, author=author)
else:
post_type = Post.ANSWER if parent.is_toplevel else Post.COMMENT
post = Post(type=post_type, content=body, tag_val="galaxy", author=author, parent=parent)
post.creation_date = post.lastedit_date = b.datetime
post.save()
tag_val = guess_tags(post.content, tag_val)
if tag_val:
post.add_tags(tag_val)
logger.info("--- creating %s: %s" % (post.get_type_display(), title))
return post
示例2: test_tagging
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def test_tagging(self):
"Testing tagging."
eq = self.assertEqual
eq(0, Tag.objects.all().count() )
# Create an admin user and a post.
title = "Hello Posts!"
email = "[email protected]"
jane = User.objects.create(email=email)
html = "<b>Hello World!</b>"
post = Post(title=title, author=jane, type=Post.FORUM, content=html)
post.save()
post.add_tags("t1,t2, t3")
eq(3, Tag.objects.all().count())
post = Post(title=title, author=jane, type=Post.FORUM, content=html)
post.save()
post.add_tags("t1, t2, t3, t2, t1, t1")
t1 = Tag.objects.get(name="t1")
t3 = Tag.objects.get(name="t3")
eq(2, t1.count)
eq(2, t3.count)
post.add_tags("t2 t4")
t1 = Tag.objects.get(name="t1")
t3 = Tag.objects.get(name="t3")
eq(1, t1.count)
eq(1, t3.count)
示例3: create_post
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def create_post(self, user, post_type, days=3):
# Create a post.
title = "Post 1, title needs to be sufficiently long"
content = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
'tempor incididunt ut labore et dolore magna aliqua.')
tag_val = 'tag_val'
post = Post(title=title, content=content, tag_val=tag_val, author=user, type=post_type, )
post.save()
post.creation_date = datetime.today() - timedelta(days=days)
post.save()
return post
示例4: create_a_post
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def create_a_post(user):
title = "Post 1, title needs to be sufficiently long"
content = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
'tempor incididunt ut labore et dolore magna aliqua.')
post_type = Post.QUESTION
tag_val = 'tag_val'
post = Post(title=title, content=content, tag_val=tag_val, author=user, type=post_type)
post.save()
# Triggers a new post save.
post.add_tags(post.tag_val)
return post
示例5: ApiPostTest
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
class ApiPostTest(TestCase):
def setUp(self):
# Disable haystack logger (testing will raise errors on more_like_this field in templates).
haystack_logger.setLevel(logging.CRITICAL)
# Create a user.
self.user = User.objects.create(email='[email protected]', password='...')
# Create tags.
t1 = Tag.objects.create(name='mytag1')
t2 = Tag.objects.create(name='mytag2')
# Create a post.
title = "Post 1, title needs to be sufficiently long"
content = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
'tempor incididunt ut labore et dolore magna aliqua.')
post_type = Post.QUESTION
self.post = Post(title=title, content=content, author=self.user, type=post_type)
self.post.save()
self.post.tag_set.add(t1)
self.post.tag_set.add(t2)
self.post.save()
def test_home_page(self):
"""
Ensure that the new post in the home page uses the right tags and links.
"""
r = self.client.get(reverse('home'))
self.assertIn('<a class="tag" href="/t/mytag1/">mytag1</a>', r.content)
self.assertIn('<a class="tag" href="/t/mytag2/">mytag2</a>', r.content)
def test_topic_page_single_tag(self):
"""
Ensure that the new post in the topic page uses the right tags and links when only 1 tag
is selected.
"""
r = self.client.get(reverse('topic-list', kwargs={'topic': 'mytag1'}))
self.assertIn('<a class="tag" href="/t/mytag1/">mytag1</a>', r.content)
self.assertIn('<a class="tag" href="/t/mytag1+mytag2/">mytag2</a>', r.content)
def test_topic_page_multiple_tags(self):
"""
Ensure that the new post in the topic page uses the right tags and links when multiple
tags are selected.
"""
r = self.client.get(reverse('topic-list', kwargs={'topic': 'mytag1+mytag2'}))
self.assertIn('<a class="tag" href="/t/mytag1+mytag2/">mytag1</a>', r.content)
self.assertIn('<a class="tag" href="/t/mytag1+mytag2/">mytag2</a>', r.content)
self.assertIn('Filtering by tags: mytag1 OR mytag2', r.content)
示例6: test_note_creation
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def test_note_creation(self):
"Testing notifications"
eq = self.assertEqual
# Create some users
title = "Test"
emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]",
"[email protected]", "[email protected]" ]
email_count = len(emails)
users, posts, user = [], [], None
parent = None
for email in emails:
# Create users.
user = User.objects.create(email=email)
users.append(user)
# A welcome message for each user.
eq(note_count(), email_count)
# Create a question.
first = users[0]
post = Post(title=title, author=first, type=Post.QUESTION)
post.save()
answers = []
for user in users:
# Every user adds an answer.
answ = Post(author=user, type=Post.ANSWER, parent=post)
answ.save()
answers.append(answ)
# Total number of posts
eq(note_count(), 21)
# Every user has one subscription to the main post
eq(email_count, Subscription.objects.all().count())
# Each user has a messages for content posted after
# they started following the thread.
for index, user in enumerate(users):
mesg_c = Message.objects.filter(user=user).count()
eq (mesg_c, email_count - index )
示例7: setUp
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def setUp(self):
# Disable haystack logger (testing will raise errors on more_like_this field in templates).
haystack_logger.setLevel(logging.CRITICAL)
# Create a user.
self.user = User.objects.create(email='[email protected]', password='...')
# Create a post.
title = "Post 1, title needs to be sufficiently long"
content = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
'tempor incididunt ut labore et dolore magna aliqua.')
post_type = Post.QUESTION
tag_val = 'tag_val'
post = Post(title=title, content=content, tag_val=tag_val, author=self.user,
type=post_type)
post.save()
# Create a vote.
self.vote = Vote.objects.create(author=self.user, post=post, type=Vote.UP)
示例8: import_posts
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def import_posts(fname, uname):
logger.info('Extracting posts from file...')
allposts = get_posts(fname)
logger.info('Extracting users from file...')
allusers = get_all_users(uname)
post_count=0
for single in allposts:
uid = int(single[0])
title = single[1]
body = single[2]
date = single[3]
logger.info('Fetched post : %s' % title)
#Getting the post user
user = get_user(uid,allusers)
#List to hold answer posts which could not be matched
orphans = []
if title.startswith('Re:'):
try:
ptitle = title[4:]
parent = Post.objects.get(title=ptitle)
post = Post(title=title, content=body, author=user, type= Post.ANSWER, creation_date=date)
post.parent=parent
post.root=parent
post.save()
post_count+=1
except:
post = Post(title=title, author=user, type= Post.ANSWER, content=body, creation_date=date)
orphans.append(post)
else:
post = Post(title=title, content=body, author=user, type = Post.QUESTION, creation_date=date)
post.save()
post_count+=1
#Now try to match posts which could not be matched before
if orphans:
logger.info('Matching posts which could not be matched earlier')
for post in orphans:
try:
title = post.title
ptitle = title[4:]
parent = Post.objects.get(title__startswith=ptitle)
post.parent=parent
post.root=parent
post.save()
post_count+=1
except:
pass
print post_count, ' posts created'
logger.info('DONE!')
示例9: test_post_creation
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def test_post_creation(self):
"Testing post creation."
eq = self.assertEqual
# Create an admin user and a post.
title = "Hello Posts!"
email = "[email protected]"
jane = User.objects.create(email=email)
html = "<b>Hello World!</b>"
post = Post(title=title, author=jane, type=Post.FORUM, content=html)
post.save()
# Get the object fresh.
post = Post.objects.get(pk=post.id)
eq(post.type, Post.FORUM)
eq(post.root, post)
eq(post.parent, post)
# Subscriptions are automatically created
sub = Subscription.objects.get(user=jane)
eq(sub.user, jane)
eq(sub.post, post)
# A new post triggers a message to the author.
email = "[email protected]"
john = User.objects.create(email=email)
answer = Post(author=john, parent=post, type=Post.ANSWER)
answer.save()
eq(answer.root, post)
eq(answer.parent, post)
eq(answer.type, Post.ANSWER)
# Add comment. The parent will override the post type.
email = "[email protected]"
bob = User.objects.create(email=email)
comment = Post(author=bob, type=Post.FORUM, parent=answer)
comment.save()
eq(comment.root, post)
eq(comment.parent, answer)
eq(comment.type, Post.COMMENT)
# Everyone posting in a thread gets a subscription to the root post of the
subs = Subscription.objects.filter(post=post)
eq(len(subs), 3)
示例10: post
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
def post(self, request, *args, **kwargs):
user = request.user
post = self.get_obj()
post = post_permissions(request, post)
# The default return url
response = HttpResponseRedirect(post.root.get_absolute_url())
if not post.is_editable:
messages.warning(request, "You may not moderate this post")
return response
# Initialize the form class.
form = self.form_class(request.POST, pk=post.id)
# Bail out on errors.
if not form.is_valid():
messages.error(request, "%s" % form.errors)
return response
# A shortcut to the clean form data.
get = form.cleaned_data.get
# These will be used in updates, will bypasses signals.
query = Post.objects.filter(pk=post.id)
root = Post.objects.filter(pk=post.root_id)
action = get('action')
if action == (OPEN, TOGGLE_ACCEPT) and not user.is_moderator:
messages.error(request, "Only a moderator may open or toggle a post")
return response
if action == TOGGLE_ACCEPT and post.type == Post.ANSWER:
# Toggle post acceptance.
post.has_accepted=not post.has_accepted
post.save()
has_accepted = Post.objects.filter(root=post.root, type=Post.ANSWER, has_accepted=True).count()
root.update(has_accepted=has_accepted)
return response
if action == MOVE_TO_ANSWER and post.type == Post.COMMENT:
# This is a valid action only for comments.
messages.success(request, "Moved post to answer")
query.update(type=Post.ANSWER, parent=post.root)
root.update(reply_count=F("reply_count") + 1)
return response
if action == MOVE_TO_COMMENT and post.type == Post.ANSWER:
# This is a valid action only for answers.
messages.success(request, "Moved post to answer")
query.update(type=Post.COMMENT, parent=post.root)
root.update(reply_count=F("reply_count") - 1)
return response
# Some actions are valid on top level posts only.
if action in (CLOSE_OFFTOPIC, DUPLICATE) and not post.is_toplevel:
messages.warning(request, "You can only close or open a top level post")
return response
if action == OPEN:
query.update(status=Post.OPEN)
messages.success(request, "Opened post: %s" % post.title)
return response
if action in CLOSE_OFFTOPIC:
query.update(status=Post.CLOSED)
messages.success(request, "Closed post: %s" % post.title)
content = html.render(name="messages/offtopic_posts.html", user=post.author, comment=get("comment"), post=post)
comment = Post(content=content, type=Post.COMMENT, parent=post, author=user)
comment.save()
return response
if action == CROSSPOST:
content = html.render(name="messages/crossposted.html", user=post.author, comment=get("comment"), post=post)
comment = Post(content=content, type=Post.COMMENT, parent=post, author=user)
comment.save()
return response
if action == DUPLICATE:
query.update(status=Post.CLOSED)
posts = Post.objects.filter(id__in=get("dupe"))
content = html.render(name="messages/duplicate_posts.html", user=post.author, comment=get("comment"), posts=posts)
comment = Post(content=content, type=Post.COMMENT, parent=post, author=user)
comment.save()
return response
if action == DELETE:
# Delete marks a post deleted but does not remove it.
# Remove means to delete the post from the database with no trace.
# Posts with children or older than some value can only be deleted not removed
# The children of a post.
children = Post.objects.filter(parent_id=post.id).exclude(pk=post.id)
# The condition where post can only be deleted.
delete_only = children or post.age_in_days > 7 or post.vote_count > 1 or (post.author != user)
#.........这里部分代码省略.........
示例11: ApiPostTest
# 需要导入模块: from biostar.apps.posts.models import Post [as 别名]
# 或者: from biostar.apps.posts.models.Post import save [as 别名]
class ApiPostTest(TestCase):
def setUp(self):
# Disable haystack logger (testing will raise errors on more_like_this field in templates).
haystack_logger.setLevel(logging.CRITICAL)
# Create a user.
self.user = User.objects.create(email='[email protected]', password='...')
# Create a post.
title = "Post 1, title needs to be sufficiently long"
content = ('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod'
'tempor incididunt ut labore et dolore magna aliqua.')
post_type = Post.QUESTION
tag_val = 'tag_val'
self.post = Post(title=title, content=content, tag_val=tag_val, author=self.user,
type=post_type)
self.post.save()
# Create a vote.
self.vote = Vote.objects.create(author=self.user, post=self.post, type=Vote.UP)
def test_invalid_post_id(self):
r = self.client.get(reverse('api-post', kwargs={'id': 1115}))
self.assertEqual(r.status_code, 404)
def test_naked_post(self):
"""
A naked post with no replies, no bookmarks, no comments, ...
"""
r = self.client.get(reverse('api-post', kwargs={'id': self.post.id}))
content = json.loads(r.content)
expected_post = {
"answer_count": self.post.root.reply_count,
"author": "test",
"author_id": self.user.id,
"book_count": self.post.book_count,
"comment_count": self.post.comment_count,
"creation_date": datetime_to_iso(self.post.creation_date),
"has_accepted": self.post.has_accepted,
"id": self.post.id,
"lastedit_date": datetime_to_iso(self.post.lastedit_date),
"lastedit_user_id": self.user.id,
"parent_id": self.post.id,
"rank": float(self.post.rank),
"reply_count": self.post.reply_count,
"root_id": self.post.id,
"status": self.post.get_status_display(),
"status_id": self.post.status,
"subs_count": 1,
"tag_val": "tag_val",
"thread_score": self.post.thread_score,
"title": self.post.title,
"type": self.post.get_type_display(),
"type_id": self.post.type,
"url": 'http://example.com{}'.format(self.post.get_absolute_url()),
"view_count": self.post.view_count,
"vote_count": self.post.vote_count,
"xhtml": self.post.content
}
self.assertDictEqual(content, expected_post)
def test_post(self):
"""
Regular post with replies, bookmarks, comments, ...
"""
self.post.reply_count = 3
self.post.book_count = 4
self.post.comment_count = 5
self.post.subs_count = 6
self.post.view_count = 9
self.post.vote_count = 7
self.post.thread_score = 8
self.post.has_accepted = True
self.post.rank = 5.5
self.post.save()
expected_post = {
"answer_count": self.post.root.reply_count,
"author": "test",
"author_id": self.user.id,
"book_count": self.post.book_count,
"comment_count": self.post.comment_count,
"creation_date": datetime_to_iso(self.post.creation_date),
"has_accepted": self.post.has_accepted,
"id": self.post.id,
"lastedit_date": datetime_to_iso(self.post.lastedit_date),
"lastedit_user_id": self.user.id,
"parent_id": self.post.id,
"rank": float(self.post.rank),
"reply_count": self.post.reply_count,
"root_id": self.post.id,
"status": self.post.get_status_display(),
"status_id": self.post.status,
"subs_count": self.post.subs_count,
"tag_val": "tag_val",
"thread_score": self.post.thread_score,
"title": self.post.title,
"type": self.post.get_type_display(),
"type_id": self.post.type,
#.........这里部分代码省略.........