Example #1
0
def create_post(request):
	user = request.user
	blog_id = request.POST.get('blog_id')
	blog = Blog.objects.get(pk = blog_id)
	post_id = request.POST.get('post_id')
	if user not in blog.authors.all():
		return HttpResponse("Sorry you are not authorized to post here")
	else:
		if post_id:
			post = Post.objects.get(pk=post_id)
			if user != post.author:
				return HttpResponse("Sorry you are not authorized to edit here")
		else:
			post = Post()
		post.author = user
		post.blog = blog
		post.title = request.POST.get('title')
		post.body = request.POST.get('body')
		
		tags = request.POST.get('tags')
		tag_names = tags.split(',')
		if not tags.strip():
			tag_names = [constant.NO_TAG]
		tag_set = set(tag_names)
		tag_list = []
		for tname in tag_set:
			tname_s = tname.strip()
			try:
				tag = Tag.objects.get(name__iexact=tname_s)
			except Tag.DoesNotExist:
				tag = None
			if not tag:
				tag = Tag()
				tag.name = tname_s
				tag.save()
			tag_list.append(tag)
		post.save()
		post.tags = tag_list;
		post.save()
		return HttpResponseRedirect(reverse('myblog:index'))
Example #2
0
  def post(self):
    '''
    Create new post
    '''
    post_id = None
    if 'id' in request.json:
      post_id = request.json['id']

    title = request.json['title']
    body = request.json['body']
    tags = request.json['tags']
    tags = [slugify(item) for item in tags]

    if (not title) or (not body):
      return jsonify({
        'error': 'title & body is required.',
        'data': {'title': title,
          'body': body,
          'slug': slugify(title)
           }
        })
    if post_id:
      post = Post.objects.get(id=post_id)
    else:
      post = Post(author=current_user.id)

    post.title = title
    post.body = body
    post.slug = slugify(title)
    post.tags = tags
    post.save()
    return jsonify({
        'success': 'success',
        'post' : post,
        'edit_url': post.get_edit_url()
        })
Example #3
0
To run this you must first start a django shell:
python manage.py shell
Once the shell is up, go:
run ./test_data.py
It will silently populate the database.
"""

from myblog.models import Post
from django.contrib.auth.models import User

p1 = Post(title='My First Post', text='This is the first post I\'ve written')
all_users = User.objects.all()

p1.author = all_users[0]
p1.full_clean()
p1.save()

p1.title = p1.title + " (updated)"
p1.save()

p2 = Post(title="Another post",
          text="The second one created",
          author=all_users[0]).save()
p3 = Post(title="The third one",
          text="With the word 'heffalump'",
          author=all_users[0]).save()
p4 = Post(title="Posters are a great decoration",
          text="When you are a poor college student",
          author=all_users[0]).save()