from django.db import models
from django.utils.html import format_html
from sorl.thumbnail import get_thumbnail
from sorl.thumbnail.fields import ImageField
from sno.models import Sno


class SnoGalleries(models.Model):
    class Meta:
        verbose_name = 'Фотография в галереи СНО'
        verbose_name_plural = 'Фотографии в галереи СНО'

    name = models.CharField('Название фото', max_length=255, blank=True, null=True)
    photo = ImageField(verbose_name='Фото', max_length=255)
    description = models.TextField('Описание', blank=True, null=True)
    sno = models.ForeignKey(Sno, verbose_name='СНО', on_delete=models.CASCADE)
    date_created = models.DateField('Дата', auto_now_add=True)

    def photo_preview(self):
        img = get_thumbnail(self.photo, '75x75', crop='center')
        return format_html('<a href="{}" target="_blank"><img style="width:75px; height:75px;" src="{}"></a>',
                           self.photo.url, img.url)

    photo_preview.short_description = 'Фото'

    def __str__(self):
        return '%s (%s)' % (self.name, self.sno.short_name)
