/
models.py
61 lines (50 loc) · 1.88 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
EMBED_TYPES = (
("video", "Video"),
("photo", "Photo"),
("link", "Link"),
("rich", "Rich"),
)
class Embed(models.Model):
"""
When embed code is fetched from a provider (eg, youtube) we cache that code
in the database so we don't need to ask for it again.
This model is used for caching the embed html code. It also stores some
metadata which gets displayed in the editor.
If an instance of this model is deleted, it will be automatically refetched
next time the embed code is needed.
"""
url = models.TextField()
max_width = models.SmallIntegerField(null=True, blank=True)
hash = models.CharField(max_length=32, unique=True, db_index=True)
type = models.CharField(max_length=10, choices=EMBED_TYPES)
html = models.TextField(blank=True)
title = models.TextField(blank=True)
author_name = models.TextField(blank=True)
provider_name = models.TextField(blank=True)
thumbnail_url = models.TextField(blank=True)
width = models.IntegerField(null=True, blank=True)
height = models.IntegerField(null=True, blank=True)
last_updated = models.DateTimeField(auto_now=True)
cache_until = models.DateTimeField(null=True, blank=True, db_index=True)
class Meta:
verbose_name = _("embed")
verbose_name_plural = _("embeds")
@property
def ratio(self):
if self.width and self.height:
return self.height / self.width
@property
def ratio_css(self):
ratio = self.ratio
if ratio:
return str(ratio * 100) + "%"
@property
def is_responsive(self):
if not getattr(settings, "WAGTAILEMBEDS_RESPONSIVE_HTML", False):
return False
return self.ratio is not None
def __str__(self):
return self.url