def add_level_two(request, user, body): level_one_id = get_key(body, 'levelOneId') level_two_id = body.get('id', None) title = get_key(body, 'title') try: level_one = ArticalLevelOne.objects.get(pk=level_one_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到该语言') if level_two_id: try: level_two = ArticalLevelTwo.objects.get(pk=level_two_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到分组信息') else: level_two = ArticalLevelTwo() level_two.level_one = level_one level_two.title = title level_two.save() return res_cross_success(None)
def catch_exception(*args): try: result = fn(*args) except CodeMsgEmptyException as empty: return res_cross(empty.code, None, empty.message) except CodeMsgException as empty: return res_cross(empty.code, None, empty.message) return result
def safe_create_artical_with_uuid(request, user, body, files): markdown_file = files['file'] artical_title = get_key(body, 'title') artical_uuid = get_key(body, 'uuid') artical_level_two_id = get_key(body, 'levelTwoId') try: level_two = ArticalLevelTwo.objects.get(pk=artical_level_two_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未匹配到二级菜单') try: artical = Artical.objects.get(uuid=artical_uuid) artical.title = artical_title except Exception as e: artical = Artical() artical.uuid = artical_uuid artical.title = artical_title no = ArticalNo() no.save() artical.no = no return private_create_artical(level_two=level_two, artical=artical, artical_title=artical_title, artical_route_path=None, mark_down_file=markdown_file, content_type='mark_down_file')
def article_detail(request, id, user, body): try: article = Article.objects.get(no=id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到详情') return res_cross_success(model_article_tool.article_to_obj(article))
def artical_add(request, user, body, files): artical_id = body.get('id', None) artical_title = get_key(body, 'title') artical_level_two_id = get_key(body, 'levelTwoId') artical_route_path = body.get('routePath', None) mark_down_file = None artical = None if artical_id: try: artical = Artical.objects.get(pk=artical_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到该文章') if len(files): mark_down_file = files['file'] try: level_two = ArticalLevelTwo.objects.get(pk=artical_level_two_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未匹配到二级菜单') content_type = None if artical_route_path: content_type = 'route_path' if mark_down_file: content_type = 'mark_down_file' return private_create_artical(level_two=level_two, artical=artical, artical_title=artical_title, artical_route_path=None, mark_down_file=mark_down_file, content_type=content_type)
def remove_level_two(request, user, body): level_two_id = body.get('id', None) try: level_two = ArticalLevelTwo.objects.get(pk=level_two_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到分组信息') exist = Artical.objects.filter( level_two=level_two).order_by('-create_time') if len(exist): return res_cross('1001', None, '当前分组下存在文章,请先移动文章') level_two.delete() return res_cross_success(None)
def article_delete(request, user, body): """ 删除文章 """ node = model_article_tool.article_by_no(body.get('id', None)) if not node: return res_cross('3001', None, '异常') if Article.objects.filter(s_node=node).count(): return res_cross_error('存在子节点,无法删除') node.delete() return res_cross_success()
def level_one_detail(request, user, body): level_one_id = get_key(body, 'id') try: level_one = ArticalLevelOne.objects.get(pk=level_one_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到详情') return res_cross_success(level_one.to_obj())
def artical_detail(request, user, body): artical_id = get_key(body, 'id') try: artical = Artical.objects.get(pk=artical_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到详情') return res_cross_success(artical.to_obj())
def private_create_artical(level_two, artical, artical_title, artical_route_path, mark_down_file, content_type): if not artical: artical = Artical() no = ArticalNo() no.save() artical.no = no artical.title = artical_title artical.content_type = content_type artical.level_two = level_two if mark_down_file: try: content = mark_down_file.read() except Exception as e: return res_cross('1001', None, '分析MarkDown文件失败') content_html = md2html(content) artical.mark_down_file = mark_down_file tmp_directory = os.path.join(MEDIA_ROOT, 'tmp') if not os.path.exists(tmp_directory): os.makedirs(tmp_directory) tmp_artical_directory = os.path.join(tmp_directory, 'artical') if not os.path.exists(tmp_artical_directory): os.makedirs(tmp_artical_directory) tmp_file = os.path.join(tmp_directory, uuid.uuid4().hex) html_file = open(tmp_file, 'w') html_file.write(content_html) html_file.close() html_read_file = File(open(tmp_file, 'r')) artical.html_file = html_read_file os.remove(tmp_file) artical.save() return res_cross_success(artical.to_obj())
def article_directory_create(request, user, body): """ 创建子文件夹 """ title = datetime.datetime.now().strftime('%Y%m%d%H%M%S') node = model_article_tool.article_by_no(body.get('sId', None)) if not node or node.node_type != ArticleNodeType.Directory: return res_cross('3001', None, '异常') article = Article() article.no = ArticleNo.get_no() article.title = title article.s_node = node article.node_type = ArticleNodeType.Directory article.content_type = ArticleContentType.NONE article.save() return res_cross_success()
def article_create(request, user, body): """ 创建文章 """ s_node = model_article_tool.article_by_no(body.get('sId', None)) if not s_node or s_node.node_type != ArticleNodeType.Directory: return res_cross('3001', None, '异常') article = Article() article.no = ArticleNo.get_no() article.title = datetime.datetime.now().strftime('%Y%m%d%H%M%S') article.s_node = s_node article.node_type = ArticleNodeType.Article article.save() return res_cross_success(model_article_tool.article_to_obj(article))
def level_two_detail(request, user, body): level_two_id = get_key(body, 'id') try: level_two = ArticalLevelTwo.objects.get(pk=level_two_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到详情') return res_cross_success({ 'detail': level_two.to_obj(), 'levelOneList': [ item.to_obj() for item in ArticalLevelOne.objects.all().order_by('-order') ] })
def article_update(request, user, body, files): """ 更新文章 """ article_id = body.get('id', None) article_title = get_key(body, 'title') article_route_path = body.get('routePath', None) mark_down_file = None article = None if article_id: try: article = Article.objects.get(no=article_id) except Exception as e: logger.info(e) return res_cross('1001', None, '未查询到该文章') if len(files): mark_down_file = files['file'] content_type = ArticleContentType.NONE if article_route_path: content_type = ArticleContentType.Route if mark_down_file: content_type = ArticleContentType.MD article.route_path = article_route_path article.title = article_title article.content_type = content_type if mark_down_file: try: content = mark_down_file.read() except Exception as e: return res_cross('1001', None, '分析MarkDown文件失败') content_html = md2html(content) article.mark_down_file = mark_down_file tmp_directory = os.path.join(MEDIA_ROOT, 'tmp') if not os.path.exists(tmp_directory): os.makedirs(tmp_directory) tmp_article_directory = os.path.join(tmp_directory, 'article') if not os.path.exists(tmp_article_directory): os.makedirs(tmp_article_directory) tmp_file = os.path.join(tmp_directory, uuid.uuid4().hex) html_file = open(tmp_file, 'w') html_file.write(content_html) html_file.close() html_read_file = File(open(tmp_file, 'r')) article.html_file = html_read_file os.remove(tmp_file) try: article.save() return res_cross_success(model_article_tool.article_to_obj(article)) except Exception as e: logger.error(e) return res_cross_error('更新失败')