def dir_contents(request):
  dir_dict_list = []
  first = request.GET.get("first")
  if first:
    src = request.GET['from']
    if src == 'dataset':
      if 'dataset_name' not in request.GET:
        raise Exception('No dataset supplied')        
      ds_name = request.GET['dataset_name']
      mnt_pnt = '/%s'%ds_name
      dirs = []
      if os.path.isdir(mnt_pnt):
        dirs = os.listdir(mnt_pnt)
      if not dirs:
        d_dict = {'id':mnt_pnt, 'text':'/','icon':'fa','children':False,'data':{'dir':mnt_pnt},'parent':"#"}
        dir_dict_list.append(d_dict)
      for dir in dirs:
        if os.path.isdir('%s/%s'%(mnt_pnt, dir)):
          subdirs, err = _has_subdirs('%s/%s'%(mnt_pnt, dir))
          if err:
            raise Exception(err)
          #subdirs = os.listdir('%s/%s'%(mnt_pnt, dir))
          if subdirs:
            d_dict = {'id':'%s/%s'%(mnt_pnt,dir), 'text':dir,'icon':'fa fa-angle-right','children':True,'data':{'dir':'%s/%s'%(mnt_pnt, dir)},'parent':"#"}
          else:
            d_dict = {'id':'%s/%s'%(mnt_pnt,dir), 'text':dir,'icon':'fa','children':False,'data':{'dir':'%s/%s'%(mnt_pnt, dir)},'parent':"#"}
          dir_dict_list.append(d_dict)
    elif src == 'pool':
      if 'pool_name' not in request.GET:
        pools, err = zfs.get_pools()
        if err:
          raise Exception(err)
        p = pools[0]
      else:
        pool = request.GET['pool_name']
        p, err = zfs.get_pool(pool)
        if err:
          raise Exception(err)
      dir_dict_list = []
      for ds in p["datasets"]:
        if ds['properties']['type']['value'] == 'filesystem':
          mnt_pnt = ds['properties']['mountpoint']['value']
          #subdirs = os.listdir(mnt_pnt)
          subdirs, err = _has_subdirs(mnt_pnt)
          if err:
            raise Exception(err)
          name = os.path.basename(mnt_pnt)
          if subdirs:
            d_dict = {'id':mnt_pnt, 'text':name,'icon':'fa fa-angle-right','children':True,'data':{'dir':mnt_pnt},'parent':"#"}
          else:
            d_dict = {'id':mnt_pnt, 'text':name,'icon':'fa','children':False,'data':{'dir':mnt_pnt},'parent':"#"}
          dir_dict_list.append(d_dict)
  else:
    if 'dir' in request.GET and request.GET['dir'] != '/':
      path = request.GET['dir']
    else:
      path = request.GET.get("pool_name")
    dirs = os.listdir(path)
    #print 'path ', path
    for d in dirs:
      #print 'dir', d
      true = True
      if os.path.isdir(path+"/"+d):
        parent = path
        subdirs, err = _has_subdirs('%s/%s'%(path, d))
        if err:
          raise Exception(err)
        '''
        contents = os.listdir('%s/%s'%(path, d))
        subdirs = False
        for content in contents:
          if os.path.isdir('%s/%s/%s'%(path,d, content)):
            subdirs = True
            break
        '''
        #print 'subdirs ', subdirs
        if subdirs:
          #print 'yes'
          d_dict = {'id':path+"/"+d, 'text':d,'icon':'fa fa-angle-right','children':True,'data':{'dir':path+"/"+d},'parent':parent}
        else:
          #print 'no'
          d_dict = {'id':path+"/"+d, 'text':d,'icon':'fa','children':False,'data':{'dir':path+"/"+d},'parent':parent}
      	dir_dict_list.append(d_dict)
  return HttpResponse(json.dumps(dir_dict_list),content_type='application/json')

  '''