def creat_project(request): cur_dir = Directory("./Projects/") creator_id = request.session['user_id'] project_name = request.POST['name'] if (project_name == ''): return render(request, 'login/initial.html', locals()) type = request.POST['type'] new_pro = models.Project.objects.create(project_name=project_name, creator_id=creator_id, type=type) proname = str(creator_id) + '_' + project_name cur_dir.create_folder(proname) return render(request, 'login/initial.html', locals())
def index(request, project_id): # print("come index") user_pro = models.Project.objects.get(project_id=project_id) request.session["cur_type"] = user_pro.type cur_pro_name = user_pro.project_name real_pro_name = str(user_pro.creator_id) + '_' + cur_pro_name request.session['cur_project'] = project_id cur_dir = Directory('./Projects/' + real_pro_name + '/') request.session["cur_base"] = './Projects/' + real_pro_name + '/' # cur_dir.create_folder('test1') all_url = [(0, "root")] files_list = cur_dir.get_files_and_folders() return render(request, 'login/index.html', {'file_names': files_list, "url": all_url})
def __init__(self, compiler, project_name, main_document): try: assert compiler in self.__compiler_list except AssertionError: print('Compiler is not support!') self.compiler = compiler self.project_name = project_name self.main_document = main_document self.__project_path = self.project_name self.__latex_path = self.__project_path + '/' self.__output_path = self.__project_path + '{}.pdf'.format( main_document) self.__directory = Directory(self.__project_path)
def open_cur_pro_fold(request): dir_path = request.POST['url_list'] dir_list = dir_path.split('/') print(dir_list) dir_list.pop(0) dir_list.pop(0) if len(dir_list) != 0: dir_list[-1] = dir_list[-1].split(' ')[-1] print(dir_list) # dir_path = dir_path[7:] cur_base = request.session['cur_base'] list2path = cur_base for i in dir_list: list2path += i + '/' list2path = list2path[:-1] print(list2path) if not isfile(list2path): return_url_list = [(0, 'root')] i = 1 for dl in dir_list: return_url_list.append((i, dl)) i += 1 diro = Directory(list2path + "/") return_ff_list = diro.get_files_and_folders() # return render(request, 'login/index.html', {'is_file': False, 'url': return_url_list, 'file_names': return_ff_list, 'content': None}) # return render(request, 'login/index.html', {'file_names': files_list, "url": all_url}) return HttpResponse( json.dumps({'is_file': False, 'url': return_url_list, 'file_names': return_ff_list, 'content': None})) else: request.session["cur_file"] = list2path with codecs.open(list2path, "r", encoding='utf-8') as f: file = f.read() print(file) request.session["cur_edit_file"] = list2path return HttpResponse(json.dumps({"is_file": True, 'content': file}))
def creat_fold(request): try: if_exist = models.Project.objects.get(creator_id= request.session["user_id"] , project_id= request.session["cur_project"]) cur_url = request.POST["position"] print(cur_url) cur_url = cur_url.strip('root/') file_name = request.POST['name'] if (file_name == ''): return HttpResponse(json.dumps({"is_ok": True})) print(file_name) now_pro = models.Project.objects.get(project_id=request.session["cur_project"]) base_url = request.session['cur_base'] + cur_url + "/" cur_dir = Directory(base_url) print(base_url) cur_dir.create_folder(file_name) print('creat_fold succeed') return HttpResponse(json.dumps({"is_ok": True})) except models.Project.DoesNotExist: if_exist = models.Authorize.objects.get(user_id=request.session["user_id"], project_id=request.session["cur_project"]) if (if_exist.can_write == 0): return HttpResponse(json.dumps({"is_ok": False})) cur_url = request.POST["position"] print(cur_url) cur_url = cur_url.strip('root/') file_name = request.POST['name'] if (file_name == ''): return HttpResponse(json.dumps({"is_ok": True})) print(file_name) now_pro = models.Project.objects.get(project_id=request.session["cur_project"]) base_url = request.session['cur_base'] + cur_url + "/" cur_dir = Directory(base_url) print(base_url) cur_dir.create_folder(file_name) print('creat_fold succeed') return HttpResponse(json.dumps({"is_ok": True}))
def delete(request): # ./Project/11713021_test/ cur_base = request.session['cur_base'] # root/test1/test2/ dir_path = request.POST.get("position") dir_path = dir_path.replace('root/', '') print(dir_path) ff_name = dir_path + request.POST.get("ff_name") post_fix = splitext(ff_name)[-1] no_file_dir = cur_base + dir_path.strip(ff_name) print(no_file_dir) diro = Directory(no_file_dir) if isfile(no_file_dir + ff_name): diro.delete_file(ff_name) else: diro.delete_folder(ff_name) return HttpResponse("sfsdf")
def upload(request): ''' :param request: dir_path example: test/file.zip or test1/test2/file.tex; bit_stream; :return: nothing ''' try: if_exist = models.Project.objects.get(creator_id= request.session["user_id"] , project_id= request.session["cur_project"]) cur_base = request.session['cur_base'] dir_path = request.POST.get("position") dir_path = dir_path.replace('root/', '') print(dir_path) bit_stream = request.FILES.get('upload_file') file_name = dir_path + request.POST.get("file_name") print("upload file name:" + file_name) # file_name = dir_path.split('/')[-1] post_fix = splitext(file_name)[-1] no_file_dir = cur_base + dir_path.strip(file_name) print(no_file_dir) diro = Directory(no_file_dir) # print(post_fix) if post_fix == '.zip': zip_path = './Temp/zips/' with open(zip_path + file_name, 'wb') as zip_file: for chunk in bit_stream.chunks(): zip_file.write(chunk) diro.unzip(file_name, no_file_dir) else: print((no_file_dir + file_name)) with open((no_file_dir + file_name), 'wb') as common_file: for chunk in bit_stream.chunks(): common_file.write(chunk) print('success') return HttpResponse(json.dumps({"is_ok":True})) except: if_exist = models.Authorize.objects.get(user_id= request.session["user_id"] , project_id=request.session["cur_project"]) if(if_exist.can_write == 0): return HttpResponse(json.dumps({"is_ok": False})) cur_base = request.session['cur_base'] dir_path = request.POST.get("position") dir_path = dir_path.replace('root/', '') print(dir_path) bit_stream = request.FILES.get('upload_file') file_name = dir_path + request.POST.get("file_name") print("upload file name:" + file_name) # file_name = dir_path.split('/')[-1] post_fix = splitext(file_name)[-1] no_file_dir = cur_base + dir_path.strip(file_name) print(no_file_dir) diro = Directory(no_file_dir) # print(post_fix) if post_fix == '.zip': zip_path = './Temp/zips/' with open(zip_path + file_name, 'wb') as zip_file: for chunk in bit_stream.chunks(): zip_file.write(chunk) diro.unzip(file_name, no_file_dir) else: print((no_file_dir + file_name)) with open((no_file_dir + file_name), 'wb') as common_file: for chunk in bit_stream.chunks(): common_file.write(chunk) print('success') return HttpResponse(json.dumps({"is_ok": True}))
class LatexPdf: compiler = 'xelatex' # default compiler project_name = 'test1' # TODO 处理空格 main_document = 'test.tex' __compiler_list = ['xelatex', 'lualatex', 'pdflatex', 'latex'] __project_path = './' + project_name + '/' __latex_path = __project_path + main_document __output_path = '' __compile_state = True __logs = '' def __init__(self, compiler, project_name, main_document): try: assert compiler in self.__compiler_list except AssertionError: print('Compiler is not support!') self.compiler = compiler self.project_name = project_name self.main_document = main_document self.__project_path = self.project_name self.__latex_path = self.__project_path + '/' self.__output_path = self.__project_path + '{}.pdf'.format( main_document) self.__directory = Directory(self.__project_path) # self.__compile_state = True # self.__logs = '' # self.__log_path = './'+project_name+'/'+'{}.log'.format(project_name) def get_tex_files_list(self): return self.__directory.get_tex_files_list() def get_files_and_folders(self): return self.__directory.get_files_and_folders() def build_pdf(self): builder = latex.build.LatexMkBuilder(pdflatex=self.compiler) try: pdf = builder.build_pdf(self.__latex_path, self.main_document) pdf.save_to(self.__output_path) except LatexBuildError as e: print('compile failed') self.__compile_state = False for err in e.get_errors(): err_str = '' location = 'Error in {0[filename]}, line {0[line]}: {0[error]}\r\n'.format( err) content = ' {}\r\n'.format(err['context'][1]) err_str += location err_str += content self.__logs += err_str print(len(self.__logs)) print(self.__logs) def __get_log(self): # log = open(self.__log_path,'r') # log_text = log.read() # log.close() return self.__logs def get_response(self): return self.__compile_state, self.__output_path, self.__get_log()
def __init__(self,project_name, main_document): self.project_name= project_name self.main_document= main_document self.__directory = Directory(self.__project_path)