cart maybe?

This commit is contained in:
Martin Quarda 2024-10-13 15:32:29 +02:00
parent 01f3bfcb2a
commit d691b62cdf
3 changed files with 34 additions and 5 deletions

View File

@ -76,7 +76,7 @@ class Product(models.Model):
class Cart(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') items = models.ManyToManyField(Product, through='CartProduct')

View File

@ -19,7 +19,11 @@ import PIL.Image
import random import random
from collections import OrderedDict 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 from alkator.settings import COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_TEST
@ -342,6 +346,20 @@ def products(request):
]), content_type='application/json') ]), 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): def results(request):
results = [] results = []
n = 1 n = 1

View File

@ -80,9 +80,20 @@ class Main extends Component {
}) })
} }
addToCart = (id) =>{ addToCart = (id) =>{
this.setState({ fetch(addr_prefix + "/api/cart/add?id=" + id).then(resp => resp.json()).then(json=>{
cart: [...this.state.cart, id] if(json.reason){
}); this.setState({
status_text: data.reason,
status: "failed",
})
}else{
this.setState({
status_text: data.success,
status: "success",
cart: [...this.state.cart, id],
});
}
})
} }
onSubmit(event){ onSubmit(event){
event.preventDefault(); event.preventDefault();