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

Loads and resaves same file. Round trip working.

This commit is contained in:
2025-01-21 11:29:20 +00:00
parent 01afb09a6e
commit 65cdeb4994
2 changed files with 29 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ from django.core.exceptions import ValidationError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from troggle.core.models.troggle import DataIssue, Person from troggle.core.models.troggle import DataIssue, Person
from troggle.parsers.users import register_user from troggle.parsers.users import register_user, save_users
""" """
This is the new individual user login registration, instead of everyone signing This is the new individual user login registration, instead of everyone signing
@@ -32,8 +32,10 @@ def register(request, username=None):
if current_user != form_user: if current_user != form_user:
print(f"## UNAUTHORIZED Password reset ## {current_user} {form_user}") print(f"## UNAUTHORIZED Password reset ## {current_user} {form_user}")
return render(request, "login/register.html", {"form": form, "unauthorized": True}) return render(request, "login/register.html", {"form": form, "unauthorized": True})
# create User in the system and refresh stored encrypted user list and git commit it. # create User in the system and refresh stored encrypted user list and git commit it:
register_user(un, email, password=pw, pwhash=None) updated_user = register_user(un, email, password=pw, pwhash=None)
save_users()
# to do, login automatically, and redirect to control panel ?
return HttpResponseRedirect("/accounts/login/") return HttpResponseRedirect("/accounts/login/")
else: else:
if current_user: if current_user:

View File

@@ -22,7 +22,7 @@ todo = """
- [copy these from paper notes] - [copy these from paper notes]
""" """
USERS_FILE = "users_e.json" USERS_FILE = "users.json"
ENCRYPTED_DIR = "encrypted" ENCRYPTED_DIR = "encrypted"
def register_user(u, email, password=None, pwhash=None): def register_user(u, email, password=None, pwhash=None):
@@ -46,16 +46,22 @@ def register_user(u, email, password=None, pwhash=None):
except Exception as e: except Exception as e:
print(f"Exception <{e}>\n{len(User.objects.all())} users now in db:\n{User.objects.all()}") print(f"Exception <{e}>\n{len(User.objects.all())} users now in db:\n{User.objects.all()}")
raise raise
return user
def get_key():
key = settings.LONGTERM_SECRET_KEY # Django generated
k = base64.urlsafe_b64encode(key.encode("utf8")[:32]) # make Fernet compatible
f = Fernet(k)
return f
def load_users(): def load_users():
"""These are the previously registered users of the troggle system. """These are the previously registered users of the troggle system.
""" """
PARSER_USERS = "_users" PARSER_USERS = "_users"
DataIssue.objects.filter(parser=PARSER_USERS).delete() DataIssue.objects.filter(parser=PARSER_USERS).delete()
key = settings.LONGTERM_SECRET_KEY # Django generated f = get_key()
k = base64.urlsafe_b64encode(key.encode("utf8")[:32]) # make Fernet compatible
f = Fernet(k)
jsonfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE jsonfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE
jsonurl = "/" + str(Path(ENCRYPTED_DIR) / USERS_FILE) jsonurl = "/" + str(Path(ENCRYPTED_DIR) / USERS_FILE)
@@ -83,7 +89,12 @@ def load_users():
print(f" - {len(users_list)} users read from JSON") print(f" - {len(users_list)} users read from JSON")
for userdata in users_list: for userdata in users_list:
if userdata["username"]: if not userdata["username"]:
message = f"! user: BAD username for {userdata} in {jsonfile}"
print(message)
DataIssue.objects.update_or_create(parser=PARSER_USERS, message=message, url=jsonurl)
continue
else:
if userdata["username"] in [ "expo", "expoadmin" ]: if userdata["username"] in [ "expo", "expoadmin" ]:
continue continue
if "encrypted" not in userdata: if "encrypted" not in userdata:
@@ -102,14 +113,15 @@ def load_users():
return None return None
if "pwhash" in userdata: if "pwhash" in userdata:
pwhash = userdata["pwhash"] pwhash = userdata["pwhash"]
register_user(u, email, pwhash=pwhash) new_user = register_user(u, email, pwhash=pwhash)
else: else:
register_user(u, email) new_user = register_user(u, email)
else: save_users()
print(f" - user: BAD username for {userdata} ")
def save_users():
f = get_key()
ru = [] ru = []
print(f"\n + Exporting users, encrypted emails, and password hashes") print(f"\n + Saving users, encrypted emails, and password hashes")
for u in User.objects.all(): for u in User.objects.all():
if u.username in ["expo", "expoadmin"]: if u.username in ["expo", "expoadmin"]:
continue continue
@@ -120,7 +132,7 @@ def load_users():
print(f" - {u.username} - {original}") print(f" - {u.username} - {original}")
jsondict = { "registered_users": ru } jsondict = { "registered_users": ru }
encryptedfile = settings.EXPOWEB / ENCRYPTED_DIR / "encrypt.json" encryptedfile = settings.EXPOWEB / ENCRYPTED_DIR / USERS_FILE
if settings.DEVSERVER: if settings.DEVSERVER:
with open(encryptedfile, 'w', encoding='utf-8') as json_f: with open(encryptedfile, 'w', encoding='utf-8') as json_f:
json.dump(jsondict, json_f, indent=1) json.dump(jsondict, json_f, indent=1)