def save(request): user = request.user sdicts = {} sdicts["result"] = 0 product_name = request.POST.get('product_name', '').strip() ftp_info = request.POST.get('ftp_info', '').strip() ftp_domain = request.POST.get('ftp_domain', '').strip() file_path = request.POST.get('file_path', '').strip() if ftp_info: try: ftp_info_list = ftp_info.split(',') ftp_host = ftp_info_list[0].strip() ftp_port = ftp_info_list[1].strip() ftp_username = ftp_info_list[2].strip() ftp_password = ftp_info_list[3].strip() except: sdicts["msg"] = u'资源ftp站点信息有误!' return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) else: ftp_host = FTP_HOST ftp_port = FTP_PORT ftp_username = FTP_USERNAME ftp_password = FTP_PASSWORD if not ftp_domain: ftp_domain = FTP_DOMAIN if not file_path: product_pinyin_list = lazy_pinyin(product_name, errors='ignore') file_path = ''.join(product_pinyin_list) ftp_domain = ftp_domain.replace('\\', '/').strip('/') file_path = file_path.replace('\\', '/').strip('/') #验证路径是否存在 product = _product.get_product_by_params(name=product_name) if product: product_list = _product.get_products_by_params().exclude(id=product.id) else: product_list = _product.get_products_by_params() for product_exist in product_list: if file_path == product_exist.file_path: sdicts["msg"] = u'业务路径已存在' return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) product = _product.get_or_create_product_by_params(name=product_name)[0] product.ftp_host = ftp_host product.ftp_port = ftp_port product.ftp_username = base64.b64encode(ftp_username) product.ftp_password = base64.b64encode(ftp_password) product.ftp_domain = ftp_domain product.file_path = file_path product.create_user = user product.update_user = user product.save() sdicts["result"] = 1 sdicts["msg"] = u'保存业务信息成功' return HttpResponse(json.dumps(sdicts, ensure_ascii=False))
def ajax_get_group_option(request): if request.method == "POST": product_id = int(request.POST.get('product_id')) res = [] product = _product.get_product_by_params(id=product_id) groups = _product.get_productInfos_by_params(product=product) for group in groups: obj = [group.id, group.group_name] res.append(obj) result = json.dumps(res) return HttpResponse(result)
def ajax_get_upload_info(request): sdicts = {} sdicts['result'] = 0 if request.method == "POST": product_id = request.POST.get("product_id", 0) info_id = request.POST.get('info_id', 0) fsize = int(request.POST.get('size', 0)) file_name = request.POST.get('name') info = _product.get_ftpUploadLog_by_params(id=info_id) if product_id and info: if info.log: if 'success' in info.log: file_url = os.path.join(info.ftp_domain, info.ftp_path, info.file_name) file_url = file_url.replace('\\', '/') sdicts['file_url'] = file_url sdicts['percent'] = 100 sdicts['result'] = 1 else: product = _product.get_product_by_params(id=product_id) username = base64.b64decode(product.ftp_username) password = base64.b64decode(product.ftp_password) remotepath = product.file_path host = product.ftp_host port = int(product.ftp_port) ftp = FTP() ftp.set_debuglevel(2) #打开调试级别2,显示详细信息;0为关闭调试信息 try: ftp.connect(host, port) ftp.login(username, password) remotepath_list = remotepath.split('/') for remotedir in remotepath_list: ftp.cwd(remotedir) rsize = ftp.size(file_name) sdicts['percent'] = '%.f' % (rsize * 1.0 / fsize * 100) except Exception, e: sdicts['percent'] = 0 print e ftp.set_debuglevel(0) ftp.quit() sdicts['result'] = 1
def file_upload(request): user = request.user result_dict = {'result': 0} sfile = request.FILES.get("Filedata", None) product_id = request.POST.get("product_id", 0) if sfile and product_id: ftpUploadLogs = _product.get_ftpUploadLogs_by_params().order_by('-id') if ftpUploadLogs: info_num = ftpUploadLogs[0].id else: info_num = 0 product = _product.get_product_by_params(id=product_id) file_name = sfile.name result_dict = web_upload(sfile, file_name) ftp_domain = product.ftp_domain remotepath = product.file_path host = product.ftp_host port = int(product.ftp_port) ftpUploadLog = _product.create_ftpUploadLog_by_params(user=user, product=product) ftpUploadLog.ftp_host = host ftpUploadLog.ftp_port = port ftpUploadLog.ftp_domain = ftp_domain ftpUploadLog.ftp_path = remotepath ftpUploadLog.file_name = file_name ftpUploadLog.save() thread = threading.Thread(target=ftp_upload, args=(user, product, file_name, ftpUploadLog)) # thread_name = 'Thread-history-%s'%history.id # thread.setName(thread_name) thread.start() result_dict['info_id'] = info_num + 1 # result_dict =ftp_upload(user,product,sfile,file_name) return HttpResponse(json.dumps(result_dict), content_type="application/json")
def ajax_get_productinfo(request): sdicts = {} sdicts['result'] = 0 if request.method == "POST": product_name = request.POST.get('product_name', 0) product_pinyin_list = lazy_pinyin(product_name, errors='ignore') file_path_default = ''.join(product_pinyin_list) sdicts['file_path_default'] = file_path_default product = _product.get_product_by_params(name=product_name) if product: sdicts['ftp_host'] = product.ftp_host sdicts['ftp_port'] = product.ftp_port sdicts['ftp_username'] = base64.b64decode(product.ftp_username) sdicts['ftp_password'] = base64.b64decode(product.ftp_password) sdicts['ftp_domain'] = product.ftp_domain sdicts['file_path'] = product.file_path sdicts['result'] = 1 return HttpResponse(json.dumps(sdicts))
def ajax_get_productinfo(request): sdicts = {} sdicts['result'] = 0 if request.method == "POST": product_id = request.POST.get('product_id', 0) res = [] product = _product.get_product_by_params(id=product_id) if product: groups = _product.get_productInfos_by_params(product=product) for group in groups: obj = [group.id, group.group_name, group.group_path] res.append(obj) sdicts['ftp_host'] = product.ftp_host sdicts['ftp_port'] = product.ftp_port sdicts['ftp_username'] = base64.b64decode(product.ftp_username) sdicts['ftp_password'] = base64.b64decode(product.ftp_password) sdicts['file_name'] = product.file_name sdicts['group_list'] = res sdicts['result'] = 1 return HttpResponse(json.dumps(sdicts))
def edit(request): user = request.user sdicts = {} sdicts["result"] = 0 product_id = request.POST.get('product_id', 0) ftp_info = request.POST.get('ftp_info') file_name = request.POST.get('file_name', '').strip() group_name = request.POST.get('group_name') group_path = request.POST.get('group_path') group_id = request.POST.get('group_id') try: ftp_info_list = ftp_info.split(',') ftp_host = ftp_info_list[0].strip() ftp_port = ftp_info_list[1].strip() ftp_username = ftp_info_list[2].strip() ftp_password = ftp_info_list[3].strip() except: sdicts["msg"] = u'资源ftp站点信息有误!' return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) product = _product.get_product_by_params(id=product_id) if not product: sdicts["msg"] = u'业务不存在!' return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) product.file_name = file_name product.ftp_host = ftp_host product.ftp_port = ftp_port product.ftp_username = base64.b64encode(ftp_username) product.ftp_password = base64.b64encode(ftp_password) # product.create_user = user product.update_user = user product.save() group_name_list = group_name.split(',') group_path_list = group_path.split(',') group_id_list = [] if group_id: group_id_list = group_id.split(',') for index, gname in enumerate(group_name_list): gname = gname.strip() gpath = group_path_list[index] gpath = gpath.strip() if index < len(group_id_list): group_id = group_id_list[index] productinfo = _product.get_productInfo_by_params(id=group_id) productinfo.group_name = gname productinfo.group_path = gpath productinfo.update_user = user productinfo.save() else: productinfo = _product.create_productInfo_by_params( product=product) productinfo.group_name = gname productinfo.group_path = gpath productinfo.create_user = user productinfo.update_user = user productinfo.save() sdicts["result"] = 1 sdicts["msg"] = u'修改业务信息成功' return HttpResponse(json.dumps(sdicts, ensure_ascii=False))
def ajax_file_upload(request): user = request.user sdicts = {} sdicts["result"] = 0 now = datetime.datetime.now() if request.method == "POST": fileUploadLog = _product.create_fileUploadLog_by_params(user=user) try: f = request.FILES.get("file_road", None) product_id = request.POST.get("product_id") group_id = request.POST.get("group_id") #存储文件 if f: fileUploadLog.file_name = f.name product = _product.get_product_by_params(id=product_id) if f.name != product.file_name: sdicts["msg"] = u'请上传正确的公告文件!' fileUploadLog.log = u'file name incorrect' fileUploadLog.end_time = datetime.datetime.now() fileUploadLog.save() return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) if f.size > 1024 * 1024: sdicts["msg"] = u'上传文件不允许超过1M!' fileUploadLog.log = u'file size greater than 1M' fileUploadLog.end_time = datetime.datetime.now() fileUploadLog.save() return HttpResponse(json.dumps(sdicts, ensure_ascii=False)) fpath = settings.REL_MEDIA_ROOT fpath = os.path.join(fpath, '%s' % product_id) if not os.path.exists(fpath): os.makedirs(fpath) fpath = os.path.join(fpath, '%s' % group_id) if not os.path.exists(fpath): os.makedirs(fpath) fpath = os.path.join(fpath, '%s' % user.username) if not os.path.exists(fpath): os.makedirs(fpath) fpath = os.path.join(fpath, '%s' % now.strftime("%Y%m%d%H%M%S")) if not os.path.exists(fpath): os.makedirs(fpath) filepath = os.path.join(fpath, f.name) fileUploadLog.file_path = filepath of = open(filepath, 'wb+') for chunk in f.chunks(): of.write(chunk) of.close() hash_value = calc_md5(filepath) fileUploadLog.log = 'file upload success' fileUploadLog.end_time = datetime.datetime.now() fileUploadLog.save() sdicts['result'] = 1 sdicts["msg"] = u'文件上传成功,请点"发布公告"!' sdicts["filepath"] = filepath sdicts["filename"] = f.name sdicts["hash_value"] = hash_value except Exception, e: print e sdicts["msg"] = u'上传失败!' fileUploadLog.log = e fileUploadLog.end_time = datetime.datetime.now() fileUploadLog.save()