complete left project tree
This commit is contained in:
parent
54c70ee061
commit
fa0d953aba
|
@ -1,86 +1,93 @@
|
||||||
import os
|
#encoding:utf-8
|
||||||
from projects.models import Project
|
import os
|
||||||
|
import sys
|
||||||
|
from projects.models import Project
|
||||||
url_head = "/projects"
|
|
||||||
project_name = ""
|
|
||||||
project_id = ""
|
# ondbclick的参数为:project_id,relative_path,以及文件名
|
||||||
|
# 其中relative_path是相对于项目根路径的
|
||||||
# 将叶子节点的href替换成url
|
|
||||||
# 传入一个工程根目录路径,返回一个project_tree对应的html代码
|
project_name = ""
|
||||||
# 思路是基于递归的深度优先遍历
|
project_id = ""
|
||||||
def getHtml(projectPath):
|
#######获取当前层的文件夹和文件
|
||||||
global project_name,project_id
|
def getindex(path):
|
||||||
project_name = projectPath[projectPath.rfind('/')+1:]
|
dirs=[]
|
||||||
project_id = str(Project.objects.get(name=project_name).pk)
|
files=[]
|
||||||
|
for file in os.listdir(path):
|
||||||
html_str = "<ul>"
|
if os.path.isdir(os.path.join(path, file)):
|
||||||
files = os.listdir(projectPath)
|
dirs.append(file)
|
||||||
|
else:
|
||||||
for i in range(len(files)):
|
files.append(file)
|
||||||
filePath = projectPath + os.path.sep + files[i]
|
return dirs,files
|
||||||
|
|
||||||
if os.path.isdir(filePath):
|
#####生成节点树##########
|
||||||
tag_html = '<li class = "parent-item">\n'
|
def genetree(path,parentdirs):
|
||||||
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
dirs,files=getindex(path)
|
||||||
project_id,files[i], files[i], files[i])
|
# 如果parentdir不为空,那么htmlstr应该为"",否则会有问题,建议上次提交时的文件
|
||||||
tag_html += getInnerHtml(projectPath, filePath)
|
if parentdirs =="":
|
||||||
tag_html += '</li>\n'
|
isParentDirNull=True
|
||||||
else:
|
else:
|
||||||
# 叶子节点,替换成绝对路径
|
isParentDirNull=False
|
||||||
url = url_head+"/"+project_name+"/"+files[i]
|
if isParentDirNull:
|
||||||
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
htmlstr = "<ul>"
|
||||||
project_id,files[i], files[i], files[i])
|
else:
|
||||||
|
htmlstr = ""
|
||||||
html_str += tag_html
|
# 处理多级单文件夹目录
|
||||||
html_str += "</ul>"
|
# parentdir用来保存那个多级单文件父目录
|
||||||
return html_str
|
if len(dirs)==1:
|
||||||
|
parentdirs += (dirs[0] + os.sep)
|
||||||
# 合并文件夹(将下级只有一个文件夹的连续文件夹拼接)
|
current_dir = os.path.join(path, dirs[0])
|
||||||
# 例如,假设net目录下只有ui目录,ui目录下只有css目录,css下有1.css,2.css,
|
htmlstr += genetree(current_dir,parentdirs)
|
||||||
# 那么就将net,ui,css合并成net/ui/css/
|
else:
|
||||||
def getFiles(path):
|
for dir in dirs:
|
||||||
files = os.listdir(path)
|
# 合成ondbclick 字符串
|
||||||
flag = False
|
index = path.find(project_name)
|
||||||
while len(files) == 1 and os.path.isdir(path + os.path.sep + files[0]):
|
relative_path = path[index+len(project_name):]+"/"+dir
|
||||||
path = path + os.path.sep + files[0]
|
dirname = parentdirs + dir
|
||||||
files = os.listdir(path)
|
# ondbclick_str = '<text ondbclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname)
|
||||||
flag = True
|
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,dirname)
|
||||||
|
ondbclick_str += (dirname +"</text>")
|
||||||
return path, files, flag
|
|
||||||
|
# 形成li字符串
|
||||||
|
htmlstr += "<li>"+ondbclick_str
|
||||||
def getInnerHtml(projectPath, path):
|
|
||||||
html_str = '<ul class = "item sub-item" >\n'
|
dir = os.path.join(path, dir)
|
||||||
# files = os.listdir(path)
|
htmlstr += genetree(dir,"")
|
||||||
path_before = path
|
htmlstr += "</li>"
|
||||||
path, files, flag = getFiles(path)
|
# 循环遍历完毕,parentdirs应该设置为空
|
||||||
# print(path)
|
if parentdirs != "":
|
||||||
for i in range(len(files)):
|
parentdirs = ""
|
||||||
filePath = path + os.path.sep + files[i]
|
|
||||||
|
for file in files:
|
||||||
index = path.find(project_name)
|
# 合成ondbclick 字符串
|
||||||
relative_path = path[index+len(project_name):]+"/"+files[i]
|
index = path.find(project_name)
|
||||||
|
relative_path = path[index+len(project_name):]+"/"+file
|
||||||
if os.path.isdir(filePath):
|
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,relative_path,file)
|
||||||
tag_html = '<li>\n'
|
ondbclick_str += (file +"</text>")
|
||||||
# 如果有多级层叠的情况
|
# 合成li
|
||||||
if flag:
|
htmlstr += "<li data-jstree='{\"icon\":\"glyphicon glyphicon-leaf\"}'>"+ ondbclick_str+"</li>"
|
||||||
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
|
||||||
project_id,relative_path, files[i], path.replace(path_before, '')+"/"+files[i])
|
|
||||||
else:
|
if isParentDirNull:
|
||||||
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
htmlstr += "</ul>"
|
||||||
project_id,relative_path, files[i], files[i])
|
return htmlstr
|
||||||
tag_html += getInnerHtml(projectPath, filePath)
|
|
||||||
tag_html += '</li>\n'
|
|
||||||
else:
|
def getHtml(path):
|
||||||
# 替换成绝对路径
|
htmlstr = '<div id="jstree">'
|
||||||
# index = path.find(project_name)
|
try:
|
||||||
# url = url_head+"/"+path[index:]+"/"+files[i]
|
# 在这里把最开始的目录给加上
|
||||||
# tag_html = '<li><a href="%s"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
global project_name,project_id
|
||||||
# url, files[i])
|
project_name = path.split("/")[-1]
|
||||||
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","%s","%s")><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
project_id = str(Project.objects.get(name=project_name).pk)
|
||||||
project_id,relative_path, files[i], files[i])
|
|
||||||
html_str += tag_html
|
ondbclick_str = '<text ondblclick=add_tab("%s","%s","%s")>' %(project_id,"",project_name)
|
||||||
html_str += '</ul>\n'
|
ondbclick_str += (project_name+"</text>")
|
||||||
return html_str
|
htmlstr += "<ul><li>"+ ondbclick_str
|
||||||
|
htmlstr += genetree(path,"")
|
||||||
|
htmlstr += "</li></ul>"
|
||||||
|
except:
|
||||||
|
htmlstr += genetree(path,"")
|
||||||
|
finally:
|
||||||
|
htmlstr += "</div>"
|
||||||
|
return htmlstr
|
|
@ -0,0 +1,86 @@
|
||||||
|
import os
|
||||||
|
from projects.models import Project
|
||||||
|
|
||||||
|
|
||||||
|
url_head = "/projects"
|
||||||
|
project_name = ""
|
||||||
|
project_id = ""
|
||||||
|
|
||||||
|
# 将叶子节点的href替换成url
|
||||||
|
# 传入一个工程根目录路径,返回一个project_tree对应的html代码
|
||||||
|
# 思路是基于递归的深度优先遍历
|
||||||
|
def getHtml(projectPath):
|
||||||
|
global project_name,project_id
|
||||||
|
project_name = projectPath[projectPath.rfind('/')+1:]
|
||||||
|
project_id = str(Project.objects.get(name=project_name).pk)
|
||||||
|
|
||||||
|
html_str = "<ul>"
|
||||||
|
files = os.listdir(projectPath)
|
||||||
|
|
||||||
|
for i in range(len(files)):
|
||||||
|
filePath = projectPath + os.path.sep + files[i]
|
||||||
|
|
||||||
|
if os.path.isdir(filePath):
|
||||||
|
tag_html = '<li class = "parent-item">\n'
|
||||||
|
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
||||||
|
project_id,files[i], files[i], files[i])
|
||||||
|
tag_html += getInnerHtml(projectPath, filePath)
|
||||||
|
tag_html += '</li>\n'
|
||||||
|
else:
|
||||||
|
# 叶子节点,替换成绝对路径
|
||||||
|
url = url_head+"/"+project_name+"/"+files[i]
|
||||||
|
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","/%s","/%s") class = "item"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
||||||
|
project_id,files[i], files[i], files[i])
|
||||||
|
|
||||||
|
html_str += tag_html
|
||||||
|
html_str += "</ul>"
|
||||||
|
return html_str
|
||||||
|
|
||||||
|
# 合并文件夹(将下级只有一个文件夹的连续文件夹拼接)
|
||||||
|
# 例如,假设net目录下只有ui目录,ui目录下只有css目录,css下有1.css,2.css,
|
||||||
|
# 那么就将net,ui,css合并成net/ui/css/
|
||||||
|
def getFiles(path):
|
||||||
|
files = os.listdir(path)
|
||||||
|
flag = False
|
||||||
|
while len(files) == 1 and os.path.isdir(path + os.path.sep + files[0]):
|
||||||
|
path = path + os.path.sep + files[0]
|
||||||
|
files = os.listdir(path)
|
||||||
|
flag = True
|
||||||
|
|
||||||
|
return path, files, flag
|
||||||
|
|
||||||
|
|
||||||
|
def getInnerHtml(projectPath, path):
|
||||||
|
html_str = '<ul class = "item sub-item" >\n'
|
||||||
|
# files = os.listdir(path)
|
||||||
|
path_before = path
|
||||||
|
path, files, flag = getFiles(path)
|
||||||
|
# print(path)
|
||||||
|
for i in range(len(files)):
|
||||||
|
filePath = path + os.path.sep + files[i]
|
||||||
|
|
||||||
|
index = path.find(project_name)
|
||||||
|
relative_path = path[index+len(project_name):]+"/"+files[i]
|
||||||
|
|
||||||
|
if os.path.isdir(filePath):
|
||||||
|
tag_html = '<li>\n'
|
||||||
|
# 如果有多级层叠的情况
|
||||||
|
if flag:
|
||||||
|
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
||||||
|
project_id,relative_path, files[i], path.replace(path_before, '')+"/"+files[i])
|
||||||
|
else:
|
||||||
|
tag_html += '<a href = "javascript:void(0)" ondblclick=add_tab("%s","%s","/%s") class = "item" show = "0" ><i class = "fa fa-folder color-blue"></i> %s </a>\n' % (
|
||||||
|
project_id,relative_path, files[i], files[i])
|
||||||
|
tag_html += getInnerHtml(projectPath, filePath)
|
||||||
|
tag_html += '</li>\n'
|
||||||
|
else:
|
||||||
|
# 替换成绝对路径
|
||||||
|
# index = path.find(project_name)
|
||||||
|
# url = url_head+"/"+path[index:]+"/"+files[i]
|
||||||
|
# tag_html = '<li><a href="%s"><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
||||||
|
# url, files[i])
|
||||||
|
tag_html = '<li><a href="javascript:void(0)" ondblclick=add_tab("%s","%s","%s")><i class = "fa fa-file-text-o color-blue"></i > %s </a></li>\n' % (
|
||||||
|
project_id,relative_path, files[i], files[i])
|
||||||
|
html_str += tag_html
|
||||||
|
html_str += '</ul>\n'
|
||||||
|
return html_str
|
|
@ -1,86 +0,0 @@
|
||||||
#encoding:utf-8
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
from projects.models import Project
|
|
||||||
|
|
||||||
|
|
||||||
# ondbclick的参数为:project_id,relative_path,以及文件名
|
|
||||||
# 其中relative_path是相对于项目根路径的
|
|
||||||
|
|
||||||
project_name = ""
|
|
||||||
project_id = ""
|
|
||||||
#######获取当前层的文件夹和文件
|
|
||||||
def getindex(path):
|
|
||||||
dirs=[]
|
|
||||||
files=[]
|
|
||||||
for file in os.listdir(path):
|
|
||||||
if os.path.isdir(os.path.join(path, file)):
|
|
||||||
dirs.append(file)
|
|
||||||
else:
|
|
||||||
files.append(file)
|
|
||||||
return dirs,files
|
|
||||||
|
|
||||||
#####生成节点树##########
|
|
||||||
def genetree(path,parentdirs):
|
|
||||||
dirs,files=getindex(path)
|
|
||||||
# 如果parentdir不为空,那么htmlstr应该为"",否则会有问题,建议上次提交时的文件
|
|
||||||
if parentdirs =="":
|
|
||||||
isParentDirNull=True
|
|
||||||
else:
|
|
||||||
isParentDirNull=False
|
|
||||||
if isParentDirNull:
|
|
||||||
htmlstr = "<ul>"
|
|
||||||
else:
|
|
||||||
htmlstr = ""
|
|
||||||
# 处理多级单文件夹目录
|
|
||||||
# parentdir用来保存那个多级单文件父目录
|
|
||||||
if len(dirs)==1:
|
|
||||||
parentdirs += (dirs[0] + os.sep)
|
|
||||||
current_dir = os.path.join(path, dirs[0])
|
|
||||||
htmlstr += genetree(current_dir,parentdirs)
|
|
||||||
else:
|
|
||||||
for dir in dirs:
|
|
||||||
# 合成ondbclick 字符串
|
|
||||||
index = path.find(project_name)
|
|
||||||
relative_path = path[index+len(project_name):]+"/"+dir
|
|
||||||
dirname = parentdirs + dir
|
|
||||||
ondbclick_str = 'ondbclick("%s","%s","%s")' %(project_id,relative_path,dirname)
|
|
||||||
# 形成li字符串
|
|
||||||
htmlstr += "<li "+ondbclick_str +" >"+ dirname
|
|
||||||
dir = os.path.join(path, dir)
|
|
||||||
htmlstr += genetree(dir,"")
|
|
||||||
htmlstr += "</li>"
|
|
||||||
# 循环遍历完毕,parentdirs应该设置为空
|
|
||||||
if parentdirs != "":
|
|
||||||
parentdirs = ""
|
|
||||||
|
|
||||||
for file in files:
|
|
||||||
# 合成ondbclick 字符串
|
|
||||||
index = path.find(project_name)
|
|
||||||
relative_path = path[index+len(project_name):]+"/"+file
|
|
||||||
ondbclick_str = 'ondbclick("%s","%s","%s")' %(project_id,relative_path,file)
|
|
||||||
# 合成li
|
|
||||||
htmlstr += "<li data-jstree='{\"icon\":\"glyphicon glyphicon-leaf\"}' "+ ondbclick_str+" >"+file+"</li>"
|
|
||||||
|
|
||||||
|
|
||||||
if isParentDirNull:
|
|
||||||
htmlstr += "</ul>"
|
|
||||||
return htmlstr
|
|
||||||
|
|
||||||
|
|
||||||
def getHtml(path):
|
|
||||||
htmlstr = '<div id="jstree">'
|
|
||||||
try:
|
|
||||||
# 在这里把最开始的目录给加上
|
|
||||||
global project_name,project_id
|
|
||||||
project_name = path.split("/")[-1]
|
|
||||||
project_id = str(Project.objects.get(name=project_name).pk)
|
|
||||||
|
|
||||||
htmlstr += "<ul><li>"+ project_name
|
|
||||||
htmlstr += genetree(path,"")
|
|
||||||
htmlstr += "</li></ul>"
|
|
||||||
except:
|
|
||||||
htmlstr += genetree(path,"")
|
|
||||||
finally:
|
|
||||||
htmlstr += "</div>"
|
|
||||||
return htmlstr
|
|
Loading…
Reference in New Issue