views.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # -*-coding:utf-8 -*-
  2. import traceback
  3. from common.common import (get_user_from_request, login_required,
  4. params,JsonResponse,save_bs64img_local)
  5. from django.views.decorators.csrf import csrf_exempt
  6. from models import *
  7. # Create your views here.
  8. @csrf_exempt
  9. @params
  10. def content_show(request,offset):
  11. # obj, created = Article.objects.get_or_create(type=int(offset))
  12. # if not created:
  13. # print("exit---------")
  14. # JsonResponse("exit")
  15. # else:
  16. # obj.title = "hhhhhh"+offset
  17. # obj.img_url = "http://"
  18. # obj.type = int(offset)
  19. # obj.content = "hahhahahha"+offset
  20. content_data = Article.objects.filter(type=int(offset)).first()
  21. dict_data={}
  22. if content_data:
  23. dict_data["title"]=content_data.title
  24. dict_data["img_url"]=content_data.img_url
  25. dict_data["type"]=content_data.type
  26. dict_data["content"]=content_data.content
  27. res = {"code":0,"msg":"show sucess!","content_data":dict_data}
  28. else:
  29. res = {"code":-1,"msg":"get empty data!",}
  30. return JsonResponse(res)
  31. @csrf_exempt
  32. @params
  33. def content_edit(request,offset):
  34. data = request.PARAMS
  35. if not data:
  36. return JsonResponse({"code": -1, "msg": u"参数不完整"})
  37. title = data.get("title",None)
  38. img_url = data.get("img_url",None)
  39. content = data.get("content",None)
  40. # page_type = data.get("page_type",None)
  41. obj = Article.objects.filter(type=int(offset)).first()
  42. print("ccccccc",obj)
  43. if obj:
  44. obj.title = title
  45. obj.img_url = img_url
  46. obj.content = content
  47. obj.save()
  48. res = {"code":0, "msg":"sucess",}
  49. else:
  50. obj,flag = Article.objects.get_or_create(title = title,type=int(offset))
  51. obj.img_url = img_url
  52. obj.content = content
  53. obj.save()
  54. res = {"code":0,"msg":"success",}
  55. return JsonResponse(res)
  56. @csrf_exempt
  57. # @params
  58. def banner_show(request):
  59. """
  60. 横幅展示
  61. return data list
  62. """
  63. objs = Banner.objects.all()
  64. if objs:
  65. data_list=[]
  66. for obj_banner in objs:
  67. dict_data={}
  68. dict_data["bid"] = obj_banner.id
  69. dict_data["position"] = obj_banner.position
  70. dict_data["img_url"] = obj_banner.img_url
  71. dict_data["display_title"] = obj_banner.display_title
  72. dict_data["article_title"] = obj_banner.article_title
  73. dict_data["article_url"] = obj_banner.article_url
  74. dict_data["author"] = obj_banner.author
  75. dict_data["content"] = obj_banner.content
  76. dict_data["create_time"] = obj_banner.create_time
  77. # print(obj_banner.img_url)
  78. data_list.append(dict_data)
  79. res = {"code":0,"msg":"get data sucess!","content_data":data_list}
  80. else:
  81. res = {"code":-1,"msg":"get no data",}
  82. return JsonResponse(res)
  83. @csrf_exempt
  84. @params
  85. def banner_add(request):
  86. """
  87. 添加横幅内容
  88. """
  89. req_params = request.PARAMS
  90. if req_params:
  91. try:
  92. position = req_params.get("position")
  93. img_url = req_params.get("img_url",None)
  94. display_title = req_params.get("display_title",None)
  95. article_url = req_params.get("article_url",None)
  96. article_title = req_params.get("article_title",None)
  97. author = req_params.get("author",None)
  98. content = req_params.get("content",None)
  99. # img_path = save_bs64img_local(img_url)
  100. # print("banner_edit*****_img_",img_path)
  101. if position!=0 and Banner.objects.filter(position=position):
  102. return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  103. obj= Banner.objects.create(position=position,img_url=img_url,display_title=display_title,
  104. article_url=article_url,article_title=article_title,author=author,content=content)
  105. if obj:
  106. res = {"code": 0, "msg": "banner add sucess"}
  107. else:
  108. res = {"code": -1, "msg": "添加失败"}
  109. except:
  110. traceback.print_exc()
  111. else:
  112. res={"code": -1, "msg": u"参数不完整"}
  113. return JsonResponse(res)
  114. @csrf_exempt
  115. @params
  116. def banner_edit(request):
  117. """
  118. 编辑横幅内容
  119. """
  120. req_params = request.PARAMS
  121. if req_params:
  122. try:
  123. bid = req_params.get("bid")
  124. position = int(req_params.get("position",None))
  125. img_url = req_params.get("img_url",None)
  126. display_title = req_params.get("display_title",None)
  127. article_url = req_params.get("article_url",None)
  128. article_title = req_params.get("article_title",None)
  129. author = req_params.get("author",None)
  130. content = req_params.get("content",None)
  131. print("banner_edit*****_img_url",img_url)
  132. # img_path = save_bs64img_local(img_url)
  133. # print("banner_edit*****_img_",img_path)
  134. # if position!=0 and Banner.objects.filter(position=position):
  135. # return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  136. obj= Banner.objects.filter(id=bid).first()
  137. if obj:
  138. # 处理横幅位置重复,0 表示未展示
  139. if position!=0 and obj.position!=position:
  140. obj_postions=Banner.objects.all()
  141. list_postion =[]
  142. for ob in obj_postions:
  143. list_postion.append(int(ob.position))
  144. print("00000000",position)
  145. print("11111111",list_postion)
  146. if position in list_postion:
  147. print("333333")
  148. return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  149. obj.position = position
  150. obj.img_url = img_url
  151. obj.display_title = display_title
  152. obj.article_url = article_url
  153. obj.author = author
  154. obj.content = content
  155. obj.save()
  156. res = {"code": 0, "msg": "banner edit sucess"}
  157. else:
  158. res = {"code": -1, "msg": "banner edit fail"}
  159. except:
  160. traceback.print_exc()
  161. else:
  162. res={"code": -1, "msg": u"参数不完整"}
  163. return JsonResponse(res)
  164. @csrf_exempt
  165. @params
  166. def banner_delete(request):
  167. """
  168. 删除横幅
  169. """
  170. req_params = request.PARAMS
  171. if req_params:
  172. try:
  173. bid = req_params.get("bid")
  174. obj= Banner.objects.filter(id=bid).first()
  175. if obj:
  176. obj.delete()
  177. res = {"code": 0, "msg": "banner delete sucess"}
  178. else:
  179. res = {"code": -1, "msg": "banner delete fail"}
  180. except:
  181. traceback.print_exc()
  182. else:
  183. res={"code": -1, "msg": u"参数不完整"}
  184. return JsonResponse(res)
  185. @csrf_exempt
  186. def news_show(request):
  187. """
  188. 最新动态
  189. return data list
  190. """
  191. objs = News.objects.all()
  192. if objs:
  193. data_list=[]
  194. for obj_news in objs:
  195. dict_data={}
  196. dict_data["nid"] = obj_news.id
  197. dict_data["title"] = obj_news.title
  198. dict_data["create_time"] = obj_news.create_time
  199. dict_data["author"] = obj_news.author
  200. data_list.append(dict_data)
  201. res = {"code":0,"msg":"show sucess!","content_data":data_list}
  202. else:
  203. res = {"code":-1,"msg":"no data"}
  204. return JsonResponse(res)
  205. @csrf_exempt
  206. @params
  207. def news_add(request):
  208. """
  209. 新增动态
  210. """
  211. req_params = request.PARAMS
  212. if req_params:
  213. try:
  214. img_url = req_params.get("img_url",None)
  215. title = req_params.get("title")
  216. author = req_params.get("author")
  217. content = req_params.get("content")
  218. obj= News.objects.create(title=title,img_url=img_url,author=author,content=content)
  219. if obj:
  220. res = {"code": 0, "msg": "news add sucess"}
  221. else:
  222. res = {"code": -1, "msg": "news add fail"}
  223. except:
  224. traceback.print_exc()
  225. else:
  226. res={"code": -1, "msg": u"参数不完整"}
  227. return JsonResponse(res)
  228. @csrf_exempt
  229. @params
  230. def news_look(request):
  231. """
  232. 动态查看
  233. """
  234. req_params = request.PARAMS
  235. if req_params:
  236. try:
  237. dict_data = {}
  238. nid = req_params.get("nid")
  239. obj = News.objects.filter(id=nid).first()
  240. if obj:
  241. dict_data["title"] = obj.title
  242. dict_data["create_time"] = obj.create_time
  243. dict_data["author"] = obj.author
  244. dict_data["content"] = obj.content
  245. dict_data["img_url"] = obj.img_url
  246. print("news_look-----",dict_data)
  247. res={"code": 0, "msg": "sucess","look_data":dict_data}
  248. else:
  249. res={"code": -1, "msg": "get data fail"}
  250. except:
  251. traceback.print_exc()
  252. else:
  253. res={"code": -1, "msg": u"参数不完整"}
  254. return JsonResponse(res)
  255. @csrf_exempt
  256. @params
  257. def news_delete(request):
  258. """
  259. 删除横幅
  260. """
  261. req_params = request.PARAMS
  262. if req_params:
  263. try:
  264. bid = req_params.get("nid")
  265. obj= News.objects.filter(id=bid).first()
  266. if obj:
  267. obj.delete()
  268. res = {"code": 0, "msg": "news delete sucess"}
  269. else:
  270. res = {"code": -1, "msg": "news delete fail"}
  271. except:
  272. traceback.print_exc()
  273. else:
  274. res={"code": -1, "msg": u"参数不完整"}
  275. return JsonResponse(res)