def newProjectAjax(request): try: #get information from the Ajax post data r_projectType = request.POST.get('projectType') r_projectName= request.POST.get('projectName') r_projectDescription = request.POST.get('projectDescription') user = request.user newProject = Project(project_name=r_projectName, project_type=r_projectType, description=r_projectDescription) newProject.save() member = Member(project= newProject, user = user, mem_type = "Owner") member.save() root = Directory(parent = None, name = "Root", project_id = newProject, level = '0') root.save() newfile = File(parent = root, data= "", project_id = newProject.project_id, file_name = "readme", file_type = "txt") newfile.save() return JsonResponse({'response':'success', 'projectid': newProject.project_id}) except: pass #if here then somthing went wrong return JsonResponse({'response':'fail'})
from codeapp.models import Project, Member, File, Directory # p = Project(project_name = "new project1", description = "new p", ) p.save() r = Directory(parent = None, name = "Root", project_id = p, level = 0) r.save() d1= Directory(parent = r, name = "D1", project_id = p, level = 1) d1.save() d2= Directory(parent = r, name = "D2", project_id = p, level = 1) d2.save() d1a= Directory(parent = d1, name = "D1a", project_id = p, level = 2) d1a.save() f = File(parent = d1a, data = '', file_name = 'test', file_type = 'txt', share_link='ffsafsa') f.save() f2= File(parent = d1, data = '', file_name = 'test2', file_type = 'txt', share_link='ffsafsa') f2.save() root = Directory.objects.get(project_id= p, name="Root") rootdirs = root.children.all() # def getDirectoryTree(project): # tree = {} # root = Directory.objects.get(project_id= project, name="Root") # rootdirs = root.children.all() # for d0 in rootdirs: # lvl 0 directory # tree['lvl0dirs'] = rootdirs # for d1 in d0.children.all(): # lvl 1 directory # tree[d0.name+'_dirs'] = d0.children.all() # files = File.objects.filter(parent = d1) # if files: # tree[d1.name+'_files'] = files