From 672c84e4264260a8489920dfd0cd44cfb98f6df1 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Mon, 11 May 2026 00:25:39 +0100 Subject: [PATCH] 1/3 done file saving --- core/views/new_hole.py | 96 +++++++++++++++++++++++++++++++---------- templates/new_hole.html | 2 +- 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/core/views/new_hole.py b/core/views/new_hole.py index 72143477..55d94cd5 100644 --- a/core/views/new_hole.py +++ b/core/views/new_hole.py @@ -1,11 +1,26 @@ import re +import subprocess + from django import forms from django.core.exceptions import ValidationError from django.utils.safestring import mark_safe from django.conf import settings from troggle.parsers.people import who_is_this -from core.position_utils import which_area # file-type import, not module type. +from troggle.core.utils import ( + get_cookie_max_age, + WriteAndCommitError, + add_commit, + current_expo, + get_editor, + git_commit, + git_string, + sanitize_name, + is_identified_user, + write_and_commit, +) + +from troggle.core.position_utils import which_area # file-type import, not module type. class NewHoleForm(forms.Form): """The validation on this form is a bit of a beast, sorry. @@ -289,10 +304,14 @@ class NewHoleForm(forms.Form): # Return the whitespace-stripped version to be saved return clean_text + + +from textwrap import dedent from django.shortcuts import render, redirect from django.contrib import messages def new_hole(request): + identified_login = is_identified_user(request.user) areatext = None if request.method == 'POST': form = NewHoleForm(request.POST, request.FILES) @@ -313,38 +332,71 @@ def new_hole(request): else: form = NewHoleForm() - return render(request, 'new_hole.html', {'form': form, "areatext": areatext}) + return render(request, 'new_hole.html', {'form': form, + "identified_login": identified_login, + "areatext": areatext}) def get_auto_file(): auto_gps_file = settings.SURVEX_DATA / "fixedpts/gps/auto.svx" if not auto_gps_file.exists(): auto_gps_file.parent.mkdir(parents=True, exist_ok=True) with open(auto_gps_file, "w") as auto: - auto.write("""; Auto-created GPS fixes from 2026 for new caves in BOTH 1623 and 1626 - *begin - *cs out UTM33 - *cs LONG-LAT - """) - return auto_gps_file + auto.write(dedent(""" ; Auto-created GPS fixes from 2026 for new caves in BOTH 1623 and 1626 + + *begin + *cs out UTM33 + *cs LONG-LAT + *end + """)) + + with open(auto_gps_file, "r") as auto: + old_content = auto.readlines() + + content_list = [line for line in old_content if "*end" not in line] + content = "".join(content_list) + print(content) + return auto_gps_file, content def process_new_hole(form, area): """ + ➜Detect if logged-in & set editor using the cookie system - Create a fixed point record by appending to :loser:/fixedpts/gps/auto.svx - Create a new Cave record with associated new Entrance record - Do a git commit (expoweb) of the new Cave and Entrance description files - Do a git commit (loser) of the new GPS position - (git commits use your logged-in identity in the "Who are you ?" field) - Update the database with this new information - + ✅ Create a fixed point record by inserting into :loser:/fixedpts/gps/auto.svx + ✅ Do a git commit (loser) of the new GPS position + ➜Create a new Cave description file + ➜Create an associated new Entrance description file + ➜Do a git commit (expoweb) of the new Cave and Entrance description files + ➜Update the database with this new Cave + ➜Update the database with this new Entrance + writes: + *fix 1623.g2025-bz-06 47.6964481 13.816103 0 + we do not put "reference" in the *fix because we know it is used in the Entrance we are creating + ❌BUT we do put reference in now because we haven't written the Entrance bit of the code yet! """ - auto_gps_file = get_auto_file() - fix_line = f"*fix {area}.g{form.cleaned_data.get("cave_id")} {form.cleaned_data.get("gps_lat")} {form.cleaned_data.get("gps_long")} 0.0" + auto_gps_file, content = get_auto_file() + fix_id = f"{area}.g{form.cleaned_data.get("cave_id").lower()}" + fix_line = f"*fix {fix_id} reference {form.cleaned_data.get("gps_lat")} {form.cleaned_data.get("gps_long")} 0\n" print(fix_line) - with open(auto_gps_file, "w") as auto: - auto.write(fix_line) - auto.write(f"*entrance {area}.g{form.cleaned_data.get("cave_id")}") - - + content += f"\n; {form.cleaned_data.get("discovery_date")} wallet: {form.cleaned_data.get("survey_wallet")} \n" + content += fix_line + content += f"*entrance {fix_id}\n" + content +=f"\n*end\n" + + editor = git_string(form.cleaned_data["who_are_you"]) + files = [(auto_gps_file, content, "utf8")] + + try: + write_and_commit(files, f"Online *fix {fix_id} ", editor) + except PermissionError: + message = f"CANNOT save this file.\nPERMISSIONS incorrectly set on server for this file {auto_gps_file}. Ask a nerd to fix this." + return render(request, "errors/generic.html", {"message": message}) + except WriteAndCommitError as e: + message = f"CANNOT git on server for this file {auto_gps_file}.\n{e}\nEdits may not be committed.\nAsk a nerd to fix this." + return render(request, "errors/generic.html", {"message": e.message}) + except subprocess.SubprocessError as e: + message = f"CANNOT update server for this file {auto_gps_file}.\n{e}\nEdits may not be committed.\nAsk a nerd to fix this." + return render(request, "errors/generic.html", {"message": message}) + except: + raise return \ No newline at end of file diff --git a/templates/new_hole.html b/templates/new_hole.html index 53df2a1d..efbdf1f7 100644 --- a/templates/new_hole.html +++ b/templates/new_hole.html @@ -134,7 +134,7 @@