2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-17 18:17:07 +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, alphabet_suffix,
current_expo, current_expo,
get_cookie, get_cookie,
git_add,
git_string, git_string,
sanitize_name, sanitize_name,
unique_slug, unique_slug,
@@ -53,7 +54,12 @@ todo = """
class FilesForm(forms.Form): # not a model-form, just a form-form class FilesForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField() 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 class FilesRenameForm(forms.Form): # not a model-form, just a form-form
uploadfiles = forms.FileField() uploadfiles = forms.FileField()
renameto = forms.CharField(strip=True, required=False) renameto = forms.CharField(strip=True, required=False)
@@ -703,12 +709,16 @@ def dwgupload(request, folder=None, gitdisable="no"):
urlfile = Path("/dwgdataraw/") / folder urlfile = Path("/dwgdataraw/") / folder
urldir = Path("/dwgupload/") / folder urldir = Path("/dwgupload/") / folder
editor = get_cookie(request)
form = FilesForm() form = FilesForm()
if request.method == "POST": if request.method == "POST":
form = FilesForm(request.POST, request.FILES) form = FilesForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
# print(f'! - FORM dwgupload - POST valid: "{request.FILES["uploadfiles"]}" ') # print(f'! - FORM dwgupload - POST valid: "{request.FILES["uploadfiles"]}" ')
editor = form.cleaned_data["who_are_you"]
editor = git_string(editor)
f = request.FILES["uploadfiles"] f = request.FILES["uploadfiles"]
multiple = request.FILES.getlist("uploadfiles") multiple = request.FILES.getlist("uploadfiles")
savepath = Path(settings.DRAWINGS_DATA, folder) savepath = Path(settings.DRAWINGS_DATA, folder)
@@ -725,6 +735,12 @@ def dwgupload(request, folder=None, gitdisable="no"):
git = "echo" git = "echo"
# print(f'git DISABLED {f.name}') # 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: if multiple:
for f in multiple: for f in multiple:
# print(f'! - FORM dwgupload - file {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..' f'! - FORM dwgupload - \n!! Permissions failure ?! on attempting to save file "{f.name}" in "{savepath}". Attempting to continue..'
) )
if "saved_filename" in locals(): 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) actual_saved.append(saved_filename)
if gitdisable != "yes": if gitdisable != "yes":
dr_add = subprocess.run( commands = git_add(filepath, dirpath)
[git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True # dr_add = subprocess.run(
) # [git, "add", saved_filename], cwd=dirpath, capture_output=True, text=True
msgdata = ( # )
dr_add.stderr # msgdata = (
+ "\n" # dr_add.stderr
+ dr_add.stdout # + "\n"
+ "\nreturn code: " # + dr_add.stdout
+ str(dr_add.returncode) # + "\nreturn code: "
) # + str(dr_add.returncode)
# message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata # )
# print(message) # # message = f'! - FORM dwgupload - Success: git ADD on server for this file {saved_filename}.' + msgdata
if dr_add.returncode != 0: # # print(message)
msgdata = ( # if dr_add.returncode != 0:
"Ask a nerd to fix this.\n\n" # msgdata = (
+ dr_add.stderr # "Ask a nerd to fix this.\n\n"
+ "\n\n" # + dr_add.stderr
+ dr_add.stdout # + "\n\n"
+ "\n\nreturn code: " # + dr_add.stdout
+ str(dr_add.returncode) # + "\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" # message = (
+ msgdata # 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}) # print(message)
# return render(request, "errors/generic.html", {"message": message})
dwgfile, created = DrawingFile.objects.get_or_create( dwgfile, created = DrawingFile.objects.get_or_create(
dwgpath=saved_filename, dwgname=Path(f.name).stem, filesize=f.size 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=}" message = f"! - FORM dwgupload - Nothing actually saved. All were refused. {actual_saved=}"
print(message) print(message)
# GET request starts here
files = [] files = []
dirs = [] dirs = []
# print(f'! - FORM dwgupload - start {folder=} \n"{dirpath=}" \n"{dirpath.parent=}" \n"{dirpath.exists()=}"') # 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, "filesaved": filesaved,
"actual_saved": actual_saved, "actual_saved": actual_saved,
"refused": refused, "refused": refused,
"who_are_you": editor,
}, },
) )

View File

@@ -8,8 +8,15 @@
<p style="font-family: monospace; font-weight: bold; font-size: 130%; text-align: center"> <p style="font-family: monospace; font-weight: bold; font-size: 130%; text-align: center">
<div style = "max-width:35%; margin-left:20%; text-align: center; " > <div style = "max-width:35%; margin-left:20%; text-align: center; " >
<form method ='post' enctype ="multipart/form-data"> <form method ='post' enctype ="multipart/form-data">
{% csrf_token %} {% 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" <input class="fancybutton" type = "file" multiple="multiple"
name = "uploadfiles" id="uploadfiles" /> name = "uploadfiles" id="uploadfiles" />
<br><br><br> <br><br><br>