def delete_cate(request, cate_id): cate_filter = dict() cate_filter['cate_id'] = cate_id count = Article.objects.filter(**cate_filter).count() if count > 0: return response_json(0, [], "该类别下有文章,不允许删除") cate = Cate.objects.filter(**cate_filter).first() cate.delete() return response_json(1, [], "删除成功", reverse('admin.cate.index'))
def article_look_num_incr(request): param = request.GET article_id = param.get('article_id') article = Article.objects.filter(article_id=article_id).first() if article is None: return response_json(0, [], '参数有误') article.look_num = article.look_num + 1 article.save() return response_json(1, [], '操作成功')
def save_cate(request): parm = request.POST cate_id = parm.get('cate_id', 0) title = parm.get('title', '') picture = parm.get('picture', '') cate = CateDao.createOrUpdate(cate_id, title, picture) if type(cate) == Cate: return response_json(1, [], "保存成功", reverse('admin.cate.index')) else: return response_json(0, "")
def file_upload_for_editormd(request): myFile = request.FILES.get("editormd-image-file", None) # 获取上传的文件,如果没有文件,则默认为None type_key = 'FT_ARTICLE_IMG' res = {} if not myFile: res['success'] = 0 res['msg'] = '未找到上传文件' return HttpResponse(json.dumps(res, ensure_ascii=False), content_type="application/json,charset=utf-8") upload_file_type = UploadFile.objects.filter(type_key=type_key).first() if upload_file_type == None: return response_json(False, {}, '未找到对应key') root_directory = os.path.dirname( django_cms.settings.__file__) + "/static/uploadfiles/" + upload_file_type.path + "/" if not os.path.isdir(root_directory): os.makedirs(root_directory) now = int(time.time()) # 获得文件名称 filename = myFile.name file = os.path.splitext(filename) filename, type = file destination = open(os.path.join(root_directory, str(now) + type), 'wb+') # 打开特定的文件进行二进制的写操作 for chunk in myFile.chunks(): # 分块写入文件 destination.write(chunk) destination.close() res['success'] = 1 res['url'] = "/static/uploadfiles/" + upload_file_type.path + "/" + str(now) + type return HttpResponse(json.dumps(res, ensure_ascii=False), content_type="application/json,charset=utf-8")
def article_save(request): param = request.POST article = ArticleDao.createOrUpdate(article_id=param.get( 'article_id', 0), cate_id=param.get('cate_id', 1), title=param.get('title'), content=param.get('content', '')) tagsinput = param.get('tagsinput') LabelDao.update_lable_article(tagsinput, article.article_id) return response_json(1, [], "保存成功", reverse('admin.article.index'))
def label_list(request): label_list = LabelDao.get_all_label() res = [] for label in label_list: item = {} item['label_id'] = label.label_id item['title'] = label.title item['article_count'] = LabelArticle.objects.filter( label_id=label.label_id).count() res.append(item) return response_json(1, res)
def article_list(request): # 获得所有数据 param = request.GET cate_id = param.get('cate_id', 0) title = param.get('title', '') label_id = param.get('label_id', 0) post_date = param.get('post_date', '') filter_dict = dict() if len(title) > 0: filter_dict['title__icontains'] = title if cate_id: filter_dict['cate_id'] = cate_id if len(post_date) > 0: #发表时间大于当天00:00:00,小于23:59:59 filter_dict['updated_at__gte'] = post_date + " 00:00:00" filter_dict['updated_at__lte'] = post_date + " 23:59:59" article_list = ArticleDao.getArticleList(filter_dict, param.get('page', 1), label_id=label_id) res = {} res['total'] = article_list[1] article_list = article_list[0] article_info = [] res['page_size'] = ConstDao.getPageNum() for article in article_list: item = {} item['article_id'] = article.article_id item['title'] = article.title item['content'] = article.content[0:138] item['look_num'] = article.look_num item['rate_num'] = article.rate_num item['comment_num'] = article.comment_num item['cate_id'] = article.cate_id cate = Cate.objects.filter(cate_id=article.cate_id).first() if cate: item['cate_url'] = get_file_url(request, cate.picture) item['cate_title'] = cate.title else: item['cate_url'] = '' item['cate_title'] = '' if type(article.updated_at) == datetime: item['updated_at'] = article.updated_at.strftime( '%Y-%m-%d %H:%M:%S') else: item['updated_at'] = article.updated_at article_info.append(item) res['article_info'] = article_info return response_json(1, res)
def file_upload(request): myFile = request.FILES.get("file", None) # 获取上传的文件,如果没有文件,则默认为None type_key = request.POST.get('type_key') if not myFile: return response_json(False, {'data': {'file_path': '11', 'file_id': 0, 'file_oldname': 'test'}}); upload_file_type = UploadFile.objects.filter(type_key=type_key).first() if upload_file_type == None: return response_json(False, {}, '未找到对应key') root_directory = os.path.dirname( django_cms.settings.__file__) + "/static/uploadfiles/" + upload_file_type.path + "/" if not os.path.isdir(root_directory): os.makedirs(root_directory) now = int(time.time()) # 获得文件名称 name = request.POST.get('name') file = os.path.splitext(name) filename, type = file destination = open(os.path.join(root_directory, str(now) + type), 'wb+') # 打开特定的文件进行二进制的写操作 for chunk in myFile.chunks(): # 分块写入文件 destination.write(chunk) destination.close() return response_json(True, { 'data': {'file_path': "/static/uploadfiles/" + upload_file_type.path + "/" + str(now) + type, 'file_id': 0, 'file_oldname': myFile.name}});
def cate_list(request): cate_list = CateDao.getAllCate() res = [] for cate in cate_list: item = {} item['cate_id'] = cate.cate_id item['title'] = cate.title item['cate_img_url'] = get_file_url(request, cate.picture) item['article_count'] = Article.objects.filter( cate_id=cate.cate_id).count() item['look_num'] = Article.objects.filter( cate_id=cate.cate_id).aggregate( Sum('look_num')).get('look_num__sum') res.append(item) return response_json(1, res)
def rate_value_submit(request): article_id = request.GET.get('article_id') rate_num = request.GET.get('rate_num') rate_modal = ArticleRate() rate_modal.article_id = article_id rate_modal.rate_value = rate_num rate_modal.ip = get_client_ip(request) rate_modal.created_at = TimeDao.get_current_time() rate_modal.save() rate_value_modal = ArticleRate.objects.filter( article_id=article_id).aggregate(Avg('rate_value')) article = Article.objects.filter(article_id=article_id).first() if article: article.rate_num = nums_with_two_point( rate_value_modal['rate_value__avg']) article.save() return response_json(1, [], '操作成功')
def comment_update(request): param = request.GET article_id = param.get('article_id', 0) comments = param.get('comment', "") article_comment = ArticleComment() article_comment.article_id = article_id article_comment.comment = comments ip = "" if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_FOR'] else: if 'REMOTE_ADDR' in request.META and len( str(request.META['REMOTE_ADDR'])) > 0: ip = request.META['REMOTE_ADDR'] else: ip = '未知用户' article_comment.remote_addr = ip article_comment.locate_area = "中国" article_comment.save() return response_json(1, [], '发表成功')
def logout(request): do_logout(request) return response_json(1, "")
def article_delete(request, article_id): #判断,如果该文章的标签是 ArticleDao.deleteArticle(article_id) return response_json(1, [], "删除成功", reverse('admin.article.index'))
def article_detail(request): article_id = request.GET.get('article_id', 0) article_info = ArticleDao.getArticleDetail(article_id) return response_json(1, article_info)