add git commit to file saving in 3 places

This commit is contained in:
Philip Sargent 2021-12-30 19:07:17 +00:00
parent 26454bf6c6
commit d1e6125d15
2 changed files with 35 additions and 20 deletions

View File

@ -37,6 +37,28 @@ todo='''- Move utility function into utils.py
- Restore constraint: unique_together = (("area", "kataster_number"), ("area", "unofficial_number"))
'''
def writetrogglefile(filepath, filecontent):
'''Set permissions to rw-rw-r-- and commit the new saved file to git'''
filepath = Path(filepath)
cwd = filepath.parent
filename = filepath.name
git = settings.GIT
try:
with open(filepath, "w") as f:
os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.write(filecontent)
print(f'WRITING{cwd}---{filename} ')
call([git, "add", filename], 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):
short_name = models.CharField(max_length=100)
name = models.CharField(max_length=200, blank=True, null=True)
@ -202,19 +224,15 @@ class Cave(TroggleModel):
return res
def writeDataFile(self):
try:
f = open(os.path.join(settings.CAVEDESCRIPTIONS, self.filename), "w")
except:
subprocess.call(settings.FIX_PERMISSIONS)
f = open(os.path.join(settings.CAVEDESCRIPTIONS, self.filename), "w")
filepath = os.path.join(settings.CAVEDESCRIPTIONS, self.filename)
t = loader.get_template('dataformat/cave.xml')
#c = Context({'cave': self})
c = dict({'cave': self})
u = t.render(c)
# u8 = u.encode("utf-8")
# f.write(u8)
f.write(u)
f.close()
u = t.render(c)
writetrogglefile(filepath, u)
return
def getArea(self):
areas = self.area.all()
@ -382,18 +400,14 @@ class Entrance(TroggleModel):
return Path(settings.ENTRANCEDESCRIPTIONS, self.filename)
def writeDataFile(self):
try:
f = open(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename), "w")
except:
subprocess.call(settings.FIX_PERMISSIONS)
f = open(os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename), "w")
filepath = os.path.join(settings.ENTRANCEDESCRIPTIONS, self.filename)
t = loader.get_template('dataformat/entrance.xml')
c = dict({'entrance': self})
u = t.render(c)
# u8 = u.encode("utf-8")
# f.write(u8)
f.write(u)
f.close()
writetrogglefile(filepath, u)
return
class LogbookEntry(TroggleModel):
"""Single parsed entry from Logbook

View File

@ -312,8 +312,9 @@ def editexpopage(request, path):
git = settings.GIT
try:
with open(filepath, "w") as f:
os.chmod(filepath, 0o664) # set file permissions to rw-rw-r--
f.write(result)
#print(f'WROTE {cwd}---{filename} ')
print(f'WRITING{cwd}---{filename} ')
subprocess.call([git, "add", filename], cwd=cwd)
subprocess.call([git, "commit", "-m", 'Edit this page'], cwd=cwd)
except PermissionError: