def next_stage(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): run_time = utils.update_stage(self) stage_index = self.auction_document['current_stage'] self.auction_document['stages'][stage_index - 1].update({ 'passed': True }) if stage['type'].startswith(DUTCH): LOGGER.info( '---------------- SWITCH DUTCH VALUE ----------------' ) self.auction_document['stages'][stage_index]['time']\ = run_time if stage_index == 1: self.auction_document['current_phase'] = DUTCH self.audit['timeline'][DUTCH]['timeline']['start']\ = run_time old = self.auction_document['stages'][stage_index - 1].get( 'amount', '' ) or self.auction_document['initial_value'] LOGGER.info('Switched dutch phase value from {} to {}'.format( old, stage['amount']) ) turn = 'turn_{}'.format(stage_index) self.audit['timeline'][DUTCH][turn] = { 'amount': stage['amount'], 'time': run_time, } else: self.end_dutch()
def switch_to_sealedbid(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): self._end_sealedbid = Event() run_time = utils.update_stage(self) self.auction_document['current_phase'] = SEALEDBID self.audit['timeline'][SEALEDBID]['timeline']['start'] =\ run_time spawn(self.add_bid) LOGGER.info("Swithed auction to {} phase".format(SEALEDBID))
def start_auction(self): self.generate_request_id() self.audit['timeline']['auction_start']['time']\ = datetime.now(tzlocal()).isoformat() LOGGER.info('---------------- Start auction ----------------', extra={ "JOURNAL_REQUEST_ID": self.request_id, "MESSAGE_ID": AUCTION_WORKER_SERVICE_END_FIRST_PAUSE }) self.get_auction_info() with lock_bids(self), update_auction_document(self): self.auction_document["current_stage"] = 0 LOGGER.info("Switched current stage to {}".format( self.auction_document['current_stage']))
def form_handler(): auction = app.config['auction'] form = app.bids_form.from_json(request.json) form.auction = auction form.document = auction.auction_document current_time = datetime.now(timezone('Europe/Kiev')) current_phase = form.document.get('current_phase') if not form.validate(): app.logger.info( "Bidder {} with client_id {} wants place bid {} in {} on phase {} " "with errors {}".format(request.json.get('bidder_id', 'None'), session.get('client_id', ''), request.json.get('bid', 'None'), current_time.isoformat(), current_phase, repr(form.errors)), extra=prepare_extra_journal_fields(request.headers)) return {'status': 'failed', 'errors': form.errors} if current_phase == DUTCH: with lock_bids(auction): ok = auction.add_dutch_winner({ 'amount': str(form.data['bid']), 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'] }) if not isinstance(ok, Exception): app.logger.info("Bidder {} with client {} has won" " dutch on value {}".format( form.data['bidder_id'], session.get('client_id'), form.data['bid'])) return {"status": "ok", "data": form.data} else: app.logger.info("Bidder {} with client_id {} wants place" " bid {} in {} on dutch " "with errors {}".format( request.json.get('bidder_id', 'None'), session.get('client_id'), request.json.get('bid', 'None'), current_time.isoformat(), repr(ok)), extra=prepare_extra_journal_fields( request.headers)) return {"status": "failed", "errors": [repr(ok)]} elif current_phase == SEALEDBID: try: auction.bids_queue.put({ 'amount': str(form.data['bid']), 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'] }) return {"status": "ok", "data": form.data} except Exception as e: return {"status": "failed", "errors": [repr(e)]} elif current_phase == BESTBID: ok = auction.add_bestbid({ 'amount': str(form.data['bid']), 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'] }) if not isinstance(ok, Exception): app.logger.info( "Bidder {} with client {} has won dutch on value {}".format( form.data['bidder_id'], session.get('client_id'), form.data['bid'])) return {"status": "ok", "data": form.data} else: app.logger.info( "Bidder {} with client_id {} wants place" " bid {} in {} on dutch " "with errors {}".format(request.json.get('bidder_id', 'None'), session.get('client_id'), request.json.get('bid', 'None'), current_time.isoformat(), repr(ok)), extra=prepare_extra_journal_fields(request.headers)) return {"status": "failed", "errors": [repr(ok)]} else: return { 'status': 'failed', 'errors': { 'form': ['Bids period expired.'] } }
def switch_to_bestbid(self, stage): with utils.lock_bids(self), utils.update_auction_document(self): self.auction_document['current_phase'] = BESTBID self.audit['timeline'][BESTBID]['timeline']['start'] = utils.update_stage(self)
def form_handler(): auction = app.config['auction'] raw_data = request.json if 'bid' in raw_data: raw_data['bid'] = str(raw_data['bid']) form = app.bids_form.from_json(raw_data) form.auction = auction form.document = auction.auction_document current_time = datetime.now(timezone('Europe/Kiev')) current_phase = form.document.get('current_phase') current_stage = form.document.get('current_stage') if not form.validate(): app.logger.info( "Bidder {} with client_id {} wants place bid {} in {} on phase {} " "with errors {}".format(request.json.get('bidder_id', 'None'), session.get('client_id', ''), request.json.get('bid', 'None'), current_time.isoformat(), current_phase, repr(form.errors)), extra=prepare_extra_journal_fields(request.headers)) return {'status': 'failed', 'errors': form.errors} if current_phase == DUTCH: with lock_bids(auction): bidder_id = form.data['bidder_id'] if bidder_id not in auction.mapping: auction.get_auction_info() if bidder_id not in auction.mapping: app.logger.fatal( "CRITICAL! Bad bidder, that not registered in API" ) # XXX TODO create a way to ban this user return {"status": "failed", "errors": [["Bad bidder!"]]} ok = auction.add_dutch_winner({ 'amount': form.data['bid'], 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'], 'current_stage': current_stage }) if not isinstance(ok, Exception): app.logger.info("Bidder {} with client {} has won" " dutch on value {}".format( form.data['bidder_id'], session.get('client_id'), form.data['bid'])) return {"status": "ok", "data": form.data} else: app.logger.info("Bidder {} with client_id {} wants place" " bid {} in {} on dutch " "with errors {}".format( request.json.get('bidder_id', 'None'), session.get('client_id'), request.json.get('bid', 'None'), current_time.isoformat(), repr(ok)), extra=prepare_extra_journal_fields( request.headers)) return {"status": "failed", "errors": [[repr(ok)]]} elif current_phase == SEALEDBID: try: if hasattr(auction, '_end_sealedbid'): if not auction._end_sealedbid.is_set(): auction.bids_queue.put({ 'amount': form.data['bid'], 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'] }) return {"status": "ok", "data": form.data} return {"status": "failed", "errors": ['Forbidden']} except Exception as e: return {"status": "failed", "errors": [repr(e)]} elif current_phase == BESTBID: ok = auction.add_bestbid({ 'amount': form.data['bid'], 'time': current_time.isoformat(), 'bidder_id': form.data['bidder_id'] }) if not isinstance(ok, Exception): app.logger.info( "Bidder {} with client {} has won dutch on value {}".format( form.data['bidder_id'], session.get('client_id'), form.data['bid'])) return {"status": "ok", "data": form.data} else: app.logger.info( "Bidder {} with client_id {} wants place" " bid {} in {} on dutch " "with errors {}".format(request.json.get('bidder_id', 'None'), session.get('client_id'), request.json.get('bid', 'None'), current_time.isoformat(), repr(ok)), extra=prepare_extra_journal_fields(request.headers)) return {"status": "failed", "errors": [repr(ok)]} else: return { 'status': 'failed', 'errors': { 'form': ['Bids period expired.'] } }
def switch_to_bestbid(self, stage): with simple.lock_bids(self), simple.update_auction_document(self): run_time = simple.update_stage(self) self.auction_document['current_phase'] = BESTBID self.audit['timeline'][BESTBID]['timeline']['start'] = run_time