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