Skip to content

Commit

Permalink
fix: minor fixes for demo
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Caspi <[email protected]>
  • Loading branch information
diegocaspi and Diego Caspi authored Jun 23, 2024
1 parent 8e12938 commit 94455b8
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/modules/carts/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def update_cart(buyer_id: int, product: Product, quantity: int) -> (Cart | None,
# if the product sequence is different from the reservation sequence, delete the reservation and return None, product
if product.sequence != product_reservation.product_sequence:
product_reservation.deleted_at = db.func.now()
# TODO should create a new reservation?
db.session.commit()
return None, product

Expand Down
1 change: 0 additions & 1 deletion app/modules/orders/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ def create_buyer_order(cart: Cart) -> (
if invalid_reservations:
for r in invalid_reservations:
r.deleted_at = db.func.now()
# TODO: should create a new reservation?
db.session.commit()
return None, invalid_reservations, OrderCreationErrorReason.INVALID_PRODUCTS

Expand Down
1 change: 1 addition & 0 deletions app/modules/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ProductHistory(db.Model):
price: Mapped[float] = mapped_column("price", db.Float, nullable=False)
currency: Mapped[str] = mapped_column("currency", db.String(3), nullable=False)
stock: Mapped[int] = mapped_column("stock", db.Integer, nullable=False)
locked_stock: Mapped[int] = mapped_column("locked_stock", db.Integer, nullable=False, default=0)

product = db.relationship("Product", back_populates="history")

Expand Down
1 change: 0 additions & 1 deletion app/templates/carts/macros/cart_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ <h3 class="text-xl font-semibold tracking-tight text-gray-900">{{ reservation.pr
{{ reservation_editor(reservation) }}
</div>
</div>
{# TODO needs to be warned if product is invalid: is the stock is 0, is the sequence changed etc. #}
</div>
{%- endmacro %}

Expand Down
18 changes: 9 additions & 9 deletions app/templates/layouts/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
this.originalProfile = this.profile;
this.profileEditMode = false;
} else {
alert("Failed to update user"); // TODO find the will to do
alert("Failed to update user");
}
}

Expand All @@ -182,21 +182,21 @@
profileEditMode: false,
editUser,
profile: {
destination_address: "{{ current_user.buyers[0].destination_address }}",
card_number: "{{ current_user.buyers[0].card_number|string }}",
destination_address: {{ current_user.buyers[0].destination_address|tojson }},
card_number: {{ current_user.buyers[0].card_number|tojson }},

{% if current_user.sellers %}
iban: "{{ current_user.sellers[0].iban }}",
show_soldout: "{{ current_user.sellers[0].show_soldout_products|string }}" === "True",
iban: {{ current_user.sellers[0].iban|tojson }},
show_soldout: {{ current_user.sellers[0].show_soldout_products|tojson }},
{% endif %}
},
originalProfile: {
destination_address: "{{ current_user.buyers[0].destination_address }}",
card_number: "{{ current_user.buyers[0].card_number }}",
destination_address: {{ current_user.buyers[0].destination_address|tojson }},
card_number: {{ current_user.buyers[0].card_number|tojson }},

{% if current_user.sellers %}
iban: "{{ current_user.sellers[0].iban }}",
show_soldout: "{{ current_user.sellers[0].show_soldout_products|string }}" === "True",
iban: {{ current_user.sellers[0].iban|tojson }},
show_soldout: {{ current_user.sellers[0].show_soldout_products|tojson }},
{% endif %}
},
}));
Expand Down
7 changes: 5 additions & 2 deletions app/templates/orders/buyer_order_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@
</tr>
</thead>
<tbody>
{% for reservation in order.cart.reservations %}
<!-- exclude reservations with deleted_at not null -->
{% for reservation in order.cart.reservations if reservation.deleted_at == None %}
<tr class="bg-white border-b bg-gray-100">
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap">
{{ reservation.product.name }}
<a href="{{ url_for('products.product_view', product_guid=reservation.product.guid) }}">
{{ reservation.product.name }}
</a>
</th>
<td class="px-6 py-4">
{{ reservation.product.seller.user.given_name }} {{ reservation.product.seller.user.family_name }}
Expand Down
1 change: 0 additions & 1 deletion app/templates/orders/macros/buyer_orders_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

{% macro order_card(order, current_user) -%}
{% set reservations = order.cart.reservations|selectattr('deleted_at', 'none')|list %}
{# TODO: account for currency differences between reservations #}
{% set ns = namespace(total=0) %}
{% for reservation in reservations %}
{% set ns.total = ns.total + (reservation.quantity * reservation.product.price) %}
Expand Down
1 change: 0 additions & 1 deletion app/templates/orders/macros/seller_orders_list.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{% macro order_card(order, current_user) -%}
{# TODO: account for currency differences between reservations #}
{% set ns = namespace(total=0) %}
{% for product in order.ordered_products %}
{% set ns.total = ns.total + (product.quantity * product.product.price) %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/products/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
{% else %}
<div class="flex flex-col h-[calc(100vh-4rem)]">
<div class="text-2xl font-semibold px-4 flex items-center justify-between border-b h-20">
<div class="text-2xl font-semibold px-4 flex items-center justify-between border-b min-h-20">
Your products
<a
class="bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300
Expand Down
2 changes: 0 additions & 2 deletions migrations/versions/50d9a9739e0f_products_locked_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
branch_labels = None
depends_on = None

# TODO update trigger @DiegoCaspi


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
Expand Down
63 changes: 63 additions & 0 deletions migrations/versions/f2bde8be828c_product_history_locked_stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""product history locked stock
Revision ID: f2bde8be828c
Revises: c9ce5d5552a5
Create Date: 2024-06-23 09:48:43.589979
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'f2bde8be828c'
down_revision = 'c9ce5d5552a5'
branch_labels = None
depends_on = None


create_trigger_fun = """
CREATE OR REPLACE FUNCTION update_product_history()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO product_history(product_id, sequence, price, currency, stock, locked_stock, deleted_at)
VALUES (NEW.id, NEW.current_sequence, NEW.current_price, NEW.current_currency, NEW.current_stock, NEW.current_locked_stock, NEW.deleted_at);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
"""

create_trigger = """
CREATE OR REPLACE TRIGGER after_update_product_history
AFTER INSERT OR UPDATE OF current_price, current_currency, current_stock, current_locked_stock, deleted_at ON products
FOR EACH ROW EXECUTE FUNCTION update_product_history();
"""

drop_trigger = """
DROP TRIGGER after_update_product_history ON products;
"""

drop_trigger_fun = """
DROP FUNCTION update_product_history;
"""


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('product_history', schema=None) as batch_op:
batch_op.add_column(sa.Column('locked_stock', sa.Integer(), nullable=False))

op.execute(create_trigger_fun)
op.execute(create_trigger)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('product_history', schema=None) as batch_op:
batch_op.drop_column('locked_stock')

op.execute(drop_trigger)
op.execute(drop_trigger_fun)
# ### end Alembic commands ###

0 comments on commit 94455b8

Please sign in to comment.