本文整理汇总了Python中forums.models.Post类的典型用法代码示例。如果您正苦于以下问题:Python Post类的具体用法?Python Post怎么用?Python Post使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Post类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post_preview_async
def post_preview_async(request):
"""Ajax preview of posts."""
statsd.incr('forums.preview')
post = Post(author=request.user, content=request.POST.get('content', ''))
post.author_post_count = 1
return jingo.render(request, 'forums/includes/post_preview.html',
{'post_preview': post})
示例2: post_new
def post_new(request, topic_id, quoted_post_id=None):
topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
if topic.is_closed:
if not request.user.is_staff:
messages.error(request, "You are not allowed to post in closed topics.")
return redirect(topic.get_absolute_url())
else:
messages.info(request, "Note: This topic is closed.")
if not can_post_reply(request.user, topic.forum):
messages.error(request, "You are not allowed to reply on this forum.")
return redirect(topic.get_absolute_url())
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
content = form.cleaned_data['content']
post = Post(topic=topic, author=request.user,
author_ip=request.META['REMOTE_ADDR'], content=content)
post.save()
messages.success(request, "Your reply has been saved.")
return redirect(post.get_absolute_url())
else:
form = PostForm()
if quoted_post_id:
try:
quoted_post = Post.objects.get(pk=quoted_post_id, topic=topic)
form.initial = {'content': "[quote=%s]%s[/quote]" %
(quoted_post.author, quoted_post.content)}
except Post.DoesNotExist:
messages.warning(request, "You tried to quote a post which "
"doesn't exist or it doesn't belong to topic you are "
"replying to. Nice try, Kevin.")
return {'topic': topic, 'form': form}
示例3: add_thread
def add_thread(request, forum_id):
# First try finding the (sub)forum
forum = get_object_or_404(Subforum, pk=forum_id)
# Load template
template = loader.get_template('forums/add_thread.html')
# We do different things for POST and GET
if request.method == 'POST':
# Grab the info from the post thingy
try:
title = request.POST['title']
content = request.POST['content']
# If something goes wrong...
except (KeyError):
return render(request, 'forums/add_thread.html', {
'forum': forum,
'error_message': "You didn't provide needed data!",
})
# If we get both info out well?
else:
# If content is empty, error out
if not content or not title:
return render(request, 'forums/add_thread.html', {
'forum': forum,
'error_message': "Please do not leave content or title empty :< !",
})
# Create and write post into the database, wee
t = Thread(subforum=Subforum.objects.get(pk=forum.id),
creator=request.user, title=title, creation_date=timezone.now(),
sticky=False)
if not t:
return render(request, 'forums/add_thread.html', {
'forum': forum,
'error_message': "Gooby please... I have no idea what just happened, but you errored out (thread object creation).",
})
t.save()
p = Post(thread=t, poster=request.user, title=title, content=content,
is_op=True, pub_date=timezone.now())
if not p:
t.delete()
return render(request, 'forums/add_thread.html', {
'forum': forum,
'error_message': "Gooby please... I have no idea what just happened, but you errored out (post object creation).",
})
p.save()
# For good measure, do a HttpResponseRedirect
return HttpResponseRedirect(reverse(show_thread, args=(t.id,)))
else:
return render(request, 'forums/add_thread.html', {
'forum': forum
})
示例4: test_save_new_post_timestamps
def test_save_new_post_timestamps(self):
# Saving a new post should allow you to override auto_add_now-
# and auto_now-like functionality.
created_ = datetime(1992, 1, 12, 10, 12, 32)
p = Post(thread=self.thread, content='bar', author=self.user,
created=created_, updated=created_)
p.save()
eq_(created_, p.created)
eq_(created_, p.updated)
示例5: test_with_activity
def test_with_activity(self):
"""Test the page with some activity."""
# Add a reply
post = Post(thread_id=4, content='lorem ipsum', author_id=118577)
post.save()
# Verify activity on the page
response = self.client.get(reverse('dashboards.review'), follow=True)
eq_(200, response.status_code)
doc = pq(response.content)
eq_(1, len(doc('ol.actions li')))
示例6: test_save_new_post_no_timestamps
def test_save_new_post_no_timestamps(self):
"""
Saving a new post should behave as if auto_add_now was set on
created and auto_now set on updated.
"""
p = Post(thread=self.thread, content='bar', author=self.user)
p.save()
now = datetime.datetime.now()
self.assertDateTimeAlmostEqual(now, p.created, self.delta)
self.assertDateTimeAlmostEqual(now, p.updated, self.delta)
示例7: test_delete_last_and_only_post_in_thread
def test_delete_last_and_only_post_in_thread(self):
"""Deleting the only post in a thread should delete the thread"""
forum = Forum.objects.get(pk=1)
thread = Thread(title="test", forum=forum, creator_id=118533)
thread.save()
post = Post(thread=thread, content="test", author=thread.creator)
post.save()
eq_(1, thread.post_set.count())
post.delete()
eq_(0, Thread.uncached.filter(pk=thread.id).count())
示例8: post_preview_async
def post_preview_async(request):
"""Ajax preview of posts."""
statsd.incr('forums.preview')
post = Post(author=request.user, content=request.POST.get('content', ''))
post.author_post_count = 1
if show_new_sumo(request):
template = 'forums/includes/post_preview-new.html'
else:
template = 'forums/includes/post_preview.html'
return jingo.render(request, template, {'post_preview': post})
示例9: form_valid
def form_valid(self, form):
body = form.cleaned_data['message']
user = self.request.user
post = Post(topic=self.topic, body=body, user=user)
post.save()
post.topic.last_post = post
post.topic.save()
self.success_url = reverse('forums:topic', args=[self.topic.id])
return super(PostCreateView, self).form_valid(form)
示例10: save_model
def save_model(self, request, obj, form, change):
if change:
obj.modified_by = request.user
obj.first_post.modified_by = request.user
obj.first_post.content = form.cleaned_data['content']
obj.first_post.save()
obj.save()
else:
obj.author = request.user
obj.author_ip = request.META['REMOTE_ADDR']
obj.save()
post = Post(topic=obj, author=request.user,
author_ip=request.META['REMOTE_ADDR'],
content=form.cleaned_data['content'])
post.save()
示例11: new_thread
def new_thread(request, forum_slug):
"""Start a new thread."""
forum = get_object_or_404(Forum, slug=forum_slug)
user = request.user
if not forum.allows_posting_by(user):
if forum.allows_viewing_by(user):
raise PermissionDenied
else:
raise Http404
if request.method == 'GET':
form = NewThreadForm()
return jingo.render(request, 'forums/new_thread.html',
{'form': form, 'forum': forum})
form = NewThreadForm(request.POST)
post_preview = None
if form.is_valid():
if 'preview' in request.POST:
thread = Thread(creator=request.user,
title=form.cleaned_data['title'])
post_preview = Post(thread=thread, author=request.user,
content=form.cleaned_data['content'])
post_preview.author_post_count = \
post_preview.author.post_set.count()
else:
thread = forum.thread_set.create(creator=request.user,
title=form.cleaned_data['title'])
thread.save()
statsd.incr('forums.thread')
post = thread.new_post(author=request.user,
content=form.cleaned_data['content'])
post.save()
NewThreadEvent(post).fire(exclude=post.author)
# Add notification automatically if needed.
if Setting.get_for_user(request.user, 'forums_watch_new_thread'):
NewPostEvent.notify(request.user, thread)
url = reverse('forums.posts', args=[forum_slug, thread.id])
return HttpResponseRedirect(urlparams(url, last=post.id))
return jingo.render(request, 'forums/new_thread.html',
{'form': form, 'forum': forum,
'post_preview': post_preview})
示例12: form_valid
def form_valid(self, form):
body = form.cleaned_data['message']
user = UserProfile.objects.get(user=User.objects.get(username=self.request.user.username))
post = Post(topic=self.topic, body=body, user=user)
post.save()
post.topic.last_post = post
post.topic.save()
view = View.objects.filter(topic=self.topic)
for v in view:
v.visited = False
v.save()
topic_page = self.topic.posts_range()[-1]
self.success_url = ('/forums/topic/' + str(self.topic.id) + '/page' + str(topic_page))
return super(PostCreateView, self).form_valid(form)
示例13: add_post
def add_post(request, thread_id):
# First try finding the thread
thread = get_object_or_404(Thread, pk=thread_id)
# Load template
template = loader.get_template('forums/add_post.html')
# We do different things for POST and GET
if request.method == 'POST':
# Grab the info from the post thingy
try:
title = request.POST['title']
content = request.POST['content']
# If something goes wrong...
except (KeyError):
return render(request, 'forums/add_post.html', {
'thread': thread,
'error_message': "You didn't provide needed data!",
})
# If we get both info out well?
else:
# If content is empty, error out
if not content:
return render(request, 'forums/add_post.html', {
'thread': thread,
'error_message': "Please do not leave content empty :< !",
})
# If title is empty we just use Re: <thread title>
if not title:
title = "Re: " + thread.title
# Create and write post into the database, wee
p = Post(thread=thread, poster=request.user, title=title,
content=content, pub_date=timezone.now())
p.save()
# For good measure, do a HttpResponseRedirect
return HttpResponseRedirect(reverse(show_thread, args=(thread.id,)))
# And here is what GET n' shit does
else:
return render(request, 'forums/add_post.html', {
'thread': thread,
})
示例14: test_delete_thread_with_last_forum_post
def test_delete_thread_with_last_forum_post(self):
"""Deleting the thread with a forum's last post should
update the last_post field on the forum
"""
forum = Forum.objects.get(pk=1)
last_post = forum.last_post
# add a new thread and post, verify last_post updated
thread = Thread(title="test", forum=forum, creator_id=118533)
thread.save()
post = Post(thread=thread, content="test", author=thread.creator)
post.save()
forum = Forum.objects.get(pk=1)
eq_(forum.last_post.id, post.id)
# delete the post, verify last_post updated
thread.delete()
forum = Forum.objects.get(pk=1)
eq_(forum.last_post.id, last_post.id)
eq_(Thread.objects.filter(pk=thread.id).count(), 0)
示例15: test_last_post_updated
def test_last_post_updated(self):
"""Adding/Deleting the last post in a thread and forum should
update the last_post field
"""
thread = Thread.objects.get(pk=4)
user = User.objects.get(pk=118533)
# add a new post, then check that last_post is updated
new_post = Post(thread=thread, content="test", author=user)
new_post.save()
forum = Forum.objects.get(pk=1)
thread = Thread.objects.get(pk=thread.id)
eq_(forum.last_post.id, new_post.id)
eq_(thread.last_post.id, new_post.id)
# delete the new post, then check that last_post is updated
new_post.delete()
forum = Forum.objects.get(pk=1)
thread = Thread.objects.get(pk=thread.id)
eq_(forum.last_post.id, 25)
eq_(thread.last_post.id, 25)