def get(self, id): user_id = current_user.get('user_id') role = current_user.get('role') portfolio = portfolios.get_portfolio(id) if not portfolio: return {'success': False, 'message': 'Portfolio not found'}, 404 elif portfolio['user_id'] != user_id and role != UserRoles.ADMIN.value: return { 'success': False, 'message': 'You are only allowed to publish your own portfolios' }, 403 publish_res = portfolios.publish_portfolio(id) if publish_res['updated']: return {'success': True} else: return {'success': False, 'message': 'Portfolio already published'}
def post(self): args = request.json print('Processing optimization job submission for', args) user_id = current_user.get('user_id') task_id = task_optimize.delay(args['name'], args['tickers'], args['benchmark_index'], args['start_date'], args['end_date'], user_id, args['interval']) return {'task_id': str(task_id)}, 202
def get(self, id, name): user_id = current_user.get('user_id') role = current_user.get('role') portfolio = portfolios.get_portfolio(id) if not portfolio: return {'success': False, 'message': 'Portfolio not found'}, 404 elif portfolio['user_id'] != user_id and role != UserRoles.ADMIN.value: return { 'success': False, 'message': 'You are only allowed to rename your own portfolios' }, 403 rename_res = portfolios.rename_portfolio(id, name) if rename_res['updated']: portfolio['name'] = name return {'success': True, 'portfolio': portfolio} else: return {'success': False, 'message': 'Portfolio name is unchanged'}
def get(self, id): user_id = current_user.get('user_id') role = current_user.get('role') portfolio = portfolios.get_portfolio(id) if not portfolio: return {'success': False, 'message': 'Portfolio not found'}, 404 elif portfolio['user_id'] != user_id and role != UserRoles.ADMIN.value: return { 'success': False, 'message': 'You are only allowed to delete your own portfolios' }, 403 rename_res = portfolios.delete_portfolio(id) if rename_res['deleted']: return { 'success': True, 'message': f'Successfully deleted {portfolio["name"]}.' } else: return { 'success': False, 'message': 'Portfolio does not exist or could not be deleted' }
def deleteComment(id): if request.method == 'DELETE': try: db = DatabaseConnection() comment = db.call_procedure('GetCommentOfUser', [id, current_user.get('id')])[0] if comment: db.call_procedure('DeleteComment', [id], True) return jsonify( {'msg': serverMessage["deleteCommentSuccessful"]}), 202 else: return jsonify( {'msg': serverMessage["deleteCommentNotAuthorized"]}), 403 except Exception as e: raise e return jsonify({'success': False}), 400
def createComment(id): if request.method == 'POST': try: data = request.get_json(force=True) if not data.get('body'): return jsonify({'msg': serverMessage["missingInputField"]}), 400 db = DatabaseConnection() db.call_procedure('CreateComment', [data.get('body'), current_user.get('id'), id], True) newComment = db.call_procedure('GetLastComment')[0] return jsonify({ 'msg': serverMessage["createCommentSuccessful"], 'newComment': newComment }), 201 except Exception as e: raise e return jsonify({'success': False}), 400
def updateComment(id): if request.method == 'PATCH': try: data = request.get_json(force=True) db = DatabaseConnection() comment = db.call_procedure('GetCommentOfUser', [id, current_user.get('id')])[0] if comment: db.call_procedure('UpdateComment', [id, data.get('body')], True) else: return jsonify( {'msg': serverMessage["editCommentNotAuthorized"]}), 403 return jsonify({'msg': serverMessage["editCommentSuccessful"]}), 200 except Exception as e: raise e return jsonify({'success': False}), 400
def createPost(): if request.method == 'POST': try: data = request.get_json(force=True) if not data.get('title') or not data.get('body'): return jsonify({'msg': serverMessage["missingInputField"]}), 400 db = DatabaseConnection() db.call_procedure( 'CreatePost', [data.get('title'), data.get('body'), current_user.get('id')], True) newPostID = db.call_procedure('GetPostLastIndex')[0] return jsonify({ 'msg': serverMessage["createPostSuccessful"], 'newPostID': newPostID.get('id') }), 201 except Exception as e: raise e return jsonify({'success': False}), 400
def is_admin(): if current_user.get('role') == UserRoles.ADMIN.value: return True else: return False
def get(self): user_id = current_user.get('user_id') return portfolios.get_portfolios_by_user(user_id)