allow users without being logged in to switch shop locations

modified:   make_post_sell/templates/product.j2
	modified:   make_post_sell/templates/shop_locations.j2
	modified:   make_post_sell/views/shop_location.py
This commit is contained in:
Russell Ballestrini 2024-06-09 11:00:19 -04:00
parent 8705842e45
commit 6706223f17
3 changed files with 23 additions and 9 deletions

View file

@ -75,7 +75,11 @@
{% if inventory and inventory.quantity > 0 %}
<a href="/cart/add?product_id={{ product.id }}" class="product-download-button mps-button">Add To Cart</a>
{% else %}
<a class="product-download-button mps-button" style="background-color: red; cursor: not-allowed;" disabled>Sold Out</a>
<button class="product-download-button mps-button" style="background-color: red; cursor: not-allowed;" disabled>Sold Out</button>
{% if request.shop.shop_locations.all()|length > 1 %}
<p>This item is sold out at this location.
<br> You can <a href="{{ request.route_url('shop_locations', shop_id=request.shop.id, _query={'next': request.url}) }}">switch shop locations</a> to check availability.</p>
{% endif %}
{% endif %}
{% else %}
<a href="/cart/add?product_id={{ product.id }}" class="product-download-button mps-button">Add To Cart</a>

View file

@ -11,14 +11,14 @@
<ul>
{% for location in locations %}
<li>
<a href="{{ request.route_url('shop_location_switch', shop_id=shop.id, location_id=location.id) }}">{{ location.name }}</a>
{% if request.user.can_edit_shop(request.shop) %}
<a href="{{ request.route_url('shop_location_switch', shop_id=shop.id, location_id=location.id, _query={'next': request.params.get('next', request.url)}) }}">{{ location.name }}</a>
{% if request.user and request.user.can_edit_shop(request.shop) %}
<a href="{{ request.route_url('shop_location_edit', shop_id=shop.id, location_id=location.id) }}" class="button">&#9881; edit</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% if request.user.can_edit_shop(request.shop) %}
{% if request.user and request.user.can_edit_shop(request.shop) %}
<a href="{{ request.route_url('shop_location_new', shop_id=shop.id) }}" class="shop-new-button mps-button">Add New Location</a>
{% endif %}
</section>

View file

@ -171,21 +171,31 @@ def shop_locations(request):
"locations": request.shop.shop_locations,
}
@view_config(route_name="shop_location_switch")
def shop_location_switch(request):
shop_id = request.shop.id
location_id = request.matchdict.get("location_id")
next_url = request.params.get("next", request.route_url("shop", shop_id=shop_id))
# Fetch the location to ensure it exists and belongs to the shop
location = request.dbsession.query(ShopLocation).filter_by(id=location_id, shop_id=shop_id).first()
location = (
request.dbsession.query(ShopLocation)
.filter_by(id=location_id, shop_id=shop_id)
.first()
)
if not location:
request.session.flash(("Location not found or does not belong to this shop.", "error"))
request.session.flash(
("Location not found or does not belong to this shop.", "error")
)
return HTTPFound(request.route_url("shop_locations", shop_id=shop_id))
# Update the session with the selected location
request.session["shop_location_id"] = str(location.id)
request.session.flash((f"You are now viewing the {location.name} shop location", "success"))
request.session.flash(
(f"You are now viewing the {location.name} shop location", "success")
)
# Redirect to the shop home page
return HTTPFound(request.route_url("shop", shop_id=shop_id))
# Redirect to the next URL or the shop home page
return HTTPFound(next_url)