34 lines
1.3 KiB
Python
34 lines
1.3 KiB
Python
import uuid
|
|
from django.db import models
|
|
from dashboard.models import TimeStampModel, Organization, ProductCategory, ProductType, SoftDeleteModel
|
|
import os
|
|
from uuid import uuid4
|
|
from django_resized import ResizedImageField
|
|
from simple_history.models import HistoricalRecords
|
|
|
|
# Create your models here.
|
|
|
|
def path_and_rename(instance, filename):
|
|
upload_to = 'product/'
|
|
ext = filename.split('.')[-1]
|
|
if instance.pk:
|
|
filename = '{}.{}'.format(instance.pk, ext)
|
|
else:
|
|
filename = '{}.{}'.format(uuid4().hex, ext)
|
|
return os.path.join(upload_to, filename)
|
|
|
|
|
|
class Product(TimeStampModel, SoftDeleteModel):
|
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
|
name = models.CharField(max_length=255, blank=True, null=True)
|
|
product_picture = ResizedImageField(upload_to=path_and_rename, blank=True, null=True)
|
|
manufacturer = models.CharField(max_length=255, blank=True, null=True)
|
|
description = models.TextField(blank=True, null=True)
|
|
product_category = models.ForeignKey(ProductCategory, models.DO_NOTHING, blank=True, null=True)
|
|
product_type = models.ForeignKey(ProductType, models.DO_NOTHING, blank=True, null=True)
|
|
organization = models.ForeignKey(Organization, models.DO_NOTHING, blank=True, null=True)
|
|
history = HistoricalRecords()
|
|
|
|
def __str__(self):
|
|
return self.name
|