Пример #1
0
def test_allocating_to_a_batch_reduces_the_available_quantity() -> None:
    batch = Batch("batch-001", "SMALL-TABLE", qty=20, eta=today)
    line = OrderLine('order-ref', "SMALL-TABLE", 2)

    batch.allocate(line)

    assert batch.available_quantity == 18
Пример #2
0
def test_prefers_current_stock_batches_to_shipments() -> None:
    in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None)
    shipment_batch = Batch("shipment-batch", "RETRO-CLOCK", 100, eta=tomorrow)
    line = OrderLine("oref", "RETRO-CLOCK", 10)

    allocate(line, [in_stock_batch, shipment_batch])

    assert in_stock_batch.available_quantity == 90
    assert shipment_batch.available_quantity == 100
Пример #3
0
def test_prefers_earlier_batches() -> None:
    earliest = Batch("speedy-batch", "MINIMALIST-SPOON", 100, eta=today)
    medium = Batch("normal-batch", "MINIMALIST-SPOON", 100, eta=tomorrow)
    latest = Batch("slow-batch", "MINIMALIST-SPOON", 100, eta=later)
    line = OrderLine("order1", "MINIMALIST-SPOON", 10)

    allocate(line, [medium, earliest, latest])

    assert earliest.available_quantity == 90
    assert medium.available_quantity == 100
    assert latest.available_quantity == 100
Пример #4
0
def test_returns_allocated_batch_ref() -> None:
    in_stock_batch = Batch("in-stock-batch-ref",
                           "HIGHBROW-POSTER",
                           100,
                           eta=None)
    shipment_batch = Batch("shipment-batch-ref",
                           "HIGHBROW-POSTER",
                           100,
                           eta=tomorrow)
    line = OrderLine("oref", "HIGHBROW-POSTER", 10)
    allocation = allocate(line, [in_stock_batch, shipment_batch])
    assert allocation == in_stock_batch.reference
Пример #5
0
def new_batch():
    """
    Route pour créer un nouveau lot
    """
    if request.method == 'POST':
        if request.form['exp_date'] == '' or request.form['quantity'] == '':
            flash('Données manquantes ⚠️⚠️')
        else:
            batch = Batch(exp_date=request.form['exp_date'],
                          medicine_id=request.form['medicine_id'],
                          quantity=request.form['quantity'])
            db.session.add(batch)
            db.session.flush()
            db.session.commit()

            json_object = {
                'batch_id': int(batch.batch_id),
                'sender_id': int(current_user.id),
                'quantity': int(request.form['quantity'])
            }

            new_batch_adress = "{}register_batch".format(
                CONNECTED_NODE_ADDRESS)
            requests.post(new_batch_adress,
                          json=json_object,
                          headers={'Content-type': 'application/json'})

            return redirect(url_for('user_medicine'))

    return redirect(url_for('user_medicine'))
Пример #6
0
def batch():
    form = BatchForm()
    if form.validate_on_submit():
        batch = Batch(id_batch=form.id_batch.data, name_batch=form.name_batch.data, date_batch=form.date_batch.data, instrument=form.instrument.data, primer=form.primer.data)
        
        #save the model to the database
        db.session.add(batch)
        db.session.commit()
        
        flash('Congratulations, you are now a registered batch!')
        return redirect(url_for('index'))
    return render_template('batch.html', title='validate', form=form)
Пример #7
0
def test_batch_equality() -> None:
    batch1 = Batch('batch-001', 'SIMPLE-TABLE', 10, eta=today)
    batch2 = Batch('batch-001', 'SIMPLE-CHAIR', 5, eta=today)

    assert batch1 == batch2
Пример #8
0
def test_cannot_allocate_if_skus_do_not_match() -> None:
    batch = Batch("batch-001", "UNCOMFORTABLE-CHAIR", 100, eta=None)
    different_sku_line = OrderLine("order-123", "EXPENSIVE-TOASTER", 10)
    assert batch.can_allocate(different_sku_line) is False
Пример #9
0
def make_batch_and_line(sku: str, batch_qty: int,
                        line_qty: int) -> tuple[Batch, OrderLine]:
    return (Batch("batch-001", sku, batch_qty,
                  eta=today), OrderLine("order-123", sku, line_qty))
Пример #10
0
def test_raises_out_of_stock_exception_if_cannot_allocate() -> None:
    batch = Batch('batch1', 'SMALL-FORK', 10, eta=today)
    allocate(OrderLine('order1', 'SMALL-FORK', 10), [batch])

    with pytest.raises(OutOfStock, match='SMALL-FORK'):
        allocate(OrderLine('order2', 'SMALL-FORK', 1), [batch])
Пример #11
0

def getbatchlist(filelist):
    def chunks(li, n):
        for i in range(0, len(li), n):
            yield li[i:i + n]

    return list(chunks(filelist, 5))


static_path = settings.STATICFILES_DIRS[0]
raw_path = os.path.join(static_path, 'raw')
dataset_path = os.path.join(static_path, 'dataset')
raw_files = fnmatch.filter(os.listdir(raw_path), '*.{}'.format(img_extension))
for chunk in getbatchlist(raw_files):
    b = Batch()
    b.save()
    for i in chunk:
        j = unicode(uuid4()) + '.{}'.format(img_extension)
        print(("batch: %s,src: %s, dst: %s") % (b, i, j))
        Image(batch=b, src_path=j, raw_path=i).save()
        _dst = os.path.join(dataset_path, j)
        _src = os.path.join(raw_path, i)

        shutil.move(src=_src, dst=_dst)

User.objects.all().delete()

User = get_user_model()
User.objects.create_superuser(username="******", password="******", email="")
user01 = User.objects.create_user(username="******",
Пример #12
0
def send_batch():
    """
    Route pour envoyer un lot entre 2 ID utilisateurs de blockchains
    """
    transactions = fetch_current_actor_transactions()
    if request.method == 'POST':
        if request.form['batch_id'] == '' or request.form[
                'recipient_id'] == '' or request.form['quantity'] == '':
            flash('Données manquantes ⚠️⚠️')
        else:
            user_owner_batch = False
            batch_quantity = 0
            # Vérifiez que l'utilisateur est le propriétaire du lot qu'il essaie d'envoyer
            # L'utilisateur est le propriétaire de le lot qui est dans la liste des transactions
            # association à son identifiant.
            for t in transactions:
                if int(request.form['batch_id']) == int(t['batch_id']):
                    user_owner_batch = True
                    batch_origin = Batch.query.filter_by(
                        batch_id=request.form['batch_id']).first_or_404()
                    print('l246R, on à envoyons le: ', batch_origin)

            if user_owner_batch:
                if int(batch_origin.quantity) == int(
                        request.form['quantity']):  #Envoyer tout le lot
                    json_object = {
                        'batch_id': int(request.form['batch_id']),
                        'sender_id': int(current_user.id),
                        'recipient_id': int(request.form['recipient_id']),
                        'quantity': int(batch_origin.quantity)
                    }
                    new_transaction_address = "{}new_transaction".format(
                        CONNECTED_NODE_ADDRESS)
                    response = requests.post(
                        new_transaction_address,
                        json=json_object,
                        headers={'Content-type': 'application/json'})

                elif int(batch_origin.quantity) > int(
                        request.form['quantity']):  # Divisez le lot en 2
                    print("Breaking the batch in 2")
                    size_batch_1 = int(batch_origin.quantity) - int(
                        request.form['quantity'])
                    size_batch_2 = int(request.form['quantity'])

                    # Ajouter de nouveaux lots
                    # lot 1 garder le même propriétaire
                    # lot 2 est envoyé au nouveau propriétaire
                    batch1 = Batch(exp_date=batch_origin.exp_date,
                                   medicine_id=batch_origin.medicine_id,
                                   quantity=size_batch_1,
                                   parent_batch_id=batch_origin.batch_id)
                    batch2 = Batch(exp_date=batch_origin.exp_date,
                                   medicine_id=batch_origin.medicine_id,
                                   quantity=size_batch_2,
                                   parent_batch_id=batch_origin.batch_id)
                    db.session.add(batch1)
                    db.session.add(batch2)
                    db.session.flush()
                    db.session.commit()

                    json_object1 = {
                        'batch_id': int(batch1.batch_id),
                        'sender_id': int(current_user.id),
                        'quantity': int(batch1.quantity)
                    }

                    json_object2 = {
                        'batch_id': int(batch2.batch_id),
                        'sender_id': int(current_user.id),
                        'quantity': int(batch2.quantity)
                    }
                    new_batch_adress = "{}register_batch".format(
                        CONNECTED_NODE_ADDRESS)
                    requests.post(new_batch_adress,
                                  json=json_object1,
                                  headers={'Content-type': 'application/json'})
                    requests.post(new_batch_adress,
                                  json=json_object2,
                                  headers={'Content-type': 'application/json'})

                    mine_blockchain()  #Ajouter un nouveau lot à la blockchain

                    json_object = {
                        'batch_id': int(batch2.batch_id),
                        'sender_id': int(current_user.id),
                        'recipient_id': int(request.form['recipient_id']),
                        'quantity': int(batch2.quantity)
                    }
                    new_transaction_address = "{}new_transaction".format(
                        CONNECTED_NODE_ADDRESS)
                    response = requests.post(
                        new_transaction_address,
                        json=json_object,
                        headers={'Content-type': 'application/json'})
                    print('l307R', new_transaction_address)

                elif batch_origin.quantity < request.form[
                        'quantity']:  # Ne fait rien
                    flash('Vous n\'avez pas cette quantité de médicament.')

            else:
                flash('Vous n\'êtes pas propriétaire du lot')
            return redirect(url_for('user_transactions'))
Пример #13
0
def send_batch():
    """
    Route to send a batch between 2 blockchains users IDs
    """
    transactions = fetch_current_actor_transactions()
    if request.method == 'POST':
        if request.form['batch_id'] == '' or request.form[
                'recipient_id'] == '' or request.form['quantity'] == '':
            flash('Missing data')
        else:
            user_owner_batch = False
            batch_quantity = 0
            # Check that the user is the owner of the batch he is trying to send
            # The user is the owner is the batch is in the list of transactions
            # associated with his id.
            for t in transactions:
                if int(request.form['batch_id']) == int(t['batch_id']):
                    user_owner_batch = True
                    batch_origin = Batch.query.filter_by(
                        batch_id=request.form['batch_id']).first_or_404()

            if user_owner_batch:
                if int(batch_origin.quantity) == int(
                        request.form['quantity']):  #Send all the batch
                    json_object = {
                        'batch_id': int(request.form['batch_id']),
                        'sender_id': int(current_user.id),
                        'recipient_id': int(request.form['recipient_id']),
                        'quantity': int(batch_origin.quantity)
                    }
                    new_transaction_address = "{}new_transaction".format(
                        CONNECTED_NODE_ADDRESS)
                    response = requests.post(
                        new_transaction_address,
                        json=json_object,
                        headers={'Content-type': 'application/json'})

                elif int(batch_origin.quantity) > int(
                        request.form['quantity']):  # Split the batch in 2
                    print("Breaking the batch in 2")
                    size_batch_1 = int(batch_origin.quantity) - int(
                        request.form['quantity'])
                    size_batch_2 = int(request.form['quantity'])

                    # Add new batches
                    # batch1 keep the same owner
                    # batch2 is send to the new owner
                    batch1 = Batch(exp_date=batch_origin.exp_date,
                                   medicine_id=batch_origin.medicine_id,
                                   quantity=size_batch_1,
                                   parent_batch_id=batch_origin.batch_id)
                    batch2 = Batch(exp_date=batch_origin.exp_date,
                                   medicine_id=batch_origin.medicine_id,
                                   quantity=size_batch_2,
                                   parent_batch_id=batch_origin.batch_id)
                    db.session.add(batch1)
                    db.session.add(batch2)
                    db.session.flush()
                    db.session.commit()

                    json_object1 = {
                        'batch_id': int(batch1.batch_id),
                        'sender_id': int(current_user.id),
                        'quantity': int(batch1.quantity)
                    }

                    json_object2 = {
                        'batch_id': int(batch2.batch_id),
                        'sender_id': int(current_user.id),
                        'quantity': int(batch2.quantity)
                    }
                    new_batch_adress = "{}register_batch".format(
                        CONNECTED_NODE_ADDRESS)
                    requests.post(new_batch_adress,
                                  json=json_object1,
                                  headers={'Content-type': 'application/json'})
                    requests.post(new_batch_adress,
                                  json=json_object2,
                                  headers={'Content-type': 'application/json'})

                    mine_blockchain()  #Add the new batch to the blockchain

                    json_object = {
                        'batch_id': int(batch2.batch_id),
                        'sender_id': int(current_user.id),
                        'recipient_id': int(request.form['recipient_id']),
                        'quantity': int(batch2.quantity)
                    }
                    new_transaction_address = "{}new_transaction".format(
                        CONNECTED_NODE_ADDRESS)
                    response = requests.post(
                        new_transaction_address,
                        json=json_object,
                        headers={'Content-type': 'application/json'})

                elif batch_origin.quantity > request.form[
                        'quantity']:  # Don't do anything
                    flash('You don\'t have that quantity of medicine.')

            else:
                flash('You are not the owner of the batch')
            return redirect(url_for('user_transactions'))