website/website/contrib/unsplash/models.py

38 lines
951 B
Python
Raw Permalink Normal View History

2022-08-19 13:57:25 +01:00
from typing import TypedDict
from django.db import models
from django.utils import timezone
2022-09-20 09:24:00 +01:00
from wagtail.search import index
2022-08-19 13:57:25 +01:00
class ImageURLs(TypedDict):
raw: str
full: str
regular: str
small: str
thumb: str
2023-06-14 09:13:49 +01:00
SIZES = {"full": 2000, "regular": 1080, "small": 400, "thumb": 200}
2022-09-20 09:24:00 +01:00
class UnsplashPhoto(index.Indexed, models.Model):
unsplash_id = models.CharField(unique=True, max_length=11, db_index=True)
data = models.JSONField()
created = models.DateTimeField(auto_now_add=True)
data_last_updated = models.DateTimeField(default=timezone.now)
2022-09-20 09:24:00 +01:00
search_fields = [
index.SearchField("unsplash_id", boost=10),
index.SearchField("get_description"),
]
def get_description(self) -> str:
return self.data["description"]
2022-08-19 13:57:25 +01:00
def get_image_urls(self) -> ImageURLs:
return self.data["urls"]
def get_thumbnail_url(self) -> str:
2022-08-19 13:57:25 +01:00
return self.get_image_urls()["thumb"]