From 3fb99eb4be5f1c3199bdc2bab3c81f3195bf8710 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Thu, 13 Feb 2025 15:10:12 +0000 Subject: lgoing/cookie interaction betetr --- core/utils.py | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) (limited to 'core/utils.py') diff --git a/core/utils.py b/core/utils.py index 5e03971..26be1a0 100644 --- a/core/utils.py +++ b/core/utils.py @@ -168,25 +168,6 @@ def current_expo(): else: return settings.EPOCH.year # this is 1970 -def is_identified_user(user): - if user.is_anonymous: - return False - if user.username in ["expo", "expoadmin"]: - return False - return True - -def get_git_string(user): - if not is_identified_user(user): - return None - else: - people = Person.objects.filter(user=user) - if len(people) != 1: - # someone like "fluffy-bunny" not associated with a Person - return None - person = people[0] - return f"{person.fullname} <{user.email}>" - - def parse_aliases(aliasfile): """Reads a long text string containing pairs of strings: (alias, target) @@ -233,14 +214,40 @@ def parse_aliases(aliasfile): return [(None, None)], "Fail on file reading" return aliases, report +def get_editor(request): + current_user = request.user + if is_identified_user(current_user): + return get_git_string(current_user) + else: + return get_cookie(request) + +def is_identified_user(user): + if user.is_anonymous: + return False + if user.username in ["expo", "expoadmin"]: + return False + return True + +def get_git_string(user): + if not is_identified_user(user): + return None + else: + people = Person.objects.filter(user=user) + if len(people) != 1: + # someone like "fluffy-bunny" not associated with a Person + return None + person = people[0] + return f"{person.fullname} <{user.email}>" + def get_cookie(request): """The initial idea of having a default turned out to be a bad idea as people just ignore the field. - if the default value is blank, then the form validation code makes the user type something in, - so having a blank is best. + if the default value is blank, then the form validation code makes the user type something in. + So having a blank is best if the cookie is to be displayed as the first value seen on a form. + But if the cookie is to be stored, then "Unset" may be better. """ # NO_COOKIE_DEFAULT = 'Unset Cookie ' # print(f"-- Getting cookie...") - editor_id = request.COOKIES.get('editor_id', "") # if no cookie, then default string "" + editor_id = request.COOKIES.get('editor_id', "") editor = git_string(editor_id) # belt and braces, should have been validity checked on saving already # print(f"-- Cookie to be used: {editor=}") return editor @@ -268,7 +275,6 @@ def git_string(author_string): print(f"++ Not git-compatible author string '{author_string}', replacing as '{editor}'") return editor - def git_add(filename, cwd, commands=[]): """Add a file to the list of Staged files ready for a later git commit """ -- cgit v1.2.3