class TestPeerShop(TestCase): def setUp(self): user = User.objects.create_user('test', '', 'test') self.item = Item( user=user, image=PROJECT_PATH + '/test/IMG_0699.JPG', thumb=PROJECT_PATH + '/test/IMG_0699_thumbnail.JPG', title='keyboard', description='kkk', price=10 ) super(Item, self.item).save() user2 = User.objects.create_user('test2', '', 'test2') self.bid = Bid( bidUser=user2, bidPrice=10, itemUser=user, item=self.item, status='-' ) self.bid.save() def test_views_integration(self): c = Client() response = c.get(reverse('peerShop:main')) assert response.status_code == 200 assert 'PeerShop' in response.content response = c.get(reverse('peerShop:bid-list')) assert response.status_code == 200 assert 'PeerShop' in response.content response = c.get(reverse('peerShop:shop', args=('test',))) assert response.status_code == 200 assert 'PeerShop' in response.content response = c.get(reverse('peerShop:item-detail', args=(self.item.id,))) assert response.status_code == 200 assert self.item.title in response.content def test_login_view(self): c = Client() assert c.login(username='******', password='******') response = c.get(reverse('peerShop:shop', args=('test',))) assert response.status_code == 200 assert 'PeerShop' in response.content def test_bid_delete(self): c = Client() assert c.login(username='******', password='******') response = c.post( reverse('peerShop:bid-delete', args=(self.bid.id,)), ) assert response.status_code == 200 assert 'Deleted' in response.content, response.content assert not Bid.objects.filter(id=self.bid.id).exists()
def bidding(request, user_id, auction_id): auction = get_object_or_404(Auction, pk=auction_id) # bids = Bid.objects.filter(id=auction_id) if request == "POST": bid = Bid() bid.bidder = user_id bid.bid_amount = request.POST['bid_amount'] bid.save() return render(request, 'auction_detail.html', {'bid': bid}) return render(request, 'bidding.html', {{'auction': auction}})
def bid(request, id): if request.method == "POST"\ and len(Auction.objects.filter(id=id)) > 0\ and request.user is not Auction.objects.get(id=id).seller\ and not Auction.objects.get(id=id).resolved\ and not Auction.objects.get(id=id).banned: getcontext().prec = 2 auction = Auction.objects.get(id=id) bid = Bid() bid.auction = auction bid.bidder = request.user price = Decimal(request.POST["price"]) bids = Bid.objects.filter(auction_id=auction).order_by("price") if len(bids) > 0 and bids.last().bidder.id is request.user.id: messages.add_message(request, messages.ERROR, "You already have the highest bid!") if request.POST.get("next") is not None: return redirect(request.POST.get("next")) else: return HttpResponseRedirect('/auction/' + id + '/') if len(bids) > 0 and price > bids.last().price and price.as_tuple( ).exponent == -2: bid.price = price elif price >= auction.priceMin and price.as_tuple().exponent == -2: bid.price = price else: messages.add_message( request, messages.ERROR, "The bid must exceed the minimum price or the highest bid, whichever is higher, " "by at least 0.01 (always use 2 decimals).") if request.POST.get("next") is not None: return redirect(request.POST.get("next")) else: return HttpResponseRedirect('/auction/' + id + '/') bid.time = datetime.now() if auction.a_hash != Auction.objects.get(id=id).a_hash: messages.add_message( request, messages.ERROR, "The auction has either been edited or a new bid has been made since last time the " "page was loaded. Please try bidding again.") return redirect('/auction/' + id + '/') bid.save() auction.a_hash = hash(auction.name + auction.description + str(auction.due) + str( Bid.objects.filter(auction_id=id).order_by( "price").first().price) + salt) auction.save() # auction.new_bid_notify() update_session_stats(request, "bid") messages.add_message(request, messages.INFO, "Bid created") return redirect(request.POST.get("next"))
def project_view(request, project_id): """ Renders an individual project page, where users can ask questions, look at asked questions/responses, and submit a bid. """ proj = Project.objects.get(id=int(project_id)) bid_success = False if request.method == "POST": # User submitted a bid on this project form = BidSubmissionForm(request.POST) if form.is_valid(): team_members = form.cleaned_data["team_members"] description = form.cleaned_data["description"] section = Section.objects.get(students__id=request.user.id) new_bid = Bid(team_members=team_members, description=description, project=proj, is_approved=False, student=request.user) new_bid.save() request.user.profile.bids.add(new_bid) bid_success = True # TODO notify the user on the UI that the bid was submitted new_notification = Notification( recipient=request.user, subject="Bid Submitted", text="You submitted a bid on '{}' with team members {}.\ If the bid is awarded, you will be \ notified here.".format( proj.name, team_members)) new_notification.save() bid_on = request.user.profile.bids.filter(project=proj).exists() form = BidSubmissionForm() questions = Question.objects.filter(project__id=proj.id) question_form = QuestionForm() context = { "project": proj, "form": form, "bid_success": bid_success, # For showing a message that the bid was successfully saved "question_form": question_form, "questions": questions, "bid_on": bid_on } return render(request, "project.html", context)
def bid(request, auction_id): """ Allows a user to bid on a particular item """ if request.method != 'POST': return JsonResponse({'error': 'Must be called with post'}, status=405) auctions = Auction.objects.filter(id=auction_id) if len(auctions) == 0: return JsonResponse({'error': 'That auction does not exist'}, status=404) auction = auctions[0] if pytz.utc.localize(datetime.now()) > auction.expires: return JsonResponse({'error': 'This auction has ended'}) try: bid_amount = price_to_cents(request.POST.get('bprice')) except (TypeError, ValueError) as err: print err return JsonResponse({'error': "Invalid input for bid amount"}, status=400) try: with transaction.atomic(): bids = Bid.objects.filter(auction=auction).order_by('-bid_amount') if (len(bids) > 0 and bids[0].bid_amount >= bid_amount) or ( auction.list_price > bid_amount): return JsonResponse( {'error': "Entered amount is lower than current bid"}) bid = Bid() bid.bidder = request.user bid.auction = auction bid.bid_amount = bid_amount bid.save() auction.cur_price = bid_amount auction.save() AUCTIONEER.bid(auction, bid) except IntegrityError as err: print err return JsonResponse({'error': "You've already been outbid"}) return JsonResponse({'success': "You're the highest bidder"})
def bid(request, auction_id): """ Allows a user to bid on a particular item """ if request.method != 'POST': return JsonResponse({'error': 'Must be called with post'}, status=405) auctions = Auction.objects.filter(id=auction_id) if len(auctions) == 0: return JsonResponse({'error': 'That auction does not exist'}, status=404) auction = auctions[0] if pytz.utc.localize(datetime.now()) > auction.expires: return JsonResponse({'error': 'This auction has ended'}) try: bid_amount = price_to_cents(request.POST.get('bprice')) except (TypeError, ValueError) as err: print err return JsonResponse({'error': "Invalid input for bid amount"}, status=400) try: with transaction.atomic(): bids = Bid.objects.filter(auction=auction).order_by('-bid_amount') if (len(bids) > 0 and bids[0].bid_amount >= bid_amount) or (auction.list_price > bid_amount): return JsonResponse({'error': "Entered amount is lower than current bid"}) bid = Bid() bid.bidder = request.user bid.auction = auction bid.bid_amount = bid_amount bid.save() auction.cur_price = bid_amount auction.save() AUCTIONEER.bid(auction, bid) except IntegrityError as err: print err return JsonResponse({'error': "You've already been outbid"}) return JsonResponse({'success': "You're the highest bidder"})
def bid(request, nfl_id): u = request.user allow_bids = u.is_authenticated() and (not settings.LOCK_BIDS) and util.is_1_waiver_period() if allow_bids: p = Player.objects.get(nfl_id=nfl_id) team = Team.objects.get(owner=u) roster = Player.objects.filter(dflteam=team).order_by("position") if request.method == "POST": val = int(request.POST.get("bidvalue")) if val < 0: val = 0 if val > team.account: val = team.account pk_to_drop = request.POST.get("Drop") dropee = Player.objects.get(pk=pk_to_drop) priority = int(request.POST.get("Priority")) # priority can not be the same as any other unprocessed bids with same drop same_drop = Bid.objects.filter(team=team).filter(processed=False).filter(drop=dropee) other_prios = [] for sd in same_drop: other_prios.append(sd.priority) while priority in other_prios: priority += 1 b = Bid(team=team, amount=val, player=p, drop=dropee, priority=priority) b.save() # if bid-sum is larger than account, all bids must have unique prio bidsum = Bid.objects.filter(team=team).filter(processed=False).aggregate(Sum("amount")) if bidsum["amount__sum"] > team.account: active_bids = Bid.objects.filter(team=team).filter(processed=False).order_by("priority", "date") count = 0 for b in active_bids: count += 1 b.priority = count b.save() return HttpResponseRedirect("/team") else: return render(request, "bid.html", {"player": p, "roster": roster, "team": team}) else: return HttpResponseRedirect("/")
def make_bid(args): listing_id = args['listing_id'] bidder_email = args['bidder_email'] bid_amount = args['bid_amount'] listing = Listing.objects.filter(listing_id=listing_id) bidder_profile = Profile.objects.get(email=bidder_email) if len(listing) == 0: return False, ERROR_NO_SUCH_LISTING bids = Bid.objects.filter(listing_id=listing_id) bid_id = str(listing_id) + str(len(bids)) bid = Bid(listing_id=listing_id, bid_id=bid_id, bidder_email=bidder_email, amount=bid_amount, status=0) bid.save() recent_bids = json.loads(str(bidder_profile.recent_bids)) if len(recent_bids) == NUM_RECENT_BIDS: del recent_bids[0] recent_bids.append(listing_id) bidder_profile.__dict__['recent_bids'] = json.dumps(recent_bids) bidder_profile.save() listing = listing[0] listing_bids = json.loads(listing.bids) listing_bids.append(bid_id) listing.__dict__['bids'] = json.dumps(listing_bids) listing.save() owner = Profile.objects.get(profile_id=listing.profile_id) locale.setlocale(locale.LC_ALL, '') notification_title = 'Someone made a bid!' notification_description = '%s made a bid of %s!' % (bidder_email, locale.currency(float(bid_amount))) extras = {'bid_id' : bid_id} notification.create(notification_title, notification_description, owner.email, owner.password, extras) return True, None
def create_bid(request, bid_id): if request.method == 'GET': data = {} if bid_id: # When editing a bid bid = Bid.objects.get(id=bid_id) data = bid.as_dict() data['bid_id'] = bid_id return render(request, 'new_bid_form.html', data) if request.method == 'POST': data = request.POST if not bid_id: # Create a new database Bid bid = Bid() else: # When editing bid = Bid.objects.get(id=bid_id) # Delete Respondents group Respondent.objects.filter(bid__id=bid_id).delete() # Delete Deliverables Deliverable.objects.filter(bid__id=bid_id).delete() bid.create_from_dict(data, request.user) bid.save() nbr_group = int(data.get("saved_groups", '0')) i = 0 while (i < nbr_group): respondent = Respondent() respondent.create_from_dict(data, request.user, bid, str(i)) respondent.save() methodology = Methodology() methodology.create_from_dict(data, request.user, respondent, str(i)) methodology.save() i += 1 # Create / Save Deliverables if quantity is not null deliverables = { 'english_to_english': 'Transcripts from English to English', 'french_to_english': 'Transcripts from French to English', 'content_analysis': 'Content Analysis', 'topline_report': 'Topline report', 'full_report': 'Full Report', 'summary_report': 'Summary Report', 'discussion_guide_design': 'Discussion Guide Design', 'screener_design': 'Screener Design', 'other_deliverable': data.get('other_deliverable', '') } for key in deliverables.keys(): qty = data.get(key + '_qty', '') name = key if key == 'other_deliverable': name = deliverables[key] if qty != '': x = int(qty) deliverable = Deliverable() deliverable.create_from_dict(data, request.user, bid, key, x, name) deliverable.save() return HttpResponseRedirect('/crc/list_bids')