2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-21 23:01:52 +00:00

initial text file editing stuff

This commit is contained in:
Philip Sargent 2024-07-03 11:48:38 +03:00
parent 704ff8335d
commit cb81a066db
4 changed files with 94 additions and 3 deletions

View File

@ -16,12 +16,15 @@ import troggle.settings as settings
from troggle.core.models.caves import Cave
from troggle.core.utils import WriteAndCommitError, write_and_commit
from troggle.core.views.editor_helpers import HTMLarea
from troggle.core.views.uploads import edittxtpage
from .auth import login_required_if_public
"""Formerly a separate package called 'flatpages' written by Martin Green 2011.
This was NOT django.contrib.flatpages which stores HTML in the database, so the name was changed to expopages.
Then it was incorporated into troggle directly, rather than being an unnecessary external package.
This is a succession of hacks and needs to be redisgned and refactored.
"""
default_head = """<head>
@ -356,7 +359,7 @@ def getmimetype(path):
return "application/zip"
return ""
@login_required_if_public
@ensure_csrf_cookie
def editexpopage(request, path):
@ -377,6 +380,12 @@ def editexpopage(request, path):
+ "<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body"
)
# detect if this is a text file for editing
filepath = Path(settings.EXPOWEB) / path
if filepath.suffix == ".txt":
print("Editing TEXT file", filepath)
return edittxtpage(request, path, filepath)
try:
filepath = Path(settings.EXPOWEB) / path
o = open(filepath, "r", encoding="utf8")

View File

@ -146,9 +146,45 @@ class TextForm(forms.Form): # not a model-form, just a form-form
class ExpofileRenameForm(forms.Form): # not a model-form, just a form-form
renameto = forms.CharField(strip=True, required=False)
class ExpotextfileForm(forms.Form): # not a model-form, just a form-form
text = forms.CharField(strip=True, required=False)
class LogbookEditForm(forms.Form): # not a model-form, just a form-form
author = forms.CharField(strip=True, required=False)
def edittxtpage(request, path, filepath):
"""Editing a .txt file on expoweb/
"""
def simple_get():
form = ExpotextfileForm()
return render(
request,
"textfileform.html",
{
"form": form,
"path": path,
"filepath": filepath,
"text": text,
},
)
if not filepath.is_file():
print(f"Not a file: {filepath}")
errpage = f"<html>" + default_head + f"<h3>File not found '{filepath}'<br><br>failure detected in expowebpage() in views.expo.py</h3> </body>"
return HttpResponse(errpage)
try:
with open(filepath) as f:
text = f.read()
except IOError:
print("### File reading failue, but it exists.. ### ", filepath)
filefound = False
if request.method == "GET":
return simple_get()
elif request.method == "POST":
pass
@login_required_if_public
def logbookedit(request, year=None, slug=None):
"""Edit a logbook entry

View File

@ -855,7 +855,7 @@ def read_cave(filename, mvf=None, cave=None):
# we have already checked for uniqueness but the Cave object may/should be already created by the Entrance parsing
manual_edit = False
# The Cave object might be known by another (alias) name
# The Cave object should already have been created when reading the entrance_data file
caves = Cave.objects.filter(filename=filename)
if len(caves) ==1:
cave = caves[0]

View File

@ -0,0 +1,46 @@
{% extends "base.html" %}
{% block title %}Online text file editor{% endblock %}
{% block content %}
<h2>Editing text file (not fully working yet)</h2>
<style>
input, textarea, pre {font-family: monospace; font-weight: bold; text-align:left; font-size: 100%; padding: 0.5em; }
textarea {text-align:left }
pre {text-align:left; font-size: 110% }
</style>
<div style = "max-width:95%; margin-left:4%; font-family: monospace; font-weight: bold; font-size: 110%; text-align: left; " >
File: {{path}} <br>
Full path on server: {{filepath}}
<form method ='post' >
{% csrf_token %}
<br />
<textarea {% if not user.username %} disabled{% endif %}
rows="{% if textrows%}{{textrows}}{% else %}20{% endif %}" cols="100"
label = "" name = "text"
required />{% if text %}{{text}}{% else %}We had a lot of fun...{% endif %}
</textarea>
<br>
[Edit the text by just typing in the box.]
<button class="fancybutton2" style="padding: 0.5em 25px; margin-left: 110px" name="Save" type = "submit"
title="Saves the file in UTF-8 characterset" value = "Save" >
Save the file
</button>
</form>
<br />
</div>
<br />
<hr />
{% endblock %}