def file(request, project_id): """文件列表 & 添加文件夹""" parent_object = None folder_id = request.GET.get('folder', '') if folder_id.isdecimal(): parent_object = models.File.objects.filter( id=int(folder_id), file_type=2, project=request.tracer.project).first() if request.method == "GET": # 获取导航栏 breadcrumb_list = [] parent = parent_object while parent: breadcrumb_list.insert(0, {'id': parent.id, 'name': parent.name}) parent = parent.parent # 当前目录下所有的文件 & 文件夹获取 queryset = models.File.objects.filter(project=request.tracer.project) if parent_object: # 进入某目录 file_object_list = queryset.filter( parent=parent_object).order_by('-file_type') else: # 进入根目录 file_object_list = queryset.filter( parent__isnull=True).order_by('-file_type') form = FolderModelForm(request, parent_object) context = { 'form': form, 'file_object_list': file_object_list, 'breadcrumb_list': breadcrumb_list, 'folder_object': parent_object } return render(request, 'file.html', context) # 添加文件夹. fid = request.POST.get('fid', '') edit_object = None if fid.isdecimal(): edit_object = models.File.objects.filter( id=int(fid), file_type=2, project=request.tracer.project).first() if edit_object: form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.project = request.tracer.project form.instance.file_type = 2 form.instance.update_user = request.tracer.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'errors': form.errors})
def file(request, project_id): parent_object = None folder_id = request.GET.get('folder', '') if folder_id.isdecimal(): parent_object = FileRepository.objects.filter( id=int(folder_id), project=request.tracer.project, file_type=2).first() # get请求, 查看页面 if request.method == 'GET': # 文件导航条获取,循环获取父级目录的名称 breadcrumb_list = [] parent = parent_object while parent: # breadcrumb_list.insert(0, {'id': parent.id, 'name': parent.name}) breadcrumb_list.insert(0, model_to_dict(parent, fields=['id', 'name'])) parent = parent.parent # 获取当前文件夹下的所有文件名 queryset = FileRepository.objects.filter(project=project_id) if not parent_object: folder_list = queryset.filter(parent__isnull=True).order_by('-file_type') else: folder_list = queryset.filter(parent=parent_object).order_by('-file_type') form = FolderModelForm(request, parent_object) # 上下文 context = { 'form': form, 'folder_list': folder_list, # 文件夹列表 'breadcrumb_list': breadcrumb_list, # 文件夹导航条 'folder_object': parent_object # 父级文件夹 } return render(request, 'web/file.html', context) # 如果是post请求,添加文件夹或者修改文件夹 fid = request.POST.get('fid', '') # 获取需要编辑的文件夹名称 edit_object = None # 需要编辑的文件夹 # 如果需要编辑的文件夹id 存在就获取需要编辑的文件夹对象 if fid.isdecimal(): edit_object = FileRepository.objects.filter(id=int(fid), project_id=project_id, file_type=2).first() if edit_object: # 将需要编辑的文件夹对象传给表单类验证 form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: # 新建文件夹直接POST数据传给表单类 form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.file_type = 2 form.instance.update_user = request.tracer.user form.instance.project = request.tracer.project form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def file(request, project_id): parent_object = None folder_id = request.GET.get('folder', '') if folder_id.isdecimal(): parent_object = models.FileRepository.objects.filter( id=folder_id, file_type=2, project=request.blog.project).first() #GET请求为查看页面 if request.method == 'GET': breadcrumd_list = [] parent = parent_object while parent: # breadcrumd_list.insert(0,{'id':parent.id,'name':parent.name}) breadcrumd_list.insert(0, model_to_dict(parent, ['id', 'name'])) parent = parent.parent queryset = models.FileRepository.objects.filter( project=request.blog.project, ) if parent_object: #进入某一个目录 file_object_list = queryset.filter( parent=parent_object).order_by('-file_type') else: file_object_list = queryset.filter( parent__isnull=True).order_by('-file_type') #进入根目录 form = FolderModelForm(request, parent_object) context = { 'form': form, 'file_object_list': file_object_list, 'breadcrumd_list': breadcrumd_list, 'folder_object': parent_object } return render(request, 'file.html', context) fid = request.POST.get('fid', '') edit_object = None if fid.isdecimal(): edit_object = models.FileRepository.objects.filter( id=int(fid), file_type=2, project=request.blog.project).first() if edit_object: form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: form = FolderModelForm(request, parent_object, request.POST) #POST请求为添加文件 if form.is_valid(): form.instance.project = request.blog.project form.instance.file_type = 2 form.instance.update_user = request.blog.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'errors': form.errors})
def file(request, project_id): """展示文件列表&添加文件夹""" parent_object = None folder_id = request.GET.get('folder', '') if folder_id.isdecimal(): parent_object = models.FileRepository.objects.filter( id=int(folder_id), file_type=2, project=request.lxyker.project, ).first() # 查看文件列表 if request.method == 'GET': breadcrumb_list = list() parent = parent_object while parent: breadcrumb_list.insert(0, {'id': parent.id, 'name': parent.name}) parent = parent.parent # 获取当前目录下所有的 文件夹+文件 queryset = models.FileRepository.objects.filter(project=request.lxyker.project).order_by('-file_type') if parent_object: folder_obj_list = queryset.filter(parent=parent_object) else: folder_obj_list = queryset.filter(parent__isnull=True) form = FolderModelForm(request, parent_object) folder_object = parent_object return render(request, 'web/file.html', locals()) # 添加/修改文件夹 fid = request.POST.get('fid', '') edit_object = None if fid.isdecimal(): edit_object = models.FileRepository.objects.filter(id=int(fid), file_type=2, project=request.lxyker.project).first() if edit_object: form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.project = request.lxyker.project form.instance.file_type = 2 form.instance.update_user = request.lxyker.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def file(request, project_id): """ 文件列表 & 添加文件夹 """ parent_object = None # 定义新建文件的父目录 folder_id = request.GET.get('folder', '') if folder_id.isdecimal(): # 判断folder_id是否为数字 # 判断父目录是否存在,若存在,则赋值 parent_object = models.FileRepository.objects.filter(id=int(folder_id), file_type=2, project=request.tracer.project).first() # GET 查看页面 if request.method == 'GET': # 利用while True构建file导航条 breadcrumb_list = [] parent = parent_object while parent: # breadcrumb_list.insert(0, {'id': parent.id, 'name': parent.name}) # model_to_dict方法将对象转化为dict breadcrumb_list.insert(0, model_to_dict(parent, ['id', 'name'])) parent = parent.parent # 当前目录下所有的文件 & 文件夹获取到即可 queryset = models.FileRepository.objects.filter(project=request.tracer.project) if parent_object: # 为真,则表示是在某个目录下,false则表示在根目录下 # 进入了某目录 file_object_list = queryset.filter(parent=parent_object).order_by('-file_type') else: # 根目录 file_object_list = queryset.filter(parent__isnull=True).order_by('-file_type') form = FolderModelForm(request, parent_object) # 构建返回值 context = { 'form': form, "file_object_list": file_object_list, "breadcrumb_list": breadcrumb_list, 'folder_object': parent_object } return render(request, 'file.html', context) # POST 添加文件夹 & 文件夹的修改 """ 接收fid:判断是添加文件夹还是修改,fid存在是修改,不存在是添加 """ fid = request.POST.get('fid', '') edit_object = None # 如果fid存在,取出要修改的对象 if fid.isdecimal(): edit_object = models.FileRepository.objects.filter(id=int(fid), file_type=2, project=request.tracer.project).first() if edit_object: form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: form = FolderModelForm(request, parent_object, data=request.POST) # 无论是修改还是添加都需要验证和保存 # form = FolderModelForm(request, parent_object) if form.is_valid(): form.instance.project = request.tracer.project form.instance.file_type = 2 form.instance.update_user = request.tracer.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def file(request, project_id): """ 文件列表 & 添加文件夹 """ parent_object = None # 当进入文件夹时携带 folder 参数 folder_id = request.GET.get('folder', "") if folder_id.isdecimal(): parent_object = models.FileRepository.objects.filter( id=int(folder_id), file_type=2, project=request.tracer.project).first() # GET 查看页面 if request.method == "GET": # 文件夹面包屑导航 breadcrumb_list = [] parent = parent_object while parent: # 当没有父级文件夹时 parent false # breadcrumb_list.insert(0, {'id': parent.id, 'name': parent.name}) breadcrumb_list.insert(0, model_to_dict(parent, ['id', 'name'])) parent = parent.parent # 当前目录下所有的文件 & 文件夹获取到即可 queryset = models.FileRepository.objects.filter( project=request.tracer.project) if parent_object: # 进入了某目录 file_object_list = queryset.filter( parent=parent_object).order_by('-file_type') else: # 根目录 file_object_list = queryset.filter( parent__isnull=True).order_by('-file_type') form = FolderModelForm(request, parent_object) context = { 'form': form, "file_object_list": file_object_list, "breadcrumb_list": breadcrumb_list, 'folder_object': parent_object } return render(request, 'file.html', context) # POST 添加文件夹 & 文件夹的修改 fid = request.POST.get('fid', '') edit_object = None if fid.isdecimal(): edit_object = models.FileRepository.objects.filter( id=int(fid), file_type=2, project=request.tracer.project).first() if edit_object: form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.project = request.tracer.project form.instance.file_type = 2 form.instance.update_user = request.tracer.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def file(request, project_id): parent_object = None folder_id = request.GET.get('folder_id', "") if folder_id.isdecimal(): # 拿到当前文件夹的id,判断数据库中是否存在,存在则将此文件夹作为父目录,当用户通过此文件夹点击进入时,则在下一步进行筛选出 # 当前文件夹id下属的所有文件夹和文件 parent_object = models.FileRepository.objects.filter( id=int(folder_id), file_type=2, project=request.bug_mgt.project).first() if request.method == 'GET': # 用于存放导航栏中文件夹的层级目录,返回给前端展示出文件夹导航效果 breadcrumb_list = [] parent = parent_object if parent: # 把当前点击的文件夹赋值给parent,然后循环找出这个点击的文件夹的父级文件夹,一直找到根目录,再依次添加到列表中返回前台 while parent: breadcrumb_list.insert(0, { 'id': parent.id, 'name': parent.file_name }) parent = parent.parent queryset = models.FileRepository.objects.filter( project=request.bug_mgt.project) # 展示当前目录下的文件和文件夹,若有父级文件夹,则将当前文件夹下的目录展现,若没有,则展示根目录下的文件和文件夹 if parent_object: file_object_list = queryset.filter(parent=parent_object).order_by( '-file_type') # 以文件夹、文件的形式排列 else: file_object_list = queryset.filter( parent_id__isnull=True).order_by('-file_type') form = FolderModelForm(request, parent_object) context = { 'form': form, 'file_object_list': file_object_list, 'breadcrumb_list': breadcrumb_list, 'folder_object': parent_object } return render(request, 'web/file.html', context) # post提交,用户新建文件夹 # 由于是共用一个post提交,编辑和添加文件夹则需要做区分,通过前台是否传过来fid来判断是编辑还是添加,若fid存在,则为编辑 fid = request.POST.get('fid', '') edit_object = None if fid.isdecimal(): # 获取编辑文件夹对象 edit_object = models.FileRepository.objects.filter( id=int(fid), file_type=2, project=request.bug_mgt.project).first() if edit_object: # 如果存在,则为编辑文件夹,此时传入一个instance对象(已在数据库中获取到了),通过modelform做校验,通过后就save form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: # 不存在则为添加文件夹 form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.project = request.bug_mgt.project form.instance.file_type = 2 form.instance.update_user = request.bug_mgt.user form.instance.parent = parent_object form.save() return JsonResponse({'status': True}) return JsonResponse({'status': False, 'error': form.errors})
def file(request, project_id): """文件夹列表&添加文件夹""" parent_object = None folder_id = request.GET.get("folder", "") if folder_id.isdecimal(): parent_object = models.FileRepository.objects.filter( id=int(folder_id), file_type=2, project=request.tracer.project).first() # GET查看页面 if request.method == "GET": # 面包屑导航条 breadcrumb_list = [] parent = parent_object while parent: # 方法1: breadcrumb_list.insert(0, {"id": parent.id, "name": parent.name}) breadcrumb_list.insert(0, model_to_dict( parent, ['id', 'name'])) # 自动去parent对象中取id和name,然后拼凑为字典,和方法1效果上一样 parent = parent.parent # 当前目录下所有的文件 & 文件夹获取到即可 queryset = models.FileRepository.objects.filter( project=request.tracer.project) if parent_object: # 进入某目录 file_object_list = queryset.filter( parent=parent_object).order_by("-file_type") else: # 根目录,没有父级目录 file_object_list = queryset.filter( parent__isnull=True).order_by("-file_type") form = FolderModelForm(request, parent_object) context = { "form": form, "file_object_list": file_object_list, "breadcrumb_list": breadcrumb_list, "folder_obj": parent_object # 当前目录 } return render(request, "file.html", context) # POST添加文件夹 & 文件夹的修改 fid = request.POST.get("fid", "") # fid为空说明是添加,有值说明是编辑 edit_object = None if fid.isdecimal(): # 修改 edit_object = models.FileRepository.objects.filter( id=int(fid), file_type=2, project=request.tracer.project).first() if edit_object: # 编辑 form = FolderModelForm(request, parent_object, data=request.POST, instance=edit_object) else: # 添加 form = FolderModelForm(request, parent_object, data=request.POST) if form.is_valid(): form.instance.project = request.tracer.project form.instance.file_type = 2 form.instance.update_user = request.tracer.user form.instance.parent = parent_object form.save() return JsonResponse({"status": True}) return JsonResponse({"status": False, "error": form.errors})