mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2026-03-31 12:46:03 +01:00
work in progress, now uploads several GPX files..
This commit is contained in:
@@ -83,7 +83,7 @@ class GPXuploadForm(forms.Form): # not a model-form, just a form-form
|
|||||||
"""not a git repo so we do not need an "editor" to assign blame to"""
|
"""not a git repo so we do not need an "editor" to assign blame to"""
|
||||||
prospector = forms.CharField(strip=True)
|
prospector = forms.CharField(strip=True)
|
||||||
|
|
||||||
class gpxfixForm(forms.Form): # not a model-form, just a form-form
|
class GPXfixForm(forms.Form): # not a model-form, just a form-form
|
||||||
"""not a git repo so we do not need an "editor" to assign blame to"""
|
"""not a git repo so we do not need an "editor" to assign blame to"""
|
||||||
prospector = forms.CharField(strip=True)
|
prospector = forms.CharField(strip=True)
|
||||||
areacode = forms.CharField(strip=True)
|
areacode = forms.CharField(strip=True)
|
||||||
@@ -418,9 +418,9 @@ def gpxupload(request, folder=None):
|
|||||||
folder is the "path"
|
folder is the "path"
|
||||||
"""
|
"""
|
||||||
def gpxvalid(name):
|
def gpxvalid(name):
|
||||||
if Path(name).suffix.lower() in [".xml", ".gpx"]:
|
# dangerous, we should check the actual file binary signature
|
||||||
return True # dangerous, we should check the actual file binary signature
|
return Path(name).suffix.lower() in [".xml", ".gpx"]
|
||||||
return False
|
|
||||||
|
|
||||||
print(f"gpxupload() {folder=}")
|
print(f"gpxupload() {folder=}")
|
||||||
year = current_expo()
|
year = current_expo()
|
||||||
@@ -451,7 +451,7 @@ def gpxupload(request, folder=None):
|
|||||||
urldir = f"/gpxupload/{year}"
|
urldir = f"/gpxupload/{year}"
|
||||||
|
|
||||||
print(f"gpxupload() {folder=} {dirpath=} {urlfile=} {urldir=}")
|
print(f"gpxupload() {folder=} {dirpath=} {urlfile=} {urldir=}")
|
||||||
form = FilesRenameForm()
|
|
||||||
formd = GPXuploadForm()
|
formd = GPXuploadForm()
|
||||||
print(f"gpxupload() {form=} {formd=} ")
|
print(f"gpxupload() {form=} {formd=} ")
|
||||||
|
|
||||||
@@ -469,8 +469,8 @@ def gpxupload(request, folder=None):
|
|||||||
print(f"gpxupload() {newprospector=}")
|
print(f"gpxupload() {newprospector=}")
|
||||||
try:
|
try:
|
||||||
(yearpath / newprospector).mkdir(parents=True, exist_ok=True)
|
(yearpath / newprospector).mkdir(parents=True, exist_ok=True)
|
||||||
except:
|
except Exception as e:
|
||||||
message = f'\n !! Permissions failure ?! 0 attempting to mkdir "{(yearpath / newprospector)}"'
|
message = f'\n !! Permissions failure ?! 0 attempting to mkdir "{(yearpath / newprospector)}": {e}'
|
||||||
print(message)
|
print(message)
|
||||||
raise
|
raise
|
||||||
return render(request, "errors/generic.html", {"message": message})
|
return render(request, "errors/generic.html", {"message": message})
|
||||||
@@ -481,62 +481,31 @@ def gpxupload(request, folder=None):
|
|||||||
for i in request.FILES:
|
for i in request.FILES:
|
||||||
print(" ",i)
|
print(" ",i)
|
||||||
|
|
||||||
form = FilesRenameForm(request.POST, request.FILES)
|
if True:
|
||||||
print(f"gpxupload() is the FilesRenameForm valid? {form=}")
|
|
||||||
for i in form:
|
|
||||||
print(" ",i)
|
|
||||||
|
|
||||||
if not form.is_valid():
|
|
||||||
print(f"gpxupload() Form is not valid {form=}")
|
|
||||||
else:
|
|
||||||
print(f"gpxupload() about to look at request.FILES")
|
print(f"gpxupload() about to look at request.FILES")
|
||||||
f = request.FILES["uploadfiles"]
|
f = request.FILES["uploadfiles"]
|
||||||
multiple = request.FILES.getlist("uploadfiles")
|
multiple = request.FILES.getlist("uploadfiles")
|
||||||
# NO CHECK that the files being uploaded are image files
|
# NO CHECK that the files being uploaded are image files
|
||||||
fs = FileSystemStorage(dirpath)
|
fs = FileSystemStorage(dirpath)
|
||||||
|
|
||||||
renameto = sanitize_name(request.POST["renameto"])
|
|
||||||
|
|
||||||
actual_saved = []
|
actual_saved = []
|
||||||
if multiple:
|
if multiple:
|
||||||
if len(multiple) == 1:
|
|
||||||
if renameto != "":
|
for f in multiple:
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
if gpxvalid(f.name):
|
||||||
saved_filename = fs.save(renameto, content=f)
|
|
||||||
except:
|
|
||||||
print(
|
|
||||||
f'\n !! Permissions failure ?! 1 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
|
||||||
)
|
|
||||||
if "saved_filename" in locals():
|
|
||||||
if saved_filename.is_file():
|
|
||||||
actual_saved.append(saved_filename)
|
|
||||||
filesaved = True
|
|
||||||
else: # multiple is the uploaded content
|
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
||||||
saved_filename = fs.save(f.name, content=f)
|
saved_filename = fs.save(f.name, content=f)
|
||||||
except:
|
except:
|
||||||
print(
|
print(
|
||||||
f'\n !! Permissions failure ?! 2 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
f'\n !! Permissions failure ?! 3 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
||||||
)
|
)
|
||||||
if "saved_filename" in locals():
|
if "saved_filename" in locals():
|
||||||
if saved_filename.is_file():
|
if saved_filename.is_file():
|
||||||
actual_saved.append(saved_filename)
|
actual_saved.append(saved_filename)
|
||||||
filesaved = True
|
filesaved = True
|
||||||
else: # multiple is a list of content
|
else:
|
||||||
for f in multiple:
|
print(f"gpxupload(): not a GPX file {f.name=}")
|
||||||
if gpxvalid(f.name):
|
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
|
||||||
saved_filename = fs.save(f.name, content=f)
|
|
||||||
except:
|
|
||||||
print(
|
|
||||||
f'\n !! Permissions failure ?! 3 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
|
||||||
)
|
|
||||||
if "saved_filename" in locals():
|
|
||||||
if saved_filename.is_file():
|
|
||||||
actual_saved.append(saved_filename)
|
|
||||||
filesaved = True
|
|
||||||
else:
|
|
||||||
print(f"gpxupload(): not a GPX file {f.name=}")
|
|
||||||
|
|
||||||
print(f"gpxupload() drop through")
|
print(f"gpxupload() drop through")
|
||||||
files = []
|
files = []
|
||||||
@@ -574,6 +543,11 @@ def gpxupload(request, folder=None):
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def analyse_gpx(saved_filename, content):
|
||||||
|
"""For an uploaded GPX file, analyse it to get a *fix number
|
||||||
|
"""
|
||||||
|
print(f"analyse_gpx(): {saved_filename} -- {content.name} length: {len(content)} bytes")
|
||||||
|
|
||||||
@login_required_if_public
|
@login_required_if_public
|
||||||
def gpxfix(request):
|
def gpxfix(request):
|
||||||
"""Upload a GPX file containing a single track which is actually a single static point: for averaging
|
"""Upload a GPX file containing a single track which is actually a single static point: for averaging
|
||||||
@@ -585,7 +559,7 @@ def gpxfix(request):
|
|||||||
return True # dangerous, we should check the actual file binary signature
|
return True # dangerous, we should check the actual file binary signature
|
||||||
return False
|
return False
|
||||||
|
|
||||||
year = current_expo() # read thsi from the GPX file
|
year = current_expo() # read this from the GPX file in future!
|
||||||
filesaved = False
|
filesaved = False
|
||||||
actual_saved = []
|
actual_saved = []
|
||||||
|
|
||||||
@@ -601,27 +575,24 @@ def gpxfix(request):
|
|||||||
|
|
||||||
|
|
||||||
print(f"gpxfix() {folder=} {dirpath=} {urlfile=} {urldir=}")
|
print(f"gpxfix() {folder=} {dirpath=} {urlfile=} {urldir=}")
|
||||||
form = FilesRenameForm()
|
|
||||||
formd = gpxfixForm()
|
|
||||||
print(f"gpxfix() {form=} {formd=} ")
|
|
||||||
|
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
|
formd = GPXuploadForm(request.POST)
|
||||||
print(f"gpxfix() method=POST")
|
print(f"gpxfix() method=POST")
|
||||||
for i in request.POST:
|
for i in request.POST:
|
||||||
print(" ",i)
|
print(" ",i)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
(yearpath).mkdir(parents=True, exist_ok=True)
|
(yearpath).mkdir(parents=True, exist_ok=True)
|
||||||
except:
|
except:
|
||||||
message = f'\n !! Permissions failure ?! 0 attempting to mkdir "{(yearpath)}"'
|
message = f'\n !! Permissions failure ?! 0 attempting to mkdir "{(yearpath)}"'
|
||||||
print(message)
|
print(message)
|
||||||
raise
|
raise
|
||||||
return render(request, "errors/generic.html", {"message": message})
|
return render(request, "errors/generic.html", {"message": message})
|
||||||
|
|
||||||
if "prospector" in request.POST:
|
if "prospector" in request.POST:
|
||||||
print(f"gpxfix() {request.POST=}\n {request.POST['prospector']=}")
|
print(f"gpxfix() {request.POST=}\n {request.POST['prospector']=}")
|
||||||
formd = gpxfixForm(request.POST)
|
|
||||||
if formd.is_valid():
|
if formd.is_valid():
|
||||||
newprospector = sanitize_name(request.POST["prospector"])
|
newprospector = sanitize_name(request.POST["prospector"])
|
||||||
print(f"gpxfix() {newprospector=}")
|
print(f"gpxfix() {newprospector=}")
|
||||||
@@ -631,66 +602,40 @@ def gpxfix(request):
|
|||||||
for i in request.FILES:
|
for i in request.FILES:
|
||||||
print(" ",i)
|
print(" ",i)
|
||||||
|
|
||||||
form = FilesRenameForm(request.POST, request.FILES)
|
|
||||||
print(f"gpxfix() is the FilesRenameForm valid? {form=}")
|
if not formd.is_valid():
|
||||||
for i in form:
|
print(f"gpxfix() Form is not valid {formd=}")
|
||||||
print(" ",i)
|
else:
|
||||||
|
print(f"gpxfix() about to look at request.FILES")
|
||||||
if not form.is_valid():
|
#f = request.FILES["uploadfiles"]
|
||||||
print(f"gpxfix() Form is not valid {form=}")
|
multiple = request.FILES.getlist("uploadfiles")
|
||||||
else:
|
fs = FileSystemStorage(dirpath)
|
||||||
print(f"gpxfix() about to look at request.FILES")
|
|
||||||
f = request.FILES["uploadfiles"]
|
|
||||||
multiple = request.FILES.getlist("uploadfiles")
|
|
||||||
# NO CHECK that the files being uploaded are image files
|
|
||||||
fs = FileSystemStorage(dirpath)
|
|
||||||
|
|
||||||
renameto = sanitize_name(request.POST["renameto"])
|
|
||||||
|
|
||||||
actual_saved = []
|
actual_saved = []
|
||||||
if multiple:
|
location_data = []
|
||||||
if len(multiple) == 1:
|
if multiple:
|
||||||
if renameto != "":
|
for f in multiple:
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
if gpxvalid(f.name):
|
||||||
saved_filename = fs.save(renameto, content=f)
|
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
||||||
except:
|
saved_filename = fs.save(f.name, content=f)
|
||||||
print(
|
except:
|
||||||
f'\n !! Permissions failure ?! 1 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
print(
|
||||||
)
|
f'\n !! Permissions failure ?! on attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
||||||
if "saved_filename" in locals():
|
)
|
||||||
if saved_filename.is_file():
|
if "saved_filename" in locals():
|
||||||
actual_saved.append(saved_filename)
|
if saved_filename.is_file():
|
||||||
filesaved = True
|
actual_saved.append(saved_filename)
|
||||||
else: # multiple is the uploaded content
|
filesaved = True
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
location_data.append(analyse_gpx(saved_filename, f))
|
||||||
saved_filename = fs.save(f.name, content=f)
|
else:
|
||||||
except:
|
print(f"gpxfix(): not a GPX file {f.name=}")
|
||||||
print(
|
|
||||||
f'\n !! Permissions failure ?! 2 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
|
||||||
)
|
|
||||||
if "saved_filename" in locals():
|
|
||||||
if saved_filename.is_file():
|
|
||||||
actual_saved.append(saved_filename)
|
|
||||||
filesaved = True
|
|
||||||
else: # multiple is a list of content
|
|
||||||
for f in multiple:
|
|
||||||
if gpxvalid(f.name):
|
|
||||||
try: # crashes in Django os.chmod call if on WSL, but does save file!
|
|
||||||
saved_filename = fs.save(f.name, content=f)
|
|
||||||
except:
|
|
||||||
print(
|
|
||||||
f'\n !! Permissions failure ?! 3 attempting to save "{f.name}" in "{dirpath}" {renameto=}'
|
|
||||||
)
|
|
||||||
if "saved_filename" in locals():
|
|
||||||
if saved_filename.is_file():
|
|
||||||
actual_saved.append(saved_filename)
|
|
||||||
filesaved = True
|
|
||||||
else:
|
|
||||||
print(f"gpxfix(): not a GPX file {f.name=}")
|
|
||||||
|
|
||||||
print(f"gpxfix() drop through")
|
print(f"gpxfix() drop through")
|
||||||
files = []
|
files = []
|
||||||
dirs = []
|
dirs = []
|
||||||
|
formd = GPXfixForm()
|
||||||
|
print(f"gpxfix() {formd=} ")
|
||||||
try:
|
try:
|
||||||
for f in dirpath.iterdir():
|
for f in dirpath.iterdir():
|
||||||
if f.is_dir():
|
if f.is_dir():
|
||||||
@@ -712,7 +657,7 @@ def gpxfix(request):
|
|||||||
request,
|
request,
|
||||||
"gpxfixform.html",
|
"gpxfixform.html",
|
||||||
{
|
{
|
||||||
"form": form,
|
"form": formd,
|
||||||
**context,
|
**context,
|
||||||
"urlfile": urlfile,
|
"urlfile": urlfile,
|
||||||
"urldir": urldir,
|
"urldir": urldir,
|
||||||
|
|||||||
@@ -9,47 +9,40 @@
|
|||||||
|
|
||||||
<h2>Upload GPX file for a *fix</h2>
|
<h2>Upload GPX file for a *fix</h2>
|
||||||
|
|
||||||
<div style = "max-width:35%; margin-left:20%; text-align: center; " >
|
<div style = "max-width:35%; margin-left:20%; text-align: left" >
|
||||||
<form method ='post' enctype ="multipart/form-data">
|
<form method ='post' enctype ="multipart/form-data">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<br>
|
<br>
|
||||||
<input class="fancybutton2" type = "file"
|
<input class="fancybutton2" type = "file" multiple="multiple"
|
||||||
name = "uploadfiles" id="uploadfiles" />
|
name = "uploadfiles" id="uploadfiles" />
|
||||||
<br><br><br>
|
<br><br><br>
|
||||||
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
|
||||||
label = "Rename to" name = "renameto" id="renameto"
|
|
||||||
pattern="[A-Za-z][A-Za-z0-9_-\.]*"/>
|
|
||||||
<br />
|
|
||||||
<label
|
|
||||||
style="padding: 0.5em 25px; margin-left: 110px"
|
|
||||||
for="renameto">You can rename the file<br></label>
|
|
||||||
|
|
||||||
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
||||||
label = "prospector" name = "prospector" id="prospector"
|
label = "prospector" name = "prospector" id="prospector"
|
||||||
pattern="[A-Za-z]+"
|
pattern="[A-Za-z]+"
|
||||||
placeholder="Wookey" required />
|
placeholder="Wookey" required />
|
||||||
<label
|
<label
|
||||||
style="padding: 0.5em 25px; margin-left: 110px"
|
style="padding: 0.5em 25px; margin-left:20%;"
|
||||||
for="prospector">your name<br></label>
|
for="prospector">your name<br></label>
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
||||||
label = "areacode" name = "areacode" id="areacode"
|
label = "areacode" name = "areacode" id="areacode"
|
||||||
pattern="[A-Za-z]+"
|
pattern="16[0-9]+"
|
||||||
placeholder="1623" required />
|
placeholder="1623" required />
|
||||||
<label
|
<label
|
||||||
style="padding: 0.5em 25px; margin-left: 110px"
|
style="padding: 0.5em 25px; margin-left:20%;"
|
||||||
for="areacode" ><br>the area code for this station<br></label>
|
for="areacode" >area code for this station<br></label>
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
<input class="fancybutton2" style="padding: 0.5em 25px; margin-left: 125px"
|
||||||
label = "station" name = "station" id="station"
|
label = "station" name = "station" id="station"
|
||||||
pattern="[A-Za-z]+"
|
pattern="[A-Za-z][\-A-Za-z0-9]*"
|
||||||
placeholder="p2025-WW-01" required />
|
placeholder="p2049-BW-01" required />
|
||||||
<label
|
<label
|
||||||
style="padding: 0.5em 25px; margin-left: 110px"
|
style="padding: 0.5em 25px; margin-left:20%;"
|
||||||
for="station" >proposed station identifier<br></label>
|
for="station" >proposed station identifier<br></label>
|
||||||
|
|
||||||
<br><br><br>
|
<br><br><br>
|
||||||
|
|||||||
Reference in New Issue
Block a user