From f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 17 Nov 2022 08:21:42 -0700 Subject: Add image/icon upload --- users/models/identity.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'users/models') 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 -- cgit v1.2.3