def index(): page = request.args.get('page', 1, type=int) if request.cookies.get( 'show_followed' ) == 'show_followed' and current_user.is_authenticated: query = current_user.followed_posts else: query = Post.query pagination = query.order_by(Post.created.desc()).paginate(page, per_page=10) form = PostForm() if form.validate_on_submit(): post = Post(body=form.body.data, author_id=current_user.id) post.set_updated() try: db.session.add(post) db.session.commit() except: db.session.rollback() flash('发表失败请重试') return redirect(url_for('main.index')) context = { 'pagination': pagination, 'form': form, } return render_template('main/index.html', **context)
def post_create(): form = PostForm(request.form) form.action = url_for('admin:post:create') if form.validate_on_submit(): post = Post() form.populate_obj(post) post.publik = form.publik.data or False post.author = g.user post.save() flash('Sukses membuat posting baru') return redirect(url_for('admin:post:list')) return render_template('admin/post/create.html', form=form)
def create_test_post(): for i in range(1, 7): board_id = 9 title = "板块%s——标题%s"%(board_id,i) content = "内容%s"%i board = Board.query.get(board_id) author = FrontUser.query.first() post = Post(title=title, content=content) post.board = board post.author = author db.session.add(post) db.session.commit() print("测试帖子添加成功!")
def post_update(post_id): post = Post.get(Post.id == post_id) form = PostForm(request.form, obj=post) form.action = url_for('admin:post:update', post_id=post_id) if form.validate_on_submit(): form.populate_obj(post) post.save() flash('Sukses memperbarui posting') return redirect(url_for('admin:post:list')) return render_template('admin/post/update.html', form=form)
def new_post(): post = Post.form_json(request.json) post.author = g.current_user try: db.session.add(post) db.session.commit() except Exception as e: db.session.rollback() raise ValidationError('新增失败,请重试') return jsonify(post.to_json(), 201, {'Location': url_for('api.get_post', id=post.id)})
def post_delete(post_id): if g.user and g.user.level.name == 'admin': post = None try: post = Post.get(Post.id == post_id) except Post.DoesNotExist: flash('Gagal menghapus posting') if post: post.delete_instance() flash('Sukses menghapus posting') return redirect(url_for('admin:post:list'))
def setUpClass(cls): # start Firefox try: cls.client = webdriver.Firefox() except: pass # skip these tests if the browser could not be started if cls.client: # create the application cls.app = create_app('testing') cls.app_context = cls.app.app_context() cls.app_context.push() # suppress logging to keep unittest output clean import logging logger = logging.getLogger('werkzeug') logger.setLevel("ERROR") # create the database and populate with some fake data db.create_all() Role.insert_roles() User.generate_fake(10) Post.generate_fake(10) # add an administrator user admin_role = Role.query.filter_by(permissions=0xff).first() admin = User(email='*****@*****.**', username='******', password='******', role=admin_role, confirmed=True) db.session.add(admin) db.session.commit() # start the Flask server in a thread threading.Thread(target=cls.app.run).start() # give the server a second to ensure it is up time.sleep(1)
def home(): dosens = (User.select(User, MataKuliah) .join(MataKuliah) .join(Level, JOIN.LEFT_OUTER, on=(User.level == Level.id)) .join(Tugas, JOIN.LEFT_OUTER, on=(Tugas.mata_kuliah == MataKuliah.id)) .join(Profile, on=(User.profile == Profile.id)) .group_by(User) .where(Level.name == 'dosen') .order_by(Profile.nama.asc())) dosen_id = request.args.get('dosen_id', None) if dosen_id: dosen = dosens.filter(User.id == dosen_id).get() else: dosen = dosens.first() dosen_id = dosen.id posts = Post.select().order_by(Post.id.desc()) user = g.user return render_template('mhs/home.html', dosen=dosen, dosens=dosens, posts=posts, dosen_id=dosen_id)
def home(): posts = Post.select().where(Post.publik == True) return object_list('home.html', posts, var_name='posts')
def test_comments(self): # add two users r = Role.query.filter_by(name='User').first() self.assertIsNotNone(r) u1 = User(email='*****@*****.**', username='******', password='******', confirmed=True, role=r) u2 = User(email='*****@*****.**', username='******', password='******', confirmed=True, role=r) db.session.add_all([u1, u2]) db.session.commit() # add a post post = Post(body='body of the post', author=u1) db.session.add(post) db.session.commit() # write a comment response = self.client.post( url_for('api.new_post_comment', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog'), data=json.dumps({'body': 'Good [post](http://example.com)!'})) self.assertTrue(response.status_code == 201) json_response = json.loads(response.data.decode('utf-8')) url = response.headers.get('Location') self.assertIsNotNone(url) self.assertTrue(json_response['body'] == 'Good [post](http://example.com)!') self.assertTrue( re.sub('<.*?>', '', json_response['body_html']) == 'Good post!') # get the new comment response = self.client.get( url, headers=self.get_api_headers('*****@*****.**', 'cat')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertTrue(json_response['url'] == url) self.assertTrue(json_response['body'] == 'Good [post](http://example.com)!') # add another comment comment = Comment(body='Thank you!', author=u1, post=post) db.session.add(comment) db.session.commit() # get the two comments from the post response = self.client.get( url_for('api.get_post_comments', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2) # get all the comments response = self.client.get( url_for('api.get_comments', id=post.id), headers=self.get_api_headers('*****@*****.**', 'dog')) self.assertTrue(response.status_code == 200) json_response = json.loads(response.data.decode('utf-8')) self.assertIsNotNone(json_response.get('comments')) self.assertTrue(json_response.get('count', 0) == 2)
def post_list(): posts = Post.select().order_by(Post.date_created.desc()) return object_list('admin/post/list.html', posts, var_name='posts', paginate_by=10)