summaryrefslogtreecommitdiffstats
path: root/core/utils.py
diff options
context:
space:
mode:
authorPhilip Sargent <philip.sargent@klebos.com>2022-07-18 16:57:13 +0300
committerPhilip Sargent <philip.sargent@klebos.com>2022-07-18 16:57:13 +0300
commitee9b8084612e4f34246d0c67f83db0c3f1e0ea0d (patch)
treec1f11aa62edd0ab06912b9b4c6c51726e49afb23 /core/utils.py
parent8484f26ee9e9a1e6731ed02768d7ee686bdc016a (diff)
downloadtroggle-ee9b8084612e4f34246d0c67f83db0c3f1e0ea0d.tar.gz
troggle-ee9b8084612e4f34246d0c67f83db0c3f1e0ea0d.tar.bz2
troggle-ee9b8084612e4f34246d0c67f83db0c3f1e0ea0d.zip
moved writetrogglefile() to core.utils
Diffstat (limited to 'core/utils.py')
-rw-r--r--core/utils.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/core/utils.py b/core/utils.py
index 4d0de2f..269d719 100644
--- a/core/utils.py
+++ b/core/utils.py
@@ -80,6 +80,32 @@ def GetListDir(sdir):
return res
+def writetrogglefile(filepath, filecontent):
+ '''Set permissions to rw-rw-r-- and commit the new saved file to git
+ Callers to cave.writeDataFile() or entrance.writeDataFile() should handle the exception PermissionsError explicitly
+ '''
+ # GIT see also core/views/expo.py editexpopage()
+ # GIT see also core/views/uploads.py dwgupload()
+ filepath = Path(filepath)
+ cwd = filepath.parent
+ filename = filepath.name
+ git = settings.GIT
+
+ # as the wsgi process www-data, we have group write-access but are not owner, so cannot chmod.
+ # do not trap exceptions, pass them up to the view that called this function
+ print(f'WRITING{cwd}---{filename} ')
+ with open(filepath, "w") as f:
+ f.write(filecontent)
+ #os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
+ sp = subprocess.run([git, "add", filename], cwd=cwd, capture_output=True, check=True, text=True)
+ if sp.returncode != 0:
+ print(f'git ADD {cwd}:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
+
+ sp = subprocess.run([git, "commit", "-m", f'Troggle online: cave or entrance edit -{filename}'], cwd=cwd, capture_output=True, check=True, text=True)
+ if sp.returncode != 0:
+ print(f'git COMMIT {cwd}:\n\n' + str(sp.stderr) + '\n\n' + str(sp.stdout) + '\n\nreturn code: ' + str(sp.returncode))
+ # not catching and re-raising any exceptions yet, inc. the stderr etc.,. We should do that.
+
def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}):
"""Looks up instance using lookupAttribs and carries out the following: