lists: add list delete/clear
This commit is contained in:
parent
7ba71b5eef
commit
8c95ab6fb5
65
lists
65
lists
|
@ -15,25 +15,18 @@ if not STORAGE_PATH.exists():
|
||||||
ALLOWED_LIST_NAME_CHARS = string.ascii_lowercase + string.digits + "_"
|
ALLOWED_LIST_NAME_CHARS = string.ascii_lowercase + string.digits + "_"
|
||||||
|
|
||||||
|
|
||||||
def get_user_dir():
|
|
||||||
user_dir = STORAGE_PATH / scgi.tls_client_hash[7:]
|
|
||||||
if not user_dir.is_dir():
|
|
||||||
user_dir.mkdir()
|
|
||||||
return user_dir
|
|
||||||
|
|
||||||
|
|
||||||
def show_lists():
|
def show_lists():
|
||||||
user_dir = get_user_dir()
|
user_dir = scgi.get_user_dir()
|
||||||
scgi.header(20, "text/gemini")
|
scgi.header(20, "text/gemini")
|
||||||
print("# Lists")
|
print("# Lists")
|
||||||
for child in user_dir.iterdir():
|
for child in user_dir.iterdir():
|
||||||
if child.is_file():
|
if child.is_file():
|
||||||
print(f"=> {scgi.script_name}/{child.name} 📄 {child.name}")
|
scgi.link(f"/{child.name}", f"📄 {child.name}")
|
||||||
print(f"=> {scgi.script_name}/_/create ✨ Create a new list")
|
scgi.link("/_/create", "✨ Create a new list")
|
||||||
|
|
||||||
|
|
||||||
def show_list(list_name):
|
def show_list(list_name):
|
||||||
list_file = get_user_dir() / list_name
|
list_file = scgi.get_user_dir() / list_name
|
||||||
try:
|
try:
|
||||||
with open(list_file, "rt") as f:
|
with open(list_file, "rt") as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
@ -43,17 +36,27 @@ def show_list(list_name):
|
||||||
scgi.header(20, "text/gemini")
|
scgi.header(20, "text/gemini")
|
||||||
print("# " + list_name)
|
print("# " + list_name)
|
||||||
print(content)
|
print(content)
|
||||||
print(f"=> {scgi.script_name}/{list_name}/add Append item")
|
scgi.link(f"/{list_name}/add", "Append item")
|
||||||
print(f"=> {scgi.script_name}/{list_name}/pop Pop first item")
|
scgi.link(f"/{list_name}/pop", "Pop first item")
|
||||||
|
scgi.link(f"/{list_name}/clear", "Clear list")
|
||||||
|
scgi.link(f"/{list_name}/delete", "Delete list")
|
||||||
|
|
||||||
|
|
||||||
def process_action(list_name, action):
|
def process_action(list_name, action):
|
||||||
if list_name == "_" and action == "create":
|
if list_name == "_":
|
||||||
|
if action == "create":
|
||||||
process_creation()
|
process_creation()
|
||||||
elif action == "add":
|
else:
|
||||||
|
scgi.not_found()
|
||||||
|
else:
|
||||||
|
if action == "add":
|
||||||
process_add(list_name)
|
process_add(list_name)
|
||||||
elif action == "pop":
|
elif action == "pop":
|
||||||
process_pop(list_name)
|
process_pop(list_name)
|
||||||
|
elif action == "clear":
|
||||||
|
process_clear(list_name)
|
||||||
|
elif action == "delete":
|
||||||
|
process_delete(list_name)
|
||||||
else:
|
else:
|
||||||
scgi.not_found()
|
scgi.not_found()
|
||||||
|
|
||||||
|
@ -66,7 +69,7 @@ def process_creation():
|
||||||
for char in new_list_name:
|
for char in new_list_name:
|
||||||
if char not in ALLOWED_LIST_NAME_CHARS:
|
if char not in ALLOWED_LIST_NAME_CHARS:
|
||||||
scgi.temp_error(f"Illegal character in list name: {char}")
|
scgi.temp_error(f"Illegal character in list name: {char}")
|
||||||
list_file = get_user_dir() / new_list_name
|
list_file = scgi.get_user_dir() / new_list_name
|
||||||
list_file.touch(mode=0o660)
|
list_file.touch(mode=0o660)
|
||||||
redirect_to_list(new_list_name)
|
redirect_to_list(new_list_name)
|
||||||
|
|
||||||
|
@ -78,7 +81,7 @@ def process_add(list_name):
|
||||||
if "\n" in item:
|
if "\n" in item:
|
||||||
scgi.temp_error("No new line allowed in item.")
|
scgi.temp_error("No new line allowed in item.")
|
||||||
line = f"* {item}\n"
|
line = f"* {item}\n"
|
||||||
list_file = get_user_dir() / list_name
|
list_file = scgi.get_user_dir() / list_name
|
||||||
try:
|
try:
|
||||||
with open(list_file, "at") as f:
|
with open(list_file, "at") as f:
|
||||||
f.write(line)
|
f.write(line)
|
||||||
|
@ -89,11 +92,37 @@ def process_add(list_name):
|
||||||
|
|
||||||
|
|
||||||
def process_pop(list_name):
|
def process_pop(list_name):
|
||||||
list_file = get_user_dir() / list_name
|
list_file = scgi.get_user_dir() / list_name
|
||||||
subprocess.run(["sed", "-i", "1d", str(list_file)])
|
subprocess.run(["sed", "-i", "1d", str(list_file)])
|
||||||
redirect_to_list(list_name)
|
redirect_to_list(list_name)
|
||||||
|
|
||||||
|
|
||||||
|
def process_clear(list_name):
|
||||||
|
if scgi.query_string_dec:
|
||||||
|
if scgi.query_string_dec != list_name:
|
||||||
|
redirect_to_list(list_name)
|
||||||
|
list_file = scgi.get_user_dir() / list_name
|
||||||
|
try:
|
||||||
|
with open(list_file, "wt") as f:
|
||||||
|
f.truncate()
|
||||||
|
except OSError:
|
||||||
|
scgi.temp_error()
|
||||||
|
redirect_to_list(list_name)
|
||||||
|
else:
|
||||||
|
scgi.require_input("Type the list name to confirm its clearing.")
|
||||||
|
|
||||||
|
|
||||||
|
def process_delete(list_name):
|
||||||
|
if scgi.query_string_dec:
|
||||||
|
if scgi.query_string_dec != list_name:
|
||||||
|
redirect_to_list(list_name)
|
||||||
|
list_file = scgi.get_user_dir() / list_name
|
||||||
|
list_file.unlink()
|
||||||
|
scgi.redirect_temp(scgi.script_name)
|
||||||
|
else:
|
||||||
|
scgi.require_input("Type the list name to confirm its deletion.")
|
||||||
|
|
||||||
|
|
||||||
def redirect_to_list(list_name):
|
def redirect_to_list(list_name):
|
||||||
scgi.redirect_temp(scgi.script_name + "/" + list_name)
|
scgi.redirect_temp(scgi.script_name + "/" + list_name)
|
||||||
|
|
||||||
|
|
Reference in a new issue