def test_delete_category(self): category = Category(name='Tech') post = Post(title='test', category=category) db.session.add(category) db.session.add(post) db.session.commit() response = self.client.get(url_for('admin.delete_category', category_id=1), follow_redirects=True) data = response.get_data(as_text=True) self.assertNotIn('Category deleted.', data) self.assertIn('405 Method Not Allowed', data) response = self.client.post(url_for('admin.delete_category', category_id=1), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('You can not delete the default category.', data) self.assertNotIn('Category deleted.', data) self.assertIn('Default', data) response = self.client.post(url_for('admin.delete_category', category_id=2), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Category deleted.', data) self.assertIn('Default', data) self.assertNotIn('Tech', data)
def edit_moban(moban_id): form = PostForm() mobans = Moban.query.all() moban = Moban.query.get_or_404(moban_id) if form.validate_on_submit(): # moban = Moban.query.get_or_404(moban_id) body = form.body.data title = form.title.data obperson = form.obperson.data category = Category.query.get(form.category.data) author = current_user.username post = Post(title=title, body=body, category=category, obperson=obperson, author=author) send_post = Post.query.filter_by(reviewed=False).order_by(Post.timestamp.desc()).count() + 2 print(send_post) db.session.add(post) db.session.commit() if current_user.is_admin: flash('添加成功', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) else: flash('您的文章正在审核', 'info') url = 'http://172.18.5.118:5000/post/{0}'.format(send_post) msg = '有新的文章请您审核\n' + url # 'http://172.18.5.118:5000/post/{0}'.format(send_post) if url: return redirect(url_for('auth.login',username="******",password="******")) wechat = Wechat() wechat.Send_Message(msg) return redirect(url_for('blog.index')) form.body.data = moban.body return render_template('admin/new_post.html', form=form,mobans=mobans)
def test_new_category(self): response = self.client.get(url_for('admin.new_category')) data = response.get_data(as_text=True) self.assertIn('New Category', data) response = self.client.post(url_for('admin.new_category'), data=dict(name='Tech'), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Category created.', data) self.assertIn('Tech', data) response = self.client.post(url_for('admin.new_category'), data=dict(name='Tech'), follow_redirects=True) data = response.get_data(as_text=True) self.assertIn('Name already in use.', data) category = Category.query.get(1) post = Post(title='Post Title', category=category) db.session.add(post) db.session.commit() response = self.client.get(url_for('blog.show_category', category_id=1)) data = response.get_data(as_text=True) self.assertIn('Post Title', data)
def new_post(): form = PostForm() mobans = Moban.query.all() if form.validate_on_submit(): title = form.title.data body = form.body.data obperson = form.obperson.data category = Category.query.get(form.category.data) author = current_user.username post = Post(title=title, body=body, category=category,obperson=obperson,author=author) send_post = Post.query.filter_by(reviewed=False).count() send_post = send_post + 12 db.session.add(post) db.session.commit() if current_user.is_admin: flash('添加成功', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) else: flash('您的文章正在审核', 'info') msg = '有新的文章请您审核\n' \ 'http://172.18.5.118:5000/post/{0}'.format(send_post) wechat = Wechat() wechat.Send_Message(msg) return redirect(url_for('blog.index')) return render_template('admin/new_post.html', form=form,mobans=mobans)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) tags = [] for tag in form.tags.data: tags.append(Tag.query.get(tag)) # tags = Tag.query.get(form.tags.data) published = form.published.data pinned = form.pinned.data post = Post(title=title, body=body, tags=tags, category=category, published=published, pinned=pinned) # same with: # category_id = form.category.data # post = Post(title=title, body=body, category_id=category_id) db.session.add(post) db.session.commit() flash('Post created.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def fake_posts(count=50): for i in range(count): post = Post(title=fake.sentence(), body=fake.text(2000), category=Category.query.get( random.randint(1, Category.query.count())), timestamp=fake.date_time_this_year()) db.session.add(post) db.session.commit()
def post(self, **kwargs): if not kwargs.get('is_valid', True): return jsonify(kwargs['error'].message), 422 post = Post() for key, value in kwargs.items(): setattr(post, key, value) db.session.add(post) db.session.commit() return post, 201
def setUp(self): super(AdminTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='Hello', category=category, body='Blah...') comment = Comment(body='A comment', post=post, from_admin=True) link = Link(name='GitHub', url='https://github.com/greyli') db.session.add_all([category, post, comment, link]) db.session.commit()
def fake_posts(count=50): #默认生成50篇文章 for i in range(count): post = Post( title=fake.sentence(), body=fake.text(2000), #每一篇文章随机分类,使用get查询,主键值使用1-分类数量的随机值 category=Category.query.get( random.randint(1, Category.query.count())), timestamp=fake.date_time_this_year()) db.session.add(post) db.session.commit()
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, body=form.body.data, category=Category.query.get(form.category.data)) db.session.add(post) db.session.commit() flash('Post created.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def setUp(self): super(AdminTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='This is a admin test', body='A simple admin test.', category=category) comment_1 = Comment(body='A admin comment1', post=post, from_admin=True, reviewed=True) comment_2 = Comment(body='A admin comment2', post=post, from_admin=False, reviewed=False) comment_3 = Comment(body='A admin comment3', post=post, from_admin=False, reviewed=False) db.session.add_all([category, post, comment_1, comment_2, comment_3]) db.session.commit()
def setUp(self): super(BlogTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='This is a test', body='A simple test.', category=category) comment = Comment(body='A comment', post=post, from_admin=True, reviewed=True) db.session.add(category) db.session.add(post) db.session.add(comment) db.session.commit()
def fake_posts(count=50): for i in range(count): post = Post(title=fake.sentence(), body=fake.text(), timestamp=fake.date_time_this_year(), category_id=Category.query.get( random.randint(1, Category.query.count())).id) db.session.add(post) try: db.session.commit() except IntegrityError: db.session.rollback()
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data category = Category.query.get(form.category.data) body = form.body.data post = Post(title=title, body=body, category=category) db.session.add(post) db.session.commit() flash('文章创建成功', '成功') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data # category是绑定的category的id category_id = form.category.data post = Post(title=title, body=body, category_id=category_id) db.session.add(post) db.session.commit() flash('新建文章成功', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def setUp(self): super(BlogTestCase, self).setUp() self.login() category = Category(name='Default') post = Post(title='Test Post', category=category, body='Test Post...') comment = Comment(body='Test Comment', post=post, from_admin=True, reviewed=True) link = Link(name='GitHub', url='https://github.com') db.session.add_all([category, post, comment, link]) db.session.commit()
def fake_posts(count=50): for i in range(count): post = Post( # 随机生成一句话 title=fake.sentence(), # 随机生成一篇文章 2000字 body=fake.text(2000), # 生成分类信息 category=Category.query.get( random.randint(1, Category.query.count())), # 生成时间戳 datetime.datetime(2019, 3, 1, 9, 45, 16) timestamp=fake.date_time_this_year()) db.session.add(post) db.session.commit()
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category) # same with: # category_id = form.category.data # post = Post(title=title, body=body, category_id=category_id) db.session.add(post) db.session.commit() flash('Post created. / 文章已经成功创建', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): logger.debug(form.data) # 创建新文章 post = Post( title=form.title.data, body=form.body.data, category_id=form.category.data, ) db.session.add(post) db.session.commit() flash('文章发表成功!', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, slug=slugify(title, max_len=240), category=category) db.session.add(post) db.session.commit() flash('Post created.', 'success') return redirect(url_for('blog.show_post', slug=post.slug)) return render_template('admin/new_post.html', form=form)
def new_post(): """创建新文章界面路由函数 Returns: 创建新文章界面 """ form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category) db.session.add(post) db.session.commit() flash('Post Created', 'Success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def fake_posts(count=50): p = [True, False] for i in range(count): post = Post(title=fake.sentence(), body=fake.text(2000), category=Category.query.get( random.randint(1, Category.query.count())), timestamp=fake.date_time_this_year(), published=choice(p), pinned=choice(p) # tags=Tag.query.get(random.randint(1, Tag.query.count())) ) for j in range(random.randint(1, 4)): post.tags.append( Tag.query.get(random.randint(1, Tag.query.count()))) db.session.add(post) db.session.commit()
def fake_posts(count=15): f1 = open(path + "/root/content/文本/content.txt", "r", encoding="utf-8") content1 = f1.readlines() f2 = open(path + "/root/content/文本/title.txt", "r", encoding="utf-8") head1 = f2.readlines() # f3 = open(path+"/root/content/健康/content.txt", "r", encoding="utf-8") # content2 = f3.readlines() # f4 = open(path+"/root/content/健康/title.txt", "r", encoding="utf-8") # head2 = f4.readlines() f5 = open(path + "/root/content/体育/content.txt", "r", encoding="utf-8") content3 = f5.readlines() f6 = open(path + "/root/content/体育/title.txt", "r", encoding="utf-8") head3 = f6.readlines() # f7 = open("C:/Users/12293/Desktop/程序员/content.txt", "r", encoding="utf-8") # content4 = f7.readlines() # f8 = open("C:/Users/12293/Desktop/程序员/title.txt", "r", encoding="utf-8") # head4 = f8.readlines() f9 = open(path + "/root/content/经济/content.txt", "r", encoding="utf-8") content5 = f9.readlines() f10 = open(path + "/root/content/经济/title.txt", "r", encoding="utf-8") head5 = f10.readlines() f11 = open(path + "/root/content/军事/content.txt", "r", encoding="utf-8") content6 = f11.readlines() f12 = open(path + "/root/content/军事/title.txt", "r", encoding="utf-8") head6 = f12.readlines() f13 = open(path + "/root/content/艺术/content.txt", "r", encoding="utf-8") content7 = f13.readlines() f14 = open(path + "/root/content/艺术/title.txt", "r", encoding="utf-8") head7 = f14.readlines() for i in range(count): post1 = Post(title=head1[i][:-19], body=content1[i], category=Category.query.get(1), timestamp=fake.date_time_this_year()) # post2 = Post( # title=head2[i], # body=content2[i], # category=Category.query.get(2), # timestamp=fake.date_time_this_year() # ) post3 = Post(title=head3[i][:-19], body=content3[i], category=Category.query.get(3), timestamp=fake.date_time_this_year()) # post4 = Post( # title=head4[i], # body=content4[i], # category=Category.query.get(4), # timestamp=fake.date_time_this_year() # ) post5 = Post(title=head5[i][:-19], body=content5[i], category=Category.query.get(4), timestamp=fake.date_time_this_year()) post6 = Post(title=head6[i][:-19], body=content6[i], category=Category.query.get(5), timestamp=fake.date_time_this_year()) post7 = Post(title=head7[i][:-19], body=content7[i], category=Category.query.get(6), timestamp=fake.date_time_this_year()) db.session.add(post1) # db.session.add(post2) db.session.add(post3) # db.session.add(post4) db.session.add(post5) db.session.add(post6) db.session.add(post7) db.session.commit()