本文整理汇总了Python中blog.models.BlogPost.save方法的典型用法代码示例。如果您正苦于以下问题:Python BlogPost.save方法的具体用法?Python BlogPost.save怎么用?Python BlogPost.save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blog.models.BlogPost
的用法示例。
在下文中一共展示了BlogPost.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def setUp(self):
# Create an initial object to test with
post = BlogPost(title=u'Hello Wørld', text=u'Hello World, this is dynamicresponse. ÆØÅæøå.')
post.save()
self.post = post
示例2: post
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def post(self, request):
subject = request.POST['subject']
content = request.POST['content']
slug = titleToUrl(subject)
published = request.POST['published'] == 'published'
if not subject:
messages.add_message(request, messages.INFO, 'Please add a subject.')
if not content:
messages.add_message(request, messages.INFO, 'Please add some content.')
if not BlogPost.objects.filter(slug=slug).count() == 0:
messages.add_message(request, messages.INFO, 'Please rename, you\'ve used that title before.')
if messages.get_messages(request):
params = {'subject': subject,
'content': content,
'published': published}
return render(request, 'blog_create.html', params)
else:
post = BlogPost(title=subject,
body=content,
slug=slug,
published=published)
post.save()
messages.add_message(request, messages.SUCCESS, 'Blog post created.')
return HttpResponseRedirect(reverse('blog_SinglePost', args=(slug,)))
示例3: generate_blog_post
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def generate_blog_post():
post = BlogPost()
post.name = random.choice(long_actions)
post.content = user_description
post.date = generate_datetime()
post.published = True
post.save()
示例4: StatusTests
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
class StatusTests(TestCase):
fixtures = ['devel']
def setUp(self):
self.client.login(username='admin', password='admin')
self.user = ActiveUser.objects.filter(username='admin')[0]
self.post = BlogPost(title='Hello World!',
author=self.user,
tags='hello world',
abstract='Hello World!',
text='Hello World!',
priority=BlogPost.PRIORITY_NORMAL)
self.post.save()
self.url = reverse('blog_pending_edit', kwargs={'pk': self.post.pk})
self.form = {
'title': self.post.title,
'abstract': self.post.abstract,
'tags': self.post.tags,
'text': self.post.text,
'priority': self.post.priority
}
def test_draft(self):
self.form['action'] = UserCreatePostForm.ACTION_DRAFT
response = self.client.post(self.url, self.form, follow=True)
self.post.refresh_from_db()
self.assertEqual(response.status_code, 200)
self.assertLessEqual((datetime.datetime.now() - self.post.date_modified).total_seconds(), 10)
def test_submitted(self):
self.post.status = BlogPost.STATUS_DRAFT
self.post.save()
self.form['action'] = UserCreatePostForm.ACTION_SUBMIT
response = self.client.post(self.url, self.form, follow=True)
self.assertEqual(response.status_code, 200)
def test_approved(self):
self.form['action'] = UserCreatePostForm.ACTION_APPROVE
response = self.client.post(self.url, self.form, follow=True)
self.post.refresh_from_db()
self.assertEqual(response.status_code, 200)
self.assertLessEqual((datetime.datetime.now() - self.post.date_approved).total_seconds(), 10)
def test_published(self):
self.form['action'] = UserCreatePostForm.ACTION_PUBLISH
response = self.client.post(self.url, self.form, follow=True)
self.post.refresh_from_db()
self.assertEqual(response.status_code, 200)
self.assertLessEqual((datetime.datetime.now() - self.post.date_published).total_seconds(), 10)
def test_reject(self):
self.form['action'] = UserCreatePostForm.ACTION_DELETE
response = self.client.post(self.url, self.form, follow=True)
self.assertEqual(response.status_code, 200)
示例5: save
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def save(request, url):
posts = BlogPost.objects.all()
for post in posts:
if post.id == int(url):
b = BlogPost(body=request.GET.get('body'), title=request.GET.get('title'), id=post.id,
timestamp=timezone.now())
b.save()
t = loader.get_template("article.html")
c = Context({'post': b})
return HttpResponse(t.render(c))
示例6: add_post
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def add_post(request):
get_title = request.POST['post_title']
get_post_image = request.FILES.get('post_image', None)
get_content = request.POST['post_content']
get_content_preview = content_preview_text_gen(get_content)
get_datetime = timezone.now()
get_user = request.user
p = BlogPost(title=get_title, post_image=get_post_image, content=get_content, content_preview=get_content_preview, datetime=get_datetime, author=get_user)
p.save()
return HttpResponseRedirect(reverse('blog:index'))
示例7: test_wgs84_coords
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def test_wgs84_coords(self):
"""
latitude is +/- 90 longitude is +- 180
"""
mapX = BlogSurface.objects.create(title="theWorld")
m1 = BlogPost(blog_surface=mapX, title="legal marker", lat=89.9, lng=179.9)
m1.save()
m2 = BlogPost(blog_surface=mapX, title="illegal marker", lat=99.9, lng=199.9)
with self.assertRaises(ValidationError):
m2.save()
示例8: test_drafedit
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def test_drafedit(self):
post = BlogPost(title='Hello World!',
author=self.user,
tags='hello world',
abstract='Hello World!',
text='Hello World!',
priority=BlogPost.STATUS_DRAFT)
post.save()
url = reverse('blog_draft_edit', kwargs={'pk': post.pk})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
示例9: test_save_and_retrieve_blog_posts
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def test_save_and_retrieve_blog_posts(self):
self.assertEqual(BlogPost.objects.all().count(), 0)
blog_post = BlogPost()
blog_post.title = "A post"
blog_post.date = datetime.datetime(1939, 9, 1, 5, 0, 0)
blog_post.body = "Blah blah blah"
blog_post.visible = False
blog_post.save()
self.assertEqual(BlogPost.objects.all().count(), 1)
retrieved_post = BlogPost.objects.first()
self.assertEqual(retrieved_post, blog_post)
示例10: test_create_post
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def test_create_post(self):
user = User.objects.get(username='AAA')
post = BlogPost(user=user)
post.title = "My test post"
post.content = "This is a test post"
post.save()
all_posts = BlogPost.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post, post)
self.assertEquals(only_post.title, "My test post")
示例11: test_blog
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def test_blog(self):
blog = Blog(title="test blog")
blog.save()
self.assertIsInstance(blog, Blog)
blog_post = BlogPost(blog=blog, title="test post",
content="this is content")
blog_post.save()
self.assertIsInstance(blog_post, BlogPost)
get_by_slug = BlogPost.objects.get_by_slug(blog_post.slug)
self.assertIsInstance(get_by_slug, BlogPost)
homepage = BlogPost.objects.homepage_posts()
self.assertEquals(len(homepage), 1)
示例12: PendingTests
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
class PendingTests(TestCase):
fixtures = ['devel']
def setUp(self):
self.client.login(username='admin', password='admin')
self.user = ActiveUser.objects.filter(username='admin')[0]
self.post = BlogPost(title='Hello World!',
author=self.user,
tags='hello world',
abstract='Hello World!',
text='Hello World!',
priority=BlogPost.STATUS_SUBMITTED)
self.post.save()
def test_list(self):
response = self.client.get(reverse('blog_pending_list'))
self.assertEqual(response.status_code, 200)
def test_edit(self):
url = reverse('blog_pending_edit', kwargs={'pk': self.post.pk})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
示例13: blogPostCreate
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def blogPostCreate(request,blog_id):
post = dict()
page = {
'title': "Create new blog post",
'post': post,
'blog_url':reverse('admin_blogPostCreate',kwargs={'blog_id':blog_id}),
'next_url': reverse('admin_blog',kwargs={'id':blog_id}),
'tag_url': reverse('admin_tag',kwargs={'blog_id':blog_id}),
'category_url': reverse('admin_category',kwargs={'blog_id':blog_id}),
}
if request.method == 'POST': # If the form has been submitted...
response = {'result':'ok'}
blogPost = simplejson.loads(request.raw_post_data )
tags = Tag.objects.filter(blog__id=blog_id,id__in = map(lambda x: x['id'],blogPost['tags']))
categories = Category.objects.filter(blog__id=blog_id,id__in = map(lambda x: x['id'],blogPost['categories']))
new_post = BlogPost(content=blogPost['content'],
teaser=blogPost['teaser'],
title=blogPost['title'],
slug=blogPost['slug'],
published=blogPost.get('published', False),
comments_closed=blogPost.get('comments_closed', False),
max_comment_count=blogPost['max_comment_count'],
blog_id=blog_id,
)
try:
# we need to save the object in order to add manytomany field
new_post.save()
except IntegrityError as e:
response['result'] = 'error'
response['slug_error'] = 'There is another post with slug value "%s"'%blogPost['slug']
response['error'] = e.args[0]
response['errorTitle'] = "Integrity Error"
new_post.tags.add(*tags)
new_post.categories.add(*categories)
try:
#this one saves many to many relationship
new_post.save()
except IntegrityError as e:
response['result'] = 'error'
response['slug_error'] = 'Error while adding tags to post'
response['error'] = e.args[0]
response['errorTitle'] = "Integrity Error"
return HttpResponse(simplejson.dumps(response),mimetype='text/html')
#get request: render page first time
post.update({'title':"",
'slug' :"",
'published':True,
'teaser':"",
'content':"",
'comments_closed':False,
'max_comment_count':100})
def objFactory(source):
result = dict()
result['name'] = source.name
result['selected'] = False
result['id'] = source.id
return result
post['tags'] = map(objFactory,Tag.objects.filter(blog__id=blog_id).extra(select={'lower_name':'lower(name)'}).order_by('lower_name'))
post['categories'] = map(objFactory,Category.objects.filter(blog__id=blog_id).extra(select={'lower_name':'lower(name)'}).order_by('lower_name'))
page['post_json'] = simplejson.dumps(post)
return render(request,
'admin/blogpostpage.html',
{
'formAction': reverse('admin_blogPostCreate',kwargs={'blog_id':blog_id}),
'page': page,
'user':request.user,
})
示例14: open
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
from blog.models import BlogPost
f = open("E:\\bak\\sql.txt", "r")
for line in f:
content=line.split(",");
print content[0],content[2]
p1=BlogPost(title=content[0],content=content[1],timestamp=content[2])
p1.save()
f.close()
示例15: handle
# 需要导入模块: from blog.models import BlogPost [as 别名]
# 或者: from blog.models.BlogPost import save [as 别名]
def handle(self, *args, **options):
"""
Visit http://argonemyth.me/blog/api/posts/ to see the returned list
"""
api = BlogAPI()
# Step 1: if nuke is true remove all the categories.
if options['nuke']:
# This will also delete all the posts that are associated
# with the categories.
BlogCategory.objects.all().delete()
BlogPost.objects.all().delete()
Location.objects.all().delete()
Photo.objects.all().delete()
# Step 2: Add all the locations, categories and posts back
# Updating all the locations
locations = api.locations()
for l_json in locations:
loc, created = Location.objects.get_or_create(id=l_json["id"])
loc = Location(**l_json)
loc.save()
cats = api.categories()
for c_json in cats:
# print c_json
posts = c_json.pop('blogposts')
try:
category = BlogCategory.objects.get(id=c_json["id"])
except BlogCategory.DoesNotExist:
# print "Creating a new category: "
category = BlogCategory(**c_json)
category.save()
else:
# Check if the local categories are matching the remote ones.
if c_json['slug'] != category.slug:
raise Exception('The Category is not synced.')
else:
current_data = category.__dict__.copy()
# We check if there are changes
current_data.pop('_state')
if current_data == c_json:
print "%s is already in sync" % c_json['title']
else:
# print "Updating a existing category:"
category.__dict__.update(c_json)
category.save()
# Adding all the posts from the category.
for p_url in posts:
p_json = api.get_post_by_url(p_url)
# p_json['category_id'] = category
# p_json['author'] = User.objects.get(username=p_json['author'])
tags = p_json.pop('tags')
api_url = p_json.pop('api_url')
# scan post content and grab all the embeded images
try:
post = BlogPost.objects.get(id=p_json["id"])
except BlogPost.DoesNotExist:
print "Creating a new post: %s" % p_json['title']
post = BlogPost(**p_json)
post.save()
# Download main image
if p_json['main_image']:
download_photo('/media/' + p_json['main_image'], False)
# Adding tags
if tags:
print "Adding the following tags to post %s: %s" % (post.title, (', ').join(tags))
for tag in tags:
post.tags.add(tag)
else:
if p_json['slug'] != post.slug:
raise Exception('The post is not synced')
else:
# We compare date_updated value to see the post needs to update
remote_update = parse_datetime(p_json['date_updated'])
if remote_update > post.date_updated:
print "Never version of post %s on the server, updating..." % (post.title)
post.__dict__.update(p_json)
post.save()
# else:
# print "No need to update post %s " % post.title
# Download all the images in the content - regardless if they already exist or not
images = re.findall(r'<img.* src="(?P<img>.*?)"', p_json['content'])
if images:
for img_src in images:
download_photo(img_src)
# Adding & upate all the photos in Photo albums
photos = api.photos()
for photo_json in photos:
download = False
try:
photo = Photo.objects.get(id=photo_json["id"])
except Photo.DoesNotExist:
download = True
#.........这里部分代码省略.........