Skip to content

Commit

Permalink
Book can be in multiple users' carts
Browse files Browse the repository at this point in the history
  • Loading branch information
anlandu committed Apr 27, 2020
1 parent 3e182e0 commit 8a702cc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
14 changes: 4 additions & 10 deletions payments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def success(request):
pt = PendingTransaction(user=u, balance=transaction.price, date_transacted=timezone.now(), date_settled=one_week_in_future())
pt.save()
transaction.has_been_sold = True
transaction.cart = None
transaction.cart.clear()
transaction.save()
transaction.sold_date = datetime.now()
sold_items.append(transaction)
Expand Down Expand Up @@ -95,18 +95,12 @@ def cancelled(request):
def cart_functions(user, listing_id, fn):
listing = get_object_or_404(ProductListing, pk=listing_id)
if fn == "add":
if listing.cart is not None:
if listing.cart is user.cart:
return JsonResponse({'status': 'success', 'title': listing.textbook.title, 'seller': listing.user.email})
return JsonResponse({'status': "error - already in another user's cart"})
listing.cart = user.cart
listing.cart.add(user.cart)
listing.save()
return JsonResponse({'status': 'success', 'title': listing.textbook.title, 'seller': listing.user.email})
elif fn == "remove":
if listing.cart != user.cart:
if not user.cart in listing.cart.all():
return JsonResponse({'status': "error - not authorized to perform this action"})
if listing.cart is None:
return JsonResponse({'status': "error - this item was not in the user's cart"})
listing.cart = None
listing.cart.remove(user.cart)
listing.save()
return JsonResponse({'status': 'success'})
22 changes: 22 additions & 0 deletions textbook_exchange/migrations/0003_auto_20200427_1338.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.0.3 on 2020-04-27 19:38

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('textbook_exchange', '0002_productlisting_picture_upload'),
]

operations = [
migrations.RemoveField(
model_name='productlisting',
name='cart',
),
migrations.AddField(
model_name='productlisting',
name='cart',
field=models.ManyToManyField(blank=True, to='textbook_exchange.Cart'),
),
]
2 changes: 1 addition & 1 deletion textbook_exchange/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class ProductListing(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
textbook = models.ForeignKey(Textbook, on_delete=models.CASCADE)
class_object = models.ManyToManyField(Class) # on_delete for ManyToManyField?
cart = models.ForeignKey(Cart, on_delete=models.CASCADE, null=True, blank=True) # should prevent a product listing from being in multiple carts at once
cart = models.ManyToManyField(Cart, blank=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
condition = models.CharField(max_length=10) # 'likenew', 'verygood', 'good', 'acceptable'
picture_url = models.CharField(max_length=500, default="")
Expand Down
7 changes: 3 additions & 4 deletions textbook_exchange/templates/textbook_exchange/faq.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ <h5 class="mb-0">
<div class="card-header" id="headingFour" data-toggle="collapse" data-target="#collapseFour" aria-expanded="false" aria-controls="collapseFour">
<h5 class="mb-0">
<button class="btn btn-link collapsed">
I just saw a book in the buy page! Where did it go?
A book in my cart just disappeared! Where did it go?
</button>
</h5>
</div>
<div id="collapseFour" class="collapse" aria-labelledby="headingFour" data-parent="#accordion">
<div class="card-body">
When someone adds a book to their cart, it is removed from the available book listings.
Don't worry though, once an item is added to someone's cart, they have 15 minutes
to finish the checkout process or it will be removed from their cart.
Multiple people can have a book in their cart. Therefore, it is possible that someone else already bought
it or that the seller manually marked it as sold. Try searching for other copies of the book!
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions textbook_exchange/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def post(self, request, *args, **kwargs):
if 'sold_listing' in self.request.POST:
listing_id = self.request.POST.get('sold_listing')
listing = ProductListing.objects.get(pk=listing_id)
listing.cart=None
listing.cart.clear()
listing.has_been_sold = True
listing.sold_date = datetime.now()
listing.save()
Expand Down Expand Up @@ -281,7 +281,7 @@ def get_queryset(self, *args, **kwargs):

textbook = get_object_or_404(Textbook, isbn13=url_ibsn)
product_listings = textbook.productlisting_set.all()
queryset = product_listings.filter(has_been_sold=False, cart=None)
queryset = product_listings.filter(has_been_sold=False)

if url_ordering is not None:
queryset = queryset.order_by(url_ordering)
Expand Down

0 comments on commit 8a702cc

Please sign in to comment.