class TestModel(models.Model): avatar = ImageField(upload_to='avatars', resize_source_to='source') profile_picture = ImageField(upload_to='avatars', blank=True, null=True, resize_source_to='source') card_identity_picture = ImageField(upload_to='identity_card', blank=True, null=True, resize_source_to='source_with_format')
class TestPregeneratedSizesModel(models.Model): logo = ImageField(upload_to='logos', blank=True, null=True, pregenerated_sizes=['small', 'large']) photo = ImageField( upload_to='photos', blank=True, null=True, resize_source_to='source_with_format', pregenerated_sizes=['source_with_format', 'default', 'large'])
class Image(models.Model): uploader = models.ForeignKey(User, on_delete=models.CASCADE) image_file = ImageField() def save(self, *args, **kwargs): status = super(Image, self).save(*args, **kwargs) if not hasattr(self.uploader, 'account_plan_assignement'): assignement = AccountPlanAssignement.objects.create( user=self.uploader, account_plan_id=1) sizes = assignement.account_plan.thumbnail_sizes.filter(~models.Q( thumbnail__in=self.thumbnails.all())) else: sizes = self.uploader.account_plan_assignement.account_plan.thumbnail_sizes.filter( ~models.Q(thumbnail__in=self.thumbnails.all())) storage = Storage() for size in sizes: img = ImageObject.open(self.image_file.file.file) img.thumbnail((size.width, size.height)) new_img_io = BytesIO() img.save(new_img_io, format='JPEG') splitted_ext = os.path.splitext(self.image_file.path) if len(splitted_ext) > 1: filename, ext = splitted_ext else: filename, ext = splitted_ext[0], '.jpg' filename = storage.generate_filename(filename + str(size) + ext) storage.save(filename, new_img_io) Thumbnail.objects.create(thumbnail_size=size, original_image=self, thumbnail_image=storage.open(filename)) return status
class Photo(models.Model): photo = ImageField(upload_to=FilenameGenerator(prefix='photo'), default='', blank=True) name = models.CharField(max_length=255) def __unicode__(self): return self.name
class Thumbnail(models.Model): thumbnail_size = models.ForeignKey(ThumbnailSize, on_delete=models.CASCADE) original_image = models.ForeignKey(Image, on_delete=models.CASCADE, related_name='thumbnails') thumbnail_image = ImageField() class Meta: unique_together = ('thumbnail_size', 'original_image')
class GalleryImage(models.Model): image = ImageField(verbose_name="Picture", help_text="size 900x700 px (square size 300x175px)") name = models.CharField(max_length=25, verbose_name="Picture name", help_text="Max 25 characters") description = models.TextField( null=True, default=None, blank=True, verbose_name="Description", max_length=250, help_text="Max 250 characters", ) span = models.IntegerField( default=4, validators=[ validators.MinValueValidator(1), validators.MaxValueValidator(12) ], help_text= "Digit between 1-12. How much space image should take. See bootstrap grid", ) order = models.IntegerField(default=0) site = models.ForeignKey(Site, on_delete=models.CASCADE) uuid = models.UUIDField(default=uuid.uuid4) objects = models.Manager() on_site = CurrentSiteManager() @classmethod def get_highest_order(cls): value = cls.objects.all().aggregate(Max("order"))["order__max"] return value if value is not None else 0 def get_fields_value(self): return { "image": self.image, "name": self.name, "description": self.description, "span": self.span, "order": self.order, } def __str__(self): return self.name class Meta: ordering = ["order"]
class Aplication(models.Model): category = models.ForeignKey(Category, related_name='aplications', on_delete=models.CASCADE) name = models.CharField(max_length=255) slug = models.SlugField() isActive = models.BooleanField(blank=False, default=True) description = models.CharField(max_length=2550, blank=True) image = ImageField(upload_to='uploads/', blank=True, null=True) thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(300, 200)], format='JPEG', options={'quality': 80}) class Meta: ordering = ('-name', ) def __str__(self): return self.name def get_image(self): if self.image: return 'http://127.0.0.1:8000' + self.image.url return '' def get_thumbnail(self): if self.thumbnail: return 'http://127.0.0.1:8000' + self.thumbnail.url else: if self.image: self.thumbnail = self.make_thumbnail(self.image) self.save() # return 'http://127.0.0.1:8000' logging.info(self.thumbnail.path) return 'http://127.0.0.1:8000' + self.thumbnail.url else: return 'http://127.0.0.1:8000' + settings.MEDIA_URL + "logo-prodasen.png/" def get_absolute_url(self): return f'/{self.category.slug}/{self.slug}/'
class TestModel(models.Model): avatar = ImageField(upload_to='avatars', resize_source_to='source') profile_picture = ImageField(upload_to='avatars', blank=True, null=True, resize_source_to='source')
class Tutorial(models.Model): tags = models.ManyToManyField(Tag, related_name='tutorials', blank=True) # tag = models.ForeignKey(Tag, related_name='tutorials', on_delete=models.CASCADE) aplication = models.ForeignKey(Aplication, related_name='tutorials', on_delete=models.CASCADE) name = models.CharField(max_length=255) slug = models.SlugField() image = ImageField(upload_to='uploads/', blank=True, null=True) pdf = models.FileField(upload_to='uploads/pdf/%Y/%m/%d', blank=False, null=True) thumbnail = ImageSpecField(source='image', processors=[ResizeToFill(300, 200)], format='JPEG', options={'quality': 80}) date_added = models.DateTimeField(auto_now_add=True) # class Meta: class Meta: ordering = ('-date_added', ) def __str__(self): return self.name def get_absolute_url(self): return f'/{self.aplication.category.slug}/{self.aplication.slug}/{self.slug}/' def get_image(self): if self.image: return 'http://127.0.0.1:8000' + self.image.url return '' def get_pdf(self): if self.pdf: return 'http://127.0.0.1:8000' + self.pdf.url return '' def get_aplication(self): if self.aplication_set: return self.aplication_set.all() else: return '' def get_thumbnail(self): if self.thumbnail: return 'http://127.0.0.1:8000' + self.thumbnail.url else: if self.image: self.thumbnail = self.make_thumbnail(self.image) self.save() # return 'http://127.0.0.1:8000' logging.info(self.thumbnail.path) return 'http://127.0.0.1:8000' + self.thumbnail.url else: return 'http://127.0.0.1:8000' + settings.MEDIA_URL + "logo-prodasen.png/" def make_thumbnail(self, image, size=(300, 200)): img = Image.open(image) img.convert('RGB') img.thumbnail(size) thumb_io = BytesIO() img.save(thumb_io, 'JPEG', quality=85) name = image.path + "." + img.format.lower() thumbnail = File(thumb_io, name=name) return thumbnail