|
@@ -1,6 +1,9 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+import json
|
|
|
import os
|
|
|
+import shutil
|
|
|
+import time
|
|
|
import traceback
|
|
|
|
|
|
from django.http import HttpResponse
|
|
@@ -8,8 +11,11 @@ from django.utils.decorators import method_decorator
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
from django.views.generic.base import View
|
|
|
|
|
|
-from Ansjer.config import BASE_DIR
|
|
|
+from Ansjer.config import BASE_DIR, SERVER_TYPE
|
|
|
+from Ansjer.config_formal import SERVER_DOMAIN
|
|
|
+from Ansjer.config_test import SERVER_DOMAIN
|
|
|
from Model.models import FAQModel
|
|
|
+from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from var_dump import var_dump
|
|
@@ -45,25 +51,49 @@ class FAQUploadView(View):
|
|
|
# return response.json(404)
|
|
|
|
|
|
try:
|
|
|
- path = '/'.join((BASE_DIR, 'static/FAQImages')).replace('\\', '/') + '/'
|
|
|
+ redisObject = RedisObject()
|
|
|
+ path = '/'.join((BASE_DIR, 'static/FAQImages/tmp')).replace('\\', '/') + '/'
|
|
|
+ # path = '/'.join((BASE_DIR, 'static/{user}/FAQImages'.format(user=token.userID))).replace('\\', '/') + '/'
|
|
|
if not os.path.exists(path):
|
|
|
os.makedirs(path)
|
|
|
- file_name = path + str(fileName)
|
|
|
- if os.path.exists(file_name):
|
|
|
- os.remove(file_name)
|
|
|
- destination = open(file_name, 'wb+')
|
|
|
- for chunk in fileName.chunks():
|
|
|
- destination.write(chunk)
|
|
|
- destination.close()
|
|
|
- else:
|
|
|
- file_name = path + str(fileName)
|
|
|
- if os.path.exists(file_name):
|
|
|
- os.remove(file_name)
|
|
|
-
|
|
|
- destination = open(file_name, 'wb+')
|
|
|
- for chunk in fileName.chunks():
|
|
|
- destination.write(chunk)
|
|
|
- destination.close()
|
|
|
+
|
|
|
+ # 先从redis中取出token对应的图片信息
|
|
|
+ images = redisObject.get_data(key=token.token)
|
|
|
+ if images is not False:
|
|
|
+ images = json.loads(images)
|
|
|
+ # 判断此次编辑是否已经存在对应的图片
|
|
|
+ if images.__contains__(str(fileName)):
|
|
|
+ file_name = images[str(fileName)]
|
|
|
+ index = file_name.find('static/')
|
|
|
+ filePath = file_name[index:]
|
|
|
+ filePath = SERVER_DOMAIN + 'faq/image/' + filePath
|
|
|
+ # filePath = "http://192.168.136.35:8000/" + 'faq/image/' + filePath
|
|
|
+ return response.json(0, {'filePath': filePath})
|
|
|
+
|
|
|
+ # redis中没有对应的图片信息
|
|
|
+ tmp_name = str(fileName)
|
|
|
+ suffix = tmp_name[tmp_name.find('.'):]
|
|
|
+ tmp_file_name = int(time.time())
|
|
|
+ tmp_file_name = '{file_name}{suffix}'.format(file_name=tmp_file_name, suffix=suffix)
|
|
|
+
|
|
|
+ file_name = path + str(tmp_file_name)
|
|
|
+ if os.path.exists(file_name):
|
|
|
+ os.remove(file_name)
|
|
|
+
|
|
|
+ destination = open(file_name, 'wb+')
|
|
|
+ for chunk in fileName.chunks():
|
|
|
+ destination.write(chunk)
|
|
|
+ destination.close()
|
|
|
+
|
|
|
+ # 把图片信息保存到redis中
|
|
|
+ images = redisObject.get_data(token.token)
|
|
|
+ if images is False:
|
|
|
+ images = json.dumps({})
|
|
|
+
|
|
|
+ images = json.loads(images)
|
|
|
+ images[tmp_name] = file_name
|
|
|
+ redisObject.set_data(key=token.token, val=json.dumps(images), expire=3600)
|
|
|
+
|
|
|
except Exception as e:
|
|
|
errorInfo = traceback.format_exc()
|
|
|
print('上传文件错误: %s' % errorInfo)
|
|
@@ -71,8 +101,8 @@ class FAQUploadView(View):
|
|
|
else:
|
|
|
index = file_name.find('static/')
|
|
|
filePath = file_name[index:]
|
|
|
- # filePath = SERVER_DOMAIN + 'faq/image/' + filePath
|
|
|
- filePath = "http://192.168.136.35:8000/" + 'faq/image/' + filePath
|
|
|
+ filePath = SERVER_DOMAIN + 'faq/image/' + filePath
|
|
|
+ # filePath = "http://192.168.136.35:8000/" + 'faq/image/' + filePath
|
|
|
return response.json(0, {'filePath': filePath})
|
|
|
|
|
|
|
|
@@ -134,17 +164,17 @@ class FAQView(View):
|
|
|
return response.json(token.code)
|
|
|
|
|
|
if operation == 'add':
|
|
|
- return self.do_add(token.userID, request_dict, response)
|
|
|
+ return self.do_add(token, request_dict, response)
|
|
|
elif operation == 'query':
|
|
|
return self.do_query(token.userID, request_dict, response)
|
|
|
elif operation == 'update':
|
|
|
- return self.do_update(token.userID, request_dict, response)
|
|
|
+ return self.do_update(token, request_dict, response)
|
|
|
elif operation == 'delete':
|
|
|
return self.do_delete(token.userID, request_dict, response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
|
- def do_add(self, userID, request_dict, response):
|
|
|
+ def do_add(self, token, request_dict, response):
|
|
|
|
|
|
# own_permission = ModelService.check_perm(userID=userID, permID=120)
|
|
|
# if own_permission is not True:
|
|
@@ -154,10 +184,35 @@ class FAQView(View):
|
|
|
content = request_dict.get('content', None)
|
|
|
|
|
|
if title and content:
|
|
|
- FAQModel.objects.create(**{
|
|
|
- 'title': title,
|
|
|
- 'content': content
|
|
|
- })
|
|
|
+ try:
|
|
|
+ # 对content中的图片路径进行修改
|
|
|
+ content = str(content)
|
|
|
+ content = content.replace('faq/image/static/FAQImages/tmp', 'faq/image/static/FAQImages')
|
|
|
+
|
|
|
+ # 取出redis中保存的此次上传的图片信息
|
|
|
+ redisObject = RedisObject()
|
|
|
+ images = redisObject.get_data(key=token.token)
|
|
|
+ if images is not False:
|
|
|
+ images = json.loads(images)
|
|
|
+
|
|
|
+ # 把图片从临时文件移动到FAQ资源文件夹下
|
|
|
+ for k, v in images.items():
|
|
|
+ start_index1 = v.find('tmp/')
|
|
|
+ start_index2 = start_index1 + 4
|
|
|
+ new_path = v[0:start_index1] + v[start_index2:]
|
|
|
+ shutil.move(v, new_path)
|
|
|
+
|
|
|
+ FAQModel.objects.create(**{
|
|
|
+ 'title': title,
|
|
|
+ 'content': content
|
|
|
+ })
|
|
|
+
|
|
|
+ # 删除redis中token对应的信息
|
|
|
+ redisObject.del_data(key=token.token)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(174)
|
|
|
+
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
return response.json(444)
|
|
@@ -193,7 +248,7 @@ class FAQView(View):
|
|
|
else:
|
|
|
return response.json(444)
|
|
|
|
|
|
- def do_update(self, userID, request_dict, response):
|
|
|
+ def do_update(self, token, request_dict, response):
|
|
|
# own_permission = ModelService.check_perm(userID=userID, permID=130)
|
|
|
# if own_permission is not True:
|
|
|
# return response.json(404)
|
|
@@ -209,9 +264,28 @@ class FAQView(View):
|
|
|
data['title'] = title
|
|
|
|
|
|
if content:
|
|
|
+ content = str(content)
|
|
|
+ content = content.replace('faq/image/static/FAQImages/tmp', 'faq/image/static/FAQImages')
|
|
|
+
|
|
|
+ # 取出redis中保存的此次上传的图片信息
|
|
|
+ redisObject = RedisObject()
|
|
|
+ images = redisObject.get_data(key=token.token)
|
|
|
+ if images is not False:
|
|
|
+ images = json.loads(images)
|
|
|
+
|
|
|
+ # 把图片从临时文件移动到FAQ资源文件夹下
|
|
|
+ for k, v in images.items():
|
|
|
+ start_index1 = v.find('tmp/')
|
|
|
+ start_index2 = start_index1 + 4
|
|
|
+ new_path = v[0:start_index1] + v[start_index2:]
|
|
|
+ shutil.move(v, new_path)
|
|
|
+ # 删除redis中token对应的信息
|
|
|
+ redisObject.del_data(key=token.token)
|
|
|
+
|
|
|
data['content'] = content
|
|
|
|
|
|
FAQModel.objects.filter(id=id).update(**data)
|
|
|
+
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
return response.json(444)
|
|
@@ -225,7 +299,8 @@ class FAQView(View):
|
|
|
id = request_dict.get('id', None)
|
|
|
if id:
|
|
|
try:
|
|
|
- FAQModel.objects.filter(id=id).delete()
|
|
|
+ faq_qs = FAQModel.objects.filter(id=id)
|
|
|
+ faq_qs.delete()
|
|
|
except Exception as e:
|
|
|
print(e)
|
|
|
return response.json(173)
|