def test_get_post_by_tag_name(self): file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG") file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG") p = Post.create(author_id=1, description="test", file_name=None, tag_list="food,fashion", choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)]) self.assertIn(p, Post.get_posts_by_tag("food")) self.assertIn(p, Post.get_posts_by_tag("fashion")) self.assertNotIn(p, Post.get_posts_by_tag("apple"))
def post(self, topic_id): title = self.get_argument('title', None) keywords = self.get_argument('keywords', None) content = self.get_argument('content', '') keep_silent = int(self.get_argument('keep_silent', 0)) is_draft = int(self.get_argument('is_draft', 0)) if not all([title, keywords]): raise exceptions.EmptyFields() else: can_post = yield gen.maybe_future(Topic.can_post(topic_id)) if not can_post: raise exceptions.TopicIsNotAccepted exists = yield gen.maybe_future(Post.get_by_title(title)) if exists: raise exceptions.PostTitleAlreadyExists() else: username = self.current_user yield gen.maybe_future( Post.create(username, topic_id, title, keywords, content, keep_silent=keep_silent, is_draft=is_draft)) # Update gold. update_gold.apply_async(('new_post', username))
def post_add(): form = PostForm() if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render( form=form, categories=all_categories, ) if request.method == 'POST': post = Post.create( category=post_get('category-id'), post_text=post_get('text'), title=post_get('title'), slug=post_get('slug'), user=app.current_user.user_id, date_posted=datetime.now(), draft=bool(int(post_get('draft'))), show_on_index=bool(post_get('show-on-index')), language=post_get('language'), ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def init_database(): database.connect() database.create_tables([Post, User]) User.truncate_table() Post.truncate_table() repos = load_json('repos.json') for repo in repos: user, created = build_user(repo['owner']['login']) Post.create(title=repo['name'], slug=repo['full_name'], body=repo['description'], user=user) database.close()
def post(self): form = PostForm() if request.method == 'POST': if form.validate_on_submit(): post = Post.create() form.populate_obj(post) f = request.files.get('file') post.user = current_user if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 post.update_score(page_view=1) post.save() if form.remain.data: url = url_for('PostsView:put', id=post.id) else: url = url_for('PostsView:get', id=post.id) return resp(url, redirect=True, message=gettext('Stamp succesfully created')) else: return resp('admin/posts/edit.html', status=False, form=form, message=gettext('Invalid submission, please check the message below')) return resp('admin/posts/edit.html', form=form)
def writing(request, sid): try: username = request.session.get('username') user = User.objects.filter(username=username)[0] stitle = BsTitle.objects.filter(sid=sid)[0] sid = sid if request.method == 'GET': data = { 'username': username, 'sid': sid, } return render(request, 'app/writing.html', data) else: article = request.POST.get('article') content = request.POST.get('content') post = Post.create(article=article, content=content, author=user, stitle=stitle) post.save() print('888888888888888') return redirect('/index/') except Exception as e: return render(request, '404.html', locals())
def post_add(): form = PostForm() if request.method == 'GET': all_categories = Category.select() template = env.get_template('post/add.html') return template.render( form=form, categories=all_categories, ) if request.method == 'POST': post = Post.create( category=post_get('category-id'), post_text=post_get('text'), title=post_get('title'), slug=post_get('slug'), user=app.current_user.user_id, date_posted=datetime.now(), draft=bool(int(post_get('draft'))), show_on_index=post_get_checkbox('show_on_index'), language=post_get('language'), ) post_id = post.post_id post.save() add_new_tags(post_get('tags'), post_id) redirect('/post/' + str(post_id))
def test_show_all_followed_posts(self): """test the show_all_followed_posts method""" u1, u2 = self.test_create_users() file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG") file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG") p1 = Post.create(author_id=u1.user_id, description="test", file_name=None, tag_list=None, choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)]) all_followed_post = u2.followed_posts() self.assertIn(p1, all_followed_post)
def create(): data = request.json user = get_user() try: post = Post.create(**data, user=user) return jsonify({'error': False, 'message': 'Post successfully created', 'data':post.to_dict}), 201 except: return jsonify({'error':True, 'message': 'Something went wrong :('}), 500
def test_get_all_tags_by_post_id(self): file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG") file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG") p = Post.create(author_id=1, description="test", file_name=None, tag_list="food,fashion", choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)]) tags = Tag.get_tags_by_post_id(p.post_id) tag_names = [str(tag.tag_name) for tag in tags] self.assertIn('food', tag_names) self.assertIn('fashion', tag_names) self.assertNotIn("apple", tag_names)
def create_post(): form = PostForm(request.form) form.tags.choices = [(tag.id, tag.name) for tag in Tag.get()] if request.method == 'POST': if form.validate(): title, body, timestamp = form['title'].data, form[ 'body'].data, form['timestamp'].data tags = [Tag.get_first(id=int(tag_id)) for tag_id in form.tags.data] try: Post.create(title=title, body=body, timestamp=timestamp, tags=tags) except Exception as e: flash(str(e), 'alert') else: flash('Пост успешно выложен', 'success') return redirect(url_for('index')) else: flash('Не все поля заполнены', 'alert') return render_template('edit_post.html', form=form, title='Создание поста', button='Создать')
def test_add_remove_tag(self): """ 测试添加删除标签 :return: """ post = Post.create(title='hahd') tags = [i.tag for i in post.tags.all()] self.assertListEqual(tags, []) self.assertTrue(post.tags.count() == 0) tag = Tag.create(title='摄影') post.add_tag(tag) tags = [i.tag for i in post.tags.all()] self.assertListEqual(tags, [tag]) self.assertTrue(post.tags.all() == tag.posts.all()) post.remove_tag(tag) self.assertTrue(post.tags.count() == 0)
def test_collect_post(self): p = Post.create(title='haha') f = Favorite.create(title='数学') self.assertListEqual(f.posts.all(), []) self.assertTrue(f.posts.count() == 0) self.assertTrue(p.favorites.count() == 0) f.collect(p) self.assertListEqual([i.post for i in f.posts.all()], [p]) self.assertTrue(f.posts.count() == 1) self.assertTrue(p.favorites.count() == 1) self.assertTrue(f.has_collect_post(p)) f.uncollect(p) self.assertTrue(f.posts.count() == 0) self.assertTrue(p.favorites.count() == 0) self.assertFalse(f.has_collect_post(p))
def test_create_post(self): """test to create a new post with file objects and make sure choices data are added to Choice table""" file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG") file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG") p = Post.create(author_id=1, description="test", file_name=None, tag_list=None, choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)]) choices = Choice.get_choices_by_post_id(p.post_id) choices_text = [choice.choice_text for choice in choices] choices_file = [choice.file_name for choice in choices] self.assertEqual(p.author_id, 1) self.assertEqual(p.description, "test") self.assertEqual(p.file_name, None) self.assertIn("text_choice1", choices_text) self.assertIn("text_choice2", choices_text) self.assertEqual([p], Post.get_all_posts()) for choice in choices: self.assertIn(hashlib.sha512(str(choice.choice_id)).hexdigest(), choices_file) return p
def post(self): form = PostForm() if form.validate_on_submit(): try: if not form.validate(): raise Exception(_('ERROR_INVALID_SUBMISSION')) remain = request.values.get('remain', False, bool) post = Post.create() form.populate_obj(post) post.user = current_user f = request.files.get('file') if f: picture = Picture.create() picture.save_file(f, current_user) post.cover_picture_id = picture.id if picture else 0 # init the score post.update_score(page_view=1) post.editor_version = 1 post.save() Feed.clear_feed_cache() if post.is_draft: message = _('POST_DRAFT_SAVE_SUCESS') else: message = _('POST_PUBLIC_SAVE_SUCESS') if remain: url = url_for('PostsView:put', id=post.id, remain='y') else: url = url_for('PostsView:get', id=post.id) return render_view(url, redirect=True, message=message) except Exception as e: flash(e.message, 'error') return render_view('admin/posts/edit.html', form=form)
def create_post(): s = search() if s: return s form = WriteArticleForm() if form.validate_on_submit(): post = Post.create(title=form.title.data, body=form.body.data, author=current_user._get_current_object(), disable_comment=form.disable_comment.data) for each in form.tags.data: tag = Tag.query.get_or_404(each) if tag.add_post(post): db.session.add(tag) flash('标签{}添加成功'.format(tag.title), category='success') flash('文章添加成功', category='success') return redirect(url_for('.posts')) return render_template('post/create_post.html', form=form)
def post(self, *args, **kwargs): post = Post.create(**request.json) return {'data': post.serialize(), 'status': 'success'}