def add_level_one(request, user, body): title = get_key(body, 'title') ArticalLevelOne(user=user, title=title).save() return res_cross_success(None)
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 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 article_directory_update(request, user, body): """ 创建子文件夹 """ title = get_key(body, 'title') node = model_article_tool.article_by_no(body.get('id', None)) node.title = title node.save() return res_cross_success()
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 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_manager_list(request, user, body): all = Artical.objects.all().order_by('-create_time') result = [{ 'id': artical.id, 'title': artical.title, "levelOne": artical.level_two.level_one.to_obj(), 'levelTwo': artical.level_two.to_obj(), 'markDownFilePath': parse_to_url(artical.mark_down_file.path), 'scan': artical.scan } for artical in all] return res_cross_success(result)
def artical_list(request, user, body): result = [ item.to_obj_with_level_two_list() for item in ArticalLevelOne.objects.all().order_by('-order') ] for level_one in result: for level_two in level_one['levelTwoList']: level_two['articalList'] = [ artical.simple_to_obj() for artical in Artical.objects.filter( level_two_id=level_two['id']).order_by('-create_time') ] return res_cross_success(result)
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 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_list(request, user, body): """ 获取文章列表 """ return res_cross_success(model_article_tool.article_list())
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('更新失败')
def level_two_list(request, user, body): return res_cross_success([ item.to_obj() for item in ArticalLevelTwo.objects.all().order_by('-order') ])