FAQController.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import traceback
  5. from django.utils.decorators import method_decorator
  6. from django.views.decorators.csrf import csrf_exempt
  7. from django.views.generic.base import View
  8. from Ansjer.config import BASE_DIR
  9. from Object.ResponseObject import ResponseObject
  10. from Object.TokenObject import TokenObject
  11. class FAQUploadView(View):
  12. @method_decorator(csrf_exempt)
  13. def dispatch(self, request, *args, **kwargs):
  14. return super(FAQUploadView, self).dispatch(request, *args, **kwargs)
  15. def get(self, request, *args, **kwargs):
  16. request.encoding = 'utf-8'
  17. request_dict = request.GET
  18. fileName = request.FILES.get('fileName', None)
  19. return self.validate(fileName, request_dict)
  20. def post(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. request_dict = request.POST
  23. fileName = request.FILES.get('fileName', None)
  24. return self.validate(fileName, request_dict)
  25. def validate(self, fileName, request_dict):
  26. token = TokenObject(request_dict.get('token', None))
  27. response = ResponseObject()
  28. if token.code != 0:
  29. return response.json(token.code)
  30. try:
  31. path = '/'.join((BASE_DIR, 'static/FAQImages')).replace('\\', '/') + '/'
  32. if not os.path.exists(path):
  33. os.makedirs(path)
  34. file_name = path + str(fileName)
  35. if os.path.exists(file_name):
  36. os.remove(file_name)
  37. destination = open(file_name, 'wb+')
  38. for chunk in fileName.chunks():
  39. destination.write(chunk)
  40. destination.close()
  41. else:
  42. file_name = path + str(fileName)
  43. if os.path.exists(file_name):
  44. os.remove(file_name)
  45. destination = open(file_name, 'wb+')
  46. for chunk in fileName.chunks():
  47. destination.write(chunk)
  48. destination.close()
  49. except Exception as e:
  50. errorInfo = traceback.format_exc()
  51. print('上传文件错误: %s' % errorInfo)
  52. return response.json(700, {'details': repr(e)})
  53. else:
  54. index = file_name.find('static/')
  55. filePath = file_name[index:]
  56. return response.json(0, {'filePath': filePath})
  57. class FAQView(View):
  58. @method_decorator(csrf_exempt)
  59. def dispatch(self, *args, **kwargs):
  60. return super(FAQView, self).dispatch(*args, **kwargs)
  61. def post(self, request, *args, **kwargs):
  62. request.encoding = 'utf-8'
  63. request_dict = request.POST
  64. operation = kwargs.get('operation', None)
  65. return self.validate(request_dict, operation)
  66. def get(self, request, *args, **kwargs):
  67. request.encoding = 'utf-8'
  68. request_dict = request.GET
  69. operation = kwargs.get('operation', None)
  70. return self.validate(request_dict, operation)
  71. def validate(self, request_dict, operation):
  72. return