summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Godwin2022-11-17 08:21:42 -0700
committerAndrew Godwin2022-11-17 12:21:44 -0700
commitf5eafb0ca0af3ed064202abbe99bfbeef8bbb74d (patch)
treed01453e94f371956e7e989351b51e6ed6eb42541
parent7f8e792402b66dbb4a721be4f44306d528931b86 (diff)
downloadtakahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.tar.gz
takahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.tar.bz2
takahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.zip
Add image/icon upload
-rw-r--r--.gitignore1
-rw-r--r--activities/models/post.py4
-rw-r--r--core/uploads.py15
-rw-r--r--static/css/style.css12
-rw-r--r--takahe/settings/base.py3
-rw-r--r--takahe/urls.py17
-rw-r--r--templates/activities/_post.html6
-rw-r--r--templates/base.html7
-rw-r--r--templates/identity/view.html7
-rw-r--r--templates/settings/_menu.html2
-rw-r--r--templates/settings/profile.html19
-rw-r--r--users/models/identity.py33
-rw-r--r--users/views/admin.py52
-rw-r--r--users/views/settings.py105
14 files changed, 205 insertions, 78 deletions
diff --git a/.gitignore b/.gitignore
index 3853bb5..2a5c47b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
*.psql
*.sqlite3
.venv
+/media/
notes.md
diff --git a/activities/models/post.py b/activities/models/post.py
index caa2981..25afdda 100644
--- a/activities/models/post.py
+++ b/activities/models/post.py
@@ -27,8 +27,10 @@ class PostStates(StateGraph):
"""
post = await instance.afetch_full()
# Non-local posts should not be here
+ # TODO: This seems to keep happening. Work out how?
if not post.local:
- raise ValueError(f"Trying to run handle_new on a non-local post {post.pk}!")
+ print(f"Trying to run handle_new on a non-local post {post.pk}!")
+ return cls.fanned_out
# Build list of targets - mentions always included
targets = set()
async for mention in post.mentions.all():
diff --git a/core/uploads.py b/core/uploads.py
new file mode 100644
index 0000000..ef235f0
--- /dev/null
+++ b/core/uploads.py
@@ -0,0 +1,15 @@
+import base64
+import os
+import uuid
+
+from django.utils import timezone
+
+
+def upload_namer(prefix, instance, filename):
+ """
+ Names uploaded images, obscuring their original name with a random UUID.
+ """
+ now = timezone.now()
+ _, old_extension = os.path.splitext(filename)
+ new_filename = base64.b32encode(uuid.uuid4().bytes).decode("ascii")
+ return f"{prefix}/{now.year}/{now.month}/{now.day}/{new_filename}{old_extension}"
diff --git a/static/css/style.css b/static/css/style.css
index 3c1ef49..b3495b5 100644
--- a/static/css/style.css
+++ b/static/css/style.css
@@ -449,6 +449,11 @@ form .button.delete {
background: var(--color-delete);
}
+form button.secondary,
+form .button.secondary {
+ background: var(--color-bg-menu);
+}
+
form button.toggle,
form .button.toggle {
background: var(--color-bg-main);
@@ -475,6 +480,13 @@ h1.identity {
margin: 15px 0 20px 15px;
}
+h1.identity .banner {
+ width: 870px;
+ height: auto;
+ display: block;
+ margin: 0 0 20px 0;
+}
+
h1.identity .icon {
width: 80px;
height: 80px;
diff --git a/takahe/settings/base.py b/takahe/settings/base.py
index e45133d..b98b9a0 100644
--- a/takahe/settings/base.py
+++ b/takahe/settings/base.py
@@ -107,3 +107,6 @@ STATICFILES_DIRS = [
]
ALLOWED_HOSTS = ["*"]
+
+MEDIA_ROOT = BASE_DIR / "media"
+MEDIA_URL = "/media/"
diff --git a/takahe/urls.py b/takahe/urls.py
index 638dabd..5f5d5c5 100644
--- a/takahe/urls.py
+++ b/takahe/urls.py
@@ -1,5 +1,9 @@
+import re
+
+from django.conf import settings as djsettings
from django.contrib import admin as djadmin
-from django.urls import path
+from django.urls import path, re_path
+from django.views.static import serve
from activities.views import posts, timelines
from core import views as core
@@ -19,6 +23,11 @@ urlpatterns = [
name="settings",
),
path(
+ "settings/profile/",
+ settings.ProfilePage.as_view(),
+ name="settings_profile",
+ ),
+ path(
"settings/interface/",
settings.InterfacePage.as_view(),
name="settings_interface",
@@ -87,4 +96,10 @@ urlpatterns = [
path(".stator/runner/", stator.RequestRunner.as_view()),
# Django admin
path("djadmin/", djadmin.site.urls),
+ # Media files
+ re_path(
+ r"^%s(?P<path>.*)$" % re.escape(djsettings.MEDIA_URL.lstrip("/")),
+ serve,
+ kwargs={"document_root": djsettings.MEDIA_ROOT},
+ ),
]
diff --git a/templates/activities/_post.html b/templates/activities/_post.html
index 9d8db3b..14b1cbf 100644
--- a/templates/activities/_post.html
+++ b/templates/activities/_post.html
@@ -2,11 +2,7 @@
{% load activity_tags %}
<div class="post" data-takahe-id="{{ post.id }}">
- {% if post.author.icon_uri %}
- <img src="{{post.author.icon_uri}}" class="icon">
- {% else %}
- <img src="{% static "img/unknown-icon-128.png" %}" class="icon">
- {% endif %}
+ <img src="{{ post.author.local_icon_url }}" class="icon">
<time>
{% if post.visibility == 0 %}
diff --git a/templates/base.html b/templates/base.html
index bce5e1b..616d5b6 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -44,11 +44,14 @@
{% if not request.identity %}
No Identity
<img src="{% static "img/unknown-icon-128.png" %}" title="No identity selected">
+ {% elif request.identity.icon %}
+ {{ request.identity.username }}
+ <img src="{{ request.identity.icon.url }}" title="{{ request.identity.handle }}">
{% elif request.identity.icon_uri %}
- {{ request.identity.username }} <small>@{{ request.identity.domain_id }}</small>
+ {{ request.identity.username }}
<img src="{{ request.identity.icon_uri }}" title="{{ request.identity.handle }}">
{% else %}
- {{ request.identity.username }} <small>@{{ request.identity.domain_id }}</small>
+ {{ request.identity.username }}
<img src="{% static "img/unknown-icon-128.png" %}" title="{{ request.identity.handle }}">
{% endif %}
</a>
diff --git a/templates/identity/view.html b/templates/identity/view.html
index e4118c9..c830fc5 100644
--- a/templates/identity/view.html
+++ b/templates/identity/view.html
@@ -9,11 +9,10 @@
</nav>
<h1 class="identity">
- {% if identity.icon_uri %}
- <img src="{{identity.icon_uri}}" class="icon">
- {% else %}
- <img src="{% static "img/unknown-icon-128.png" %}" class="icon">
+ {% if identity.local_image_url %}
+ <img src="{{ identity.local_image_url }}" class="banner">
{% endif %}
+ <img src="{{ identity.local_icon_url }}" class="icon">
{% if request.identity %}
<form action="{{ identity.urls.action }}" method="POST" class="inline follow">
diff --git a/templates/settings/_menu.html b/templates/settings/_menu.html
index bdae143..4f71651 100644
--- a/templates/settings/_menu.html
+++ b/templates/settings/_menu.html
@@ -1,5 +1,5 @@
<nav>
- <a href="#" {% if section == "profile" %}class="selected"{% endif %}>Profile</a>
+ <a href="{% url "settings_profile" %}" {% if section == "profile" %}class="selected"{% endif %}>Profile</a>
<a href="#" {% if section == "interface" %}class="selected"{% endif %}>Interface</a>
<a href="#" {% if section == "filtering" %}class="selected"{% endif %}>Filtering</a>
</nav>
diff --git a/templates/settings/profile.html b/templates/settings/profile.html
new file mode 100644
index 0000000..1a7c29f
--- /dev/null
+++ b/templates/settings/profile.html
@@ -0,0 +1,19 @@
+{% extends "base.html" %}
+
+{% block title %}Profile - Settings{% endblock %}
+
+{% block content %}
+ {% block menu %}
+ {% include "settings/_menu.html" %}
+ {% endblock %}
+ <form action="." method="POST" enctype="multipart/form-data" >
+ {% csrf_token %}
+ {% for field in form %}
+ {% include "forms/_field.html" %}
+ {% endfor %}
+ <div class="buttons">
+ <a href="{{ request.identity.urls.view }}" class="button secondary">View Profile</a>
+ <button>Save</button>
+ </div>
+ </form>
+{% endblock %}
diff --git a/users/models/identity.py b/users/models/identity.py
index 4bbaeaf..d4ab720 100644
--- a/users/models/identity.py
+++ b/users/models/identity.py
@@ -1,5 +1,3 @@
-import base64
-import uuid
from functools import partial
from typing import Optional, Tuple
from urllib.parse import urlparse
@@ -10,9 +8,11 @@ from asgiref.sync import async_to_sync, sync_to_async
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from django.db import models
+from django.templatetags.static import static
from django.utils import timezone
from core.ld import canonicalise
+from core.uploads import upload_namer
from stator.models import State, StateField, StateGraph, StatorModel
from users.models.domain import Domain
@@ -33,15 +33,6 @@ class IdentityStates(StateGraph):
return "updated"
-def upload_namer(prefix, instance, filename):
- """
- Names uploaded images etc.
- """
- now = timezone.now()
- filename = base64.b32encode(uuid.uuid4().bytes).decode("ascii")
- return f"{prefix}/{now.year}/{now.month}/{now.day}/{filename}"
-
-
class Identity(StatorModel):
"""
Represents both local and remote Fediverse identities (actors)
@@ -128,6 +119,26 @@ class Identity(StatorModel):
else:
return f"/@{self.username}@{self.domain_id}/"
+ def local_icon_url(self):
+ """
+ Returns an icon for us, with fallbacks to a placeholder
+ """
+ if self.icon:
+ return self.icon.url
+ elif self.icon_uri:
+ return self.icon_uri
+ else:
+ return static("img/unknown-icon-128.png")
+
+ def local_image_url(self):
+ """
+ Returns a background image for us, returning None if there isn't one
+ """
+ if self.image:
+ return self.image.url
+ elif self.image_uri:
+ return self.image_uri
+
### Alternate constructors/fetchers ###
@classmethod
diff --git a/users/views/admin.py b/users/views/admin.py
index c1210f1..165572c 100644
--- a/users/views/admin.py
+++ b/users/views/admin.py
@@ -1,6 +1,4 @@
import re
-from functools import partial
-from typing import ClassVar, Dict
from django import forms
from django.db import models
@@ -11,6 +9,7 @@ from django.views.generic import FormView, RedirectView, TemplateView
from core.models import Config
from users.decorators import admin_required
from users.models import Domain, Identity, User
+from users.views.settings import SettingsPage
@method_decorator(admin_required, name="dispatch")
@@ -19,7 +18,7 @@ class AdminRoot(RedirectView):
@method_decorator(admin_required, name="dispatch")
-class AdminSettingsPage(FormView):
+class AdminSettingsPage(SettingsPage):
"""
Shows a settings page dynamically created from our settings layout
at the bottom of the page. Don't add this to a URL directly - subclass!
@@ -27,32 +26,6 @@ class AdminSettingsPage(FormView):
template_name = "admin/settings.html"
options_class = Config.SystemOptions
- section: ClassVar[str]
- options: Dict[str, Dict[str, str]]
-
- def get_form_class(self):
- # Create the fields dict from the config object
- fields = {}
- for key, details in self.options.items():
- config_field = self.options_class.__fields__[key]
- if config_field.type_ is bool:
- form_field = partial(
- forms.BooleanField,
- widget=forms.Select(
- choices=[(True, "Enabled"), (False, "Disabled")]
- ),
- )
- elif config_field.type_ is str:
- form_field = forms.CharField
- else:
- raise ValueError(f"Cannot render settings type {config_field.type_}")
- fields[key] = form_field(
- label=details["title"],
- help_text=details.get("help_text", ""),
- required=details.get("required", False),
- )
- # Create a form class dynamically (yeah, right?) and return that
- return type("SettingsForm", (forms.Form,), fields)
def load_config(self):
return Config.load_system()
@@ -60,27 +33,6 @@ class AdminSettingsPage(FormView):
def save_config(self, key, value):
Config.set_system(key, value)
- def get_initial(self):
- config = self.load_config()
- initial = {}
- for key in self.options.keys():
- initial[key] = getattr(config, key)
- return initial
-
- def get_context_data(self):
- context = super().get_context_data()
- context["section"] = self.section
- return context
-
- def form_valid(self, form):
- # Save each key
- for field in form:
- self.save_config(
- field.name,
- form.cleaned_data[field.name],
- )
- return redirect(".")
-
class BasicPage(AdminSettingsPage):
diff --git a/users/views/settings.py b/users/views/settings.py
index 877ad01..c3c166b 100644
--- a/users/views/settings.py
+++ b/users/views/settings.py
@@ -1,9 +1,14 @@
+from functools import partial
+from typing import ClassVar, Dict
+
+from django import forms
+from django.shortcuts import redirect
from django.utils.decorators import method_decorator
-from django.views.generic import RedirectView
+from django.views.generic import FormView, RedirectView
+from PIL import Image, ImageOps
from core.models import Config
from users.decorators import identity_required
-from users.views.admin import AdminSettingsPage
@method_decorator(identity_required, name="dispatch")
@@ -11,7 +16,8 @@ class SettingsRoot(RedirectView):
url = "/settings/interface/"
-class SettingsPage(AdminSettingsPage):
+@method_decorator(identity_required, name="dispatch")
+class SettingsPage(FormView):
"""
Shows a settings page dynamically created from our settings layout
at the bottom of the page. Don't add this to a URL directly - subclass!
@@ -19,6 +25,32 @@ class SettingsPage(AdminSettingsPage):
options_class = Config.IdentityOptions
template_name = "settings/settings.html"
+ section: ClassVar[str]
+ options: Dict[str, Dict[str, str]]
+
+ def get_form_class(self):
+ # Create the fields dict from the config object
+ fields = {}
+ for key, details in self.options.items():
+ config_field = self.options_class.__fields__[key]
+ if config_field.type_ is bool:
+ form_field = partial(
+ forms.BooleanField,
+ widget=forms.Select(
+ choices=[(True, "Enabled"), (False, "Disabled")]
+ ),
+ )
+ elif config_field.type_ is str:
+ form_field = forms.CharField
+ else:
+ raise ValueError(f"Cannot render settings type {config_field.type_}")
+ fields[key] = form_field(
+ label=details["title"],
+ help_text=details.get("help_text", ""),
+ required=details.get("required", False),
+ )
+ # Create a form class dynamically (yeah, right?) and return that
+ return type("SettingsForm", (forms.Form,), fields)
def load_config(self):
return Config.load_identity(self.request.identity)
@@ -26,6 +58,27 @@ class SettingsPage(AdminSettingsPage):
def save_config(self, key, value):
Config.set_identity(self.request.identity, key, value)
+ def get_initial(self):
+ config = self.load_config()
+ initial = {}
+ for key in self.options.keys():
+ initial[key] = getattr(config, key)
+ return initial
+
+ def get_context_data(self):
+ context = super().get_context_data()
+ context["section"] = self.section
+ return context
+
+ def form_valid(self, form):
+ # Save each key
+ for field in form:
+ self.save_config(
+ field.name,
+ form.cleaned_data[field.name],
+ )
+ return redirect(".")
+
class InterfacePage(SettingsPage):
@@ -37,3 +90,49 @@ class InterfacePage(SettingsPage):
"help_text": "If enabled, changes all 'Post' buttons to 'Toot!'",
}
}
+
+
+@method_decorator(identity_required, name="dispatch")
+class ProfilePage(FormView):
+ """
+ Lets the identity's profile be edited
+ """
+
+ template_name = "settings/profile.html"
+
+ class form_class(forms.Form):
+ name = forms.CharField(max_length=500)
+ summary = forms.CharField(widget=forms.Textarea, required=False)
+ icon = forms.ImageField(required=False)
+ image = forms.ImageField(required=False)
+
+ def get_initial(self):
+ return {
+ "name": self.request.identity.name,
+ "summary": self.request.identity.summary,
+ }
+
+ def get_context_data(self):
+ context = super().get_context_data()
+ context["section"] = "profile"
+ return context
+
+ def form_valid(self, form):
+ # Update identity name and summary
+ self.request.identity.name = form.cleaned_data["name"]
+ self.request.identity.summary = form.cleaned_data["summary"]
+ # Resize images
+ icon = form.cleaned_data.get("icon")
+ image = form.cleaned_data.get("image")
+ if icon:
+ resized_image = ImageOps.fit(Image.open(icon), (400, 400))
+ icon.open()
+ resized_image.save(icon)
+ self.request.identity.icon = icon
+ if image:
+ resized_image = ImageOps.fit(Image.open(image), (1500, 500))
+ image.open()
+ resized_image.save(image)
+ self.request.identity.image = image
+ self.request.identity.save()
+ return redirect(".")