From 2a3690d1c148da5dd799052403ba7290e1fb7de0 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 17 Nov 2022 19:31:00 -0700 Subject: Return images and summary in actor --- .gitignore | 1 + core/html.py | 3 ++- core/ld.py | 17 +++++++++++++++++ takahe/settings/base.py | 6 ++++-- takahe/urls.py | 4 +--- templates/activities/_post.html | 4 +++- templates/identity/view.html | 6 ++++++ users/models/identity.py | 22 ++++++++++++++++++++-- 8 files changed, 54 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 2a5c47b..5f0eef3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.psql *.sqlite3 .venv +/*.env /media/ notes.md diff --git a/core/html.py b/core/html.py index fd41a50..5045b16 100644 --- a/core/html.py +++ b/core/html.py @@ -20,12 +20,13 @@ def sanitize_post(post_html: str) -> str: Only allows a, br, p and span tags, and class attributes. """ cleaner = bleach.Cleaner( - tags=["a", "br", "p", "span"], + tags=["br", "p"], attributes={ # type:ignore "a": allow_a, "p": ["class"], "span": ["class"], }, filters=[LinkifyFilter], + strip=True, ) return mark_safe(cleaner.clean(post_html)) diff --git a/core/ld.py b/core/ld.py index 6692dab..59b867c 100644 --- a/core/ld.py +++ b/core/ld.py @@ -1,4 +1,5 @@ import datetime +import os import urllib.parse as urllib_parse from typing import Dict, List, Optional, Union @@ -436,3 +437,19 @@ def parse_ld_date(value: Optional[str]) -> Optional[datetime.datetime]: return datetime.datetime.strptime(value, DATETIME_FORMAT).replace( tzinfo=datetime.timezone.utc ) + + +def media_type_from_filename(filename): + _, extension = os.path.splitext(filename) + if extension == ".png": + return "image/png" + elif extension in [".jpg", ".jpeg"]: + return "image/png" + elif extension == ".gif": + return "image/gif" + elif extension == ".apng": + return "image/apng" + elif extension == ".webp": + return "image/webp" + else: + return "application/octet-stream" diff --git a/takahe/settings/base.py b/takahe/settings/base.py index b98b9a0..dd89818 100644 --- a/takahe/settings/base.py +++ b/takahe/settings/base.py @@ -108,5 +108,7 @@ STATICFILES_DIRS = [ ALLOWED_HOSTS = ["*"] -MEDIA_ROOT = BASE_DIR / "media" -MEDIA_URL = "/media/" + +# Note that this MUST be a fully qualified URL in production +MEDIA_URL = os.environ.get("TAKAHE_MEDIA_URL", "/media/") +MEDIA_ROOT = os.environ.get("TAKAHE_MEDIA_ROOT", BASE_DIR / "media") diff --git a/takahe/urls.py b/takahe/urls.py index c2d9d6b..0b23d7d 100644 --- a/takahe/urls.py +++ b/takahe/urls.py @@ -1,5 +1,3 @@ -import re - from django.conf import settings as djsettings from django.contrib import admin as djadmin from django.urls import path, re_path @@ -99,7 +97,7 @@ urlpatterns = [ path("djadmin/", djadmin.site.urls), # Media files re_path( - r"^%s(?P.*)$" % re.escape(djsettings.MEDIA_URL.lstrip("/")), + r"^media/(?P.*)$", serve, kwargs={"document_root": djsettings.MEDIA_ROOT}, ), diff --git a/templates/activities/_post.html b/templates/activities/_post.html index 5de8bc7..3d455ea 100644 --- a/templates/activities/_post.html +++ b/templates/activities/_post.html @@ -2,7 +2,9 @@ {% load activity_tags %}
- + + +