2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2025-12-19 00:17:11 +00:00

lgoing/cookie interaction betetr

This commit is contained in:
2025-02-13 15:10:12 +00:00
parent 3a3e5765f9
commit 3fb99eb4be
4 changed files with 131 additions and 84 deletions

View File

@@ -17,8 +17,11 @@ from PIL import Image
import troggle.settings as settings
from troggle.core.utils import ( COOKIE_MAX_AGE,
WriteAndCommitError, get_cookie, git_string, write_binary_file,
write_and_commit, current_expo, random_slug, ensure_dir_exists
WriteAndCommitError, get_editor,
git_string,
write_binary_file, write_and_commit,
current_expo, random_slug, ensure_dir_exists,
is_identified_user
)
from .auth import login_required_if_public
@@ -115,47 +118,71 @@ def dms2dd(degrees, minutes, seconds, direction):
def extract_gps(dict):
"""Produce a set of annotations to add to an image description
The problem is that at any time one or more of the exif data points might
be missing from a particular photo, even GPSVersionID, so we need a lot
of data existence checking or it will crash.
"""
def is_present(gpsifd):
item = getattr(piexif.GPSIFD, gpsifd)
if item in dict:
print(f" {gpsifd} = {item}")
return dict[item]
return None
def extract(gpsifd):
if item:=is_present(gpsifd): # walrus
n, d = item
return n/d
return None
def rational(tup):
nom, denom = tup
return nom/denom
def extract(gpsifd):
print(piexif.GPSIFD)
n, d = dict[getattr(piexif.GPSIFD, gpsifd)]
return n/d
compass_points = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"]
bearing = extract("GPSImgDirection")
compass_lookup = round(bearing / 45)
nsew = compass_points[compass_lookup]
if dict[piexif.GPSIFD.GPSImgDirectionRef] == b"M":
ref = "Magnetic"
elif dict[piexif.GPSIFD.GPSImgDirectionRef] == b"T":
ref = "True"
direction = f"Direction of view: {nsew:2} ({bearing:.0f}°{ref})"
if bearing := extract("GPSImgDirection"):
compass_lookup = round(bearing / 45)
nsew = compass_points[compass_lookup]
if item := is_present("GPSImgDirectionRef"):
if item == b"M":
ref = "Magnetic"
elif item == b"T":
ref = "True"
else:
ref =""
direction = f"Direction of view: {nsew:2} ({bearing:.0f}°{ref})"
else:
direction = ""
print(f"{dict[piexif.GPSIFD.GPSVersionID]=}")
if version := is_present("GPSVersionID"):
print(f"GPS exif {version=}")
if alt := extract("GPSAltitude"):
altitude = f"{alt:.0f}m above sea-level"
else:
altitude = ""
alt = extract("GPSAltitude")
altitude = f"{alt:.0f}m above sea-level"
ds = dict[piexif.GPSIFD.GPSDateStamp]
hf, mf, sf = dict[piexif.GPSIFD.GPSTimeStamp]
h = rational(hf)
m = rational(mf)
s = rational(sf)
timestamp_utc = f"{ds.decode()} {h:02.0f}:{m:02.0f}:{s:02.0f} +00:00 UTC"
if ds := is_present("GPSDateStamp"):
ds = ds.decode()
else:
ds = ""
if item := is_present("GPSTimeStamp"):
hf, mf, sf = item
h = rational(hf)
m = rational(mf)
s = rational(sf)
timestamp_utc = f"{ds} {h:02.0f}:{m:02.0f}:{s:02.0f} +00:00 UTC"
else:
timestamp_utc = f"{ds}"
print(direction)
print(altitude)
print(timestamp_utc)
# location = dms2dd()
# location = dms2dd() # to do...
return f"{direction}<br />{altitude}</br />{timestamp_utc}<br />"
@@ -176,12 +203,18 @@ def new_image_form(request, path):
directory = get_dir(path)
# print(f"new_image_form(): {directory=} {path=}")
editor = get_cookie(request)
identified_login = is_identified_user(request.user)
editor = get_editor(request)
# print(f"{django_settings.FILE_UPLOAD_MAX_MEMORY_SIZE=}")
# FILE_UPLOAD_MAX_MEMORY_SIZE = 0 # force uploaded files to be temporary in /tmp, not in-memory
if request.method == "POST":
# print(f"new_image_form(): POST ")
form = NewWebImageForm(request.POST, request.FILES, directory=directory)
form = NewWebImageForm(request.POST, request.FILES, directory=directory)
if identified_login:
# disable editing the git id string as we get it from the logged-on user data
form.fields["who_are_you"].widget.attrs["readonly"]="readonly"
print(form.fields["who_are_you"].widget.attrs)
if form.is_valid():
# print(f"new_image_form(): form is valid ")
year = form.cleaned_data["year"]
@@ -271,14 +304,15 @@ def new_image_form(request, path):
# print(full_path, full_path.parent)
full_path.parent.mkdir(parents=True, exist_ok=True)
try:
change_message = form.cleaned_data["change_message"]
# change_message = form.cleaned_data["change_message"]
write_and_commit(
[
(desc_path, image_page, "utf-8"),
(image_path, ib.getbuffer(), False),
(thumb_path, tb.getbuffer(), False),
],
f"{change_message} - online adding of an image",
# f"{change_message} - online adding of an image",
f"Online adding of an image",
editor # this works, a new who_are_you typed on the Image form is used as the git comment
)
except WriteAndCommitError as e:
@@ -302,6 +336,11 @@ def new_image_form(request, path):
"year": "", "photographer": extract_git_name(editor),
"change_message": "Uploading photo"}
form = NewWebImageForm(directory=directory, initial=initial )
if identified_login:
# disable editing the git id string as we get it from the logged-on user data
form.fields["who_are_you"].widget.attrs["readonly"]="readonly"
print(form.fields["who_are_you"].widget.attrs)
# print(f"new_image_form(): POST and not POST ")
template = loader.get_template("new_image_form.html")
htmlform = template.render({"form": form, "path": path}, request)
@@ -322,6 +361,8 @@ def save_original_in_expofiles(f, year, photographer):
get recorded properly in original format.
Django does small files <2.5 MB in memory, which is a pain.
to do: also store a *.url file with the image file saying where it is used in the handbook.
"""
if photographer:
photographer = photographer.strip().replace(" ","")
@@ -376,15 +417,18 @@ class NewWebImageForm(forms.Form):
year = forms.CharField(
widget=forms.TextInput(attrs={"size": "60", "placeholder": "Year photo was taken"}), required=False
)
change_message = forms.CharField(
widget=forms.Textarea(attrs={"cols": 80, "rows": 3, "placeholder": "Describe the change made (for git)"}), required=False
)
# change_message = forms.CharField(
# widget=forms.Textarea(attrs={"cols": 80, "rows": 3, "placeholder": "Describe the change made (for git)"}), required=False
# )
who_are_you = forms.CharField(
widget=forms.TextInput(
attrs={"size": 60, "placeholder": "You are editing this page, who are you ? e.g. 'Becka' or 'Animal <mta@gasthof.expo>'",
"style": "vertical-align: text-top;"}
widget=forms.TextInput(attrs={"style": "font-size: 90%", "size": "75",
"placeholder": "Anathema Device <anathema@potatohut.expo>",
"title":"Type in your real name, and your email between angle-brackets."
}),
# label = "Editor",
)
)
def __init__(self, *args, **kwargs):
self.directory = Path(kwargs.pop("directory"))