Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Cycle(models.Model):
- STARS = (
- (1, 'one'),
- (2, 'two'),
- (3, 'three'),
- (4, 'four'),
- (5, 'five'),
- )
- category = models.ForeignKey(Category, related_name='cycle', on_delete=models.CASCADE)
- supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
- name = models.CharField(max_length=120, db_index=True)
- slug = models.SlugField(max_length=120, db_index=True)
- image = models.ImageField(upload_to='upload/cycle/%Y/%m/%d', blank=True)
- description = models.TextField(blank=True)
- price = models.DecimalField(max_digits=10, decimal_places=2)
- stock = models.PositiveIntegerField()
- vote = models.SmallIntegerField(choices=STARS, default=5)
- available = models.BooleanField(default=True)
- created = models.DateTimeField(auto_now_add=True)
- updated = models.DateTimeField(auto_now=True)
- class Meta:
- ordering = ('name', )
- verbose_name = 'Cycle'
- verbose_name_plural = 'Cycles'
- def __str__(self):
- return self.name
- class Cart(models.Model):
- user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
- items = models.ManyToManyField(Cycle, through='CartItem')
- timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
- updated = models.DateTimeField(auto_now_add=False, auto_now=True)
- subtotal = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
- tax_percentage = models.DecimalField(max_digits=10, decimal_places=5, default=0.085)
- tax_total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
- total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
- active = models.BooleanField(default=True)
- objects = CartManager()
- class Meta:
- verbose_name = 'Cart'
- verbose_name_plural = 'Carts'
- def __str__(self):
- return str(self.id)
- def update_total(self):
- subtotal = 0
- items = self.cartitem_set.all()
- for item in items:
- subtotal += item.line_item_total
- self.subtotal = '%.2f'%(subtotal)
- self.save()
- def is_complete(self):
- self.active = False
- self.save()
- class CartItem(models.Model):
- cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
- item = models.ForeignKey(Cycle, on_delete=models.CASCADE)
- quantity = models.PositiveIntegerField(default=1)
- line_item_total = models.DecimalField(max_digits=10, decimal_places=2)
- class Meta:
- verbose_name = 'Cart Item'
- verbose_name_plural = 'Cart Items'
Add Comment
Please, Sign In to add comment