forked from divanov11/ecom_steps
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
|
||
# Create your models here. | ||
|
||
class Customer(models.Model): | ||
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE) | ||
name = models.CharField(max_length=200, null=True) | ||
email = models.CharField(max_length=200) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class Product(models.Model): | ||
name = models.CharField(max_length=200) | ||
price = models.FloatField() | ||
digital = models.BooleanField(default=False,null=True, blank=True) | ||
image = models.ImageField(null=True, blank=True) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
@property | ||
def imageURL(self): | ||
try: | ||
url = self.image.url | ||
except: | ||
url = '' | ||
return url | ||
|
||
class Order(models.Model): | ||
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True, blank=True) | ||
date_ordered = models.DateTimeField(auto_now_add=True) | ||
complete = models.BooleanField(default=False) | ||
transaction_id = models.CharField(max_length=100, null=True) | ||
|
||
def __str__(self): | ||
return str(self.id) | ||
|
||
@property | ||
def get_cart_total(self): | ||
orderitems = self.orderitem_set.all() | ||
total = sum([item.get_total for item in orderitems]) | ||
return total | ||
|
||
@property | ||
def get_cart_items(self): | ||
orderitems = self.orderitem_set.all() | ||
total = sum([item.quantity for item in orderitems]) | ||
return total | ||
|
||
class OrderItem(models.Model): | ||
product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True) | ||
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True) | ||
quantity = models.IntegerField(default=0, null=True, blank=True) | ||
date_added = models.DateTimeField(auto_now_add=True) | ||
|
||
@property | ||
def get_total(self): | ||
total = self.product.price * self.quantity | ||
return total | ||
|
||
class ShippingAddress(models.Model): | ||
customer = models.ForeignKey(Customer, on_delete=models.SET_NULL, null=True) | ||
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True) | ||
address = models.CharField(max_length=200, null=False) | ||
city = models.CharField(max_length=200, null=False) | ||
state = models.CharField(max_length=200, null=False) | ||
zipcode = models.CharField(max_length=200, null=False) | ||
date_added = models.DateTimeField(auto_now_add=True) | ||
|
||
def __str__(self): | ||
return self.address |