cart maybe?
This commit is contained in:
parent
01f3bfcb2a
commit
d691b62cdf
@ -76,7 +76,7 @@ class Product(models.Model):
|
||||
|
||||
|
||||
class Cart(models.Model):
|
||||
user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT)
|
||||
user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT, related_name='cart')
|
||||
items = models.ManyToManyField(Product, through='CartProduct')
|
||||
|
||||
|
||||
|
@ -19,7 +19,11 @@ import PIL.Image
|
||||
import random
|
||||
from collections import OrderedDict
|
||||
|
||||
from .models import User, ALKATOR_CHOICES_DICT, ALKATOR_CLASSES, Profile, Racer, Invoice, Product, InvoiceProduct
|
||||
from .models import (
|
||||
User, ALKATOR_CHOICES_DICT, ALKATOR_CLASSES,
|
||||
Profile, Racer, Invoice, Product, InvoiceProduct,
|
||||
Cart, CartProduct
|
||||
)
|
||||
from alkator.settings import COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_TEST
|
||||
|
||||
|
||||
@ -342,6 +346,20 @@ def products(request):
|
||||
]), content_type='application/json')
|
||||
|
||||
|
||||
def cart_add(request):
|
||||
_id = request.GET['id']
|
||||
user = request.user
|
||||
if not user.cart:
|
||||
user.cart = Cart(user=user)
|
||||
user.cart.save()
|
||||
cart_product = CartProduct(
|
||||
product=Product.objects.get(id=_id),
|
||||
cart=user.cart,
|
||||
quantity=1,
|
||||
)
|
||||
cart_product.save()
|
||||
|
||||
|
||||
def results(request):
|
||||
results = []
|
||||
n = 1
|
||||
|
@ -80,10 +80,21 @@ class Main extends Component {
|
||||
})
|
||||
}
|
||||
addToCart = (id) =>{
|
||||
fetch(addr_prefix + "/api/cart/add?id=" + id).then(resp => resp.json()).then(json=>{
|
||||
if(json.reason){
|
||||
this.setState({
|
||||
cart: [...this.state.cart, id]
|
||||
status_text: data.reason,
|
||||
status: "failed",
|
||||
})
|
||||
}else{
|
||||
this.setState({
|
||||
status_text: data.success,
|
||||
status: "success",
|
||||
cart: [...this.state.cart, id],
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
onSubmit(event){
|
||||
event.preventDefault();
|
||||
let form = document.getElementById("form");
|
||||
|
Loading…
x
Reference in New Issue
Block a user