def cms_article_query(request): form = cmsArticleQueryForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') author = form.cleaned_data.get('author') category = form.cleaned_data.get('category') c_page = form.cleaned_data.get('c_page') #目前只能按标题查询,此处有待完善 articles = ArticleModel.objects.filter( Q(title__contains=title) & Q(author__username__contains=author) & Q(category__name__contains=category)) article_list = list( articles.annotate(author_name=F('author__username'), category_name=F('category__name')).values( 'uid', 'title', 'author_name', 'category_name', 'release_time', 'read_count', 'top').order_by('-top__top_time', '-release_time')) for article in article_list: if article['top']: article['top'] = True else: article['top'] = False context = page(c_page, article_list, query_name='article') return bnjson.json_result(message='查询成功!', data=context) else: return form.error_json_resopnse()
def cms_article_top(request): form = cmsTopArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') article = ArticleModel.objects.filter(pk=uid).first() if article: if article.top: article.top.save() # article.save(update_fields=['top']) return bnjson.json_result(message='已再次置顶') else: top = TopModel() top.save() article.top = top article.save(update_fields=['top']) return bnjson.json_result(message='已置顶') else: return bnjson.json_params_error(message='没有这篇文章,无法置顶') else: return form.error_json_resopnse()
def cms_author_delete(request): form = cmsDeleteAuthorForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('author_uid') author = frontAuthModel.objects.filter(pk=uid).first() if author: author.delete() return bnjson.json_result(message='该用户已被删除') else: return bnjson.json_params_error(message='没有该用户!') else: return form.error_json_resopnse()
def cms_article_delete(request): form = cmsDeleteArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') article = ArticleModel.objects.filter(pk=uid).first() if article: data = model_to_dict(article, fields=['title', 'author__username']) article.delete() return bnjson.json_result(message='文章已删除', data=data) else: return bnjson.json_params_error(message='没有这篇文章,无法删除') else: return form.error_json_resopnse()
def cms_add_tag(request): form = cmsAddTagForm(request.POST) if form.is_valid(): name = form.cleaned_data.get('name') tag = TagModel.objects.filter(name=name).first() if tag: return bnjson.json_params_error(message='该标签已存在') else: tag = TagModel(name=name) tag.save() data = {'id': tag.id, 'name': tag.name} return bnjson.json_result(data=data) else: return form.error_json_resopnse()
def cms_add_category(request): form = cmsAddCategoryForm(request.POST) if form.is_valid(): name = form.cleaned_data.get('name') category = CategoryModel.objects.filter(name=name).first() if category: return bnjson.json_params_error(message='该分类已存在') else: category = CategoryModel(name=name) category.save() data = {'id': category.id, 'name': category.name} return bnjson.json_result(data=data) else: return form.error_json_resopnse()
def cms_article_untop(request): form = cmsTopArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') article = ArticleModel.objects.filter(pk=uid).first() if article: if article.top: article.top.delete() return bnjson.json_result(message='已取消置顶') else: bnjson.json_params_error(message='这篇文章未置顶,无法进行置顶操作') else: return bnjson.json_params_error(message='没有这篇文章,无法置顶') else: return form.error_json_resopnse()
def cms_article_modify(request, uid): if request.method == 'GET': article = ArticleModel.objects.filter(pk=uid).first() categorys = CategoryModel.objects.all().order_by('pk') tags = TagModel.objects.all().order_by('pk') if article: context = { 'article': article, 'categorys': categorys, 'tags': tags, 'article_tags': list(article.tags.values_list('id', flat=True)) } return render(request, 'cms_article_modify.html', context=context) else: return bnjson.json_params_error(message='没有这篇文章') else: form = cmsArticleModifyForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') category_id = form.cleaned_data.get('category_id') category = CategoryModel.objects.filter(id=category_id).first() content = form.cleaned_data.get('content') article = ArticleModel.objects.filter(pk=uid).first() article.title = title article.category = category article.content_html = content article.save(update_fields=[ 'title', 'category', 'content_html', 'update_time' ]) tagIds = request.POST.getlist('tagIds') tags = [] if tagIds: for tag_id in tagIds: tag = TagModel.objects.filter(pk=tag_id).first() if tag: tags.append(tag) print(tags) article.tags.set(tags) return bnjson.json_result(message='修改成功') else: return form.error_json_resopnse()