def get_article_list(board_Name, page): data = [] offset = 5 board_id = session.query(Board).filter_by(name=board_Name).first().id articles = session.query(Article).filter_by(board=board_id).order_by(Article.pub_date.desc())[page*5-offset:offset*page] for article in articles: info = { 'id': article.id, 'title': article.title, 'pub_date': article.pub_date, } data.append(info) return jsonify(data)
def delete_board(board_name): board = session.query(Board).filter_by(name=board_name).first() if board: if 'session' in web_session: user_id = redisSession.open_session(web_session['session']) if board.master == int(user_id): delete(board) response = { 'status': 'success', 'message': 'Delete board successfully' } return response, 200 else: response = { 'status': 'fail', 'message': 'Permission denied' } return response, 401 else: response = { 'status': 'fail', 'message': 'Login required' } return response, 403 else: response = { 'status': 'fail', 'message': 'Undefined board' } return response, 404
def create_article(data, board_Name): if 'session' in web_session: user_id = redisSession.open_session(web_session['session']) if user_id: article = Article( board = session.query(Board).filter_by(name=board_Name).first().id, writer = user_id, title = data['title'], content = data['content'] ) save(article) response = { 'status': 'success', 'message': 'Create article successfully' } return response, 201 else: response = { 'status': 'fail', 'message': 'Permission denied' } return response, 401 else: response = { 'status': 'fail', 'message': 'Login required' } return response, 403
def create_board(name): board = session.query(Board).filter_by(name=name).first() if not board: if 'session' in web_session: user_id = redisSession.open_session(web_session['session']) if user_id: board = Board( name = name, master = user_id ) save(board) response = { 'status': 'success', 'message': 'Create board successfully' } return response, 201 else: response = { 'status': 'fail', 'message': 'Permission denied' } return response, 403 else: response = { 'status': 'fail', 'message': 'Login required' } return response, 403 else: response = { 'status': 'fail', 'message': 'Already existed board' } return response, 409
def register(data): user = session.query(User).filter_by(fullname=data['fullname']).first() if not user: email = session.query(User).filter_by(email=data['email']).first() if not email: new_user = User(fullname=data['fullname'], password=data['password'], email=data['email']) save(new_user) response = { 'status': 'success', 'message': 'Successfully registered' } return response, 201 else: response = {'status': 'fail', 'message': 'Email already exists'} return response, 409 else: response = {'status': 'fail', 'message': 'User already exists'} return response, 409
def update_board(new_name, old_name): board = session.query(Board).filter_by(name=old_name).first() if board: if not session.query(Board).filter_by(name=new_name).first(): if 'session' in web_session: user_id = redisSession.open_session(web_session['session']) if board.master == int(user_id): board.name = new_name save(board) response = { 'status': 'success', 'message': 'Update board successfully' } return response, 200 else: response = { 'status': 'fail', 'message': 'Permission denied' } return response, 401 else: response = { 'status': 'fail', 'message': 'Login required' } return response, 403 else: response = { 'status': 'fail', 'message': 'Already existed board' } return response, 409 else: response = { 'status': 'fail', 'message': 'Undefined board' } return response, 404
def get_article(board_Name, article_id): article = get_article_one(board_Name, article_id) if article: user = session.query(User).filter_by(id=article.writer).first() data = { 'title': article.title, 'pub_date': article.pub_date, 'writer': user.fullname, 'content': article.content, } return jsonify(data) else: response = { 'status': 'fail', 'message': 'Undefined article' } return response, 404
def login(data): user = session.query(User).filter_by(email=data['email']).first() if user: if user.verify_password(data['password']): session_key = redisSession.create_session(user.id) web_session['session'] = session_key response = { 'status': 'success', 'message': 'Successfully Logged in' } return response, 200 else: response = {'status': 'fail', 'message': 'Invalid Password'} return response, 409 else: response = {'status': 'fail', 'message': 'Undefined User'} return response, 404
def get_article_limit(board_Name): board_id = session.query(Board).filter_by(name=board_Name).first().id return session.query(Article).filter_by(board=board_id).order_by(Article.pub_date.desc())[0:5]
def get_article_one(board_Name, article_id): board_id = session.query(Board).filter_by(name=board_Name).first().id return session.query(Article).filter_by(board=board_id, id=article_id).first()
def get_board_list(page): offset = 5 return session.query(Board).order_by(Board.name)[page*5-offset:offset*page]