2024-12-31 19:56:46 -05:00
|
|
|
import math
|
|
|
|
import time
|
|
|
|
from urllib.parse import quote as escape_url
|
|
|
|
|
|
|
|
from django.core.handlers.wsgi import WSGIRequest
|
|
|
|
from django.db import IntegrityError
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
|
|
|
|
from tblog.models import tBPost
|
|
|
|
|
|
|
|
from .helper import get_user_object, get_username, render_template
|
|
|
|
|
|
|
|
|
|
|
|
def auth(request: WSGIRequest) -> HttpResponseRedirect:
|
|
|
|
resp = HttpResponseRedirect("/")
|
|
|
|
if "remove" in request.GET:
|
|
|
|
resp.set_cookie("session_id", "", max_age=0)
|
|
|
|
else:
|
2025-01-10 18:34:30 -05:00
|
|
|
resp.set_cookie("session_id", request.GET.get("sessionid") or "", max_age=60 * 60 * 24 * 365)
|
2024-12-31 19:56:46 -05:00
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def index(request: WSGIRequest) -> HttpResponse:
|
|
|
|
username = get_username(request)
|
|
|
|
|
|
|
|
if username:
|
|
|
|
user = get_user_object(username, i_promise_this_user_exists=True)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
request, "index.html",
|
|
|
|
username=username,
|
|
|
|
blog_count=user.posts.count() # type: ignore
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
request, "noauth/index.html"
|
|
|
|
)
|
|
|
|
|
|
|
|
def write(request: WSGIRequest) -> HttpResponse:
|
|
|
|
username = get_username(request)
|
2025-01-04 15:41:02 -05:00
|
|
|
if not username:
|
2024-12-31 19:56:46 -05:00
|
|
|
return render_template(
|
2025-01-04 15:41:02 -05:00
|
|
|
request, "noauth/index.html"
|
2024-12-31 19:56:46 -05:00
|
|
|
)
|
|
|
|
|
2025-01-04 15:41:02 -05:00
|
|
|
repopulate = {}
|
|
|
|
error = None
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
url = (request.POST.get("url") or "").strip().replace(" ", "-").lower()
|
|
|
|
title = (request.POST.get("title") or "").strip()
|
|
|
|
content = (request.POST.get("raw") or "").strip()
|
|
|
|
fmt = request.POST.get("format")
|
|
|
|
|
|
|
|
repopulate = {
|
|
|
|
"url": url,
|
|
|
|
"title": title,
|
|
|
|
"content": content,
|
|
|
|
"format": fmt
|
|
|
|
}
|
|
|
|
|
|
|
|
if not (url and title and content) or fmt not in ["plain", "mono", "markdown", "html"] or len(url) > 250 or len(title) > 1_000 or len(content) > 500_000:
|
|
|
|
error = "Invalid input(s)"
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
tBPost.objects.create(
|
|
|
|
u_by=get_user_object(username, i_promise_this_user_exists=True),
|
|
|
|
url=url,
|
|
|
|
title=title,
|
|
|
|
content=content,
|
|
|
|
timestamp=math.floor(time.time()),
|
|
|
|
text_format=fmt
|
|
|
|
)
|
|
|
|
except IntegrityError:
|
|
|
|
error = f"Url '{url}' already in use"
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(f"/blog/{username}/{escape_url(url)}/")
|
|
|
|
|
2024-12-31 19:56:46 -05:00
|
|
|
return render_template(
|
2025-01-04 15:41:02 -05:00
|
|
|
request, "write.html",
|
|
|
|
title="Writing",
|
|
|
|
username=username,
|
|
|
|
error=error,
|
|
|
|
repopulate=repopulate
|
2024-12-31 19:56:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def view_blog(request: WSGIRequest, username: str, url: str) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
blog = tBPost.objects.get(
|
|
|
|
u_by=get_user_object(username),
|
|
|
|
url=url
|
|
|
|
)
|
|
|
|
except tBPost.DoesNotExist:
|
|
|
|
return render_template(
|
|
|
|
request, "404.html"
|
|
|
|
)
|
|
|
|
|
|
|
|
self_username = get_username(request)
|
|
|
|
|
|
|
|
if request.method == "POST" and username == self_username:
|
|
|
|
blog.delete()
|
|
|
|
return HttpResponseRedirect("/")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
request, "blog.html",
|
|
|
|
blog=blog,
|
|
|
|
title=blog.title + " by " + blog.u_by.username,
|
|
|
|
username=self_username
|
|
|
|
)
|
|
|
|
|
2025-01-04 15:41:02 -05:00
|
|
|
def edit_blog(request: WSGIRequest, username: str, url: str) -> HttpResponse:
|
|
|
|
try:
|
|
|
|
blog = tBPost.objects.get(
|
|
|
|
u_by=get_user_object(username),
|
|
|
|
url=url
|
|
|
|
)
|
|
|
|
except tBPost.DoesNotExist:
|
|
|
|
return render_template(
|
|
|
|
request, "404.html"
|
|
|
|
)
|
|
|
|
|
|
|
|
self_username = get_username(request)
|
|
|
|
|
|
|
|
if username != self_username:
|
|
|
|
return render_template(
|
|
|
|
request, "404.html"
|
|
|
|
)
|
|
|
|
|
|
|
|
error = None
|
|
|
|
repopulate = {
|
|
|
|
"url": blog.url,
|
|
|
|
"title": blog.title,
|
|
|
|
"content": blog.content,
|
|
|
|
"format": blog.text_format
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
url = (request.POST.get("url") or "").strip().replace(" ", "-").lower() or blog.url
|
|
|
|
title = (request.POST.get("title") or "").strip() or blog.title
|
|
|
|
content = (request.POST.get("raw") or "").strip() or blog.content
|
|
|
|
fmt = request.POST.get("format") or blog.text_format
|
|
|
|
|
|
|
|
repopulate = {
|
|
|
|
"url": url,
|
|
|
|
"title": title,
|
|
|
|
"content": content,
|
|
|
|
"format": fmt
|
|
|
|
}
|
|
|
|
|
|
|
|
if not (url and title and content) or fmt not in ["plain", "mono", "markdown", "html"] or len(url) > 250 or len(title) > 1_000 or len(content) > 500_000:
|
|
|
|
error = "Invalid input(s)"
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
blog.url = url
|
|
|
|
blog.title = title
|
|
|
|
blog.content = content
|
|
|
|
blog.edited_at = math.floor(time.time())
|
|
|
|
blog.text_format = fmt
|
|
|
|
blog.save()
|
|
|
|
except IntegrityError:
|
|
|
|
error = f"Url '{url}' already in use"
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect(f"/blog/{username}/{escape_url(url)}/")
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
request, "write.html",
|
|
|
|
title="Editing",
|
|
|
|
editing=True,
|
|
|
|
username=username,
|
|
|
|
error=error,
|
|
|
|
repopulate=repopulate
|
|
|
|
)
|
|
|
|
|
2024-12-31 19:56:46 -05:00
|
|
|
def user(request: WSGIRequest, username: str) -> HttpResponse:
|
|
|
|
user = get_user_object(username)
|
|
|
|
|
|
|
|
if user:
|
|
|
|
return render_template(
|
|
|
|
request, "user.html",
|
|
|
|
username=user.username,
|
|
|
|
self_username=get_username(request),
|
2025-01-04 15:41:02 -05:00
|
|
|
posts=user.posts.order_by("-id") # type: ignore
|
2024-12-31 19:56:46 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
request, "404.html"
|
|
|
|
)
|