mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Generate thumbnails for any part images existing in the database
This commit is contained in:
parent
d505e79be8
commit
afa31b3415
28
InvenTree/part/migrations/0034_auto_20200404_1238.py
Normal file
28
InvenTree/part/migrations/0034_auto_20200404_1238.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 2.2.10 on 2020-04-04 12:38
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
from part.models import Part
|
||||||
|
from stdimage.utils import render_variations
|
||||||
|
|
||||||
|
|
||||||
|
def create_thumbnails(apps, schema_editor):
|
||||||
|
"""
|
||||||
|
Create thumbnails for all existing Part images.
|
||||||
|
"""
|
||||||
|
|
||||||
|
for part in Part.objects.all():
|
||||||
|
# Render thumbnail for each existing Part
|
||||||
|
if part.image:
|
||||||
|
part.image.render_variations()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('part', '0033_auto_20200404_0445'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(create_thumbnails),
|
||||||
|
]
|
@ -304,6 +304,16 @@ class Part(models.Model):
|
|||||||
else:
|
else:
|
||||||
return os.path.join(settings.STATIC_URL, 'img/blank_image.png')
|
return os.path.join(settings.STATIC_URL, 'img/blank_image.png')
|
||||||
|
|
||||||
|
def get_thumbnail_url(self):
|
||||||
|
"""
|
||||||
|
Return the URL of the image thumbnail for this part
|
||||||
|
"""
|
||||||
|
|
||||||
|
if self.image:
|
||||||
|
return os.path.join(settings.MEDIA_URL, str(self.image.thumbnail.url))
|
||||||
|
else:
|
||||||
|
return os.path.join(settings.STATIC_URL, 'img/blank_image.thumbnail.png')
|
||||||
|
|
||||||
def validate_unique(self, exclude=None):
|
def validate_unique(self, exclude=None):
|
||||||
""" Validate that a part is 'unique'.
|
""" Validate that a part is 'unique'.
|
||||||
Uniqueness is checked across the following (case insensitive) fields:
|
Uniqueness is checked across the following (case insensitive) fields:
|
||||||
@ -379,7 +389,7 @@ class Part(models.Model):
|
|||||||
upload_to=rename_part_image,
|
upload_to=rename_part_image,
|
||||||
null=True,
|
null=True,
|
||||||
blank=True,
|
blank=True,
|
||||||
variations={'thumbnail': (200, 200)},
|
variations={'thumbnail': (128, 128)},
|
||||||
delete_orphans=True
|
delete_orphans=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user