Beispiel #1
0
    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})
Beispiel #2
0
    def ocr():
        import re
        import os

        utils.resolve_google_credentials()

        result = utils.cloudinary_uploader(
            'buyin', request.files['image'], 'ocr',
            ['buyin_receipt', f'user_', f'buyin_'])

        msg = utils.ocr_reading(result)

        import regex
        r = regex.hard_rock(msg)

        return jsonify({**r, 'text': msg})
Beispiel #3
0
 def sendfcmtest(device_token):
     utils.resolve_google_credentials()
     return send_fcm(0,
                     0,
                     data={
                         "token": device_token,
                         "title": "A Tournament Went Live!",
                         "body":
                         "The Alamo Hold'em 2019 tournament is now live",
                         "data": {
                             "type": "tournament",
                             "id": "3",
                             "initialPath": "Tournaments",
                             "finalPath": "TourneyLobby"
                         }
                     })
    def ocr():
        import re
        import os

        utils.resolve_google_credentials()

        result = cloudinary.uploader.upload(
            request.files['image'],
            public_id='ocr',
            crop='limit',
            width=1000,
            height=1000,
            tags=['buyin_receipt', f'user_', f'buyin_'])

        msg = utils.ocr_reading(result)

        import regex
        r = regex.hard_rock(msg)

        return jsonify({**r, 'text': msg})
def send_fcm(template, user_id, data={}):

    utils.resolve_google_credentials()

    # devices = Devices.query.filter_by( user_id = user_id )
    # registration_ids = [device.token for device in devices]

    # if len(registration_ids) == 0 or push_service is None:
    #     return False

    # content = get_template_content(template, data, ['fms'])

    # if 'fms' not in content:
    #     raise APIException(
    #         f'The template {template} does not seem to have a valid FMS version')

    # if 'data_message' not in data:
    #     raise APIException('There is no data for the notification')

    result = push_service.notify_single_device(
        registration_id = data['token'],
        message_title = data['title'],
        message_body = data['body'],
        data_message = data['data']
    )
    # result = push_service.notify_multiple_devices(
    #     registration_ids = registration_ids,
    #     message_title = content['subject'],
    #     message_body = content['fms'],
    #     data_message = data['data_message']
    # )

    if result['failure'] or not result['success']:
        raise APIException('Problem sending the notification')

    return result
    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
        })