products/models.py: add basic product image classes
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import logging
|
||||
import re
|
||||
from enum import Enum
|
||||
import os.path
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
@ -38,3 +40,33 @@ class Product(models.Model):
|
||||
}
|
||||
|
||||
|
||||
class ProductImageFormat(Enum):
|
||||
THUMB_PNG = 'thumb.png'
|
||||
LARGE_JPG = 'large.jpg'
|
||||
LARGE_PNG = 'large.png'
|
||||
MED_JPG = 'med.jpg'
|
||||
MED_PNG = 'med.png'
|
||||
|
||||
|
||||
class ProductImage:
|
||||
THUMB_DIR = os.path.join(settings.ASSET_DIR, 'products/thumbs')
|
||||
THUMB_URL = '/images/products/thumbs'
|
||||
# ordered by thumbnail preference
|
||||
VIEW_TYPES = ['C', '3Q', '3QL', '3QR', '3QRL', 'P', 'PL', 'P2', 'P2L',
|
||||
'F3Q', 'L', 'PLA', 'PPS', 'PPS_WORN', 'PLD', 'OS', 'OSL',
|
||||
'T', 'TL', 'PACK', 'F', 'B', 'INT', 'ALT_INT', 'FLR',
|
||||
'FRONT', 'BACK']
|
||||
|
||||
@staticmethod
|
||||
def get_image_path(sap, image_format):
|
||||
for view in ProductImage.VIEW_TYPES:
|
||||
path = os.path.join(ProductImage.THUMB_DIR,
|
||||
'{}_{}-{}'.format(sap, view, image_format.value))
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
|
||||
@staticmethod
|
||||
def get_image_url(sap, image_format):
|
||||
path = ProductImage.get_image_path(sap, image_format)
|
||||
if path:
|
||||
return path.replace(ProductImage.THUMB_DIR, ProductImage.THUMB_URL)
|
||||
|
||||
Reference in New Issue
Block a user