def test_valid_make_bid_form(self): data = {'offer_price':100} form = UserBidForm(data=data) print form.errors self.assertTrue(form.is_valid()) testcat = add_cat('testcat') testitem = add_item(title='testitem',asking_price=100, category=testcat,owner=self.testusersp) response = self.c.post(reverse('shop:makebid', kwargs={'item_slug':'testitem'}),data=data) self.assertEqual(response.status_code, 302) self.assertEqual(Notification.objects.count(), 1)
def test_valid_update_bid_form(self): testcat = add_cat('testcat') testitem = add_item(title='testitem',asking_price=100, category=testcat,owner=self.testusersp) UserBid.objects.create(user=self.testuser,sale_item=testitem,offer_price=200) # Checking the bid before the form previousbid = UserBid.objects.get(user=self.testuser,sale_item=testitem) self.assertEqual(previousbid.offer_price, 200) data = {'offer_price':300} form = UserBidForm(data=data) print form.errors self.assertTrue(form.is_valid()) response = self.c.post(reverse('shop:makebid', kwargs={'item_slug':'testitem'}),data=data) self.assertEqual(response.status_code, 302) # Checking the bid after the form updatedbid = UserBid.objects.get(user=self.testuser,sale_item=testitem) self.assertEqual(updatedbid.offer_price, 300) self.assertEqual(Notification.objects.count(), 1)
def make_bid(request, item_slug): item = SaleItem.objects.get(slug=item_slug) context_dict = {'item': item} try: highestbid = UserBid.objects.filter(sale_item=item).order_by('-offer_price')[0] context_dict['highestbid'] = highestbid min_offer = highestbid.offer_price + 1 context_dict['min_offer'] = min_offer except: pass try: userbid = UserBid.objects.get(user=request.user,sale_item=item) except UserBid.DoesNotExist: userbid = UserBid(user=request.user,sale_item=item) if request.method == 'POST': form = UserBidForm(request.POST, instance=userbid) if form.is_valid(): form.save() notify.send(request.user, recipient=item.owner.user, verb=u' made an offer on your item: ', target=item) for ubid in item.userbid_set.all(): if ubid.user == request.user: continue notify.send(request.user, recipient=ubid.user, verb=u' bid on an item that you bid on', target=item) return redirect('shop:item', item.slug) else: print form.errors context_dict['form'] = form else: form = UserBidForm() context_dict['form'] = form return render(request, 'shop/makebid.html', context_dict)