2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 16:57:09 +00:00

Upload drawings now using git_add()

This commit is contained in:
2024-12-29 22:53:26 +00:00
parent 2623af92c4
commit 1ddd4da27b
2 changed files with 60 additions and 31 deletions

View File

@@ -16,6 +16,7 @@ from troggle.core.utils import (
alphabet_suffix,
current_expo,
get_cookie,
git_add,
git_string,
sanitize_name,
unique_slug,
@@ -53,7 +54,12 @@ todo = """
class FilesForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField()
who_are_you = forms.CharField(
widget=forms.TextInput(
attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Wookey' or 'Animal <mta@gasthof.expo>'",
"style": "vertical-align: text-top;"}
)
)
class FilesRenameForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField()
renameto = forms.CharField(strip=True, required=False)
@@ -703,12 +709,16 @@ def dwgupload(request, folder=None, gitdisable="no"):
urlfile = Path("/dwgdataraw/") / folder
urldir = Path("/dwgupload/") / folder
editor = get_cookie(request)
form = FilesForm()
if request.method == "POST":
form = FilesForm(request.POST, request.FILES)
if form.is_valid():
# print(f'! - FORM dwgupload - POST valid: "{request.FILES["uploadfiles"]}" ')
editor = form.cleaned_data["who_are_you"]
editor = git_string(editor)
f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist("uploadfiles")
savepath = Path(settings.DRAWINGS_DATA, folder)
@@ -725,6 +735,12 @@ def dwgupload(request, folder=None, gitdisable="no"):
git = "echo"
# print(f'git DISABLED {f.name}')
# For saving, and then comitting, multiple files, we should be using write_and_commit()
#
# try:
# write_and_commit([(filepath, newtext, "utf-8")], f"Online edit of {path}", editor)
# except WriteAndCommitError as e:
# return render(request, "errors/generic.html", {"message": e.message})
if multiple:
for f in multiple:
# print(f'! - FORM dwgupload - file {f} in {multiple=}')
@@ -736,36 +752,40 @@ def dwgupload(request, folder=None, gitdisable="no"):
f'! - FORM dwgupload - \n!! Permissions failure ?! on attempting to save file "{f.name}" in "{savepath}". Attempting to continue..'
)
if "saved_filename" in locals():
if Path(dirpath, saved_filename).is_file():
filepath = dirpath / saved_filename
if filepath.is_file():
actual_saved.append(saved_filename)
if gitdisable != "yes":
dr_add = subprocess.run(
[git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True
)
msgdata = (
dr_add.stderr
+ "\n"
+ dr_add.stdout
+ "\nreturn code: "
+ str(dr_add.returncode)
)
# message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata
commands = git_add(filepath, dirpath)
# dr_add = subprocess.run(
# [git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True
# )
# msgdata = (
# dr_add.stderr
# + "\n"
# + dr_add.stdout
# + "\nreturn code: "
# + str(dr_add.returncode)
# )
# # message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata
# # print(message)
# if dr_add.returncode != 0:
# msgdata = (
# "Ask a nerd to fix this.\n\n"
# + dr_add.stderr
# + "\n\n"
# + dr_add.stdout
# + "\n\nreturn code: "
# + str(dr_add.returncode)
# )
# message = (
# f"! - FORM dwgupload - CANNOT git ADD on server for this file {saved_filename}. Edits saved but not added to git.\n"
# + msgdata
# )
# print(message)
if dr_add.returncode != 0:
msgdata = (
"Ask a nerd to fix this.\n\n"
+ dr_add.stderr
+ "\n\n"
+ dr_add.stdout
+ "\n\nreturn code: "
+ str(dr_add.returncode)
)
message = (
f"! - FORM dwgupload - CANNOT git ADD on server for this file {saved_filename}. Edits saved but not added to git.\n"
+ msgdata
)
print(message)
return render(request, "errors/generic.html", {"message": message})
# return render(request, "errors/generic.html", {"message": message})
dwgfile, created = DrawingFile.objects.get_or_create(
dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size
)
@@ -824,6 +844,7 @@ def dwgupload(request, folder=None, gitdisable="no"):
message = f"! - FORM dwgupload - Nothing actually saved. All were refused. {actual_saved=}"
print(message)
# GET request starts here
files = []
dirs = []
# print(f'! - FORM dwgupload - start {folder=} \n"{dirpath=}" \n"{dirpath.parent=}" \n"{dirpath.exists()=}"')
@@ -859,5 +880,6 @@ def dwgupload(request, folder=None, gitdisable="no"):
"filesaved": filesaved,
"actual_saved": actual_saved,
"refused": refused,
"who_are_you": editor,
},
)

View File

@@ -9,7 +9,14 @@
<div style = "max-width:35%; margin-left:20%; text-align: center; " >
<form method ='post' enctype ="multipart/form-data">
{% csrf_token %}
<br>
<br /><br /> <br /><br />
<label for="who_are_you">Who are you, uploading these files?</label>
<br /><br />
<input {% if not user.username %} disabled{% endif %}
label = "Who are you" name = "who_are_you" size ="70"
title="Who are you"
placeholder="editor's name for version control e.g. 'Animal <mta@gasthof.expo>'" value="{{who_are_you}}" required/>
<br /><br /> <br>
<input class="fancybutton" type = "file" multiple="multiple"
name = "uploadfiles" id="uploadfiles" />
<br><br><br>