<commit_before>import json
import datetime

from flask import Blueprint, request, session
from google.appengine.ext import ndb

from transaction.model import Transaction, TransactionItem
from user.model import User
from product.model import Product

transaction = Blueprint('transaction', __name__)

@transaction.route('/', methods=['GET'])
def get_all():
    transactions = [t.to_dict() for t in Transaction.query().fetch()]
    return json.dumps(transactions)

@transaction.route('/<int:transaction_id>', methods=['GET'])
def get_by_id(transaction_id):
    transaction = Transaction.get_by_id(transaction_id)

    if not transaction:
        return 'Transaction id %s not found' % (transaction_id), 404

    return json.dumps(transaction.to_dict())

@transaction.route('/', methods=['POST'])
def add():
    logged_user = session['barzinga_user']
    logged_user = User.query().filter(User.email == logged_user["email"]).get()

    products = json.loads(request.form['products'])

    products_list = []
    quantity_table = {}

    for product in products:
        quantity_table[product['id']] = product['quantity']
        products_list.append(ndb.Key(Product, product['id']).get())

    print products_list

    try:
        transaction = Transaction.new(logged_user, products_list, quantity_table)
        transaction.put()
    except Exception as e:
        return str(e), 400

    return '', 204

@transaction.route('/extract', methods=['GET'])
def transactions_user():
    logged_user = session['barzinga_user']
    logged_user = User.query().filter(User.email == logged_user["email"]).get()

    transactions = Transaction.query().filter(Transaction.user == logged_user.key).fetch()

    trans = [];

    for t in transactions:
        transa = {}
        transa['id'] = str(t.key)
        transa['user'] = logged_user.name.encode('utf-8').strip()
        transa['value'] = str(t.value)
        transa['date'] = str(t.date)
        itens = []
        for it in t.items :
            item = {}
            transaction_item = it.get()
            item['product'] = str(transaction_item.product.get().description)
            item['quantity'] = str(transaction_item.quantity)
            itens.append(item)

        transa['itens'] = itens
        trans.append(transa)

    return json.dumps(trans)


@transaction.route('/extract_all', methods=['GET'])
def transactions_alll():
    transactions = Transaction.query().fetch()

    trans = [];

    for t in transactions:
        transa = {}
        transa['id'] = str(t.key)
        transa['user'] = t.user.get().name.encode('utf-8').strip()
        transa['value'] = str(t.value)
        transa['date'] = str(t.date)
        itens = []
        for it in t.items :
            item = {}
            transaction_item = it.get()
            item['product'] = transaction_item.product.get().description
            item['quantity'] = transaction_item.quantity
            itens.append(item)

        transa['itens'] = itens
        trans.append(transa)

    return json.dumps(trans)

@transaction.route('/transactions_all', methods=['GET'], strict_slashes=True)
@transaction.route('/transactions_all/<string:start>/<string:end>', methods=['GET'], strict_slashes=True)
def transactions_all(start=None, end=None):
    if start is None or end is None:
        transactions = Transaction.query().fetch()
    else:
        splitStart = start.split('-')
        from_date = datetime.datetime(year=int(splitStart[2]), month=int(splitStart[1]), day=int(splitStart[0]))
        splitEnd = end.split('-')
        to_date = datetime.datetime(year=int(splitEnd[2]), month=int(splitEnd[1]), day=int(splitEnd[0]))

        transactions = Transaction.query().filter(Transaction.date <= to_date, Transaction.date >= from_date).fetch()

    transactionsJson = [];
    for t in transactions:
        transactionJson = {}
        if t.date is not None:
            transactionJson['date'] = str(t.date.strftime('%d/%m/%y - %H:%M'))
        else:
            transactionJson['date'] = ''
        transactionJson['value'] = str(t.value)
        transactionJson['user'] = str(t.user.get().email).split('@')[0]
        itensJson = [];
        for i in t.items:
            transaction_item = i.get()
            itensJson.append(transaction_item.product.get().description)
        transactionJson["itens"] = itensJson

        transactionsJson.append(transactionJson)

    return json.dumps(transactionsJson)

@transaction.route('/sumarize_all', methods=['GET'])
def sumarize_all():
    transactions = Transaction.query().fetch()
    value =  0
    for t in transactions:
        value +=t.value

    return json.dumps(value)

@transaction.route('/dispatch', methods=['GET'])<commit_msg>agora vai<commit_after>import json
import datetime

from flask import Blueprint, request, session
from google.appengine.ext import ndb

from transaction.model import Transaction, TransactionItem
from user.model import User
from product.model import Product

transaction = Blueprint('transaction', __name__)

@transaction.route('/', methods=['GET'])
def get_all():
    transactions = [t.to_dict() for t in Transaction.query().fetch()]
    return json.dumps(transactions)

@transaction.route('/<int:transaction_id>', methods=['GET'])
def get_by_id(transaction_id):
    transaction = Transaction.get_by_id(transaction_id)

    if not transaction:
        return 'Transaction id %s not found' % (transaction_id), 404

    return json.dumps(transaction.to_dict())

@transaction.route('/', methods=['POST'])
def add():
    logged_user = session['barzinga_user']
    logged_user = User.query().filter(User.email == logged_user["email"]).get()

    products = json.loads(request.form['products'])

    products_list = []
    quantity_table = {}

    for product in products:
        quantity_table[product['id']] = product['quantity']
        products_list.append(ndb.Key(Product, product['id']).get())

    print products_list

    try:
        transaction = Transaction.new(logged_user, products_list, quantity_table)
        transaction.put()
    except Exception as e:
        return str(e), 400

    return '', 204

@transaction.route('/extract', methods=['GET'])
def transactions_user():
    logged_user = session['barzinga_user']
    logged_user = User.query().filter(User.email == logged_user["email"]).get()

    transactions = Transaction.query().filter(Transaction.user == logged_user.key).fetch()

    trans = [];

    for t in transactions:
        transa = {}
        transa['id'] = str(t.key)
        transa['user'] = logged_user.name.encode('utf-8').strip()
        transa['value'] = str(t.value)
        transa['date'] = str(t.date)
        itens = []
        for it in t.items :
            item = {}
            transaction_item = it.get()
            item['product'] = str(transaction_item.product.get().description)
            item['quantity'] = str(transaction_item.quantity)
            itens.append(item)

        transa['itens'] = itens
        trans.append(transa)

    return json.dumps(trans)


@transaction.route('/extract_all', methods=['GET'])
def transactions_alll():
    transactions = Transaction.query().fetch()

    trans = [];

    for t in transactions:
        transa = {}
        transa['id'] = str(t.key)
        transa['user'] = t.user.get().name.encode('utf-8').strip()
        transa['value'] = str(t.value)
        transa['date'] = str(t.date)
        itens = []
        for it in t.items :
            item = {}
            transaction_item = it.get()
            item['product'] = transaction_item.product.get().description
            item['quantity'] = transaction_item.quantity
            itens.append(item)

        transa['itens'] = itens
        trans.append(transa)

    return json.dumps(trans)

@transaction.route('/transactions_all/<string:start>/<string:end>', methods=['GET'], strict_slashes=True)
def transactions_all(start=None, end=None):
    if start is None or end is None:
        transactions = Transaction.query().fetch()
    else:
        splitStart = start.split('-')
        from_date = datetime.datetime(year=int(splitStart[2]), month=int(splitStart[1]), day=int(splitStart[0]))
        splitEnd = end.split('-')
        to_date = datetime.datetime(year=int(splitEnd[2]), month=int(splitEnd[1]), day=int(splitEnd[0]))

        transactions = Transaction.query().filter(Transaction.date <= to_date, Transaction.date >= from_date).fetch()

    transactionsJson = [];
    for t in transactions:
        transactionJson = {}
        if t.date is not None:
            transactionJson['date'] = str(t.date.strftime('%d/%m/%y - %H:%M'))
        else:
            transactionJson['date'] = ''
        transactionJson['value'] = str(t.value)
        transactionJson['user'] = str(t.user.get().email).split('@')[0]
        itensJson = [];
        for i in t.items:
            itemJson = {}
            transaction_item = i.get()
            itemJson['product'] = transaction_item.product.get().description
            itemJson['quantity'] = transaction_item.quantity
            itensJson.append(itemJson)
            
        transactionJson["itens"] = itensJson
        transactionsJson.append(transactionJson)

    return json.dumps(transactionsJson)

@transaction.route('/sumarize_all', methods=['GET'])
def sumarize_all():
    transactions = Transaction.query().fetch()
    value =  0
    for t in transactions:
        value +=t.value

    return json.dumps(value)

@transaction.route('/dispatch', methods=['GET'])
{'fraction_non_alphanumeric': 0.14925215925847904, 'fraction_numerical': 0.004002527912365704, 'mean_word_length': 3.101511879049676, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1534767', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2764, 'n_words': 540}