2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2026-03-22 22:32:19 +00:00

Entrances now do cookie / git author thing. + tidyup.

This commit is contained in:
2024-12-28 23:49:26 +00:00
parent e7444d20a4
commit dac3e6e288
7 changed files with 43 additions and 24 deletions

View File

@@ -164,11 +164,11 @@ class EntranceForm(ModelForm):
)
explorers = forms.CharField(required=False, widget=forms.TextInput(attrs={"size": "45"}))
# explorers = forms.CharField(required = False, widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
map_description = forms.CharField(
label="Map (is this used?)",
required=False,
widget=HTMLarea(attrs={"height": "80%", "rows": 20, "placeholder": "Enter text (using HTML)"}),
)
# map_description = forms.CharField(
# label="Map (is this used?)",
# required=False,
# widget=HTMLarea(attrs={"height": "80%", "rows": 20, "placeholder": "Enter text (using HTML)"}),
# )
location_description = forms.CharField(
label="Location",
required=False,
@@ -232,13 +232,22 @@ class EntranceForm(ModelForm):
)
alt = forms.CharField(required=False, label="Altitude (m) - from GPS if you have it, but let it settle.")
# url = forms.CharField(required=False, label="URL [usually blank]", widget=forms.TextInput(attrs={"size": "45"}))
field_order = ['name', 'entrance_description', 'explorers', 'map_description', 'location_description', 'lastvisit', 'approach', 'underground_description', 'photo', 'marking_comment', 'findability_description', 'other_description', 'bearings', 'tag_station', 'other_station', 'easting', 'northing', 'lat_wgs84', 'long_wgs84', 'alt']
who_are_you = forms.CharField(
widget=forms.TextInput(
attrs={"size": 100, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal <mta@gasthof.expo>'",
"style": "vertical-align: text-top;"}
)
)
field_order = ['name', 'entrance_description', 'explorers', 'map_description', 'location_description', 'lastvisit', 'non_public',
'findability', 'marking', 'approach', 'underground_description', 'photo', 'marking_comment', 'findability_description', 'other_description',
'bearings', 'tag_station', 'other_station', 'easting', 'northing', 'lat_wgs84', 'long_wgs84', 'alt', 'who_are_you']
class Meta:
model = Entrance
exclude = (
"cached_primary_slug",
"map_description", # No entrance has any data on this field, so it is being retired.
"filename",
"slug",
"bearings"
@@ -259,8 +268,10 @@ class EntranceForm(ModelForm):
class EntranceLetterForm(ModelForm):
"""Form to link entrances to caves, along with an entrance number.
Nb. The relationship between caves and entrances has historically been a many to many relationship.
NOTE. The relationship between caves and entrances was originally designed to be a many to many relationship.
With entrances gaining new caves and letters when caves are joined.
However, so far as I can see, this was never actually done in practice on Expo and each Entrance belongs
to only one Cave.
see https://docs.djangoproject.com/en/5.1/topics/forms/modelforms/
"""

View File

@@ -254,7 +254,7 @@ class Entrance(TroggleModel):
lat_wgs84 = models.TextField(blank=True, null=True) # manually entered not calculated
location_description = models.TextField(blank=True, null=True)
long_wgs84 = models.TextField(blank=True, null=True) # manually entered not calculated
# map_description = models.TextField(blank=True, null=True)
# map_description = models.TextField(blank=True, null=True) # retired
marking = models.CharField(max_length=2, choices=MARKING_CHOICES, default="?")
marking_comment = models.TextField(blank=True, null=True)
name = models.CharField(max_length=100, blank=True, null=True)

View File

@@ -218,7 +218,9 @@ def git_string(author_string):
return author_string
else:
editor = author_string.replace("@","_at_")
editor = re.sub('[^0-9a-zA-Z_\.]+', '*', editor)
editor = re.sub('[^0-9a-zA-Z_\.]+', '_', editor)
if editor.startswith("_"):
editor = "X" + editor
editor += f" <{editor}@potatohut.expo>"
print(f"++ Not git-compatible author string '{author_string}', replacing as '{editor}'")
return editor

View File

@@ -484,6 +484,7 @@ def edit_cave(request, path="", slug=None):
if form.is_valid():
print(f'edit_cave(): POST is valid. Editing {cave}')
editor = form.cleaned_data["who_are_you"]
editor = git_string(editor)
cave = form.save(commit=False)
# print(cave)
if not cave.filename:
@@ -645,10 +646,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
imgpath = Path(path) / cave.areacode / cave.number()
print(f"Edit Entrance {imgpath=}")
print(f"Reading cookie...")
editor_id = request.COOKIES.get('editor_id', 'Hohlenforscher <hohlenforscher@stonebridge.expo>') # if no cookie, then default string
editor = git_string(editor_id) # belt and braces, should have been validity checked on saving already
print(f"Cookie read: {editor_id=} reformatted as: {editor=}")
editor = get_cookie(request)
if request.POST:
print(f"POST Online edit of entrance: '{entrance}' where {cave=}")
@@ -682,8 +680,9 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
return render(request, "errors/generic.html", {"message": message})
else:
print(f"- POST {caveslug=} {entslug=} {entranceletter=} {path=}")
print(f"'edit_entrance(): POST is valid {caveslug=} Editing {entslug=} {entranceletter=} {path=}")
editor = entform.cleaned_data["who_are_you"]
editor = git_string(editor)
if entslug is None:
# we are creating a new entrance
entrance = entform.save(commit=False)
@@ -731,14 +730,17 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
print(f"- POST WRITE letter: '{ce}' {entrance=}")
try:
write_and_commit([entrance_file, cave_file], f"Online edit of entrance {entrance.slug}", editor)
return HttpResponseRedirect("/" + cave.url)
edit_response = HttpResponseRedirect("/" + cave.url)
edit_response.set_cookie('editor_id', editor, max_age=COOKIE_MAX_AGE) # cookie expires after COOKIE_MAX_AGE seconds
return edit_response
except Exception as e:
efilepath, econtent, eencoding = entrance_file
cfilepath, ccontent, cencoding = cave_file
message = f"- FAIL write_and_commit \n entr:'{efilepath}'\n cave:'{cfilepath}'\n\n{e}"
print(message)
return render(request, "errors/generic.html", {"message": message})
# Unlike other similar forms, we do NOT drop into this GET code after processing the POST
else: # GET the page, not POST, or if either of the forms were invalid when POSTed
entletterform = None
entletter = ""
@@ -753,7 +755,7 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
# ent only in db not on file. Interesting, let's run with it using whatever we have in the db
print(f"ENTRANCE NOT read from file: entranceletter = '{ce.entranceletter}'")
entform = EntranceForm(instance=entrance)
entform = EntranceForm(instance=entrance, initial={"who_are_you":editor})
if entslug is None:
entletterform = EntranceLetterForm()
# print(f" Getting entletter from EntranceLetterForm")
@@ -764,9 +766,9 @@ def edit_entrance(request, path="", caveslug=None, entslug=None):
print(f" Blank value: getting entletter from EntranceLetterForm")
print(f"{entletter=} ")
else:
entform = EntranceForm()
entform = EntranceForm(initial={"who_are_you":editor})
entletterform = EntranceLetterForm()
return render(
request,
"editentrance.html",

View File

@@ -457,6 +457,7 @@ def editexpopage(request, path):
pageform = ExpoPageForm(request.POST) # A form bound to the POST data
if pageform.is_valid(): # Form valid therefore write file
editor = pageform.cleaned_data["who_are_you"]
editor = git_string(editor)
# print("### \n", str(pageform)[0:300])
# print("### \n csrfmiddlewaretoken: ",request.POST['csrfmiddlewaretoken'])
if filefound:
@@ -489,6 +490,7 @@ def editexpopage(request, path):
try:
change_message = pageform.cleaned_data["change_message"]
editor = pageform.cleaned_data["who_are_you"]
editor = git_string(editor)
write_and_commit([(filepath, result, "utf-8")], f"{change_message} - online edit of {path}", editor)
except WriteAndCommitError as e:
return render(request, "errors/generic.html", {"message": e.message})

View File

@@ -324,8 +324,9 @@ def svx(request, survex_file):
if request.method == "POST": # If the form has been submitted...
rform = SvxForm(request.POST) #
if rform.is_valid(): # All validation rules pass (how do we check it against the filename and users?)
if rform.is_valid(): # All Django syntax validation rules pass (how do we check it against a valid filename and users?)
editor = rform.cleaned_data["who_are_you"]
editor = git_string(editor)
rcode = rform.cleaned_data["code"]
outputtype = rform.cleaned_data["outputtype"] # used by CodeMirror ajax I think
difflist = form.DiffCode(rcode)
@@ -352,9 +353,8 @@ def svx(request, survex_file):
if "save" in rform.data:
if request.user.is_authenticated:
if difflist:
editor = rform.cleaned_data["who_are_you"]
print(f"Saving code and editor id {editor=}")
message = form.SaveCode(rcode, editor)
message = form.SaveCode(rcode, editor) # saves file and does git thing
else:
message = "NO DIFFERENCES - so not saving the file"
else:

View File

@@ -11,7 +11,7 @@
<p>
<table>
<theader>
<tr><th>Entrance</th><th>Caves</th><th>Point</th><th>Tag Position</th><th>Other</th></tr>
<tr><th>Entrance</th><th>Caves</th><th>Point</th><th>Tag Position</th><th>Other survey point</th><th>Map</th></tr>
</theader>
<tbody>
<ul>
@@ -29,6 +29,8 @@
<td>{% if entrance.other %}
<a href="https://www.openstreetmap.org/?mlat={{ entrance.other.latlong.0|floatformat:7}}&mlon={{entrance.other.latlong.1|floatformat:7}}">{{ entrance.other.latlong.0|floatformat:5}} N, {{ entrance.other.latlong.1|floatformat:5 }} E</a>
{% endif %}</td>
<td>{{entrance.map_description}}</td>
</tr>
{% endfor %}
</tbody>