def update_buyin_image(user_id, id): close_time = utils.designated_trmnt_close_time() flight = Flights.query.get(id) if flight is None or flight.start_at < close_time: raise APIException( "Cannot buy into this flight. It either has ended, or does not exist" ) buyin = Buy_ins(user_id=user_id, flight_id=id) db.session.add(buyin) db.session.flush() if 'image' not in request.files: raise APIException('"image" property missing in the files array', 404) utils.resolve_google_credentials() result = utils.cloudinary_uploader(type='buyin', image=request.files['image'], public_id='buyin' + str(buyin.id), tags=[ 'buyin_receipt', 'user_' + str(user_id), 'buyin_' + str(buyin.id) ]) def terminate_buyin(): cloudinary.uploader.destroy('buyin' + str(buyin.id)) db.session.rollback() raise APIException('Take another photo') ocr_data = utils.ocr_reading(result) if list(ocr_data) == []: terminate_buyin() regex_data = regex.hard_rock(ocr_data) nones = 0 for val in regex_data.values(): if val is None: nones += 1 if nones > 2: terminate_buyin() buyin.receipt_img_url = result['secure_url'] db.session.commit() return jsonify({'buyin_id': buyin.id, 'receipt_data': regex_data})
def update_buyin_image(user_id, id): close_time = utils.designated_trmnt_close_time() flight = Flights.query.get(id) # if flight is None or flight.start_at < close_time: # raise APIException( # "Cannot buy into this flight. It either has ended, or does not exist") buyin = Buy_ins(user_id=user_id, flight_id=id) db.session.add(buyin) db.session.flush() if 'image' not in request.files: raise APIException('"image" property missing in the files array', 404) utils.resolve_google_credentials() result = cloudinary.uploader.upload(request.files['image'], public_id='buyin' + str(buyin.id), crop='limit', width=1000, height=1000, tags=[ 'buyin_receipt', 'user_' + str(user_id), 'buyin_' + str(buyin.id) ]) def terminate_buyin(): cloudinary.uploader.destroy('buyin' + str(buyin.id)) db.session.rollback() raise APIException('Take another photo') ocr_data = utils.ocr_reading(result) if list(ocr_data) == []: terminate_buyin() regex_data = regex.hard_rock(ocr_data) nones = 0 for val in regex_data.values(): if val is None: nones += 1 if nones > 2: terminate_buyin() if None in [regex_data['player_name'], regex_data['casino']]: terminate_buyin() ############################################# # Verify regex data against tournament data # Check player name validation = {} user = Profiles.query.get(user_id) condition = user.first_name.lower() in regex_data['player_name'].lower( ) validation['first_name'] = { 'ocr': regex_data['player_name'], 'database': user.first_name, 'valid': True if condition else False } condition = user.last_name.lower() in regex_data['player_name'].lower() validation['last_name'] = { 'ocr': regex_data['player_name'], 'database': user.last_name, 'valid': True if condition else False } condition = user.nickname.lower() in regex_data['player_name'].lower() validation['nickname'] = { 'ocr': regex_data['player_name'], 'database': user.nickname, 'valid': True if condition else False } # Check casino name trmnt_casino = flight.tournament.casino validation['casino'] = { 'ocr': regex_data['casino'], 'database': trmnt_casino, 'valid': True } casino_names = regex_data['casino'].split(' ') for name in casino_names: if name.lower() not in trmnt_casino.lower(): validation['casino']['valid'] = False break # Check date regex_timestamp = regex_data['receipt_timestamp'] try: dt = datetime.strptime(regex_timestamp, '%B %d, %Y %I:%M %p') except: dt = None valid = False if dt is not None: close = flight.start_at + hours_to_close_tournament if datetime.utcnow() < close: valid = True validation['datetime'] = { 'ocr': regex_timestamp, 'flight_start_time': flight.start_at, 'valid': valid } buyin.receipt_img_url = result['secure_url'] db.session.commit() return jsonify({ 'buyin_id': buyin.id, 'receipt_data': regex_data, 'validation': validation, 'ocr_data': ocr_data })