Move exception handling to calling View

This commit is contained in:
Philip Sargent 2021-12-30 19:28:33 +00:00
parent 5bad82b4f0
commit 84e165b8fc
2 changed files with 26 additions and 16 deletions

View File

@ -45,19 +45,13 @@ def writetrogglefile(filepath, filecontent):
filename = filepath.name filename = filepath.name
git = settings.GIT git = settings.GIT
try: # do not trap exceptions, pass them up to the view that called this function
with open(filepath, "w") as f: with open(filepath, "w") as f:
os.chmod(filepath, 0o664) # set file permissions to rw-rw-r-- os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.write(filecontent) f.write(filecontent)
print(f'WRITING{cwd}---{filename} ') print(f'WRITING{cwd}---{filename} ')
call([git, "add", filename], cwd=cwd) call([git, "add", filename], cwd=cwd)
call([git, "commit", "-m", 'Online cave or entrance edit'], cwd=cwd) call([git, "commit", "-m", 'Online cave or entrance edit'], cwd=cwd)
except PermissionError:
message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {filename}. Ask a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
except:
message = f'CANNOT git on server for this file {filename}. Edits not saved.\nAsk a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
class Area(TroggleModel): class Area(TroggleModel):

View File

@ -359,7 +359,15 @@ def edit_cave(request, slug=None):
for ceinst in ceinsts: for ceinst in ceinsts:
ceinst.cave = cave ceinst.cave = cave
ceinst.save() ceinst.save()
try:
cave.writeDataFile() cave.writeDataFile()
except PermissionError:
message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {cave.filename}. Ask a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
except:
message = f'CANNOT git on server for this file {cave.filename}. Edits not saved.\nAsk a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
return HttpResponseRedirect("/" + cave.url) return HttpResponseRedirect("/" + cave.url)
else: else:
message = f'! POST data is INVALID {cave}' message = f'! POST data is INVALID {cave}'
@ -420,7 +428,15 @@ def edit_entrance(request, caveslug=None, slug=None):
el.cave = cave el.cave = cave
el.entrance = entrance el.entrance = entrance
el.save() el.save()
try:
entrance.writeDataFile() entrance.writeDataFile()
except PermissionError:
message = f'CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {entrance.filename}. Ask a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
except:
message = f'CANNOT git on server for this file {entrance.filename}. Edits not saved.\nAsk a nerd to fix this.'
return render(request,'errors/generic.html', {'message': message})
return HttpResponseRedirect("/" + cave.url) return HttpResponseRedirect("/" + cave.url)
else: else:
message = f'! POST data is INVALID {cave}' message = f'! POST data is INVALID {cave}'