diff --git a/make_post_sell/static/css/common.css b/make_post_sell/static/css/common.css
index 6e55a08..11a06b0 100644
--- a/make_post_sell/static/css/common.css
+++ b/make_post_sell/static/css/common.css
@@ -20,7 +20,13 @@ h1 {
}
label {
- display: block;
+ display: block;
+ font-weight: bold;
+}
+
+span.asterisk {
+ color: red;
+ font-weight: bold;
}
a {
diff --git a/make_post_sell/templates/coupon_new.j2 b/make_post_sell/templates/coupon_new.j2
index d147997..b61dc2b 100644
--- a/make_post_sell/templates/coupon_new.j2
+++ b/make_post_sell/templates/coupon_new.j2
@@ -5,18 +5,6 @@
{% block content -%}
-
-
-
@@ -26,14 +14,14 @@ label {
- * required
+ * required
diff --git a/make_post_sell/views/coupon.py b/make_post_sell/views/coupon.py
index f15c6fd..16939cd 100644
--- a/make_post_sell/views/coupon.py
+++ b/make_post_sell/views/coupon.py
@@ -1,7 +1,6 @@
from pyramid.view import view_config
from . import (
- user_required,
shop_owner_required,
get_referer_or_home,
)
@@ -51,8 +50,32 @@ def coupon_new(request):
cart_qualifier = request.params.get("cart_qualifier")
if request.params:
- if not code or not description or not action_type or not action_type:
+ # Convert numeric fields to integers for validation
+ try:
+ action_value = float(action_value) if action_value else None
+ max_redemptions = int(max_redemptions) if max_redemptions else None
+ max_redemptions_per_user = (
+ int(max_redemptions_per_user) if max_redemptions_per_user else None
+ )
+ cart_qualifier = float(cart_qualifier) if cart_qualifier else None
+ except ValueError:
+ request.session.flash(("Invalid input for numeric fields.", "error"))
+ return request.params
+
+ # check for required fields.
+ if not code or not description or not action_type or not action_value:
request.session.flash(("Please submit all required fields.", "error"))
+
+ # check for negative or zero values.
+ elif (
+ action_value <= 0
+ or (max_redemptions is not None and max_redemptions <= 0)
+ or (max_redemptions_per_user is not None and max_redemptions_per_user <= 0)
+ or (cart_qualifier is not None and cart_qualifier <= 0)
+ ):
+ request.session.flash(
+ ("Numeric fields must be greater than zero.", "error")
+ )
else:
request.session.flash(("You created a new coupon!", "success"))
coupon = Coupon(
diff --git a/make_post_sell/views/shop_location.py b/make_post_sell/views/shop_location.py
index 4c81dd0..14c5ca3 100644
--- a/make_post_sell/views/shop_location.py
+++ b/make_post_sell/views/shop_location.py
@@ -7,7 +7,7 @@ from ..models.shop_location import ShopLocation
def checkbox_to_bool(checkbox):
- return True if checkbox == "on" else False
+ return checkbox == "on"
def bool_to_checkbox(boolean):
@@ -15,23 +15,34 @@ def bool_to_checkbox(boolean):
@view_config(route_name="shop_location_new", renderer="shop_location_form.j2")
+@view_config(route_name="shop_location_edit", renderer="shop_location_form.j2")
@shop_owner_required()
-def shop_location_new(request):
+def shop_location_form(request):
shop = request.shop
+ location_id = request.matchdict.get("location_id")
+ shop_location = None
- if not shop:
- request.session.flash(("Shop not found.", "error"))
- return HTTPFound(location=request.route_url("home"))
+ if location_id:
+ shop_location = (
+ request.dbsession.query(ShopLocation)
+ .filter_by(id=location_id, shop_id=shop.id)
+ .one_or_none()
+ )
+ if not shop_location:
+ request.session.flash(("Shop location not found.", "error"))
+ return HTTPFound(
+ location=request.route_url("shop_locations", shop_id=shop.id)
+ )
if request.method == "POST":
- name = request.params.get("name", "")
- address = request.params.get("address", "")
- city = request.params.get("city", "")
- state = request.params.get("state", "")
- country = request.params.get("country", "")
- postal_code = request.params.get("postal_code", "")
- days_open = request.params.get("days_open", "")
- hours_open = request.params.get("hours_open", "")
+ name = request.params.get("name", "").strip()
+ address = request.params.get("address", "").strip()
+ city = request.params.get("city", "").strip()
+ state = request.params.get("state", "").strip()
+ country = request.params.get("country", "").strip()
+ postal_code = request.params.get("postal_code", "").strip()
+ days_open = request.params.get("days_open", "").strip()
+ hours_open = request.params.get("hours_open", "").strip()
local_pickup = checkbox_to_bool(request.params.get("local_pickup", "off"))
local_delivery = checkbox_to_bool(request.params.get("local_delivery", "off"))
local_delivery_rate = request.params.get("local_delivery_rate", "0.00")
@@ -44,122 +55,112 @@ def shop_location_new(request):
"international_shipping_rate", "0.00"
)
- shop_location = ShopLocation(
- shop=shop,
- name=name,
- address=address,
- city=city,
- state=state,
- country=country,
- postal_code=postal_code,
- )
- shop_location.days_open = days_open
- shop_location.hours_open = hours_open
- shop_location.local_pickup = local_pickup
- shop_location.local_delivery = local_delivery
- shop_location.local_delivery_rate = local_delivery_rate
- shop_location.local_shipping = local_shipping
- shop_location.local_shipping_rate = local_shipping_rate
- shop_location.international_shipping = international_shipping
- shop_location.international_shipping_rate = international_shipping_rate
+ # Convert numeric fields to appropriate types for validation
+ try:
+ local_delivery_rate = float(local_delivery_rate)
+ local_shipping_rate = float(local_shipping_rate)
+ international_shipping_rate = float(international_shipping_rate)
+ except ValueError:
+ request.session.flash(("Invalid input for numeric fields.", "error"))
+ return request.params
- request.dbsession.add(shop_location)
- request.dbsession.flush()
+ # Check for negative values
+ if (
+ local_delivery_rate < 0
+ or local_shipping_rate < 0
+ or international_shipping_rate < 0
+ ):
+ request.session.flash(("Numeric fields must not be negative.", "error"))
+ # Check for required text fields
+ elif (
+ not name
+ or not address
+ or not city
+ or not state
+ or not country
+ or not postal_code
+ or not days_open
+ or not hours_open
+ ):
+ request.session.flash(("Please submit all required fields.", "error"))
+ else:
+ if not shop_location:
+ shop_location = ShopLocation(
+ shop=shop,
+ name=name,
+ address=address,
+ city=city,
+ state=state,
+ country=country,
+ postal_code=postal_code,
+ )
+ request.session.flash(
+ ("Shop location created successfully.", "success")
+ )
+ else:
+ shop_location.name = name
+ shop_location.address = address
+ shop_location.city = city
+ shop_location.state = state
+ shop_location.country = country
+ shop_location.postal_code = postal_code
+ request.session.flash(
+ ("Shop location updated successfully.", "success")
+ )
- request.session.flash(("Shop location created successfully.", "success"))
- return HTTPFound(location=request.route_url("shop_locations", shop_id=shop.id))
+ shop_location.days_open = days_open
+ shop_location.hours_open = hours_open
+ shop_location.local_pickup = local_pickup
+ shop_location.local_delivery = local_delivery
+ shop_location.local_delivery_rate = local_delivery_rate
+ shop_location.local_shipping = local_shipping
+ shop_location.local_shipping_rate = local_shipping_rate
+ shop_location.international_shipping = international_shipping
+ shop_location.international_shipping_rate = international_shipping_rate
- return {
- "action_url": request.route_url("shop_location_new", shop_id=shop.id),
- "name": "",
- "address": "",
- "city": "",
- "state": "",
- "country": "",
- "postal_code": "",
- "days_open": "",
- "hours_open": "",
- "local_pickup": False,
- "local_delivery": False,
- "local_delivery_rate": "0.00",
- "local_shipping": False,
- "local_shipping_rate": "0.00",
- "international_shipping": False,
- "international_shipping_rate": "0.00",
- }
+ request.dbsession.add(shop_location)
+ request.dbsession.flush()
+ return HTTPFound(
+ location=request.route_url("shop_locations", shop_id=shop.id)
+ )
-@view_config(route_name="shop_location_edit", renderer="shop_location_form.j2")
-@shop_owner_required()
-def shop_location_edit(request):
- shop = get_shop_by_id(request.dbsession, request.matchdict["shop_id"])
- location_id = request.matchdict["location_id"]
- shop_location = (
- request.dbsession.query(ShopLocation)
- .filter_by(id=location_id, shop_id=shop.id)
- .one_or_none()
- )
-
- if not shop_location:
- request.session.flash(("Shop location not found.", "error"))
- return HTTPFound(location=request.route_url("shop_locations", shop_id=shop.id))
-
- if request.method == "POST":
- shop_location.name = request.params.get("name", "")
- shop_location.address = request.params.get("address", "")
- shop_location.city = request.params.get("city", "")
- shop_location.state = request.params.get("state", "")
- shop_location.country = request.params.get("country", "")
- shop_location.postal_code = request.params.get("postal_code", "")
- shop_location.days_open = request.params.get("days_open", "")
- shop_location.hours_open = request.params.get("hours_open", "")
- shop_location.local_pickup = checkbox_to_bool(
- request.params.get("local_pickup", "off")
- )
- shop_location.local_delivery = checkbox_to_bool(
- request.params.get("local_delivery", "off")
- )
- shop_location.local_delivery_rate = request.params.get(
- "local_delivery_rate", "0.00"
- )
- shop_location.local_shipping = checkbox_to_bool(
- request.params.get("local_shipping", "off")
- )
- shop_location.local_shipping_rate = request.params.get(
- "local_shipping_rate", "0.00"
- )
- shop_location.international_shipping = checkbox_to_bool(
- request.params.get("international_shipping", "off")
- )
- shop_location.international_shipping_rate = request.params.get(
- "international_shipping_rate", "0.00"
- )
-
- request.dbsession.add(shop_location)
- request.dbsession.flush()
-
- request.session.flash(("Shop location updated successfully.", "success"))
- return HTTPFound(location=request.route_url("shop_locations", shop_id=shop.id))
-
- return {
- "action_url": request.route_url(
+ if location_id:
+ action_url = request.route_url(
"shop_location_edit", shop_id=shop.id, location_id=location_id
- ),
- "name": shop_location.name,
- "address": shop_location.address,
- "city": shop_location.city,
- "state": shop_location.state,
- "country": shop_location.country,
- "postal_code": shop_location.postal_code,
- "days_open": shop_location.days_open,
- "hours_open": shop_location.hours_open,
- "local_pickup": shop_location.local_pickup,
- "local_delivery": shop_location.local_delivery,
- "local_delivery_rate": shop_location.local_delivery_rate,
- "local_shipping": shop_location.local_shipping,
- "local_shipping_rate": shop_location.local_shipping_rate,
- "international_shipping": shop_location.international_shipping,
- "international_shipping_rate": shop_location.international_shipping_rate,
+ )
+ else:
+ action_url = request.route_url(
+ "shop_location_new",
+ shop_id=shop.id,
+ )
+
+ return {
+ "action_url": action_url,
+ "location_id": location_id,
+ "name": shop_location.name if shop_location else "",
+ "address": shop_location.address if shop_location else "",
+ "city": shop_location.city if shop_location else "",
+ "state": shop_location.state if shop_location else "",
+ "country": shop_location.country if shop_location else "",
+ "postal_code": shop_location.postal_code if shop_location else "",
+ "days_open": shop_location.days_open if shop_location else "",
+ "hours_open": shop_location.hours_open if shop_location else "",
+ "local_pickup": shop_location.local_pickup if shop_location else False,
+ "local_delivery": shop_location.local_delivery if shop_location else False,
+ "local_delivery_rate": shop_location.local_delivery_rate
+ if shop_location
+ else "0.00",
+ "local_shipping": shop_location.local_shipping if shop_location else False,
+ "local_shipping_rate": shop_location.local_shipping_rate
+ if shop_location
+ else "0.00",
+ "international_shipping": shop_location.international_shipping
+ if shop_location
+ else False,
+ "international_shipping_rate": shop_location.international_shipping_rate
+ if shop_location
+ else "0.00",
}