views.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. res = {"code":-1, "msg":"编辑失败"}
  51. print(res)
  52. return JsonResponse(res)
  53. @csrf_exempt
  54. # @params
  55. def banner_show(request):
  56. """
  57. 横幅展示
  58. return data list
  59. """
  60. objs = Banner.objects.all()
  61. if objs:
  62. data_list=[]
  63. for obj_banner in objs:
  64. dict_data={}
  65. dict_data["bid"] = obj_banner.id
  66. dict_data["position"] = obj_banner.position
  67. dict_data["img_url"] = obj_banner.img_url
  68. dict_data["display_title"] = obj_banner.display_title
  69. dict_data["article_title"] = obj_banner.article_title
  70. dict_data["article_url"] = obj_banner.article_url
  71. dict_data["author"] = obj_banner.author
  72. dict_data["content"] = obj_banner.content
  73. dict_data["create_time"] = obj_banner.create_time
  74. # print(obj_banner.img_url)
  75. data_list.append(dict_data)
  76. res = {"code":0,"msg":"get data sucess!","content_data":data_list}
  77. else:
  78. res = {"code":-1,"msg":"get no data",}
  79. return JsonResponse(res)
  80. @csrf_exempt
  81. @params
  82. def banner_add(request):
  83. """
  84. 添加横幅内容
  85. """
  86. req_params = request.PARAMS
  87. if req_params:
  88. try:
  89. position = req_params.get("position")
  90. img_url = req_params.get("img_url",None)
  91. display_title = req_params.get("display_title",None)
  92. article_url = req_params.get("article_url",None)
  93. article_title = req_params.get("article_title",None)
  94. author = req_params.get("author",None)
  95. content = req_params.get("content",None)
  96. # img_path = save_bs64img_local(img_url)
  97. # print("banner_edit*****_img_",img_path)
  98. if position!=0 and Banner.objects.filter(position=position):
  99. return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  100. obj= Banner.objects.create(position=position,img_url=img_url,display_title=display_title,
  101. article_url=article_url,article_title=article_title,author=author,content=content)
  102. if obj:
  103. res = {"code": 0, "msg": "banner add sucess"}
  104. else:
  105. res = {"code": -1, "msg": "添加失败"}
  106. except:
  107. traceback.print_exc()
  108. else:
  109. res={"code": -1, "msg": u"参数不完整"}
  110. return JsonResponse(res)
  111. @csrf_exempt
  112. @params
  113. def banner_edit(request):
  114. """
  115. 编辑横幅内容
  116. """
  117. req_params = request.PARAMS
  118. if req_params:
  119. try:
  120. bid = req_params.get("bid")
  121. position = int(req_params.get("position",None))
  122. img_url = req_params.get("img_url",None)
  123. display_title = req_params.get("display_title",None)
  124. article_url = req_params.get("article_url",None)
  125. article_title = req_params.get("article_title",None)
  126. author = req_params.get("author",None)
  127. content = req_params.get("content",None)
  128. print("banner_edit*****_img_url",img_url)
  129. # img_path = save_bs64img_local(img_url)
  130. # print("banner_edit*****_img_",img_path)
  131. # if position!=0 and Banner.objects.filter(position=position):
  132. # return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  133. obj= Banner.objects.filter(id=bid).first()
  134. if obj:
  135. # 处理横幅位置重复,0 表示未展示
  136. if position!=0 and obj.position!=position:
  137. obj_postions=Banner.objects.all()
  138. list_postion =[]
  139. for ob in obj_postions:
  140. list_postion.append(int(ob.position))
  141. print("00000000",position)
  142. print("11111111",list_postion)
  143. if position in list_postion:
  144. print("333333")
  145. return JsonResponse({"code": -1, "msg": "横幅位置重复!"})
  146. obj.position = position
  147. obj.img_url = img_url
  148. obj.display_title = display_title
  149. obj.article_url = article_url
  150. obj.author = author
  151. obj.content = content
  152. obj.save()
  153. res = {"code": 0, "msg": "banner edit sucess"}
  154. else:
  155. res = {"code": -1, "msg": "banner edit fail"}
  156. except:
  157. traceback.print_exc()
  158. else:
  159. res={"code": -1, "msg": u"参数不完整"}
  160. return JsonResponse(res)
  161. @csrf_exempt
  162. @params
  163. def banner_delete(request):
  164. """
  165. 删除横幅
  166. """
  167. req_params = request.PARAMS
  168. if req_params:
  169. try:
  170. bid = req_params.get("bid")
  171. obj= Banner.objects.filter(id=bid).first()
  172. if obj:
  173. obj.delete()
  174. res = {"code": 0, "msg": "banner delete sucess"}
  175. else:
  176. res = {"code": -1, "msg": "banner delete fail"}
  177. except:
  178. traceback.print_exc()
  179. else:
  180. res={"code": -1, "msg": u"参数不完整"}
  181. return JsonResponse(res)
  182. @csrf_exempt
  183. def news_show(request):
  184. """
  185. 最新动态
  186. return data list
  187. """
  188. objs = News.objects.all()
  189. if objs:
  190. data_list=[]
  191. for obj_news in objs:
  192. dict_data={}
  193. dict_data["nid"] = obj_news.id
  194. dict_data["title"] = obj_news.title
  195. dict_data["create_time"] = obj_news.create_time
  196. dict_data["author"] = obj_news.author
  197. data_list.append(dict_data)
  198. res = {"code":0,"msg":"show sucess!","content_data":data_list}
  199. else:
  200. res = {"code":-1,"msg":"no data"}
  201. return JsonResponse(res)
  202. @csrf_exempt
  203. @params
  204. def news_add(request):
  205. """
  206. 新增动态
  207. """
  208. req_params = request.PARAMS
  209. if req_params:
  210. try:
  211. img_url = req_params.get("img_url",None)
  212. title = req_params.get("title")
  213. author = req_params.get("author")
  214. content = req_params.get("content")
  215. obj= News.objects.create(title=title,img_url=img_url,author=author,content=content)
  216. if obj:
  217. res = {"code": 0, "msg": "news add sucess"}
  218. else:
  219. res = {"code": -1, "msg": "news add fail"}
  220. except:
  221. traceback.print_exc()
  222. else:
  223. res={"code": -1, "msg": u"参数不完整"}
  224. return JsonResponse(res)
  225. @csrf_exempt
  226. @params
  227. def news_look(request):
  228. """
  229. 动态查看
  230. """
  231. req_params = request.PARAMS
  232. if req_params:
  233. try:
  234. dict_data = {}
  235. nid = req_params.get("nid")
  236. obj = News.objects.filter(id=nid).first()
  237. if obj:
  238. dict_data["title"] = obj.title
  239. dict_data["create_time"] = obj.create_time
  240. dict_data["author"] = obj.author
  241. dict_data["content"] = obj.content
  242. dict_data["img_url"] = obj.img_url
  243. print("news_look-----",dict_data)
  244. res={"code": 0, "msg": "sucess","look_data":dict_data}
  245. else:
  246. res={"code": -1, "msg": "get data fail"}
  247. except:
  248. traceback.print_exc()
  249. else:
  250. res={"code": -1, "msg": u"参数不完整"}
  251. return JsonResponse(res)
  252. @csrf_exempt
  253. @params
  254. def news_delete(request):
  255. """
  256. 删除横幅
  257. """
  258. req_params = request.PARAMS
  259. if req_params:
  260. try:
  261. bid = req_params.get("nid")
  262. obj= News.objects.filter(id=bid).first()
  263. if obj:
  264. obj.delete()
  265. res = {"code": 0, "msg": "news delete sucess"}
  266. else:
  267. res = {"code": -1, "msg": "news delete fail"}
  268. except:
  269. traceback.print_exc()
  270. else:
  271. res={"code": -1, "msg": u"参数不完整"}
  272. return JsonResponse(res)