views.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # -*-coding:utf-8 -*-
  2. import json
  3. import time
  4. import traceback
  5. # from django.shortcuts import render
  6. # from django.http import HttpResponse
  7. # from django.core.serializers.json import DjangoJSONEncoder
  8. from django.contrib.auth.hashers import make_password, check_password
  9. from django.views.decorators.csrf import csrf_exempt
  10. from django.core.exceptions import ObjectDoesNotExist
  11. from models import *
  12. from common.common import (get_user_from_request, login_required,
  13. params,JsonResponse)
  14. # @login_required
  15. @csrf_exempt
  16. @params
  17. def user_manage(request):
  18. """
  19. 用户管理
  20. :param request:
  21. :return: userdata list
  22. """
  23. # user = request.session.get("user", None)
  24. data = request.PARAMS
  25. search_name = data.get("search_name", None)
  26. # search_name = "ton"
  27. print("search_name",search_name)
  28. if search_name:
  29. user_data = Users.objects.filter(username__icontains=search_name)
  30. else:
  31. user_data = Users.objects.all()
  32. print("user_manage111",user_data)
  33. data_show =[]
  34. if user_data:
  35. for list_data in user_data:
  36. dict_data = {}
  37. dict_data["uid"] = list_data.id
  38. dict_data["username"] = list_data.username
  39. dict_data["realname"] = list_data.real_name
  40. dict_data["status"] = list_data.status
  41. dict_data["permission"] = list_data.permission
  42. data_show.append(dict_data)
  43. res = {"code": 0, "msg": "get data success", "user_data": data_show}
  44. else:
  45. res = {"code": -1, "msg": "get no data!"}
  46. print(res)
  47. return JsonResponse(res)
  48. # @login_required
  49. @csrf_exempt
  50. @params
  51. def adduser(request):
  52. """
  53. 添加用户
  54. :param request:
  55. :return: True/False
  56. """
  57. req_params = request.PARAMS
  58. if req_params:
  59. try:
  60. username = req_params.get("username")
  61. password = make_password(req_params.get("password"), None,
  62. "pbkdf2_sha256")
  63. status = req_params.get("status")
  64. permission = req_params.get("permission")
  65. real_name = req_params.get("real_name",None)
  66. print("adduser---real_name",real_name)
  67. print(username,password,status,permission)
  68. obj, created = Users.objects.get_or_create(username=username)
  69. if not created:
  70. return JsonResponse({"code": -1, "msg": u"该用户名已被注册"})
  71. else:
  72. # 保存其他信息
  73. obj.username = username
  74. obj.real_name = real_name
  75. obj.password = password
  76. obj.status = int(status)
  77. obj.permission = int(permission)
  78. obj.save()
  79. return JsonResponse({"code": 0, "msg": u"注册成功"})
  80. except:
  81. traceback.print_exc()
  82. else:
  83. return JsonResponse({"code": -1, "msg": u"缺少参数"})
  84. @csrf_exempt
  85. @params
  86. def login(request):
  87. """
  88. 用户登录
  89. :param request: username, password
  90. :return: True / False
  91. """
  92. data = request.PARAMS
  93. print("login****",data)
  94. if not data:
  95. return JsonResponse({"code": -1, "msg": u"参数不完整"})
  96. username = data.get("username", None)
  97. passwd = data.get("password", None)
  98. # 获取user的password
  99. try:
  100. user = Users.objects.get(username=username)
  101. except ObjectDoesNotExist:
  102. print("user not exit********")
  103. return JsonResponse({"code": -1, "msg": u"该用户不存在"})
  104. # 检查密码是否正确
  105. check_res = check_password(passwd, user.password)
  106. print("***********")
  107. print(username)
  108. print(user.username)
  109. print(check_res)
  110. if check_res:
  111. user_info = {"uid":user.id, "username": user.username,"realname": user.real_name,
  112. "status": user.status, "permission": user.permission,}
  113. print("user_info",user_info)
  114. request.session["user"] = user_info
  115. res = {"code": 0, "msg": "success", "user": user_info}
  116. else:
  117. res = {"code": -1, "msg": "password wrong!"}
  118. print "set session: --> ", request.session.get("user", None)
  119. return JsonResponse(res)
  120. # @login_required
  121. @params
  122. def logout(request):
  123. """
  124. 退出
  125. :param request:
  126. :return:
  127. """
  128. try:
  129. del request.session["user"]
  130. except KeyError as ex:
  131. pass
  132. return JsonResponse({"code": 0, "msg": "success"})
  133. @csrf_exempt
  134. @login_required
  135. @params
  136. def modified_password(request):
  137. """
  138. 修改密码
  139. :param request: old_password, new_password
  140. :return: True / False
  141. """
  142. user = get_user_from_request(request)
  143. print("uuuuuuu",user)
  144. req_params = request.PARAMS
  145. old_password = req_params.get("old_password")
  146. new_password = req_params.get("new_password")
  147. uid = user["uid"]
  148. print("*****uid",uid)
  149. user_obj = Users.objects.filter(id=uid).first()
  150. print("-----",user_obj)
  151. if user_obj:
  152. check_old = check_password(old_password, user_obj.password)
  153. print("mmmmmmm",check_old)
  154. if check_old:
  155. new_pass = make_password(new_password, None, "pbkdf2_sha256")
  156. user_obj.password = new_pass
  157. user_obj.save()
  158. res = {"code": 0, "msg": "success"}
  159. else:
  160. res = {"code": -1, "msg": "旧密码不正确"}
  161. else:
  162. res = {"code": 2, "msg": "没有登陆"}
  163. print(res)
  164. return JsonResponse(res)
  165. @csrf_exempt
  166. # @login_required
  167. @params
  168. def update_user(request):
  169. """
  170. 编辑用户信息
  171. :param request: username
  172. :return: True / False
  173. """
  174. req_params = request.PARAMS
  175. if req_params:
  176. try:
  177. # username = req_params.get("username")
  178. uid = req_params.get("uid")
  179. status = req_params.get("status")
  180. permission = req_params.get("permission")
  181. print "status --> ", status
  182. print "permission --> ", permission
  183. user_obj = Users.objects.filter(id=uid).first()
  184. print("update-----",user_obj)
  185. if user_obj:
  186. user_obj.status = status
  187. user_obj.permission = permission
  188. user_obj.save()
  189. res = {"code":0, "msg":"updte success"}
  190. else:
  191. res = {"code":-1, "msg":"update fail"}
  192. except:
  193. traceback.print_exc()
  194. else:
  195. res = {"code":-1, "msg":"参数不完整"}
  196. print(res)
  197. return JsonResponse(res)
  198. @csrf_exempt
  199. # @login_required
  200. @params
  201. def delete_user(request):
  202. """
  203. 删除指定用户
  204. :param request: username
  205. :return: True / False
  206. """
  207. req_params = request.PARAMS
  208. uid = req_params.get("uid", None)
  209. user_obj = Users.objects.filter(id=uid).first()
  210. print(user_obj)
  211. if user_obj:
  212. user_obj.delete()
  213. res = {"code":0, "msg":"删除成功"}
  214. else:
  215. res = {"code":-1, "msg":"删除失败"}
  216. print(res)
  217. return JsonResponse(res)
  218. @csrf_exempt
  219. # @login_required
  220. @params
  221. def reader_fb(request):
  222. """
  223. 读者提问
  224. """
  225. data = request.PARAMS
  226. search_question = data.get("search_question", None)
  227. print("00000",search_question)
  228. if search_question:
  229. user_data = Reader.objects.filter(question__icontains = search_question)
  230. else:
  231. user_data = Reader.objects.all()
  232. data =[]
  233. if user_data:
  234. for list_data in user_data:
  235. dict_data = {}
  236. dict_data["rid"] = list_data.id
  237. dict_data["readername"] = list_data.readername
  238. dict_data["create_time"] = list_data.create_time
  239. dict_data["question"] = list_data.question
  240. data.append(dict_data)
  241. res = {"code": 0, "msg": "get data success", "reader_data": data}
  242. else:
  243. res = {"code": -1, "msg": "get empty data!"}
  244. print(res)
  245. return JsonResponse(res)
  246. @csrf_exempt
  247. @login_required
  248. @params
  249. def fb_question(request):
  250. """
  251. 编辑回复信息
  252. :param request: rid ,content
  253. :return: True / False
  254. """
  255. user_id = -1
  256. user = get_user_from_request(request)
  257. if user:
  258. user_id = user["uid"]
  259. else:
  260. return JsonResponse({"code":-1, "msg":"没登陆"})
  261. req_params = request.PARAMS
  262. rid = req_params.get("rid")
  263. fb_content = req_params.get("fb_content", None)
  264. reader_obj = Reader.objects.filter(id=rid).first()
  265. print("update-----",reader_obj)
  266. if reader_obj:
  267. reader_obj.fb_content = fb_content
  268. reader_obj.fb_id = user_id
  269. reader_obj.save()
  270. res = {"code":0, "msg":"success", "fb_content":fb_content}
  271. else:
  272. res = {"code":-1, "msg":"回复失败"}
  273. print(res)
  274. return JsonResponse(res)