Coverage for website/merchandise/models.py: 57.58%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2025-08-14 10:31 +0000

1from django.core.files.storage import storages 

2from django.db import models 

3 

4from thumbnails.fields import ImageField 

5 

6from utils.media.services import get_upload_to_function 

7 

8_merchandise_photo_upload_to = get_upload_to_function("merchandise") 

9 

10 

11class MerchandiseItem(models.Model): 

12 """Merchandise items. 

13 

14 This model describes merchandise items. 

15 """ 

16 

17 #: Name of the merchandise item. 

18 name = models.CharField(max_length=200) 

19 

20 #: Price of the merchandise item 

21 price = models.DecimalField( 

22 max_digits=8, 

23 decimal_places=2, 

24 ) 

25 

26 #: Description of the merchandise item 

27 description = models.TextField() 

28 

29 #: Image of the merchandise item 

30 image = ImageField( 

31 upload_to=_merchandise_photo_upload_to, 

32 storage=storages["public"], 

33 resize_source_to="source", 

34 ) 

35 

36 def __init__(self, *args, **kwargs): 

37 super().__init__(*args, **kwargs) 

38 if self.image: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true

39 self._orig_image = self.image.name 

40 else: 

41 self._orig_image = None 

42 

43 def delete(self, using=None, keep_parents=False): 

44 if self.image.name: 

45 self.image.delete() 

46 return super().delete(using, keep_parents) 

47 

48 def save(self, **kwargs): 

49 super().save(**kwargs) 

50 storage = self.image.storage 

51 

52 if self._orig_image and self._orig_image != self.image.name: 

53 storage.delete(self._orig_image) 

54 self._orig_image = None 

55 

56 def __str__(self): 

57 """Give the name of the merchandise item in the currently active locale. 

58 

59 :return: The name of the merchandise item. 

60 :rtype: str 

61 """ 

62 return str(self.name)