views.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # -*-coding:utf-8 -*-
  2. import traceback
  3. from common.common import (get_user_from_request, login_required,
  4. params,JsonResponse)
  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,"page_type":offset,"msg":"show sucess!","content_data":dict_data}
  28. else:
  29. res = {"code":-1,"page_type":offset,"msg":"show false!",}
  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["display_title"] = obj_banner.display_title
  67. dict_data["create_time"] = obj_banner.create_time
  68. dict_data["position"] = obj_banner.position
  69. data_list.append(dict_data)
  70. res = {"code":0,"msg":"get data sucess!","content_data":data_list}
  71. else:
  72. res = {"code":-1,"msg":"get no data",}
  73. return JsonResponse(res)
  74. @csrf_exempt
  75. @params
  76. def banner_add(request):
  77. """
  78. 添加横幅内容
  79. """
  80. req_params = request.PARAMS
  81. if req_params:
  82. try:
  83. position = req_params.get("position",None)
  84. img_url = req_params.get("img_url",None)
  85. display_title = req_params.get("display_title",None)
  86. article_url = req_params.get("article_url",None)
  87. article_title = req_params.get("article_title",None)
  88. author = req_params.get("author",None)
  89. content = req_params.get("content",None)
  90. obj= Banner.objects.create(position=position,img_url=img_url,display_title=display_title,
  91. article_url=article_url,article_title=article_title,author=author,content=content)
  92. res = res={"code": 0, "msg": "banner add sucess"}
  93. except:
  94. traceback.print_exc()
  95. else:
  96. res={"code": -1, "msg": u"参数不完整"}
  97. return JsonResponse(res)
  98. @csrf_exempt
  99. @params
  100. def banner_edit(request):
  101. """
  102. 编辑横幅内容
  103. """
  104. req_params = request.PARAMS
  105. if req_params:
  106. try:
  107. bid = req_params.get("bid")
  108. position = req_params.get("position",None)
  109. img_url = req_params.get("img_url",None)
  110. display_title = req_params.get("display_title",None)
  111. article_url = req_params.get("article_url",None)
  112. article_title = req_params.get("article_title",None)
  113. author = req_params.get("author",None)
  114. content = req_params.get("content",None)
  115. obj= Banner.objects.filter(id=bid)
  116. if obj:
  117. obj.id = bid
  118. obj.position = position
  119. obj.img_url = img_url
  120. obj.display_title = display_title
  121. obj.article_url = article_url
  122. obj.author = author
  123. obj.content = content
  124. obj.save()
  125. res = {"code": 0, "msg": "banner add sucess"}
  126. else:
  127. res = {"code": -1, "msg": "banner add fail"}
  128. except:
  129. traceback.print_exc()
  130. else:
  131. res={"code": -1, "msg": u"参数不完整"}
  132. return JsonResponse(res)
  133. @csrf_exempt
  134. @params
  135. def banner_delete(request):
  136. """
  137. 删除横幅
  138. """
  139. req_params = request.PARAMS
  140. if req_params:
  141. try:
  142. bid = req_params.get("bid")
  143. obj= Banner.objects.filter(id=bid).first()
  144. if obj:
  145. obj.delete()
  146. res = {"code": 0, "msg": "banner delete sucess"}
  147. else:
  148. res = {"code": -1, "msg": "banner delete fail"}
  149. except:
  150. traceback.print_exc()
  151. else:
  152. res={"code": -1, "msg": u"参数不完整"}
  153. return JsonResponse(res)
  154. @csrf_exempt
  155. def news_show(request):
  156. """
  157. 最新动态
  158. return data list
  159. """
  160. objs = News.objects.all()
  161. if objs:
  162. data_list=[]
  163. for obj_news in objs:
  164. dict_data={}
  165. dict_data["nid"] = obj_news.id
  166. dict_data["title"] = obj_news.title
  167. dict_data["create_time"] = obj_news.create_time
  168. dict_data["author"] = obj_news.author
  169. data_list.append(dict_data)
  170. res = {"code":0,"msg":"show sucess!","content_data":data_list}
  171. else:
  172. res = {"code":-1,"msg":"no data",}
  173. return JsonResponse(res)
  174. @csrf_exempt
  175. @params
  176. def news_add(request):
  177. """
  178. 新增动态
  179. """
  180. req_params = request.PARAMS
  181. if req_params:
  182. try:
  183. img_url = req_params.get("img_url",None)
  184. title = req_params.get("title",None)
  185. author = req_params.get("author",None)
  186. content = req_params.get("content",None)
  187. obj= News.objects.create(title=title,img_url=img_url,author=author,content=content)
  188. res = res={"code": 0, "msg": "banner add sucess"}
  189. except:
  190. traceback.print_exc()
  191. else:
  192. res={"code": -1, "msg": u"参数不完整"}
  193. return JsonResponse(res)
  194. @csrf_exempt
  195. @params
  196. def news_look(request):
  197. """
  198. 动态查看
  199. """
  200. req_params = request.PARAMS
  201. if req_params:
  202. try:
  203. dict_data = {}
  204. nid = req_params.get("nid")
  205. obj = News.objects.filter(id=nid).first()
  206. if obj:
  207. dict_data["title"] = obj.title
  208. dict_data["create_time"] = obj.create_time
  209. dict_data["author"] = obj.author
  210. dict_data["content"] = obj.content
  211. print("news_look-----",dict_data)
  212. res={"code": 0, "msg": "sucess","look_data":dict_data}
  213. else:
  214. res={"code": -1, "msg": "get data fail"}
  215. except:
  216. traceback.print_exc()
  217. else:
  218. res={"code": -1, "msg": u"参数不完整"}
  219. return JsonResponse(res)
  220. @csrf_exempt
  221. @params
  222. def news_delete(request):
  223. """
  224. 删除横幅
  225. """
  226. req_params = request.PARAMS
  227. if req_params:
  228. try:
  229. bid = req_params.get("nid")
  230. obj= News.objects.filter(id=bid).first()
  231. if obj:
  232. obj.delete()
  233. res = {"code": 0, "msg": "news delete sucess"}
  234. else:
  235. res = {"code": -1, "msg": "news delete fail"}
  236. except:
  237. traceback.print_exc()
  238. else:
  239. res={"code": -1, "msg": u"参数不完整"}
  240. return JsonResponse(res)