def claim_new_codes(): if Code.less_than_1000_left(): logger.info( 'We are running out of codes, fetching new ones from API...') new_codes = figure.Code.claim(data={'number': 10000}) Code.bulk_insert(new_codes) logger.info('New codes fetched and saved !')
def record_confirm(): ret = {} recorded = irrp.record_confirm() code_name = request.json["code_name"] if not recorded: return "recording failed" code = Code(name=code_name, code=recorded) code.update(db.session) ret["is_succsess"] = True ret["result"] = code.as_dict() return jsonify(ret)
def send(): params = request.get_json() email = params.get('email') md5 = hashlib.md5() password = params.get('password') new_hash = hashlib.md5(password).hexdigest() try: user_found = User.get(User.email == email) if user_found.password == new_hash: code = Code.create(user_id=user_found.id, value=random.randint(1000, 9999), charge_id='empty') code.save() sender = "Aperture" text = "Here is your code {code} to authentify yourself.".format( code=code.value) sending_message = send_message(sender, text) return jsonify({ 'error': False, 'message': 'Your code has been sent' }), 201 else: return jsonify({ 'error': True, 'message': 'Wrong email or password' }), 400 except Exception as e: return jsonify({'error': True, 'message': e}), 400
def _reveal_code(self, code_breakers: List[CodeBreaker], final_code: Code) -> None: def clear_screen(): if os.name == 'nt': os.system('cls') else: os.system('clear') # reveal a random peg position for each breaker, make sure each reveal is a different position final_code_pegs: List[Peg] = final_code.get_pegs() indexes_random_able = list(range(len(final_code_pegs))) for code_breaker in code_breakers: print( messages.MASTERMIND_REVEAL_GUIDE.format( player_name=code_breaker.get_name())) prompt() index_of_indexes_random_able: int = random.randint( 0, len(indexes_random_able) - 1) random_index_to_reveal: int = indexes_random_able[ index_of_indexes_random_able] indexes_random_able.pop(index_of_indexes_random_able) revealed_peg: Peg = final_code_pegs[random_index_to_reveal] print( messages.MASTERMIND_REVEAL_PEG.format( position=random_index_to_reveal + 1, color=revealed_peg.value)) print(messages.MASTERMIND_CLEAR_SCREEN) prompt() clear_screen()
def index(): code = request.args.get('code', '') if code: Code.objects.delete() Code.objects.insert(Code(code=code)) return redirect('http://wolfware4.com') return render_template('index.html', messages=Message.objects())
def code_create(token: Optional[str] = Header(None), name: str = Form(...), description: str = Form(...), content: str = Form(...), language: str = Form(...)): """ 创建代码 """ user = security.get_current_user(token) code = Code.create(name, description, user.id, content, language) return {"id": code.id}
def main(show_progress, *args, **kwargs): if show_progress: web_page_count = WebPageContent.select().count() progress_bar = ProgressBar(maxval=web_page_count, widgets=[ 'Progress: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' Processing web page ', Counter(), ' / ' + str(web_page_count) + '.' ]) progress_bar.start() # Create a new index for this computation last_compute_index = Code.select(fn.Max(Code.compute_index)).scalar() or 0 compute_index = last_compute_index + 1 # For each web page, we extract all code snippets and create a new record # for each snippet, saving the code's plaintext. code_extractor = CodeExtractor() for web_page_index, web_page in enumerate(WebPageContent.select(), start=1): document = BeautifulSoup(web_page.content, 'html.parser') snippets = code_extractor.extract(document) for snippet in snippets: # Screen snippets to those that have more than one space-delimited word. # This is to avoid storing single words referring to entities in code examples. word_count = len(re.split('\s', snippet.strip())) if word_count > 1: Code.create( compute_index=compute_index, code=snippet, web_page=web_page, ) if show_progress: progress_bar.update(web_page_index) if show_progress: progress_bar.finish()
def _process_prompt_breaker_guessing(self, code_breaker: CodeBreaker, final_code: Code) -> AttemptFeedback: """ prompt breaker to input the code, then return the the feedback of his attempt """ attempt_code: Optional[Code] = None while attempt_code is None: attempt_input: str = prompt() try: attempt_code = Code.parse(attempt_input, self._game_rule) except CodeParsingException: print( MessageBankInterface.get_unparsable_token_mssg( self._game_rule.get_max_code_peg(), self._game_rule.allow_blank())) return code_breaker.make_a_guess(attempt_code, final_code)
def create_payment(): params = request.get_json() token_user = params.get('token') amount = params.get('amount') label = params.get('label') card_number = params.get('card_number') card_month = params.get('card_month') card_year = params.get('card_year') card_cvc = params.get('card_cvc') card = { "number": card_number, "exp_month": card_month, "exp_year": card_year, "cvc": card_cvc } token = stripe.Token.create(card=card) if check_token(token_user) is False: return jsonify({'error': True, 'message': 'Not connected or token expired'}), 400 else: userToken = check_token(token_user) try: charge = stripe.Charge.create(card=token.id, currency="eur", amount=amount, capture=False) payment = Payment.create(amount=amount, label=label, user=userToken.user_id) payment.save() code = Code.create(user_id=userToken.user_id.id, value=random.randint(1000, 9999), charge_id=charge.id) code.save() sender = "Aperture" text = "Here is your code {code} to authentify your transaction.".format(code=code.value) sending_message = send_message(sender, text) return jsonify({'error': False, 'message': "A code to authentify your transaction has been sent to you."}), 201 except Exception as e: return jsonify({'error': True, 'message': '{error}'.format(error=e) }), 400
def auth_user(): params = request.get_json() user_code = params.get('code') user_id = params.get('user_id') try: user = User.get(User.id == user_id) code = Code.get(Code.user_id == user_id) if user_code == code.value: code.delete_instance() token_generated = generate_token() token_model = None try: token_model = Token.get(Token.user_id == user_id) data = model_to_dict(token_model) return jsonify({ 'error': False, 'message': 'User already has a token', 'token': data }) except: token = create_token(token_generated, user_id) data = model_to_dict(token) return jsonify({ 'error': False, 'message': 'Token created', 'token': data }) else: return jsonify({'error': True, 'message': 'Incorrect code'}), 400 except Exception as identifier: return jsonify({ 'error': True, 'message': 'Error during authentification' }), 400
def verify_payment(): params = request.get_json() user_id = params.get('user_id') user_code = params.get('code') try: user = User.get(User.id == user_id) code = Code.get(Code.user_id == user_id) if user_code == code.value: charge = stripe.Charge.retrieve(code.charge_id) charge.capture() code.delete_instance() return jsonify({'error': False, 'transaction': charge.outcome}) else : return jsonify({'error': True, 'message': 'Incorrect code'}), 400 except Exception as identifier: return jsonify({'error': True, 'message': 'Error during process'}), 400
def code_get(id): """ 获取代码 """ code = Code.get_code_by_id(id) return code
def test(): return jsonify({'codes': list(Code.select().dicts())}), 201