diff --git a/src/core/fixtures/new_coffee_variants.json b/src/core/fixtures/new_coffee_variants.json index af5ad89..1a38570 100644 --- a/src/core/fixtures/new_coffee_variants.json +++ b/src/core/fixtures/new_coffee_variants.json @@ -10,6 +10,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -25,6 +26,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -40,6 +42,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -55,6 +58,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -70,6 +74,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -85,6 +90,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -100,6 +106,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -115,6 +122,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -130,6 +138,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -145,6 +154,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -160,6 +170,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -175,6 +186,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -190,6 +202,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -205,6 +218,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -220,6 +234,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -235,6 +250,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -250,6 +266,7 @@ "track_inventory": false, "stock": null, "sorting": 1, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } @@ -265,6 +282,7 @@ "track_inventory": false, "stock": null, "sorting": 3, + "image": null, "created_at": "2022-02-23T18:06:57.624Z", "updated_at": "2022-02-23T18:06:57.624Z" } diff --git a/src/core/models.py b/src/core/models.py index 54eaebd..d53decf 100644 --- a/src/core/models.py +++ b/src/core/models.py @@ -121,6 +121,29 @@ class Product(models.Model): ordering = ['sorting', 'name'] +class ProductPhoto(models.Model): + product = models.ForeignKey(Product, on_delete=models.CASCADE) + image = models.ImageField(upload_to='products/images') + + def __str__(self): + return f'{self.product.name} {self.image}' + + def delete(self, *args, **kwargs): + storage, path = self.image.storage, self.image.path + + super(ProductPhoto, self).delete(*args, **kwargs) + storage.delete(path) + + # def save(self, *args, **kwargs): + # super().save(*args, **kwargs) + + # img = Image.open(self.image.path) + # if img.height > 400 or img.width > 400: + # output_size = (400, 400) + # img.thumbnail(output_size) + # img.save(self.image.path) + + class ProductVariantManager(models.Manager): def get_queryset(self): return super().get_queryset().annotate( @@ -135,7 +158,7 @@ class ProductVariant(models.Model): related_name='variants' ) image = models.ForeignKey( - 'ProductPhoto', + ProductPhoto, on_delete=models.SET_NULL, related_name='+', null=True @@ -195,29 +218,6 @@ class ProductOption(models.Model): return f'{self.name}' -class ProductPhoto(models.Model): - product = models.ForeignKey(Product, on_delete=models.CASCADE) - image = models.ImageField(upload_to='products/images') - - def __str__(self): - return self.product.name - - def delete(self, *args, **kwargs): - storage, path = self.image.storage, self.image.path - - super(ProductPhoto, self).delete(*args, **kwargs) - storage.delete(path) - - # def save(self, *args, **kwargs): - # super().save(*args, **kwargs) - - # img = Image.open(self.image.path) - # if img.height > 400 or img.width > 400: - # output_size = (400, 400) - # img.thumbnail(output_size) - # img.save(self.image.path) - - class Coupon(models.Model): type = models.CharField( max_length=20, diff --git a/src/dashboard/forms.py b/src/dashboard/forms.py index 3d75eff..b43e9d0 100644 --- a/src/dashboard/forms.py +++ b/src/dashboard/forms.py @@ -3,6 +3,7 @@ from django import forms from core import OrderStatus from core.models import ( + ProductVariant, Order, OrderLine, ShippingRate, @@ -14,6 +15,27 @@ from core.models import ( logger = logging.getLogger(__name__) +class ProductVariantUpdateForm(forms.ModelForm): + class Meta: + model = ProductVariant + fields = [ + 'name', + 'sku', + 'price', + 'weight', + 'track_inventory', + 'stock', + 'image' + ] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if self.instance: + self.fields['image'].queryset = ProductPhoto.objects.filter( + product=self.instance.product + ) + + class CouponForm(forms.ModelForm): class Meta: model = Coupon diff --git a/src/dashboard/templates/dashboard/order_detail.html b/src/dashboard/templates/dashboard/order_detail.html index 6cd9a93..3326809 100644 --- a/src/dashboard/templates/dashboard/order_detail.html +++ b/src/dashboard/templates/dashboard/order_detail.html @@ -25,7 +25,11 @@