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
import datetime
import random
from uuid import uuid4

from myblog.models import db, User, Tag, Post

# user = User(id=str(uuid4()), username='******', password='******')
# db.session.add(user)
# db.session.commit()

user = db.session.query(User).first()
tag_one = Tag(id=str(uuid4()), name='Python')
tag_two = Tag(id=str(uuid4()), name='Flask')
tag_three = Tag(id=str(uuid4()), name='SQLALchemy')
tag_four = Tag(id=str(uuid4()), name='Spark')
tag_list = [tag_one, tag_two, tag_three, tag_four]

s = "EXAMPLE TEXT"

for i in range(100):
    new_post = Post(id=str(uuid4()), title="Post" + str(i))
    new_post.user = user
    new_post.publish_date = datetime.datetime.now()
    new_post.text = s
    new_post.tags = random.sample(tag_list, random.randint(1, 3))
    db.session.add(new_post)

db.session.commit()