diff options
author | Andrew Godwin | 2022-11-17 08:21:42 -0700 |
---|---|---|
committer | Andrew Godwin | 2022-11-17 12:21:44 -0700 |
commit | f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d (patch) | |
tree | d01453e94f371956e7e989351b51e6ed6eb42541 /users/models | |
parent | 7f8e792402b66dbb4a721be4f44306d528931b86 (diff) | |
download | takahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.tar.gz takahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.tar.bz2 takahe-f5eafb0ca0af3ed064202abbe99bfbeef8bbb74d.zip |
Add image/icon upload
Diffstat (limited to 'users/models')
-rw-r--r-- | users/models/identity.py | 33 |
1 files changed, 22 insertions, 11 deletions
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 |