Beispiel #1
0
def app():
    db_fd, db_path = tempfile.mkstemp()

    app = create_app({
        'TESTING': True,
        'DATABSE': db_path
    })

    with app.app_context():
        init_db()
        get_db().executescript(_data_sql)

    yield app

    os.close(db_fd)
    os.unlink(db_path)
Beispiel #2
0
def all_sales():
    """
    All Dealer Sales
    ---
    ---
    """
    current_app.logger.info('Info level log')
    current_app.logger.warning('Warning level log')

    current_user_cpf = get_jwt_identity()

    current_month = datetime.now().month
    print(current_month)

    db = get_db()
    try:
        result = db.execute(
        '''SELECT cod, value, date, status FROM sale WHERE cpf = ?
        AND substr(date, 6, 2) = ?''', (current_user_cpf, str(current_month))
        ).fetchall()
    except IntegrityError as e:
        return 'ERROR IN SQL QUERY -> ' + e.args[0], 400
    except Exception as e:
        return 'Unknow error -> ' + e.args[0], 400
    else:
        data = {
            'payload': {
                'sales': [],
                'CPF': current_user_cpf,
            }
        }

        result = [list(x) for x in result]

        data['payload']['sales'] = [{
                                        'cod':x[0],
                                        'value': x[1],
                                        'date': x[2],
                                        'status': x[3]
                                    }
                                    for x in result]

        total_value = sum(sale['value'] for sale in data['payload']['sales'])
        
        if total_value <= 1000:
            percent_cashback = 0.1
        elif 1000 > total_value <= 1500:
            percent_cashback = 0.15
        else:
            percent_cashback = 0.2

        for idx, _ in enumerate(data['payload']['sales']):
            data['payload']['sales'][idx]['cashback'] = data['payload']['sales'][idx]['value'] * percent_cashback
            data['payload']['sales'][idx]['cashback_percent'] = str(percent_cashback * 100) + '%'

        print(data)
        return data, 200
Beispiel #3
0
def new_sale():
    """
    New Sale
    New sale by CPF
    ---
    """
    current_app.logger.info('Info level log')
    current_app.logger.warning('Warning level log')

    current_user_cpf = get_jwt_identity()

    if not request.get_json():
        return 'No json was passed', 400

    payload = request.get_json()

    if not 'cod' in payload.keys():
        return 'No cod was passed', 400
    if not 'value' in payload.keys():
        return 'No value was passed', 400

    date = datetime.today().strftime('%Y-%m-%d')

    if current_user_cpf == '15350946056':
        status = 'APROVADO'
    else:
        status = 'EM VALIDACAO'

    db = get_db()
    try:
        db.execute(
        '''INSERT INTO sale (cod, value, date, cpf, status)
            VALUES (?, ?, ?, ?, ?)''', (payload['cod'], payload['value'], date, current_user_cpf, status)
        )
    except IntegrityError as e:
            return 'ERROR IN SQL QUERY -> ' + e.args[0], 400
    except Exception as e:
        return 'Unknow error'
    else:
        db.commit()
        return 'SUCCESS', 200
Beispiel #4
0
def valid():
    """
    Validando Login
    ---
    ---
    """
    current_app.logger.info('Info level log')
    current_app.logger.warning('Warning level log')

    if not request.get_json():
        return 'No json was passed', 400

    payload = request.get_json()

    if not 'cpf' in payload.keys():
        return 'No CPF was passed', 400
    if not 'password' in payload.keys():
        return 'No Password was passed', 400

    db = get_db()
    try:
        retorno = db.execute('SELECT cpf, password FROM dealer where cpf = ?;',
                             (payload['cpf'], )).fetchone()
    except IntegrityError as e:
        return 'ERROR IN SQL QUERY -> ' + e.args[0], 400
    except Exception as e:
        return 'Unknow error'
    else:
        if retorno is None:
            return 'Nobody with this CPF in database', 400
        else:
            if check_password_hash(retorno['password'], payload['password']):
                token = create_access_token(identity=payload['cpf'])
                return {'Authorization': token}
            else:
                print(retorno['password'], payload['password'])
                return 'Wrong Password', 400

    return 'Something was wrong', 200
Beispiel #5
0
def dealer():
    """
    Seja bem vindo
    API do desafio Backend do O Boticario
    ---
    """
    current_app.logger.info('Info level log')
    current_app.logger.warning('Warning level log')

    if not request.get_json():
        return 'No json was passed', 400

    payload = request.get_json()

    if not 'fullname' in payload.keys():
        return 'No name was passed', 400
    elif not 'password' in payload.keys():
        return 'No password was passed', 400
    elif not 'cpf' in payload.keys():
        return 'No cpf was passed', 400
    elif not 'email' in payload.keys():
        return 'No email was passed', 400
    else:
        db = get_db()
        try:
            db.execute(
                '''INSERT INTO dealer (fullname, password, cpf, email)
                VALUES (?, ?, ?, ?)''',
                (payload['fullname'],
                 generate_password_hash(
                     payload['password']), payload['cpf'], payload['email']))
        except IntegrityError as e:
            return 'ERROR IN SQL QUERY -> ' + e.args[0], 400
        except Exception as e:
            return 'Unknow error'
        else:
            db.commit()

        return 'Welcome {0}'.format(payload['fullname']), 200