def publish(request): post = Post() post.author = User.objects.get(id=request.POST['author']) post.title = request.POST['title'] post.text = request.POST['text'] post.created_date = request.POST['created_date_0'] post.published_date = request.POST['published_date_0'] post.save() return post
def new_post(): post = Post.from_json(request.json) post.author = g.current_user db.session.add(post) db.session.commit() return jsonify(post.to_json()), 201, \ {'Location': url_for('api.get_post', id=post.id)}
def posts(count=100): fake = Faker() user_count = User.query.count() for i in range(count): u = User.query.offset(randint(0, user_count - 1)).first() p = Post(body=fake.text(), timestamp=fake.past_date(), author=u) db.session.add(p) db.session.commit()
def create_post(request): if request.method == 'POST': post_text = request.POST.get('the_post') response_data = {} post = Post(text=post_text, author=request.user) post.save() response_data['result'] = 'Create post successful!' response_data['text'] = post.text response_data['created'] = post.created.strftime('%B %d, %Y %I:%M %p') response_data['author'] = post.author.username return HttpResponse( json.dumps(response_data), content_type="application/json" ) else: return HttpResponse( json.dumps({"nothing to see": "this isn't happening"}), content_type="application/json" )
async def orm_insert_play(): from app1.models import Post, Tag, Hashtag, Comment post = Post( id=None, slug='test-post8', title='Test Post8', body='This is the body for test post8. I like the taste of water.', other=None, cb='test-post8 callback', creator_id=1, creator=None, owner_id=2, owner=None, #comments=None, comments=[ Comment(id=None, title='Post1 Comment1', body='Body for post1 comment1', post_id=None, post=None, creator_id=1, creator=None) ], tags=None, image=None, attributes=None, hashtags=None) #dump(post) #await Post.insert_with_relations2(post) # Get all tags keyed by name tags = await Tag.query().key_by('name').get() # Get all hastags keyed by name hashtags = await Hashtag.query().key_by('name').get() postX = [ { 'slug': 'test-post1', 'title': 'Test Post1', 'body': 'This is the body for test post1. I like the color red and green.', 'other': 'other stuff1', 'creator_id': 1, 'owner_id': 2, 'comments': [{ 'title': 'Post1 Comment1', 'body': 'Body for post1 comment1', #'post_id': 1, # No id needed, thats what post.create() does 'creator_id': 1, }], # Many-To-Many tags works with existing Model, new Model or new Dict 'tags': [ # Existing Tag tags['linux'], tags['mac'], tags['bsd'], tags[ 'bsd'], # Yes its a duplicate, testing that it doesn't fail # New Tag as Model (tag created and linked) Tag(name='test1', creator_id=4), # New Tag as Dict (tag created and linked) { 'name': 'test2', 'creator_id': 4 }, ], # Polymorphic One-To-One 'image': { 'filename': 'post1-image.png', 'size': 1234932, }, # Polymorphic One-To-Many Attributes 'attributes': [ { 'key': 'post1-test1', 'value': 'value for post1-test1' }, { 'key': 'post1-test2', 'value': 'value for post1-test2' }, { 'key': 'badge', 'value': 'IT' }, ], # Polymorphic Many-To-Many Hashtags 'hashtags': [ hashtags['important'], hashtags['outdated'], hashtags[ 'outdated'], # Yes its a duplicate, testing that it doesn't fail # New hashtag by model Hashtag(name='test1'), # New hashtag by dict { 'name': 'test2' }, ], }, ] await Post.insert_with_relations2(post)