def upload_file(): headline = request.form.get('headline') content = request.form.get('content') content = content.replace('\n', '<br/>') upfile = request.files.get('upfile') if len(headline) > 20: return 'headline-error' if len(content) > 150: return 'content-error' suffix = upfile.filename.split('.')[-1] if suffix.lower() not in [ 'jpg', 'jpeg', 'png', 'rar', 'zip', 'xls', 'txt', 'gif', 'doc', 'docx' ]: return 'invalid' name = str(int(time.time())) + '.' + suffix upfile.save( 'D:/sujun/Documents/WeChat Files/wxid_t5xuzy8q1b8i22/FileStorage/File/2020-06/webb/upload/' + name) file1 = File() file1.insert_file(filename=name, headline=headline, content=content) return 'upload-success'
def chide_file(): fileid = request.form.get('fileid') file = File() try: file.cancel_delete_file(fileid) return 'chide-success' except: return 'chide-fail'
def file_manage(): file = File() result = file.find_all_file(0, 10) total = math.ceil(file.get_total_count() / 10) return render_template('admin-file.html', result=result, page=1, total=total, type='no')
def api_delete(): fid = request.form.get('fid') file_data = get_file_data(fid) os.remove(file_data['url']) File.deleteById(file_data['id']) return jsonify({"code": 0, "msg": "删除成功"})
def file_cl(page): start = (page - 1) * 10 file = File() result = file.find_all_file(start, 10) total = math.ceil(file.get_total_count() / 10) return render_template('admin-file.html', result=result, page=page, total=total, type='no')
def deletecourse(id): course = Course(unique_id=id) file = File.queryByCid(id) print("File:", file) if (len(file) > 0): file_path = file[0][2] file_path = file_path.rsplit('/', 1)[0] print("file_path:", file_path) shutil.rmtree(file_path) File.deleteallBycourseid(id) course.delete() return jsonify({"dd": "dd"})
def api_upload(): if ("user_id" not in session): return "请访问/login进行登录。" if (request.form.get('flag') == 1): return jsonify({'code': 1, 'msg': '上传失败'}) #获取教师的id,通过user_id teacher_id = Teacher().queryidByuser(session["user_id"])[0] # 获取教师和课程信息 cid = request.form.get("cid") #通过cid获得课程名称 course = Course().queryByid(cid) cdir = cid file_dir = os.path.join(basedir, cdir) # 拼接成合法文件夹地址 file_dir = file_dir.replace('\\', '/') if not os.path.exists(file_dir): os.makedirs(file_dir) # 文件夹不存在就创建 f = request.files['myfile'] # 从表单的file字段获取文件,myfile为该表单的name值 wno = request.form.get('wno') if f and allowed_file(f.filename): # 判断是否是允许上传的文件类型 fname = f.filename print("fname:", fname) uptime = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) print("uptime:", uptime) uid = str(uuid.uuid1()) print("uid:", uid) file_path = file_dir + '/' + uptime + fname print("file_path:", file_path) spare1 = "" spare2 = "" spare3 = "" spare4 = "" # 插入数据库 newfile = File(uid, fname, file_path, cid, uptime, wno, spare2, spare3, spare4) print("newfile:", newfile) newfile.insert() f.save(os.path.join(file_path)) # 保存文件到upload目录 return jsonify({"code": 0, "msg": "上传成功"})
def get_file_data(fileid): result = File.queryById(fileid) fdata = {"id": "", "name": "", "url": "", "cid": "", "time": ""} for key, value in zip(fdata, result): fdata[key] = value return fdata
def api_show(): cid = request.form.get("cid") print("cid:", cid) print("request_data:", request.data) # 按课程id查询文件 result = File.queryByCid(cid) print("result:", result) # json数据 jdata = {"code": 0, "msg": "", "count": len(result), "data": []} # data填充 for row in result: data_row = { "fid": "", "fname": "", "furl": "", "fcid": "", "ftime": "", "wno": "" } for key, value in zip(data_row, row): data_row[key] = str(value) jdata["data"].append(data_row) print("jdata:", jdata) # print(cid) # print(result) # print(jdata) # print(jsonify(jdata)) return jsonify(jdata)
def __init__(self, out_path='output', csv_file_name='movie.csv'): ''' Handle change the csv file to xml file :param out_path: output file dir :param csv_file_name: csv file ''' self.out_path = out_path self.path = os.path.join(File.get_dir_path(), out_path) self.file = '{}/{}'.format(self.path, csv_file_name)
def read_json(self): if not File.verify_file(self.file): print ('Read failed,file {} not exist!'.format(self.file)) try: with open(self.file,'rt') as rf: return json.load(rf) except Exception as e: print ('Read json failed,error:{}'.format(e))
def csv_to_xml(self, out_xml_file='movie.xml'): ''' read the csv file content and convert it to xml file :param out_xml_file: :return: ''' if not File.verify_file(self.file): print('Read failed,file {} not exist!'.format(self.file)) else: with open(self.file, 'rt') as rf: reader = csv.reader(rf) if self.write_xml(reader, out_xml_file): print('csv {} to xml {} ok!'.format( self.file, out_xml_file)) else: print('csv {} to xml {} failed!'.format( self.file, out_xml_file))
def read_csv(self): if not File.verify_file(self.file): print('Read failed,file {} not exist!'.format(self.file)) rows = [] try: with open(self.file, 'r', encoding='utf-8') as csvfile: csv_reader = csv.reader(csvfile) for index, row in enumerate(csv_reader): if index == 0: continue rows.append(row) print('Read CSV file :{} ok!'.format(self.file)) return rows except Exception as e: print('Read CSV file failed.Error:{}'.format(e)) return None
def f_classify(page): file = File() start = (page-1) *10 result = file.find_limit_with_users(start, 10) total = math.ceil(file.get_total_file_count() / 10) return render_template('block2.html', result=result, page=page, total=total, type='share')
def file(): file = File() result = file.find_limit_with_users(0, 10) total = math.ceil(file.get_total_file_count() / 10) return render_template('block2.html', result=result, page=1, total=total, type='share')
def __init__(self,out_path='output'): path=os.path.join(File.get_dir_path(),out_path) self.file='{}/movie.json'.format(path)
def __init__(self, out_path='output', file_name='movie.csv'): path = os.path.join(File.get_dir_path(), out_path) self.file = '{}/{}'.format(path, file_name)