# -*-coding:utf-8 -*- import traceback from common.common import (get_user_from_request, login_required, params,JsonResponse,save_bs64img_local) from django.views.decorators.csrf import csrf_exempt from models import * # Create your views here. @csrf_exempt @params def content_show(request,offset): # obj, created = Article.objects.get_or_create(type=int(offset)) # if not created: # print("exit---------") # JsonResponse("exit") # else: # obj.title = "hhhhhh"+offset # obj.img_url = "http://" # obj.type = int(offset) # obj.content = "hahhahahha"+offset content_data = Article.objects.filter(type=int(offset)).first() dict_data={} if content_data: dict_data["title"]=content_data.title dict_data["img_url"]=content_data.img_url dict_data["type"]=content_data.type dict_data["content"]=content_data.content res = {"code":0,"msg":"show sucess!","content_data":dict_data} else: res = {"code":-1,"msg":"get empty data!",} return JsonResponse(res) @csrf_exempt @params def content_edit(request,offset): data = request.PARAMS if not data: return JsonResponse({"code": -1, "msg": u"参数不完整"}) title = data.get("title",None) img_url = data.get("img_url",None) content = data.get("content",None) # page_type = data.get("page_type",None) obj = Article.objects.filter(type=int(offset)).first() print("ccccccc",obj) if obj: obj.title = title obj.img_url = img_url obj.content = content obj.save() res = {"code":0, "msg":"sucess",} else: obj,flag = Article.objects.get_or_create(title = title,type=int(offset)) obj.img_url = img_url obj.content = content obj.save() res = {"code":0,"msg":"success",} return JsonResponse(res) @csrf_exempt # @params def banner_show(request): """ 横幅展示 return data list """ objs = Banner.objects.all() if objs: data_list=[] for obj_banner in objs: dict_data={} dict_data["bid"] = obj_banner.id dict_data["position"] = obj_banner.position dict_data["img_url"] = obj_banner.img_url dict_data["display_title"] = obj_banner.display_title dict_data["article_title"] = obj_banner.article_title dict_data["article_url"] = obj_banner.article_url dict_data["author"] = obj_banner.author dict_data["content"] = obj_banner.content dict_data["create_time"] = obj_banner.create_time # print(obj_banner.img_url) data_list.append(dict_data) res = {"code":0,"msg":"get data sucess!","content_data":data_list} else: res = {"code":-1,"msg":"get no data",} return JsonResponse(res) @csrf_exempt @params def banner_add(request): """ 添加横幅内容 """ req_params = request.PARAMS if req_params: try: position = req_params.get("position") img_url = req_params.get("img_url",None) display_title = req_params.get("display_title",None) article_url = req_params.get("article_url",None) article_title = req_params.get("article_title",None) author = req_params.get("author",None) content = req_params.get("content",None) # img_path = save_bs64img_local(img_url) # print("banner_edit*****_img_",img_path) if position!=0 and Banner.objects.filter(position=position): return JsonResponse({"code": -1, "msg": "横幅位置重复!"}) obj= Banner.objects.create(position=position,img_url=img_url,display_title=display_title, article_url=article_url,article_title=article_title,author=author,content=content) if obj: res = {"code": 0, "msg": "banner add sucess"} else: res = {"code": -1, "msg": "添加失败"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res) @csrf_exempt @params def banner_edit(request): """ 编辑横幅内容 """ req_params = request.PARAMS if req_params: try: bid = req_params.get("bid") position = int(req_params.get("position",None)) img_url = req_params.get("img_url",None) display_title = req_params.get("display_title",None) article_url = req_params.get("article_url",None) article_title = req_params.get("article_title",None) author = req_params.get("author",None) content = req_params.get("content",None) print("banner_edit*****_img_url",img_url) # img_path = save_bs64img_local(img_url) # print("banner_edit*****_img_",img_path) # if position!=0 and Banner.objects.filter(position=position): # return JsonResponse({"code": -1, "msg": "横幅位置重复!"}) obj= Banner.objects.filter(id=bid).first() if obj: # 处理横幅位置重复,0 表示未展示 if position!=0 and obj.position!=position: obj_postions=Banner.objects.all() list_postion =[] for ob in obj_postions: list_postion.append(int(ob.position)) print("00000000",position) print("11111111",list_postion) if position in list_postion: print("333333") return JsonResponse({"code": -1, "msg": "横幅位置重复!"}) obj.position = position obj.img_url = img_url obj.display_title = display_title obj.article_url = article_url obj.author = author obj.content = content obj.save() res = {"code": 0, "msg": "banner edit sucess"} else: res = {"code": -1, "msg": "banner edit fail"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res) @csrf_exempt @params def banner_delete(request): """ 删除横幅 """ req_params = request.PARAMS if req_params: try: bid = req_params.get("bid") obj= Banner.objects.filter(id=bid).first() if obj: obj.delete() res = {"code": 0, "msg": "banner delete sucess"} else: res = {"code": -1, "msg": "banner delete fail"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res) @csrf_exempt def news_show(request): """ 最新动态 return data list """ objs = News.objects.all() if objs: data_list=[] for obj_news in objs: dict_data={} dict_data["nid"] = obj_news.id dict_data["title"] = obj_news.title dict_data["create_time"] = obj_news.create_time dict_data["author"] = obj_news.author data_list.append(dict_data) res = {"code":0,"msg":"show sucess!","content_data":data_list} else: res = {"code":-1,"msg":"no data"} return JsonResponse(res) @csrf_exempt @params def news_add(request): """ 新增动态 """ req_params = request.PARAMS if req_params: try: img_url = req_params.get("img_url",None) title = req_params.get("title") author = req_params.get("author") content = req_params.get("content") obj= News.objects.create(title=title,img_url=img_url,author=author,content=content) if obj: res = {"code": 0, "msg": "news add sucess"} else: res = {"code": -1, "msg": "news add fail"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res) @csrf_exempt @params def news_look(request): """ 动态查看 """ req_params = request.PARAMS if req_params: try: dict_data = {} nid = req_params.get("nid") obj = News.objects.filter(id=nid).first() if obj: dict_data["title"] = obj.title dict_data["create_time"] = obj.create_time dict_data["author"] = obj.author dict_data["content"] = obj.content dict_data["img_url"] = obj.img_url print("news_look-----",dict_data) res={"code": 0, "msg": "sucess","look_data":dict_data} else: res={"code": -1, "msg": "get data fail"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res) @csrf_exempt @params def news_delete(request): """ 删除横幅 """ req_params = request.PARAMS if req_params: try: bid = req_params.get("nid") obj= News.objects.filter(id=bid).first() if obj: obj.delete() res = {"code": 0, "msg": "news delete sucess"} else: res = {"code": -1, "msg": "news delete fail"} except: traceback.print_exc() else: res={"code": -1, "msg": u"参数不完整"} return JsonResponse(res)