Basic Account Editing

This commit is contained in:
BlenderDumbass 2024-11-24 16:13:01 +02:00
parent 26c273dc53
commit 0c424ccb3d
3 changed files with 223 additions and 17 deletions

View file

@ -142,7 +142,7 @@ def Transfer(location):
# We are adding some new stuff to accounts
account["title"] = username # Visible title
account["contact"] = "" # Contact info
account["email"] = "" # Contact info
account["website"] = "" # Website
account["mastodon"] = "" # Mastodon ( useful for fediverse tags )
account["sessions"] = {} # List of cookies accosiated with this account

View file

@ -531,16 +531,78 @@ def AccountPage(server, account):
html = html + '<div class="dark_box">'
html = html +"<center><h1>"+Accounts.get(account, {}).get("title", account)+"</h1></center>"
# Website
website = Safe(Accounts.get(account, {}).get("website" , ""))
if website:
webtitle = website.replace("https://", "").replace("http://", "")
if not website.startswith("http"): website = "http://"+website
html = html + '<center>'
html = html + '<img style="vertical-align: middle" src="/icon/internet">'
html = html + '<a href="'+website+'"> '+webtitle+'</a>'
html = html + '</center>'
# Email
email = Safe(Accounts.get(account, {}).get("email" , ""))
if email:
html = html + '<center>'
html = html + '<img style="vertical-align: middle" src="/icon/frase">'
html = html + '<a href="mailto:'+email+'"> '+email+'</a>'
html = html + '</center>'
# Mastodon
mastodon = Safe(Accounts.get(account, {}).get("mastodon" , ""))
if mastodon:
# It could be mastodon url and not handle.
if "/" in mastodon:
mastodon = mastodon.replace("https://", "").replace("http://", "")
mastodon = mastodon.split("/")[1]+"@"+mastodon.split("/")[0]
mastolink = "https://"+mastodon[1:].split("@")[1]+"/@"+mastodon[1:].split("@")[0]
html = html + '<center>'
html = html + '<img style="vertical-align: middle" src="/icon/mastodon">'
html = html + '<a href="'+mastolink+'"> '+mastodon+'</a>'
html = html + '</center>'
# Matrix
matrix = Safe(Accounts.get(account, {}).get("matrix" , ""))
if matrix:
# Matrix could be the matrix.to link
if "/" in matrix:
matrix = matrix[matrix.rfind("/")+1:]
matrixlink = "https://matrix.to/#/"+matrix
html = html + '<center>'
html = html + '<img style="vertical-align: middle" src="/icon/element">'
html = html + '<a href="'+matrixlink+'"> '+matrix+'</a>'
html = html + '</center>'
if any((website, email, mastodon, matrix)):
html = html + '<br>'
html = html + '</div>'
html = html + '<div class="dark_box">'
html = html +"<center>Invited by:<br>"+User(Accounts.get(account, {}).get("invited_by", account))+"</center>"
html = html + '</div>'
html = html + '</div>'
bio = Safe(Accounts.get(account, {}).get("bio" , ""))
if bio:
html = html + '<div class="dark_box">'
html = html + markdown.convert(bio, False)
html = html + markdown.convert(bio, False)+'<br>'
html = html + '</div>'
@ -575,9 +637,10 @@ def AccountPage(server, account):
if invited:
html = html + '<center><div class="button">Invited:</div></center>'
for username in invited:
html = html + '<div class="dark_box">'
html = html + '<center>' + User(username) + '</center>\n'
html = html + '</div>'
if username in Accounts:
html = html + '<div class="dark_box">'
html = html + '<center>' + User(username) + '</center>\n'
html = html + '</div>'
html = html + LoginButton(server)
@ -633,7 +696,74 @@ def LoginPage(server):
send(server, html, 200)
def SettingsPage(server):
user = validate(server.cookie)
config = Set.Load()
# Generating <head>
html = head(title = "Settings",
description = "Settings",
config = config
)
html = html + Button(config.get("title", "My Website"), "/", image=config.get("favicon", "/icon/internet"))
html = html + Button("Public Profile", "/account/"+user.get("username", ""), image="/pictures/monkey.png")
# Main settings
html = html + """
<div class="middle_section_article">
<div class="dark_box">
<center><h2>Public Info</h2></center>
<form action="update_account">
<img style="vertical-align: middle" src="/icon/user">
<input class="button" style="width:90%" maxlength="200" name="title" placeholder="Visible Name" value='"""+user.get("title", "")+"""'>
<img style="height:40px; vertical-align: middle" src="/pictures/monkey.png">
<input class="button" style="width:90%" maxlength="200" name="avatar" placeholder="Link To Profile Picture" value='"""+user.get("avatar", "")+"""'>
<br>
<center>
<img style="vertical-align: middle" src="/icon/scene">
<b>Bio</b><br>
<textarea class="toot" rows="10" style="width:95%" maxlength="10000" name="bio">"""+Safe(user.get("bio", ""))+"""</textarea>
</center>
<img style="vertical-align: middle" src="/icon/internet">
<input class="button" style="width:90%" maxlength="200" name="website" placeholder="Personal Website" value='"""+user.get("website", "")+"""'>
<img style="vertical-align: middle" src="/icon/frase">
<input class="button" style="width:90%" maxlength="200" name="email" placeholder="Publicly Visible Email" value='"""+user.get("email", "")+"""'>
<img style="vertical-align: middle" src="/icon/mastodon">
<input class="button" style="width:90%" maxlength="200" name="mastodon" placeholder="Mastodon Handle" value='"""+user.get("mastodon", "")+"""'>
<img style="vertical-align: middle" src="/icon/element">
<input class="button" style="width:90%" maxlength="200" name="matrix" placeholder="Matrix Handle" value='"""+user.get("matrix", "")+"""'>
<button class="button" type="submit">
<img style="vertical-align: middle" src="/icon/ok">
Save
</button>
</form>
</div>
</div>
"""
send(server, html, 200)
###
def Button(text, link, icon="", image=""):
@ -727,6 +857,8 @@ def User(username, stretch=False):
def Graph(server, url):
user = validate(server.cookie)
html = """
<head>
<link media="all" href="/css" type="text/css" rel="stylesheet" /> <!-- CSS theme link -->
@ -773,18 +905,26 @@ def Graph(server, url):
# Saving the view
if server.cookie not in article.get("views", {}).get("viewers", []):
try:
article["views"]["amount"] += 1
article["views"]["viewers"].append(server.cookie)
cookies = [server.cookie]
with open(Set.Folder()+"/tabs"+url+"/metadata.json", "w") as save:
json.dump(article, save, indent=4)
except Exception as e:
print(e)
pass
if user:
# If user is logged in, we want to record
# per reading the article, on all pers
# sessions.
for cookie in user.get("sessions"):
if cookie not in cookies:
cookies.append(cookie)
for cookie in cookies:
if cookie not in article.get("views", {}).get("viewers", []):
article["views"]["amount"] += 1
article["views"]["viewers"].append(cookie)
with open(Set.Folder()+"/tabs"+url+"/metadata.json", "w") as save:
json.dump(article, save, indent=4)
send(server, html, 200)
@ -969,9 +1109,11 @@ def Login(server):
Accounts = accounts()
# Failed authentication
if username not in Accounts or hashed != Accounts[username].get("password"):
Redirect(server, "/login?wrong=username")
# Succesfull authentication
else:
account = Accounts[username]
@ -980,12 +1122,70 @@ def Login(server):
account["sessions"] = {}
account["sessions"][server.cookie] = server.headers.get("User-Agent")
folder = Set.Folder()+"/accounts"
f = Set.Folder()
folder = f+"/accounts"
with open(folder+"/"+username+".json", "w") as save:
json.dump(account, save, indent=4)
# Move the cookie arround
# When a login happens, we want to make the server know
# which articles the person already read and stuff. So
# we want to come all the cookies. Arround.
articles = allArticles()
for title in articles:
article = articles[title]
for cookie in account["sessions"]:
if cookie != server.cookie:
# Making it so it knows what you were watching previously.
if cookie in article.get("views", {}).get("viewers", [])\
and server.cookie not in article["views"]["viewers"]:
article["views"]["viewers"].append(server.cookie)
# Making it so previously logged in account would know.
# what you were watching thus far from this cookie.
if server.cookie in article.get("views", {}).get("viewers", [])\
and cookie not in article["views"]["viewers"]:
article["views"]["viewers"].append(cookie)
with open(f+"/tabs"+article.get("url")+"/metadata.json", "w") as save:
json.dump(article, save, indent=4)
Redirect(server, "/")
def UpdateAccount(server):
user = validate(server.cookie)
keys = [
"title",
"avatar",
"bio",
"website",
"email",
"mastodon",
"matrix"
]
for key in keys:
data = server.parsed.get(key, [""])[0]
user[key] = Safe(data)
f = Set.Folder()
folder = f+"/accounts"
with open(folder+"/"+user.get("username", "")+".json", "w") as save:
json.dump(user, save, indent=4)
Redirect(server, "/settings")
def DoComment(server):
user = validate(server.cookie)

View file

@ -74,12 +74,18 @@ class handler(BaseHTTPRequestHandler):
elif self.path[1:].startswith("login"):
Render.LoginPage(self)
elif self.path[1:].startswith("settings"):
Render.SettingsPage(self)
elif self.path[1:].startswith("comment"):
Render.DoComment(self)
elif self.path[1:].startswith("delete_comment"):
Render.DeleteComment(self)
elif self.path[1:].startswith("update_account"):
Render.UpdateAccount(self)
elif self.path[1:].startswith("do_login"):
Render.Login(self)