coupons cannot have 0 or negative numbers anymore.
This commit is contained in:
parent
06618b9cd2
commit
0bd2f2eb98
5 changed files with 178 additions and 155 deletions
|
|
@ -20,7 +20,13 @@ h1 {
|
|||
}
|
||||
|
||||
label {
|
||||
display: block;
|
||||
display: block;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
span.asterisk {
|
||||
color: red;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
a {
|
||||
|
|
|
|||
|
|
@ -5,18 +5,6 @@
|
|||
|
||||
|
||||
{% block content -%}
|
||||
|
||||
<style>
|
||||
label {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.astrick {
|
||||
color: red;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
|
||||
<section class="one-column">
|
||||
|
||||
<section class="new-coupon well">
|
||||
|
|
@ -26,14 +14,14 @@ label {
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<span class="astrick">*</span> required
|
||||
<span class="asterisk">*</span> required
|
||||
|
||||
<br />
|
||||
<br />
|
||||
|
||||
<form method="post" action="/s/{{ request.shop.id }}/coupon/new" onsubmit="submit.disabled = true; return true;">
|
||||
|
||||
<label for="coupon_code_input">Code <span class="astrick">*</span></label>
|
||||
<label for="coupon_code_input">Code <span class="asterisk">*</span></label>
|
||||
The coupon code customers type during cart checkout.
|
||||
<br />
|
||||
<input
|
||||
|
|
@ -49,7 +37,7 @@ label {
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="coupon_name_input">Description <span class="astrick">*</span></label>
|
||||
<label for="coupon_name_input">Description <span class="asterisk">*</span></label>
|
||||
This will appear on customer receipts and invoices.
|
||||
<br />
|
||||
<input
|
||||
|
|
@ -65,7 +53,7 @@ label {
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="action_type">Discount action <span class="astrick">*</span></label>
|
||||
<label for="action_type">Discount action <span class="asterisk">*</span></label>
|
||||
<input
|
||||
type="radio"
|
||||
id="dollar"
|
||||
|
|
@ -84,7 +72,7 @@ label {
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="action_value">Discount value <span class="astrick">*</span></label>
|
||||
<label for="action_value">Discount value <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "action_value"
|
||||
type = "number"
|
||||
|
|
|
|||
|
|
@ -10,7 +10,12 @@
|
|||
<h3>{{ "Edit" if location_id else "New" }} Shop Location</h3>
|
||||
<form method="post" action="{{ action_url }}" onsubmit="submit.disabled = true; return true;">
|
||||
|
||||
<label for="name_input">Location Name</label>
|
||||
<span class="asterisk">*</span> required
|
||||
|
||||
<br/ >
|
||||
<br/ >
|
||||
|
||||
<label for="name_input">Location Name <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "name"
|
||||
type = "text"
|
||||
|
|
@ -23,7 +28,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="address_input">Address</label>
|
||||
<label for="address_input">Address <span class="asterisk">*</span></label>
|
||||
<textarea
|
||||
name = "address"
|
||||
id = "address_input"
|
||||
|
|
@ -34,7 +39,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="city_input">City</label>
|
||||
<label for="city_input">City <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "city"
|
||||
type = "text"
|
||||
|
|
@ -47,7 +52,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="state_input">State</label>
|
||||
<label for="state_input">State <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "state"
|
||||
type = "text"
|
||||
|
|
@ -60,7 +65,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="country_input">Country</label>
|
||||
<label for="country_input">Country <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "country"
|
||||
type = "text"
|
||||
|
|
@ -73,7 +78,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="postal_code_input">Postal Code</label>
|
||||
<label for="postal_code_input">Postal Code <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "postal_code"
|
||||
type = "text"
|
||||
|
|
@ -86,7 +91,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="days_open_input">Days Open</label>
|
||||
<label for="days_open_input">Days Open <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "days_open"
|
||||
type = "text"
|
||||
|
|
@ -99,7 +104,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<label for="hours_open_input">Hours Open</label>
|
||||
<label for="hours_open_input">Hours Open <span class="asterisk">*</span></label>
|
||||
<input
|
||||
name = "hours_open"
|
||||
type = "text"
|
||||
|
|
@ -137,7 +142,7 @@
|
|||
<label for="local_delivery_rate_input">Local Delivery Rate</label>
|
||||
<input
|
||||
name = "local_delivery_rate"
|
||||
type = "text"
|
||||
type = "number"
|
||||
id = "local_delivery_rate_input"
|
||||
class = "mps-local-delivery-rate"
|
||||
value = "{{ local_delivery_rate }}"
|
||||
|
|
@ -160,7 +165,7 @@
|
|||
<label for="local_shipping_rate_input">Local Shipping Rate</label>
|
||||
<input
|
||||
name = "local_shipping_rate"
|
||||
type = "text"
|
||||
type = "number"
|
||||
id = "local_shipping_rate_input"
|
||||
class = "mps-local-shipping-rate"
|
||||
value = "{{ local_shipping_rate }}"
|
||||
|
|
@ -183,7 +188,7 @@
|
|||
<label for="international_shipping_rate_input">International Shipping Rate</label>
|
||||
<input
|
||||
name = "international_shipping_rate"
|
||||
type = "text"
|
||||
type = "number"
|
||||
id = "international_shipping_rate_input"
|
||||
class = "mps-international-shipping-rate"
|
||||
value = "{{ international_shipping_rate }}"
|
||||
|
|
@ -192,7 +197,7 @@
|
|||
<br />
|
||||
<br />
|
||||
|
||||
<input type="submit" name="submit" class="mps-submit" value="{{ "Update Location" if location_id else "Create Location" }}" />
|
||||
<input type="submit" name="submit" class="mps-submit" value="Save Location" />
|
||||
</form>
|
||||
</section>
|
||||
</section>
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue