BookSaw图书商城
立即下载
资源介绍:
Web全栈技术开发
#-*- coding:utf-8 -*-
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from datetime import *
import os, codecs
import json
import random
import re
config_path = os.path.join(os.path.dirname(__file__), "ueconfig.json")
base_dir = settings.BASE_DIR #项目的根目录
#本地上传图片时构造json返回值
class JsonResult(object):
def __init__(self,state="未知错误",url="",title="",original="",error="null"):
super(JsonResult,self).__init__()
self.state = state
self.url = url
self.title = title
self.original = original
self.error = error
#构造返回json
def buildJsonResult(result):
jsondata = {"state":result.state,"url":result.url,"title":result.title,"original":result.original,"error":result.error}
return json.dumps(jsondata)
def buildFileName(pathformat, filename):
"""
PathFormat处理
"""
dt = datetime.now()
name,ext = os.path.splitext(filename)
#创建替换字典
keys = ['{filename}', '{time}', '{yyyy}', '{yy}', '{mm}', '{dd}', '{hh}', '{ii}', '{ss}', ]
values = [name, '%H%M%S', '%Y', '%y', '%m', '%d', '%H', '%M', '%S', ]
texts = dict(zip(keys, values))
#遍历对应替换
format_text = pathformat
"""
File "/home/yc/py6/myproject-test/ueditor/controller.py", line 45, in buildFileName
for key, value in texts.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'
"""
# for key, value in texts.iteritems():
for key, value in texts.items():
format_text = format_text.replace(key, value)
#处理随机数
regstr = r'{rand:(\d+?)}'
ms = re.search(regstr, format_text)
group = ms.group()
if group:
rand_length = int(ms.groups()[0]) #获取随机数字的长度
rand_number = random.randint(1, 10**rand_length -1) #生成随机数
rand_number = str(rand_number).zfill(rand_length) #不足位数补0
format_text = format_text.replace(group, rand_number)
return dt.strftime(format_text) + ext
#读取json文件
def getConfigContent():
# name 'file' is not defined
# jsonfile = file(config_path)
jsonfile = open(config_path)
content = json.load(jsonfile)
return content
#上传配置类
class UploadConfig(object):
def __init__(self,PathFormat,UploadFieldName,SizeLimit,AllowExtensions,SavePath,Base64,Base64Filename):
super(UploadConfig,self).__init__()
self.PathFormat = PathFormat
self.UploadFieldName = UploadFieldName
self.SizeLimit = SizeLimit
self.AllowExtensions = AllowExtensions
self.SavePath = SavePath
self.Base64 = Base64
self.Base64Filename = Base64Filename
#获取json配置中的某属性值
def GetConfigValue(key):
config = getConfigContent()
return config[key]
#检查文件扩展名是否在允许的扩展名内
def CheckFileType(filename,AllowExtensions):
exts = list(AllowExtensions)
name,ext = os.path.splitext(filename)
return ext in exts
def CheckFileSize(filesize,SizeLimit):
return filesize=size:
break
jsondata = {"state":"SUCCESS","list":filelist,"start":start,"size":size,"total":index}
return HttpResponse(json.dumps(jsondata))
#返回配置信息
def configHandler(request):
content = getConfigContent()
callback = request.GET.get("callback")
if callback:
return HttpResponse("{
资源文件列表:
booksaw.zip 大约有1021个文件