def edit_share(request):

  return_dict = {}
  try:
    user_list = samba_settings.get_user_list()
    group_list = samba_settings.get_group_list()
  
    if request.method == "GET":
      # Shd be an edit request
      if "share_id" not in request.GET:
        return_dict["error"] = "Unknown share specified"
        return django.shortcuts.render_to_response('logged_in_error.html', return_dict, context_instance=django.template.context.RequestContext(request))
      share_id = request.GET["share_id"]
      try :
        share_dict = samba_settings.load_share_info("by_id", share_id)
        valid_users_list = samba_settings.load_valid_users_list(share_dict["share_id"])
      except Exception, e:
        return_dict["error"] = "Error loading share information - %s" %e
        return django.shortcuts.render_to_response('logged_in_error.html', return_dict, context_instance=django.template.context.RequestContext(request))
  
      # Set initial form values
      initial = {}
      initial["share_id"] = share_dict["share_id"]
      initial["name"] = share_dict["name"]
      initial["path"] = share_dict["path"]
      initial["display_path"] = share_dict["display_path"]
      if share_dict["guest_ok"]:
        initial["guest_ok"] = True
      else:
        initial["guest_ok"] = False
      if share_dict["browseable"]:
        initial["browseable"] = True
      else:
        initial["browseable"] = False
      if share_dict["read_only"]:
        initial["read_only"] = True
      else:
        initial["read_only"] = False
      initial["comment"] = share_dict["comment"]
  
      if valid_users_list:
        vgl = []
        vul = []
        for u in valid_users_list:
          if u["grp"]:
            vgl.append(u["name"])
          else:
            vul.append(u["name"])
        initial["users"] = vul
        initial["groups"] = vgl
  
      form = samba_shares_forms.ShareForm(initial = initial, user_list = user_list, group_list = group_list)
  
      return_dict["form"] = form
      return django.shortcuts.render_to_response('edit_share.html', return_dict, context_instance=django.template.context.RequestContext(request))
  
    else:
def create_share(request):

    return_dict = {}
    try:
        user_list = samba_settings.get_user_list()
        group_list = samba_settings.get_group_list()

        if request.method == "GET":
            #Return the form
            form = samba_shares_forms.ShareForm(user_list=user_list,
                                                group_list=group_list)
            return_dict["form"] = form
            return django.shortcuts.render_to_response(
                "create_share.html",
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))
        else:
            #Form submission so create
            return_dict = {}
            form = samba_shares_forms.ShareForm(request.POST,
                                                user_list=user_list,
                                                group_list=group_list)
            return_dict["form"] = form
            if form.is_valid():
                cd = form.cleaned_data
                name = cd["name"]
                path = "%s" % cd["path"]
                if not path:
                    return_dict["path_error"] = "Please choose a path."
                    return django.shortcuts.render_to_response(
                        "create_share.html",
                        return_dict,
                        context_instance=django.template.context.
                        RequestContext(request))

                if "comment" in cd:
                    comment = cd["comment"]
                else:
                    comment = None
                if "read_only" in cd:
                    read_only = cd["read_only"]
                else:
                    read_only = None
                if "browseable" in cd:
                    browseable = cd["browseable"]
                else:
                    browseable = None
                if "guest_ok" in cd:
                    guest_ok = cd["guest_ok"]
                else:
                    guest_ok = None
                if "users" in cd:
                    users = cd["users"]
                else:
                    users = None
                if "groups" in cd:
                    groups = cd["groups"]
                else:
                    groups = None
                vol = cd["vol"]
                #logger.debug("Create share request, name %s path %s, comment %s, read_only %s, browseable %s, guest_ok %s, users %s, groups %s, vol %s"%(name, path, comment, read_only, browseable, guest_ok, users, groups))
                try:
                    samba_settings.create_share(name, comment, guest_ok,
                                                read_only, path, display_path,
                                                browseable, users, groups)
                    samba_settings.generate_smb_conf()
                except Exception, e:
                    return_dict["error"] = "Error creating share - %s" % e
                    return django.shortcuts.render_to_response(
                        'logged_in_error.html',
                        return_dict,
                        context_instance=django.template.context.
                        RequestContext(request))

                audit_str = "Created Samba share %s" % name
                audit.audit("create_share", audit_str,
                            request.META["REMOTE_ADDR"])
                return django.http.HttpResponseRedirect(
                    '/display_shares?action=created')
            else:
def create_share(request):

  return_dict = {}
  try:
    user_list = samba_settings.get_user_list()
    group_list = samba_settings.get_group_list()
  
    if request.method == "GET":
      #Return the form
      form = samba_shares_forms.ShareForm(user_list = user_list, group_list = group_list)
      return_dict["form"] = form
      return django.shortcuts.render_to_response("create_share.html", return_dict, context_instance = django.template.context.RequestContext(request))
    else:
      #Form submission so create
      return_dict = {}
      form = samba_shares_forms.ShareForm(request.POST, user_list = user_list, group_list = group_list)
      return_dict["form"] = form
      if form.is_valid():
        cd = form.cleaned_data
        name = cd["name"]
        path = "%s"%cd["path"]
        if not path:
          return_dict["path_error"] = "Please choose a path."
          return django.shortcuts.render_to_response("create_share.html", return_dict, context_instance = django.template.context.RequestContext(request))
        
        if "comment" in cd:
          comment = cd["comment"]
        else:
          comment = None
        if "read_only" in cd:
          read_only = cd["read_only"]
        else:
          read_only = None
        if "browseable" in cd:
          browseable = cd["browseable"]
        else:
          browseable = None
        if "guest_ok" in cd:
          guest_ok = cd["guest_ok"]
        else:
          guest_ok = None
        if "users" in cd:
          users = cd["users"]
        else:
          users = None
        if "groups" in cd:
          groups = cd["groups"]
        else:
          groups = None
        vol = cd["vol"]
        #logger.debug("Create share request, name %s path %s, comment %s, read_only %s, browseable %s, guest_ok %s, users %s, groups %s, vol %s"%(name, path, comment, read_only, browseable, guest_ok, users, groups))
        try :
          samba_settings.create_share(name, comment, guest_ok, read_only, path, display_path, browseable, users, groups)
          samba_settings.generate_smb_conf()
        except Exception, e:
          return_dict["error"] = "Error creating share - %s" %e
          return django.shortcuts.render_to_response('logged_in_error.html', return_dict, context_instance=django.template.context.RequestContext(request))
  
        audit_str = "Created Samba share %s"%name
        audit.audit("create_share", audit_str, request.META["REMOTE_ADDR"])
        return django.http.HttpResponseRedirect('/display_shares?action=created')
      else:
def edit_share(request):

    return_dict = {}
    try:
        user_list = samba_settings.get_user_list()
        group_list = samba_settings.get_group_list()

        if request.method == "GET":
            # Shd be an edit request
            if "share_id" not in request.GET:
                return_dict["error"] = "Unknown share specified"
                return django.shortcuts.render_to_response(
                    'logged_in_error.html',
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))
            share_id = request.GET["share_id"]
            try:
                share_dict = samba_settings.load_share_info("by_id", share_id)
                valid_users_list = samba_settings.load_valid_users_list(
                    share_dict["share_id"])
            except Exception, e:
                return_dict[
                    "error"] = "Error loading share information - %s" % e
                return django.shortcuts.render_to_response(
                    'logged_in_error.html',
                    return_dict,
                    context_instance=django.template.context.RequestContext(
                        request))

            # Set initial form values
            initial = {}
            initial["share_id"] = share_dict["share_id"]
            initial["name"] = share_dict["name"]
            initial["path"] = share_dict["path"]
            initial["display_path"] = share_dict["display_path"]
            if share_dict["guest_ok"]:
                initial["guest_ok"] = True
            else:
                initial["guest_ok"] = False
            if share_dict["browseable"]:
                initial["browseable"] = True
            else:
                initial["browseable"] = False
            if share_dict["read_only"]:
                initial["read_only"] = True
            else:
                initial["read_only"] = False
            initial["comment"] = share_dict["comment"]

            if valid_users_list:
                vgl = []
                vul = []
                for u in valid_users_list:
                    if u["grp"]:
                        vgl.append(u["name"])
                    else:
                        vul.append(u["name"])
                initial["users"] = vul
                initial["groups"] = vgl

            form = samba_shares_forms.ShareForm(initial=initial,
                                                user_list=user_list,
                                                group_list=group_list)

            return_dict["form"] = form
            return django.shortcuts.render_to_response(
                'edit_share.html',
                return_dict,
                context_instance=django.template.context.RequestContext(
                    request))

        else: