本文整理汇总了Python中blog.models.Category类的典型用法代码示例。如果您正苦于以下问题:Python Category类的具体用法?Python Category怎么用?Python Category使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Category类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: async_add_category
def async_add_category(request):
if not request.POST:
raise Http404
data = {}
name = request.POST.get('name', False)
slug = request.POST.get('slug', False)
if name == False or slug == False:
data.update({"status": False, "message": "Invalid data."})
elif len(name) == 0 or len(slug) == 0:
data.update({"status": False, "message": "Neither field can be blank."})
else:
try:
cat = Category(name=slug,label=name) # Stupid naming of the model. Don't feel like fixing all references..
cat.save()
data.update({"status": True, "message": "Category successfully added.",
"slug": cat.name, "name": cat.label})
except:
data.update({"status": False, "message": "An unknown error occured. Please reload the page."})
return HttpResponse(simplejson.dumps(data), mimetype="application/javascript")
### END AJAX VIEWS ###
示例2: test_edit_category
def test_edit_category(self):
# Create the category
category = Category()
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Log in
self.client.login(username='bobsmith', password="password")
# Edit the category
response = self.client.post('/admin/blogengine/category/1/', {
'name': 'perl',
'description': 'The Perl programming language'
}, follow=True)
self.assertEquals(response.status_code, 200)
# Check changed successfully
self.assertTrue('changed successfully' in response.content)
# Check category amended
all_categories = Category.objects.all()
self.assertEquals(len(all_categories), 1)
only_category = all_categories[0]
self.assertEquals(only_category.name, 'perl')
self.assertEquals(only_category.description, 'The Perl programming language')
示例3: test_create_post_without_tag
def test_create_post_without_tag(self):
# Create the category
category = Category()
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Log in
self.client.login(username='bobsmith', password="password")
# Check response code
response = self.client.get('/admin/blog/post/add/')
self.assertEquals(response.status_code, 200)
# Create the new post
response = self.client.post('/admin/blog/post/add/', {
'title': 'My first post',
'text': 'This is my first post',
'pub_date_0': '2013-12-28',
'pub_date_1': '22:00:04',
'slug': 'my-first-post',
# 'site': '1',
'category': '1'
},
follow=True
)
self.assertEquals(response.status_code, 200)
# Check added successfully
self.assertTrue('added successfully' in response.content)
# Check new post now in database
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
示例4: test_create_post
def test_create_post(self):
# Create the post
post = Post()
category = Category(id=1)
# Set the attributes
post.title = 'My first post'
post.text = 'This is my first blog post'
post.pub_date = timezone.now()
category.title = 'My category'
post.category = category
# Save it
post.save()
# Check we can find it
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post, post)
# Check attributes
self.assertEquals(only_post.title, 'My first post')
self.assertEquals(only_post.text, 'This is my first blog post')
self.assertEquals(only_post.pub_date.day, post.pub_date.day)
示例5: test_creating_a_new_post_and_saving_it_to_the_database
def test_creating_a_new_post_and_saving_it_to_the_database(self):
# start by create one Category
category = Category()
category.name = "First"
category.slug = "first"
category.save()
# create new post
post = Post()
post.category = category
post.title = "First post"
post.content = "Content"
post.visable = True
# check we can save it
post.save()
# check slug
self.assertEquals(post.slug, "first-post")
# now check we can find it in the database
all_post_in_database = Post.objects.all()
self.assertEquals(len(all_post_in_database), 1)
only_post_in_database = all_post_in_database[0]
self.assertEquals(only_post_in_database, post)
# check that it's saved all attributes: category, title, content, visable and generate slug
self.assertEquals(only_post_in_database.category, category)
self.assertEquals(only_post_in_database.title, post.title)
self.assertEquals(only_post_in_database.content, post.content)
self.assertEquals(only_post_in_database.visable, post.visable)
self.assertEquals(only_post_in_database.slug, post.slug)
示例6: test_creating_a_new_comment_and_saving_it_to_the_database
def test_creating_a_new_comment_and_saving_it_to_the_database(self):
# start by create one Category and one post
category = Category()
category.name = "First"
category.slug = "first"
category.save()
# create new post
post = Post()
post.category = category
post.title = "First post"
post.content = "Content"
post.visable = True
post.save()
# create one comment
comment = Comment()
comment.name = "John"
comment.content = "This is cool"
comment.post = post
# check save
comment.save()
# now check we can find it in the database
all_comment_in_database = Comment.objects.all()
self.assertEquals(len(all_comment_in_database), 1)
only_comment_in_database = all_comment_in_database[0]
self.assertEquals(only_comment_in_database, comment)
# and check that it's saved its two attributes: name and content
self.assertEquals(only_comment_in_database.name, comment.name)
self.assertEquals(only_comment_in_database.content, comment.content)
示例7: test_all_post_feed
def test_all_post_feed(self):
# Create the category
category = Category()
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Create the tag
tag = Tag()
tag.name = 'python'
tag.description = 'The Python programming language'
tag.save()
# Create the author
author = User.objects.create_user('testuser', '[email protected]', 'password')
author.save()
# Create the site
site = Site()
site.name = 'example.com'
site.domain = 'example.com'
site.save()
# Create a post
post = Post()
post.title = 'My first post'
post.text = 'This is my first blog post'
post.slug = 'my-first-post'
post.pub_date = timezone.now()
post.author = author
post.site = site
post.category = category
# Save it
post.save()
# Add the tag
post.tags.add(tag)
post.save()
# Check we can find it
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post, post)
# Fetch the feed
response = self.client.get('/feeds/posts/')
self.assertEquals(response.status_code, 200)
# Parse the feed
feed = feedparser.parse(response.content)
# Check length
self.assertEquals(len(feed.entries), 1)
# Check post retrieved is the correct one
feed_post = feed.entries[0]
self.assertEquals(feed_post.title, post.title)
self.assertEquals(feed_post.description, post.text)
示例8: handle
def handle(self, *args, **options):
with open('upload-temp') as f:
sep = '\n' + ('-' * 70) + '\n'
fields = f.read().split(sep)
if options['edit']:
try:
p = Post.objects.get(slug=fields[1])
p.edited = datetime.now()
except Post.DoesNotExist:
raise CommandError('no post with slug "%s" exists'
% fields[1])
else:
p = Post()
p.created = datetime.now()
p.edited = None
p.title = fields[0]
p.slug = fields[1]
p.preview = fields[3]
p.content = fields[4]
p.save()
p.categories.clear()
for cat in fields[2].split(', '):
try:
c = Category.objects.get(name=cat)
except Category.DoesNotExist:
c = Category(name=cat, slug=slugify(cat))
c.save()
p.categories.add(c)
p.save()
示例9: __create_category
def __create_category(self, lang, title, desc):
cat = Category(lang=lang, title=title, desc=desc)
cat.save()
print(
"Created category : {} | {} | {} | ".format(lang, title, desc))
return cat
示例10: add
def add(request):
errors = []
if request.method == 'POST':
print request.POST
if check_edit_post_param(request.POST):
Category.insert_from_dic(request.POST.copy())
return HttpResponseRedirect('/admin/category/show/')
return render(request, 'admin/error.html', {'errors':errors})
示例11: test_edit_posts
def test_edit_posts(self):
category = Category()
category.name = 'python'
category.description = 'The python language'
category.save()
# Create the tag
tag = Tag()
tag.name = 'python'
tag.description = 'The Python programming language'
tag.save()
author = User.objects.create_user('testuser', '[email protected]', 'password')
author.save()
# site = Site()
# site.name = 'nunfuxgvn.com'
# site.domain = 'nunfuxgvn.com'
# site.save()
post = Post()
post.title = 'First Post'
post.content = 'My first post -yay'
post.slug = 'first-post'
post.pub_date = timezone.now()
post.author = author
# post.site = site
post.save()
post.tags.add(tag)
post.save()
self.client.login(username='bobsmith', password='password')
response = self.client.post('/admin/blog/post/1/', {
'title': 'Second Post',
'text': 'This is my second post',
'pub_date_0': '2014-07-17',
'pub_date_1': '11:49:24',
'slug': 'second-post',
# 'site': '1',
'category': '1',
'tags': '1'
},
follow=True
)
self.assertEquals(response.status_code, 200)
# Check post successfully changed
self.assertTrue('changed successfully' in response.content)
# Check post amended
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post.title, 'Second Post')
self.assertEquals(only_post.content, 'This is my second post')
示例12: add_category
def add_category(request):
try:
req = convertRequest(request)
category_name = req['category_name']
category = Category(category_name=category_name)
category.save()
return HttpResponse(response_json.success())
except Exception, e:
return HttpResponse(response_json.fail(traceback.format_exc()))
示例13: test_delete_post
def test_delete_post(self):
# Create the category
category = Category()
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Create the tag
tag = Tag()
tag.name = 'python'
tag.description = 'The Python programming language'
tag.save()
# Create the author
author = User.objects.create_user('testuser', '[email protected]', 'password')
author.save()
# Create the site
site = Site()
site.name = 'example.com'
site.domain = 'example.com'
site.save()
# Create the post
post = Post()
post.title = 'My first post'
post.text = 'This is my first blog post'
post.slug = 'my-first-post'
post.pub_date = timezone.now()
post.site = site
post.author = author
post.category = category
post.save()
post.tags.add(tag)
post.save()
# Check new post saved
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
# Log in
self.client.login(username='bobsmith', password="password")
# Delete the post
response = self.client.post('/admin/blogengine/post/1/delete/', {
'post': 'yes'
}, follow=True)
self.assertEquals(response.status_code, 200)
# Check deleted successfully
self.assertTrue('deleted successfully' in response.content)
# Check post deleted
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 0)
示例14: addCategory
def addCategory(request):
name = request.POST['name']
slug = request.POST['slug']
desc = request.POST['desc']
type = request.POST['type']
pid = request.POST['category_parent']
if pid == '0':
pid = None
if type and type == 'add':
try:
cats=Category.objects.filter(name=name)
if cats.count() >= 1:
messages.add_message(request,messages.INFO,'categry [%s] already exits!'%(name))
else:
cat = Category(name=name,slug=slug,desc=desc,parent_id=pid)
cat.save()
messages.add_message(request,messages.INFO,'categry [%s] save ok!'%(name))
except Exception as e:
print 'exception:',e
elif type and type == 'edit':
id = request.POST.get('id','')
cat = Category.objects.get(id=id)
cat.name=name
cat.slug=slug
cat.desc=desc
cat.parent_id = pid
cat.save()
return HttpResponseRedirect('/admin/categories')
示例15: test_category_page
def test_category_page(self):
# Create the category
category = Category()
category.name = 'python'
category.description = 'The Python programming language'
category.save()
# Create the author
author = User.objects.create_user('testuser', '[email protected]', 'password')
author.save()
# Create the site
site = Site()
site.name = 'example.com'
site.domain = 'example.com'
site.save()
# Create the post
post = Post()
post.title = 'My first post'
post.text = 'This is [my first blog post](http://127.0.0.1:8000/)'
post.slug = 'my-first-post'
post.pub_date = timezone.now()
post.author = author
post.site = site
post.category = category
post.save()
# Check new post saved
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)
only_post = all_posts[0]
self.assertEquals(only_post, post)
# Get the category URL
category_url = post.category.get_absolute_url()
# Fetch the category
response = self.client.get(category_url)
self.assertEquals(response.status_code, 200)
# Check the category name is in the response
self.assertTrue(post.category.name in response.content)
# Check the post text is in the response
self.assertTrue(markdown.markdown(post.text) in response.content)
# Check the post date is in the response
self.assertTrue(str(post.pub_date.year) in response.content)
self.assertTrue(post.pub_date.strftime('%b') in response.content)
self.assertTrue(str(post.pub_date.day) in response.content)
# Check the link is marked up properly
self.assertTrue('<a href="http://127.0.0.1:8000/">my first blog post</a>' in response.content)