def put(self, request, dir, format=None): if (request.data['name'] == ""): payload = { "info": "DIR Name Cannot Be Empty", } return JsonResponse(payload, safe=False) if (request.data['name'] == "home"): payload = { "info": "DIR Cannot Be Named As Home", } return JsonResponse(payload, safe=False) try: parent_dir = Directory.objects.get(pk=dir) if (parent_dir.user_id_id != request.user.id): payload = { "info": "You Don't Have Permissions to that DIR", } return JsonResponse(payload, safe=False) except Directory.DoesNotExist: parent_dir = None if parent_dir is None: payload = { "info": "Parent DIR Does Not Exist", } return JsonResponse(payload, safe=False) if parent_dir.is_directory == 0: payload = { "info": "Parent DIR Cannot Be A File", } return JsonResponse(payload, safe=False) existing_childs = Directory.objects.filter( parent_directory=parent_dir.pk) for child in existing_childs: if (str(child.directory_name).lower() == str( request.data['name']).lower()): payload = { "info": "Cannot Have Duplicate Folder Names", } return JsonResponse(payload, safe=False) new_dir = Directory(user_id=request.user, directory_name=request.data['name']) try: new_dir.parent_directory = parent_dir new_dir.save() except: payload = { "info": "There Was A Problem!", } return JsonResponse(payload, safe=False) payload = { "info": "Folder Created Sucessfully", "dir": new_dir.pk, "directory_name": new_dir.directory_name, } return JsonResponse(payload, safe=False)
def create_folder(request, username, repo, url="/"): try: b = request.GET["b"] except: b = 'master' if request.method == "POST": r = Repository.objects.get(name=repo, user_id=request.user.id) if url == "/": root = Directory.objects.get(name="/", repo_id=r.id, branch=b) d = Directory(dir_id=root.id, repo_id=r.id, name=request.POST["name"], path="/" + request.POST["name"] + "/", branch=b) d.save() return HttpResponseRedirect( f"/repo/{username}/{repo}/{request.POST['name']}/") else: d = Directory.objects.get(path="/" + url + "/", branch=b) dir = Directory(dir_id=d.id, path="/" + url + "/" + request.POST["name"] + "/", repo_id=r.id, name=request.POST["name"], branch=b) dir.save() return HttpResponseRedirect( f"/repo/{username}/{repo}/{url}/{request.POST['name']}/") #return to the new dir else: return render(request, "repo/folder.html", { "repo": repo, "username": username })
def register_request(request): if request.method == "POST": form = NewUserForm(request.POST) if form.is_valid(): user = form.save() if user is not None: login(request, user) home_dir = Directory(user_id=request.user, directory_name='home') try: home_dir.save() except: messages.error( request, "There Has Been A Problem, Please Try Again.") return redirect('/') messages.success(request, "Registration successful.") return redirect("/") messages.error(request, "Unsuccessful registration. Invalid information.") form = NewUserForm() return render(request=request, template_name="register.html", context={ "register_form": form, "title": "Register" })
def create_directory(request): if request.method == "POST": username = request.POST["username"] user = User.objects.get(username=username) repo = request.POST["repo"] repo = Repository.objects.get(name=repo) directory = request.POST["directory"] # /hellollo/welcomeworld/ dir = "/" subdir = "/" name = "" if directory.count("/") > 2: s = directory.split("/") for i in range(1, len(s)-2): print(i) dir += s[i] dir += "/" print(colored(dir, "green")) subdir += s[len(s)-2] + "/" name = s[len(s)-2] else: name = directory.replace("/", "") print(colored((subdir, name), "red")) subdir = Directory.objects.get(repo_id=repo.id, path=dir) d = Directory(repo_id=repo.id, dir_id=subdir.id, path=directory, name=name) d.save() return JsonResponse({"message": "success"})