Guest User

Untitled

a guest
Jun 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. class Cycle(models.Model):
  2. STARS = (
  3. (1, 'one'),
  4. (2, 'two'),
  5. (3, 'three'),
  6. (4, 'four'),
  7. (5, 'five'),
  8. )
  9.  
  10. category = models.ForeignKey(Category, related_name='cycle', on_delete=models.CASCADE)
  11. supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
  12. name = models.CharField(max_length=120, db_index=True)
  13. slug = models.SlugField(max_length=120, db_index=True)
  14. image = models.ImageField(upload_to='upload/cycle/%Y/%m/%d', blank=True)
  15. description = models.TextField(blank=True)
  16. price = models.DecimalField(max_digits=10, decimal_places=2)
  17. stock = models.PositiveIntegerField()
  18. vote = models.SmallIntegerField(choices=STARS, default=5)
  19. available = models.BooleanField(default=True)
  20. created = models.DateTimeField(auto_now_add=True)
  21. updated = models.DateTimeField(auto_now=True)
  22.  
  23. class Meta:
  24. ordering = ('name', )
  25. verbose_name = 'Cycle'
  26. verbose_name_plural = 'Cycles'
  27.  
  28. def __str__(self):
  29. return self.name
  30.  
  31.  
  32.  
  33. class Cart(models.Model):
  34. user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
  35. items = models.ManyToManyField(Cycle, through='CartItem')
  36. timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
  37. updated = models.DateTimeField(auto_now_add=False, auto_now=True)
  38. subtotal = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
  39. tax_percentage = models.DecimalField(max_digits=10, decimal_places=5, default=0.085)
  40. tax_total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
  41. total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
  42. active = models.BooleanField(default=True)
  43. objects = CartManager()
  44.  
  45. class Meta:
  46. verbose_name = 'Cart'
  47. verbose_name_plural = 'Carts'
  48.  
  49. def __str__(self):
  50. return str(self.id)
  51.  
  52. def update_total(self):
  53. subtotal = 0
  54. items = self.cartitem_set.all()
  55. for item in items:
  56. subtotal += item.line_item_total
  57. self.subtotal = '%.2f'%(subtotal)
  58. self.save()
  59.  
  60. def is_complete(self):
  61. self.active = False
  62. self.save()
  63.  
  64.  
  65.  
  66. class CartItem(models.Model):
  67. cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
  68. item = models.ForeignKey(Cycle, on_delete=models.CASCADE)
  69. quantity = models.PositiveIntegerField(default=1)
  70. line_item_total = models.DecimalField(max_digits=10, decimal_places=2)
  71.  
  72. class Meta:
  73. verbose_name = 'Cart Item'
  74. verbose_name_plural = 'Cart Items'
Add Comment
Please, Sign In to add comment