2022-08-19 13:57:25 +01:00
|
|
|
from typing import TypedDict
|
|
|
|
|
2022-07-12 09:32:05 +01:00
|
|
|
from django.db import models
|
2022-07-12 22:53:50 +01:00
|
|
|
from django.utils import timezone
|
2022-09-20 09:24:00 +01:00
|
|
|
from wagtail.search import index
|
2022-07-12 09:32:05 +01:00
|
|
|
|
|
|
|
|
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):
|
2022-07-12 09:32:05 +01:00
|
|
|
unsplash_id = models.CharField(unique=True, max_length=11, db_index=True)
|
|
|
|
data = models.JSONField()
|
2022-07-12 22:53:50 +01:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
data_last_updated = models.DateTimeField(default=timezone.now)
|
2022-07-12 15:14:27 +01:00
|
|
|
|
2022-09-20 09:24:00 +01:00
|
|
|
search_fields = [
|
|
|
|
index.SearchField("unsplash_id", boost=10),
|
|
|
|
index.SearchField("get_description"),
|
|
|
|
]
|
|
|
|
|
2022-07-12 15:14:27 +01:00
|
|
|
def get_description(self) -> str:
|
|
|
|
return self.data["description"]
|
2022-07-12 15:48:40 +01:00
|
|
|
|
2022-08-19 13:57:25 +01:00
|
|
|
def get_image_urls(self) -> ImageURLs:
|
|
|
|
return self.data["urls"]
|
2022-07-12 22:45:50 +01:00
|
|
|
|
2022-07-12 15:48:40 +01:00
|
|
|
def get_thumbnail_url(self) -> str:
|
2022-08-19 13:57:25 +01:00
|
|
|
return self.get_image_urls()["thumb"]
|