|
@@ -1,15 +1,26 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+import json
|
|
|
import os
|
|
|
+import shutil
|
|
|
+import time
|
|
|
import traceback
|
|
|
|
|
|
+from django.http import HttpResponse
|
|
|
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
|
|
|
+
|
|
|
+from Service import ModelService
|
|
|
|
|
|
|
|
|
class FAQUploadView(View):
|
|
@@ -30,32 +41,61 @@ class FAQUploadView(View):
|
|
|
return self.validate(fileName, request_dict)
|
|
|
|
|
|
def validate(self, fileName, request_dict):
|
|
|
+
|
|
|
token = TokenObject(request_dict.get('token', None))
|
|
|
|
|
|
response = ResponseObject()
|
|
|
if token.code != 0:
|
|
|
return response.json(token.code)
|
|
|
|
|
|
+ # own_permission = ModelService.check_perm(userID=token.userID, permID=120)
|
|
|
+ # if own_permission is not True:
|
|
|
+ # 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)
|
|
@@ -63,11 +103,46 @@ 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
|
|
|
return response.json(0, {'filePath': filePath})
|
|
|
|
|
|
|
|
|
-class FAQView(View):
|
|
|
+class getFAQImage(View):
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ filePath = kwargs.get('filePath', None)
|
|
|
+ filePath.encode(encoding='utf-8', errors='strict')
|
|
|
+ response = ResponseObject()
|
|
|
+ return self.getFile(filePath, response)
|
|
|
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'gb2312'
|
|
|
+ filePath = kwargs.get('filePath', None)
|
|
|
+ response = ResponseObject()
|
|
|
+ filePath.encode(encoding='gb2312', errors='strict')
|
|
|
+ return self.getFile(filePath, response)
|
|
|
+
|
|
|
+ def getFile(self, filePath, response):
|
|
|
+ if filePath:
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ return response.json(800)
|
|
|
+ fullPath = os.path.join(BASE_DIR, filePath).replace('\\', '/')
|
|
|
+
|
|
|
+ var_dump(fullPath)
|
|
|
+ if os.path.isfile(fullPath):
|
|
|
+ try:
|
|
|
+ Imagedata = open(fullPath, 'rb').read()
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(906, repr(e))
|
|
|
+ else:
|
|
|
+ return HttpResponse(Imagedata, content_type="image/jpeg")
|
|
|
+ else:
|
|
|
+ return response.json(907)
|
|
|
+
|
|
|
+
|
|
|
+class FAQView(View):
|
|
|
@method_decorator(csrf_exempt)
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
return super(FAQView, self).dispatch(*args, **kwargs)
|
|
@@ -85,4 +160,159 @@ class FAQView(View):
|
|
|
return self.validate(request_dict, operation)
|
|
|
|
|
|
def validate(self, request_dict, operation):
|
|
|
- return
|
|
|
+ token = TokenObject(request_dict.get('token', None))
|
|
|
+ response = ResponseObject()
|
|
|
+ if token.code != 0:
|
|
|
+ return response.json(token.code)
|
|
|
+
|
|
|
+ if operation == 'add':
|
|
|
+ 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, request_dict, response)
|
|
|
+ elif operation == 'delete':
|
|
|
+ return self.do_delete(token.userID, request_dict, response)
|
|
|
+ else:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ def do_add(self, token, request_dict, response):
|
|
|
+
|
|
|
+ own_permission = ModelService.check_perm(userID=token.userID, permID=120)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ title = request_dict.get('title', None)
|
|
|
+ content = request_dict.get('content', None)
|
|
|
+
|
|
|
+ if title and 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)
|
|
|
+ now_time = int(time.time())
|
|
|
+ FAQModel.objects.create(**{
|
|
|
+ 'title': title,
|
|
|
+ 'content': content,
|
|
|
+ 'add_time': now_time,
|
|
|
+ 'update_time': now_time
|
|
|
+
|
|
|
+ })
|
|
|
+
|
|
|
+ # 删除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)
|
|
|
+
|
|
|
+ def do_query(self, userID, request_dict, response):
|
|
|
+
|
|
|
+ page = request_dict.get('page', None)
|
|
|
+ line = request_dict.get('line', None)
|
|
|
+ search_key = request_dict.get('search_key', None)
|
|
|
+
|
|
|
+ if page and line:
|
|
|
+ if search_key:
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=110)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+ faq_qs = FAQModel.objects.filter(title__contains=search_key)
|
|
|
+ else:
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=100)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ faq_qs = FAQModel.objects.filter()
|
|
|
+
|
|
|
+ if not faq_qs.exists():
|
|
|
+ return response.json(0, [])
|
|
|
+
|
|
|
+ page = int(page)
|
|
|
+ line = int(line)
|
|
|
+ start = (page - 1) * line
|
|
|
+ end = start + line
|
|
|
+ faq_qs = faq_qs.values()[start:end]
|
|
|
+ return response.json(0, list(faq_qs))
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ def do_update(self, token, request_dict, response):
|
|
|
+ own_permission = ModelService.check_perm(userID=token.userID, permID=130)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ id = request_dict.get('id', None)
|
|
|
+ title = request_dict.get('title', None)
|
|
|
+ content = request_dict.get('content', None)
|
|
|
+
|
|
|
+ if id:
|
|
|
+ now_time = int(time.time())
|
|
|
+ data = {
|
|
|
+ 'update_time': now_time
|
|
|
+ }
|
|
|
+
|
|
|
+ if title:
|
|
|
+ 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)
|
|
|
+
|
|
|
+ def do_delete(self, userID, request_dict, response):
|
|
|
+
|
|
|
+ own_permission = ModelService.check_perm(userID=userID, permID=140)
|
|
|
+ if own_permission is not True:
|
|
|
+ return response.json(404)
|
|
|
+
|
|
|
+ id = request_dict.get('id', None)
|
|
|
+ if id:
|
|
|
+ try:
|
|
|
+ faq_qs = FAQModel.objects.filter(id=id)
|
|
|
+ faq_qs.delete()
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(173)
|
|
|
+ else:
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(444)
|