From ab155d835c6ca4e0e59758dc7cfc4bf88775402e Mon Sep 17 00:00:00 2001
From: Nathan Chapman
Date: Sun, 20 Mar 2022 12:32:45 -0600
Subject: [PATCH 1/2] Update post_save for OrderLine to auto-update Order
status
---
src/core/__init__.py | 2 +-
src/core/apps.py | 2 +-
src/core/models.py | 2 +
src/core/signals.py | 33 +-
.../templates/dashboard/order_detail.html | 2 +
src/dashboard/views.py | 17 +-
src/ptcoffee/config.py | 2 +-
src/ptcoffee/settings.py | 7 +-
src/ptcoffee/urls.py | 6 +-
.../scripts/controllers/cart_controller.js | 60 -
.../controllers/cartitem_controller.js | 15 -
.../scripts/controllers/product_controller.js | 23 -
src/static/scripts/index.js | 10 -
src/static/scripts/payment.js | 96 -
src/static/scripts/stimulus.js | 1944 -----------------
src/static/styles/dashboard.css | 24 +-
16 files changed, 70 insertions(+), 2175 deletions(-)
delete mode 100644 src/static/scripts/controllers/cart_controller.js
delete mode 100644 src/static/scripts/controllers/cartitem_controller.js
delete mode 100644 src/static/scripts/controllers/product_controller.js
delete mode 100644 src/static/scripts/index.js
delete mode 100644 src/static/scripts/stimulus.js
diff --git a/src/core/__init__.py b/src/core/__init__.py
index 4c67f44..0643f2b 100644
--- a/src/core/__init__.py
+++ b/src/core/__init__.py
@@ -26,7 +26,7 @@ class OrderStatus:
DRAFT = "draft" # fully editable, not finalized order created by staff users
UNFULFILLED = "unfulfilled" # order with no items marked as fulfilled
PARTIALLY_FULFILLED = (
- "partially fulfilled" # order with some items marked as fulfilled
+ "partially_fulfilled" # order with some items marked as fulfilled
)
FULFILLED = "fulfilled" # order with all items marked as fulfilled
diff --git a/src/core/apps.py b/src/core/apps.py
index 456c142..8e2dd6c 100644
--- a/src/core/apps.py
+++ b/src/core/apps.py
@@ -6,4 +6,4 @@ class CoreConfig(AppConfig):
name = 'core'
def ready(self):
- from .signals import order_created, transaction_created
+ from .signals import order_created, transaction_created, order_line_post_save
diff --git a/src/core/models.py b/src/core/models.py
index 53aa1e7..8e5e10e 100644
--- a/src/core/models.py
+++ b/src/core/models.py
@@ -122,6 +122,7 @@ class OrderManager(models.Manager):
def with_fulfillment(self):
return self.annotate(
total_quantity_fulfilled=models.Sum('lines__quantity_fulfilled'),
+ total_quantity_ordered=models.Sum('lines__quantity')
)
@@ -182,6 +183,7 @@ class Order(models.Model):
return reverse('dashboard:order-detail', kwargs={'pk': self.pk})
+
class Transaction(models.Model):
status = models.CharField(
max_length=32,
diff --git a/src/core/signals.py b/src/core/signals.py
index 98f76d7..85fcdba 100644
--- a/src/core/signals.py
+++ b/src/core/signals.py
@@ -3,9 +3,10 @@ from io import BytesIO
from django.db.models.signals import post_save
from django.dispatch import receiver
+from django.db import models
-from . import TransactionStatus
-from .models import Order, Transaction
+from . import OrderStatus, TransactionStatus
+from .models import Order, OrderLine, Transaction
from .tasks import send_order_confirmation_email
logger = logging.getLogger(__name__)
@@ -32,3 +33,31 @@ def transaction_created(sender, instance, created, **kwargs):
send_order_confirmation_email.delay(order)
instance.confirmation_email_sent = True
instance.save()
+
+def get_order_status(total_quantity_fulfilled, total_quantity_ordered):
+ if total_quantity_fulfilled >= total_quantity_ordered:
+ return OrderStatus.FULFILLED
+ elif total_quantity_fulfilled > 0:
+ return OrderStatus.PARTIALLY_FULFILLED
+ else:
+ return OrderStatus.UNFULFILLED
+
+@receiver(post_save, sender=OrderLine, dispatch_uid="order_line_post_save")
+def order_line_post_save(sender, instance, created, **kwargs):
+ if not created:
+ order = Order.objects.with_fulfillment().filter(
+ pk=instance.order.pk
+ )[0]
+
+ order.status = get_order_status(order.total_quantity_fulfilled, order.total_quantity_ordered)
+ order.save()
+
+ # order.update(
+ # status=models.Case(
+ # models.When(models.lookups.GreaterThan(models.F('total_quantity_fulfilled'), models.F('total_quantity_ordered')),
+ # then=models.Value(OrderStatus.FULFILLED)),
+ # models.When(models.lookups.GreaterThan(models.F('total_quantity_fulfilled'), 0),
+ # then=models.Value(OrderStatus.PARTIALLY_FULFILLED)),
+ # default=models.Value(OrderStatus.UNFULFILLED)
+ # )
+ # )
diff --git a/src/dashboard/templates/dashboard/order_detail.html b/src/dashboard/templates/dashboard/order_detail.html
index d20daa9..0f60d10 100644
--- a/src/dashboard/templates/dashboard/order_detail.html
+++ b/src/dashboard/templates/dashboard/order_detail.html
@@ -32,6 +32,7 @@
No items in order yet.
{% endfor %}
+
Total fulfilled: {{order.total_quantity_fulfilled}}
Fulfill →
@@ -41,6 +42,7 @@
Shipping
diff --git a/src/dashboard/views.py b/src/dashboard/views.py
index ee8af68..906ae3a 100644
--- a/src/dashboard/views.py
+++ b/src/dashboard/views.py
@@ -77,6 +77,8 @@ class OrderDetailView(DetailView):
def get_object(self):
queryset = Order.objects.filter(
pk=self.kwargs.get(self.pk_url_kwarg)
+ ).annotate(
+ total_quantity_fulfilled=Sum('lines__quantity_fulfilled'),
).select_related(
'customer',
'billing_address',
@@ -106,21 +108,6 @@ class OrderFulfillView(UpdateView):
return reverse('dashboard:order-detail', kwargs={'pk': self.object.pk})
-def order_fulfill(request, pk):
- order = Order.objects.get(pk=pk)
- OrderLineFormset = inlineformset_factory(Order, OrderLine, form=OrderLineFulfillForm, extra=0, can_delete=False)
- if request.method == "POST":
- formset = OrderLineFormset(request.POST, request.FILES, instance=order)
- if formset.is_valid():
- formset.save()
- # Do something. Should generally end with a redirect. For example:
- return HttpResponseRedirect(order.get_absolute_url())
-
- else:
- formset = OrderLineFormset(instance=order)
- return render(request, 'dashboard/order_fulfill.html', {'formset': formset, 'order': order})
-
-
class ProductListView(ListView):
model = Product
template_name = 'dashboard/product_list.html'
diff --git a/src/ptcoffee/config.py b/src/ptcoffee/config.py
index 8d0377c..b6f1d52 100644
--- a/src/ptcoffee/config.py
+++ b/src/ptcoffee/config.py
@@ -3,7 +3,7 @@ import os
load_dotenv()
-DEBUG = os.environ.get('DEBUG', True)
+DEBUG = os.environ.get('DEBUG', 'True') == 'True'
DATABASE_CONFIG = {
'ENGINE' : 'django.db.backends.postgresql',
diff --git a/src/ptcoffee/settings.py b/src/ptcoffee/settings.py
index 88355ea..aae9912 100644
--- a/src/ptcoffee/settings.py
+++ b/src/ptcoffee/settings.py
@@ -146,9 +146,12 @@ USE_TZ = True
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_URL = '/static/'
-STATIC_ROOT = BASE_DIR / 'public'
+if DEBUG:
+ STATIC_ROOT = BASE_DIR / 'public'
+else:
+ STATIC_ROOT = '/var/www/ptcoffee-dev/static/'
STATICFILES_DIRS = [BASE_DIR / 'static']
-MEDIA_URL = '/images/'
+MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
STATICFILES_FINDERS = (
diff --git a/src/ptcoffee/urls.py b/src/ptcoffee/urls.py
index d583c5f..3e60f7a 100644
--- a/src/ptcoffee/urls.py
+++ b/src/ptcoffee/urls.py
@@ -13,5 +13,7 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('__debug__/', include('debug_toolbar.urls')),
]
-urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
-urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
+
+if settings.DEBUG:
+ urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
+ urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
diff --git a/src/static/scripts/controllers/cart_controller.js b/src/static/scripts/controllers/cart_controller.js
deleted file mode 100644
index 4e2a909..0000000
--- a/src/static/scripts/controllers/cart_controller.js
+++ /dev/null
@@ -1,60 +0,0 @@
-import { getCookie, setCookie } from "../lib/cookie.js"
-import { Controller } from "../stimulus.js"
-
-export default class extends Controller {
- static get targets() {
- return [ "cartList" ]
- }
-
- connect() {
- console.log(getCookie('cart'))
- this.cart = this.getOrSetCart()
- this.element.addEventListener('addToCart', this.addItem.bind(this))
- }
-
- getOrSetCart() {
- let cart = JSON.parse(getCookie('cart'))
- if (cart === null) {
- console.log('created cart cookie')
- setCookie('cart', '{}')
- cart = {}
- } else {
- Object.keys(cart).forEach((item_id) => {
- fetch(`/${item_id}/`)
- .then((response) => response.text())
- .then((html) => {
- this.cartListTarget.innerHTML += html;
- });
- })
- }
- return cart
- }
-
- decodeCartItems(cart) {
-
- }
-
- addItem(event) {
- if (this.cart[event.detail.item] === undefined) {
- this.cart[event.detail.item] = {'quantity': 1}
- } else {
- this.cart[event.detail.item]['quantity'] += 1
- }
- setCookie('cart', JSON.stringify(this.cart))
- console.log(this.cart)
-
- fetch(`/${event.detail.item}/`)
- .then((response) => response.text())
- .then((html) => {
- this.cartListTarget.innerHTML += html;
- });
- }
-
- removeItem() {
-
- }
-
- refresh() {
-
- }
-}
\ No newline at end of file
diff --git a/src/static/scripts/controllers/cartitem_controller.js b/src/static/scripts/controllers/cartitem_controller.js
deleted file mode 100644
index 9160e00..0000000
--- a/src/static/scripts/controllers/cartitem_controller.js
+++ /dev/null
@@ -1,15 +0,0 @@
-import { getCookie, setCookie } from "../lib/cookie.js"
-import { Controller } from "../stimulus.js"
-
-export default class extends Controller {
- static get targets() {
- return [ "qty" ]
- }
-
- connect() {
- console.log(this.qtyTarget)
-
- }
-
-
-}
\ No newline at end of file
diff --git a/src/static/scripts/controllers/product_controller.js b/src/static/scripts/controllers/product_controller.js
deleted file mode 100644
index a73f119..0000000
--- a/src/static/scripts/controllers/product_controller.js
+++ /dev/null
@@ -1,23 +0,0 @@
-// import getCookie from "../get_cookie.js"
-import { Controller } from "../stimulus.js"
-
-export default class extends Controller {
- static values = { url: String }
-
- connect() {
- this.event = new CustomEvent('addToCart', {
- detail: {
- item: this.urlValue
- }
- })
- }
-
- start() {
-
- }
-
- addToCart(event) {
- event.preventDefault()
- window.dispatchEvent(this.event)
- }
-}
\ No newline at end of file
diff --git a/src/static/scripts/index.js b/src/static/scripts/index.js
deleted file mode 100644
index 0f6ffc1..0000000
--- a/src/static/scripts/index.js
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Application } from "./stimulus.js"
-
-import CartController from "./controllers/cart_controller.js"
-import CartitemController from "./controllers/cartitem_controller.js"
-import ProductController from "./controllers/product_controller.js"
-
-const application = Application.start()
-application.register("cart", CartController)
-application.register("cartitem", CartitemController)
-application.register("product", ProductController)
diff --git a/src/static/scripts/payment.js b/src/static/scripts/payment.js
index 1110467..7929099 100644
--- a/src/static/scripts/payment.js
+++ b/src/static/scripts/payment.js
@@ -60,99 +60,3 @@ paypal.Buttons({
});
}
}).render('#paypal-button-container');
-
-
-
-// RETURNED DATA
-// {
-// "id": "1WM83684A69628456",
-// "status": "COMPLETED",
-// "purchase_units": [
-// {
-// "reference_id": "default",
-// "shipping": {
-// "name": {
-// "full_name": "John Doe"
-// },
-// "address": {
-// "address_line_1": "1 Main St",
-// "admin_area_2": "San Jose",
-// "admin_area_1": "CA",
-// "postal_code": "95131",
-// "country_code": "US"
-// }
-// },
-// "payments": {
-// "captures": [
-// {
-// "id": "9AU23265T5630860D",
-// "status": "COMPLETED",
-// "amount": {
-// "currency_code": "USD",
-// "value": "13.40"
-// },
-// "final_capture": true,
-// "seller_protection": {
-// "status": "ELIGIBLE",
-// "dispute_categories": [
-// "ITEM_NOT_RECEIVED",
-// "UNAUTHORIZED_TRANSACTION"
-// ]
-// },
-// "seller_receivable_breakdown": {
-// "gross_amount": {
-// "currency_code": "USD",
-// "value": "13.40"
-// },
-// "paypal_fee": {
-// "currency_code": "USD",
-// "value": "0.96"
-// },
-// "net_amount": {
-// "currency_code": "USD",
-// "value": "12.44"
-// }
-// },
-// "links": [
-// {
-// "href": "https://api.sandbox.paypal.com/v2/payments/captures/9AU23265T5630860D",
-// "rel": "self",
-// "method": "GET"
-// },
-// {
-// "href": "https://api.sandbox.paypal.com/v2/payments/captures/9AU23265T5630860D/refund",
-// "rel": "refund",
-// "method": "POST"
-// },
-// {
-// "href": "https://api.sandbox.paypal.com/v2/checkout/orders/1WM83684A69628456",
-// "rel": "up",
-// "method": "GET"
-// }
-// ],
-// "create_time": "2022-02-28T15:33:07Z",
-// "update_time": "2022-02-28T15:33:07Z"
-// }
-// ]
-// }
-// }
-// ],
-// "payer": {
-// "name": {
-// "given_name": "John",
-// "surname": "Doe"
-// },
-// "email_address": "sb-rlst914027742@personal.example.com",
-// "payer_id": "G9RGHQ72CGKF6",
-// "address": {
-// "country_code": "US"
-// }
-// },
-// "links": [
-// {
-// "href": "https://api.sandbox.paypal.com/v2/checkout/orders/1WM83684A69628456",
-// "rel": "self",
-// "method": "GET"
-// }
-// ]
-// }
diff --git a/src/static/scripts/stimulus.js b/src/static/scripts/stimulus.js
deleted file mode 100644
index dd0f992..0000000
--- a/src/static/scripts/stimulus.js
+++ /dev/null
@@ -1,1944 +0,0 @@
-/*
-Stimulus 3.0.1
-Copyright © 2021 Basecamp, LLC
- */
-class EventListener {
- constructor(eventTarget, eventName, eventOptions) {
- this.eventTarget = eventTarget;
- this.eventName = eventName;
- this.eventOptions = eventOptions;
- this.unorderedBindings = new Set();
- }
- connect() {
- this.eventTarget.addEventListener(this.eventName, this, this.eventOptions);
- }
- disconnect() {
- this.eventTarget.removeEventListener(this.eventName, this, this.eventOptions);
- }
- bindingConnected(binding) {
- this.unorderedBindings.add(binding);
- }
- bindingDisconnected(binding) {
- this.unorderedBindings.delete(binding);
- }
- handleEvent(event) {
- const extendedEvent = extendEvent(event);
- for (const binding of this.bindings) {
- if (extendedEvent.immediatePropagationStopped) {
- break;
- }
- else {
- binding.handleEvent(extendedEvent);
- }
- }
- }
- get bindings() {
- return Array.from(this.unorderedBindings).sort((left, right) => {
- const leftIndex = left.index, rightIndex = right.index;
- return leftIndex < rightIndex ? -1 : leftIndex > rightIndex ? 1 : 0;
- });
- }
-}
-function extendEvent(event) {
- if ("immediatePropagationStopped" in event) {
- return event;
- }
- else {
- const { stopImmediatePropagation } = event;
- return Object.assign(event, {
- immediatePropagationStopped: false,
- stopImmediatePropagation() {
- this.immediatePropagationStopped = true;
- stopImmediatePropagation.call(this);
- }
- });
- }
-}
-
-class Dispatcher {
- constructor(application) {
- this.application = application;
- this.eventListenerMaps = new Map;
- this.started = false;
- }
- start() {
- if (!this.started) {
- this.started = true;
- this.eventListeners.forEach(eventListener => eventListener.connect());
- }
- }
- stop() {
- if (this.started) {
- this.started = false;
- this.eventListeners.forEach(eventListener => eventListener.disconnect());
- }
- }
- get eventListeners() {
- return Array.from(this.eventListenerMaps.values())
- .reduce((listeners, map) => listeners.concat(Array.from(map.values())), []);
- }
- bindingConnected(binding) {
- this.fetchEventListenerForBinding(binding).bindingConnected(binding);
- }
- bindingDisconnected(binding) {
- this.fetchEventListenerForBinding(binding).bindingDisconnected(binding);
- }
- handleError(error, message, detail = {}) {
- this.application.handleError(error, `Error ${message}`, detail);
- }
- fetchEventListenerForBinding(binding) {
- const { eventTarget, eventName, eventOptions } = binding;
- return this.fetchEventListener(eventTarget, eventName, eventOptions);
- }
- fetchEventListener(eventTarget, eventName, eventOptions) {
- const eventListenerMap = this.fetchEventListenerMapForEventTarget(eventTarget);
- const cacheKey = this.cacheKey(eventName, eventOptions);
- let eventListener = eventListenerMap.get(cacheKey);
- if (!eventListener) {
- eventListener = this.createEventListener(eventTarget, eventName, eventOptions);
- eventListenerMap.set(cacheKey, eventListener);
- }
- return eventListener;
- }
- createEventListener(eventTarget, eventName, eventOptions) {
- const eventListener = new EventListener(eventTarget, eventName, eventOptions);
- if (this.started) {
- eventListener.connect();
- }
- return eventListener;
- }
- fetchEventListenerMapForEventTarget(eventTarget) {
- let eventListenerMap = this.eventListenerMaps.get(eventTarget);
- if (!eventListenerMap) {
- eventListenerMap = new Map;
- this.eventListenerMaps.set(eventTarget, eventListenerMap);
- }
- return eventListenerMap;
- }
- cacheKey(eventName, eventOptions) {
- const parts = [eventName];
- Object.keys(eventOptions).sort().forEach(key => {
- parts.push(`${eventOptions[key] ? "" : "!"}${key}`);
- });
- return parts.join(":");
- }
-}
-
-const descriptorPattern = /^((.+?)(@(window|document))?->)?(.+?)(#([^:]+?))(:(.+))?$/;
-function parseActionDescriptorString(descriptorString) {
- const source = descriptorString.trim();
- const matches = source.match(descriptorPattern) || [];
- return {
- eventTarget: parseEventTarget(matches[4]),
- eventName: matches[2],
- eventOptions: matches[9] ? parseEventOptions(matches[9]) : {},
- identifier: matches[5],
- methodName: matches[7]
- };
-}
-function parseEventTarget(eventTargetName) {
- if (eventTargetName == "window") {
- return window;
- }
- else if (eventTargetName == "document") {
- return document;
- }
-}
-function parseEventOptions(eventOptions) {
- return eventOptions.split(":").reduce((options, token) => Object.assign(options, { [token.replace(/^!/, "")]: !/^!/.test(token) }), {});
-}
-function stringifyEventTarget(eventTarget) {
- if (eventTarget == window) {
- return "window";
- }
- else if (eventTarget == document) {
- return "document";
- }
-}
-
-function camelize(value) {
- return value.replace(/(?:[_-])([a-z0-9])/g, (_, char) => char.toUpperCase());
-}
-function capitalize(value) {
- return value.charAt(0).toUpperCase() + value.slice(1);
-}
-function dasherize(value) {
- return value.replace(/([A-Z])/g, (_, char) => `-${char.toLowerCase()}`);
-}
-function tokenize(value) {
- return value.match(/[^\s]+/g) || [];
-}
-
-class Action {
- constructor(element, index, descriptor) {
- this.element = element;
- this.index = index;
- this.eventTarget = descriptor.eventTarget || element;
- this.eventName = descriptor.eventName || getDefaultEventNameForElement(element) || error("missing event name");
- this.eventOptions = descriptor.eventOptions || {};
- this.identifier = descriptor.identifier || error("missing identifier");
- this.methodName = descriptor.methodName || error("missing method name");
- }
- static forToken(token) {
- return new this(token.element, token.index, parseActionDescriptorString(token.content));
- }
- toString() {
- const eventNameSuffix = this.eventTargetName ? `@${this.eventTargetName}` : "";
- return `${this.eventName}${eventNameSuffix}->${this.identifier}#${this.methodName}`;
- }
- get params() {
- if (this.eventTarget instanceof Element) {
- return this.getParamsFromEventTargetAttributes(this.eventTarget);
- }
- else {
- return {};
- }
- }
- getParamsFromEventTargetAttributes(eventTarget) {
- const params = {};
- const pattern = new RegExp(`^data-${this.identifier}-(.+)-param$`);
- const attributes = Array.from(eventTarget.attributes);
- attributes.forEach(({ name, value }) => {
- const match = name.match(pattern);
- const key = match && match[1];
- if (key) {
- Object.assign(params, { [camelize(key)]: typecast(value) });
- }
- });
- return params;
- }
- get eventTargetName() {
- return stringifyEventTarget(this.eventTarget);
- }
-}
-const defaultEventNames = {
- "a": e => "click",
- "button": e => "click",
- "form": e => "submit",
- "details": e => "toggle",
- "input": e => e.getAttribute("type") == "submit" ? "click" : "input",
- "select": e => "change",
- "textarea": e => "input"
-};
-function getDefaultEventNameForElement(element) {
- const tagName = element.tagName.toLowerCase();
- if (tagName in defaultEventNames) {
- return defaultEventNames[tagName](element);
- }
-}
-function error(message) {
- throw new Error(message);
-}
-function typecast(value) {
- try {
- return JSON.parse(value);
- }
- catch (o_O) {
- return value;
- }
-}
-
-class Binding {
- constructor(context, action) {
- this.context = context;
- this.action = action;
- }
- get index() {
- return this.action.index;
- }
- get eventTarget() {
- return this.action.eventTarget;
- }
- get eventOptions() {
- return this.action.eventOptions;
- }
- get identifier() {
- return this.context.identifier;
- }
- handleEvent(event) {
- if (this.willBeInvokedByEvent(event)) {
- this.invokeWithEvent(event);
- }
- }
- get eventName() {
- return this.action.eventName;
- }
- get method() {
- const method = this.controller[this.methodName];
- if (typeof method == "function") {
- return method;
- }
- throw new Error(`Action "${this.action}" references undefined method "${this.methodName}"`);
- }
- invokeWithEvent(event) {
- const { target, currentTarget } = event;
- try {
- const { params } = this.action;
- const actionEvent = Object.assign(event, { params });
- this.method.call(this.controller, actionEvent);
- this.context.logDebugActivity(this.methodName, { event, target, currentTarget, action: this.methodName });
- }
- catch (error) {
- const { identifier, controller, element, index } = this;
- const detail = { identifier, controller, element, index, event };
- this.context.handleError(error, `invoking action "${this.action}"`, detail);
- }
- }
- willBeInvokedByEvent(event) {
- const eventTarget = event.target;
- if (this.element === eventTarget) {
- return true;
- }
- else if (eventTarget instanceof Element && this.element.contains(eventTarget)) {
- return this.scope.containsElement(eventTarget);
- }
- else {
- return this.scope.containsElement(this.action.element);
- }
- }
- get controller() {
- return this.context.controller;
- }
- get methodName() {
- return this.action.methodName;
- }
- get element() {
- return this.scope.element;
- }
- get scope() {
- return this.context.scope;
- }
-}
-
-class ElementObserver {
- constructor(element, delegate) {
- this.mutationObserverInit = { attributes: true, childList: true, subtree: true };
- this.element = element;
- this.started = false;
- this.delegate = delegate;
- this.elements = new Set;
- this.mutationObserver = new MutationObserver((mutations) => this.processMutations(mutations));
- }
- start() {
- if (!this.started) {
- this.started = true;
- this.mutationObserver.observe(this.element, this.mutationObserverInit);
- this.refresh();
- }
- }
- pause(callback) {
- if (this.started) {
- this.mutationObserver.disconnect();
- this.started = false;
- }
- callback();
- if (!this.started) {
- this.mutationObserver.observe(this.element, this.mutationObserverInit);
- this.started = true;
- }
- }
- stop() {
- if (this.started) {
- this.mutationObserver.takeRecords();
- this.mutationObserver.disconnect();
- this.started = false;
- }
- }
- refresh() {
- if (this.started) {
- const matches = new Set(this.matchElementsInTree());
- for (const element of Array.from(this.elements)) {
- if (!matches.has(element)) {
- this.removeElement(element);
- }
- }
- for (const element of Array.from(matches)) {
- this.addElement(element);
- }
- }
- }
- processMutations(mutations) {
- if (this.started) {
- for (const mutation of mutations) {
- this.processMutation(mutation);
- }
- }
- }
- processMutation(mutation) {
- if (mutation.type == "attributes") {
- this.processAttributeChange(mutation.target, mutation.attributeName);
- }
- else if (mutation.type == "childList") {
- this.processRemovedNodes(mutation.removedNodes);
- this.processAddedNodes(mutation.addedNodes);
- }
- }
- processAttributeChange(node, attributeName) {
- const element = node;
- if (this.elements.has(element)) {
- if (this.delegate.elementAttributeChanged && this.matchElement(element)) {
- this.delegate.elementAttributeChanged(element, attributeName);
- }
- else {
- this.removeElement(element);
- }
- }
- else if (this.matchElement(element)) {
- this.addElement(element);
- }
- }
- processRemovedNodes(nodes) {
- for (const node of Array.from(nodes)) {
- const element = this.elementFromNode(node);
- if (element) {
- this.processTree(element, this.removeElement);
- }
- }
- }
- processAddedNodes(nodes) {
- for (const node of Array.from(nodes)) {
- const element = this.elementFromNode(node);
- if (element && this.elementIsActive(element)) {
- this.processTree(element, this.addElement);
- }
- }
- }
- matchElement(element) {
- return this.delegate.matchElement(element);
- }
- matchElementsInTree(tree = this.element) {
- return this.delegate.matchElementsInTree(tree);
- }
- processTree(tree, processor) {
- for (const element of this.matchElementsInTree(tree)) {
- processor.call(this, element);
- }
- }
- elementFromNode(node) {
- if (node.nodeType == Node.ELEMENT_NODE) {
- return node;
- }
- }
- elementIsActive(element) {
- if (element.isConnected != this.element.isConnected) {
- return false;
- }
- else {
- return this.element.contains(element);
- }
- }
- addElement(element) {
- if (!this.elements.has(element)) {
- if (this.elementIsActive(element)) {
- this.elements.add(element);
- if (this.delegate.elementMatched) {
- this.delegate.elementMatched(element);
- }
- }
- }
- }
- removeElement(element) {
- if (this.elements.has(element)) {
- this.elements.delete(element);
- if (this.delegate.elementUnmatched) {
- this.delegate.elementUnmatched(element);
- }
- }
- }
-}
-
-class AttributeObserver {
- constructor(element, attributeName, delegate) {
- this.attributeName = attributeName;
- this.delegate = delegate;
- this.elementObserver = new ElementObserver(element, this);
- }
- get element() {
- return this.elementObserver.element;
- }
- get selector() {
- return `[${this.attributeName}]`;
- }
- start() {
- this.elementObserver.start();
- }
- pause(callback) {
- this.elementObserver.pause(callback);
- }
- stop() {
- this.elementObserver.stop();
- }
- refresh() {
- this.elementObserver.refresh();
- }
- get started() {
- return this.elementObserver.started;
- }
- matchElement(element) {
- return element.hasAttribute(this.attributeName);
- }
- matchElementsInTree(tree) {
- const match = this.matchElement(tree) ? [tree] : [];
- const matches = Array.from(tree.querySelectorAll(this.selector));
- return match.concat(matches);
- }
- elementMatched(element) {
- if (this.delegate.elementMatchedAttribute) {
- this.delegate.elementMatchedAttribute(element, this.attributeName);
- }
- }
- elementUnmatched(element) {
- if (this.delegate.elementUnmatchedAttribute) {
- this.delegate.elementUnmatchedAttribute(element, this.attributeName);
- }
- }
- elementAttributeChanged(element, attributeName) {
- if (this.delegate.elementAttributeValueChanged && this.attributeName == attributeName) {
- this.delegate.elementAttributeValueChanged(element, attributeName);
- }
- }
-}
-
-class StringMapObserver {
- constructor(element, delegate) {
- this.element = element;
- this.delegate = delegate;
- this.started = false;
- this.stringMap = new Map;
- this.mutationObserver = new MutationObserver(mutations => this.processMutations(mutations));
- }
- start() {
- if (!this.started) {
- this.started = true;
- this.mutationObserver.observe(this.element, { attributes: true, attributeOldValue: true });
- this.refresh();
- }
- }
- stop() {
- if (this.started) {
- this.mutationObserver.takeRecords();
- this.mutationObserver.disconnect();
- this.started = false;
- }
- }
- refresh() {
- if (this.started) {
- for (const attributeName of this.knownAttributeNames) {
- this.refreshAttribute(attributeName, null);
- }
- }
- }
- processMutations(mutations) {
- if (this.started) {
- for (const mutation of mutations) {
- this.processMutation(mutation);
- }
- }
- }
- processMutation(mutation) {
- const attributeName = mutation.attributeName;
- if (attributeName) {
- this.refreshAttribute(attributeName, mutation.oldValue);
- }
- }
- refreshAttribute(attributeName, oldValue) {
- const key = this.delegate.getStringMapKeyForAttribute(attributeName);
- if (key != null) {
- if (!this.stringMap.has(attributeName)) {
- this.stringMapKeyAdded(key, attributeName);
- }
- const value = this.element.getAttribute(attributeName);
- if (this.stringMap.get(attributeName) != value) {
- this.stringMapValueChanged(value, key, oldValue);
- }
- if (value == null) {
- const oldValue = this.stringMap.get(attributeName);
- this.stringMap.delete(attributeName);
- if (oldValue)
- this.stringMapKeyRemoved(key, attributeName, oldValue);
- }
- else {
- this.stringMap.set(attributeName, value);
- }
- }
- }
- stringMapKeyAdded(key, attributeName) {
- if (this.delegate.stringMapKeyAdded) {
- this.delegate.stringMapKeyAdded(key, attributeName);
- }
- }
- stringMapValueChanged(value, key, oldValue) {
- if (this.delegate.stringMapValueChanged) {
- this.delegate.stringMapValueChanged(value, key, oldValue);
- }
- }
- stringMapKeyRemoved(key, attributeName, oldValue) {
- if (this.delegate.stringMapKeyRemoved) {
- this.delegate.stringMapKeyRemoved(key, attributeName, oldValue);
- }
- }
- get knownAttributeNames() {
- return Array.from(new Set(this.currentAttributeNames.concat(this.recordedAttributeNames)));
- }
- get currentAttributeNames() {
- return Array.from(this.element.attributes).map(attribute => attribute.name);
- }
- get recordedAttributeNames() {
- return Array.from(this.stringMap.keys());
- }
-}
-
-function add(map, key, value) {
- fetch(map, key).add(value);
-}
-function del(map, key, value) {
- fetch(map, key).delete(value);
- prune(map, key);
-}
-function fetch(map, key) {
- let values = map.get(key);
- if (!values) {
- values = new Set();
- map.set(key, values);
- }
- return values;
-}
-function prune(map, key) {
- const values = map.get(key);
- if (values != null && values.size == 0) {
- map.delete(key);
- }
-}
-
-class Multimap {
- constructor() {
- this.valuesByKey = new Map();
- }
- get keys() {
- return Array.from(this.valuesByKey.keys());
- }
- get values() {
- const sets = Array.from(this.valuesByKey.values());
- return sets.reduce((values, set) => values.concat(Array.from(set)), []);
- }
- get size() {
- const sets = Array.from(this.valuesByKey.values());
- return sets.reduce((size, set) => size + set.size, 0);
- }
- add(key, value) {
- add(this.valuesByKey, key, value);
- }
- delete(key, value) {
- del(this.valuesByKey, key, value);
- }
- has(key, value) {
- const values = this.valuesByKey.get(key);
- return values != null && values.has(value);
- }
- hasKey(key) {
- return this.valuesByKey.has(key);
- }
- hasValue(value) {
- const sets = Array.from(this.valuesByKey.values());
- return sets.some(set => set.has(value));
- }
- getValuesForKey(key) {
- const values = this.valuesByKey.get(key);
- return values ? Array.from(values) : [];
- }
- getKeysForValue(value) {
- return Array.from(this.valuesByKey)
- .filter(([key, values]) => values.has(value))
- .map(([key, values]) => key);
- }
-}
-
-class IndexedMultimap extends Multimap {
- constructor() {
- super();
- this.keysByValue = new Map;
- }
- get values() {
- return Array.from(this.keysByValue.keys());
- }
- add(key, value) {
- super.add(key, value);
- add(this.keysByValue, value, key);
- }
- delete(key, value) {
- super.delete(key, value);
- del(this.keysByValue, value, key);
- }
- hasValue(value) {
- return this.keysByValue.has(value);
- }
- getKeysForValue(value) {
- const set = this.keysByValue.get(value);
- return set ? Array.from(set) : [];
- }
-}
-
-class TokenListObserver {
- constructor(element, attributeName, delegate) {
- this.attributeObserver = new AttributeObserver(element, attributeName, this);
- this.delegate = delegate;
- this.tokensByElement = new Multimap;
- }
- get started() {
- return this.attributeObserver.started;
- }
- start() {
- this.attributeObserver.start();
- }
- pause(callback) {
- this.attributeObserver.pause(callback);
- }
- stop() {
- this.attributeObserver.stop();
- }
- refresh() {
- this.attributeObserver.refresh();
- }
- get element() {
- return this.attributeObserver.element;
- }
- get attributeName() {
- return this.attributeObserver.attributeName;
- }
- elementMatchedAttribute(element) {
- this.tokensMatched(this.readTokensForElement(element));
- }
- elementAttributeValueChanged(element) {
- const [unmatchedTokens, matchedTokens] = this.refreshTokensForElement(element);
- this.tokensUnmatched(unmatchedTokens);
- this.tokensMatched(matchedTokens);
- }
- elementUnmatchedAttribute(element) {
- this.tokensUnmatched(this.tokensByElement.getValuesForKey(element));
- }
- tokensMatched(tokens) {
- tokens.forEach(token => this.tokenMatched(token));
- }
- tokensUnmatched(tokens) {
- tokens.forEach(token => this.tokenUnmatched(token));
- }
- tokenMatched(token) {
- this.delegate.tokenMatched(token);
- this.tokensByElement.add(token.element, token);
- }
- tokenUnmatched(token) {
- this.delegate.tokenUnmatched(token);
- this.tokensByElement.delete(token.element, token);
- }
- refreshTokensForElement(element) {
- const previousTokens = this.tokensByElement.getValuesForKey(element);
- const currentTokens = this.readTokensForElement(element);
- const firstDifferingIndex = zip(previousTokens, currentTokens)
- .findIndex(([previousToken, currentToken]) => !tokensAreEqual(previousToken, currentToken));
- if (firstDifferingIndex == -1) {
- return [[], []];
- }
- else {
- return [previousTokens.slice(firstDifferingIndex), currentTokens.slice(firstDifferingIndex)];
- }
- }
- readTokensForElement(element) {
- const attributeName = this.attributeName;
- const tokenString = element.getAttribute(attributeName) || "";
- return parseTokenString(tokenString, element, attributeName);
- }
-}
-function parseTokenString(tokenString, element, attributeName) {
- return tokenString.trim().split(/\s+/).filter(content => content.length)
- .map((content, index) => ({ element, attributeName, content, index }));
-}
-function zip(left, right) {
- const length = Math.max(left.length, right.length);
- return Array.from({ length }, (_, index) => [left[index], right[index]]);
-}
-function tokensAreEqual(left, right) {
- return left && right && left.index == right.index && left.content == right.content;
-}
-
-class ValueListObserver {
- constructor(element, attributeName, delegate) {
- this.tokenListObserver = new TokenListObserver(element, attributeName, this);
- this.delegate = delegate;
- this.parseResultsByToken = new WeakMap;
- this.valuesByTokenByElement = new WeakMap;
- }
- get started() {
- return this.tokenListObserver.started;
- }
- start() {
- this.tokenListObserver.start();
- }
- stop() {
- this.tokenListObserver.stop();
- }
- refresh() {
- this.tokenListObserver.refresh();
- }
- get element() {
- return this.tokenListObserver.element;
- }
- get attributeName() {
- return this.tokenListObserver.attributeName;
- }
- tokenMatched(token) {
- const { element } = token;
- const { value } = this.fetchParseResultForToken(token);
- if (value) {
- this.fetchValuesByTokenForElement(element).set(token, value);
- this.delegate.elementMatchedValue(element, value);
- }
- }
- tokenUnmatched(token) {
- const { element } = token;
- const { value } = this.fetchParseResultForToken(token);
- if (value) {
- this.fetchValuesByTokenForElement(element).delete(token);
- this.delegate.elementUnmatchedValue(element, value);
- }
- }
- fetchParseResultForToken(token) {
- let parseResult = this.parseResultsByToken.get(token);
- if (!parseResult) {
- parseResult = this.parseToken(token);
- this.parseResultsByToken.set(token, parseResult);
- }
- return parseResult;
- }
- fetchValuesByTokenForElement(element) {
- let valuesByToken = this.valuesByTokenByElement.get(element);
- if (!valuesByToken) {
- valuesByToken = new Map;
- this.valuesByTokenByElement.set(element, valuesByToken);
- }
- return valuesByToken;
- }
- parseToken(token) {
- try {
- const value = this.delegate.parseValueForToken(token);
- return { value };
- }
- catch (error) {
- return { error };
- }
- }
-}
-
-class BindingObserver {
- constructor(context, delegate) {
- this.context = context;
- this.delegate = delegate;
- this.bindingsByAction = new Map;
- }
- start() {
- if (!this.valueListObserver) {
- this.valueListObserver = new ValueListObserver(this.element, this.actionAttribute, this);
- this.valueListObserver.start();
- }
- }
- stop() {
- if (this.valueListObserver) {
- this.valueListObserver.stop();
- delete this.valueListObserver;
- this.disconnectAllActions();
- }
- }
- get element() {
- return this.context.element;
- }
- get identifier() {
- return this.context.identifier;
- }
- get actionAttribute() {
- return this.schema.actionAttribute;
- }
- get schema() {
- return this.context.schema;
- }
- get bindings() {
- return Array.from(this.bindingsByAction.values());
- }
- connectAction(action) {
- const binding = new Binding(this.context, action);
- this.bindingsByAction.set(action, binding);
- this.delegate.bindingConnected(binding);
- }
- disconnectAction(action) {
- const binding = this.bindingsByAction.get(action);
- if (binding) {
- this.bindingsByAction.delete(action);
- this.delegate.bindingDisconnected(binding);
- }
- }
- disconnectAllActions() {
- this.bindings.forEach(binding => this.delegate.bindingDisconnected(binding));
- this.bindingsByAction.clear();
- }
- parseValueForToken(token) {
- const action = Action.forToken(token);
- if (action.identifier == this.identifier) {
- return action;
- }
- }
- elementMatchedValue(element, action) {
- this.connectAction(action);
- }
- elementUnmatchedValue(element, action) {
- this.disconnectAction(action);
- }
-}
-
-class ValueObserver {
- constructor(context, receiver) {
- this.context = context;
- this.receiver = receiver;
- this.stringMapObserver = new StringMapObserver(this.element, this);
- this.valueDescriptorMap = this.controller.valueDescriptorMap;
- this.invokeChangedCallbacksForDefaultValues();
- }
- start() {
- this.stringMapObserver.start();
- }
- stop() {
- this.stringMapObserver.stop();
- }
- get element() {
- return this.context.element;
- }
- get controller() {
- return this.context.controller;
- }
- getStringMapKeyForAttribute(attributeName) {
- if (attributeName in this.valueDescriptorMap) {
- return this.valueDescriptorMap[attributeName].name;
- }
- }
- stringMapKeyAdded(key, attributeName) {
- const descriptor = this.valueDescriptorMap[attributeName];
- if (!this.hasValue(key)) {
- this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), descriptor.writer(descriptor.defaultValue));
- }
- }
- stringMapValueChanged(value, name, oldValue) {
- const descriptor = this.valueDescriptorNameMap[name];
- if (value === null)
- return;
- if (oldValue === null) {
- oldValue = descriptor.writer(descriptor.defaultValue);
- }
- this.invokeChangedCallback(name, value, oldValue);
- }
- stringMapKeyRemoved(key, attributeName, oldValue) {
- const descriptor = this.valueDescriptorNameMap[key];
- if (this.hasValue(key)) {
- this.invokeChangedCallback(key, descriptor.writer(this.receiver[key]), oldValue);
- }
- else {
- this.invokeChangedCallback(key, descriptor.writer(descriptor.defaultValue), oldValue);
- }
- }
- invokeChangedCallbacksForDefaultValues() {
- for (const { key, name, defaultValue, writer } of this.valueDescriptors) {
- if (defaultValue != undefined && !this.controller.data.has(key)) {
- this.invokeChangedCallback(name, writer(defaultValue), undefined);
- }
- }
- }
- invokeChangedCallback(name, rawValue, rawOldValue) {
- const changedMethodName = `${name}Changed`;
- const changedMethod = this.receiver[changedMethodName];
- if (typeof changedMethod == "function") {
- const descriptor = this.valueDescriptorNameMap[name];
- const value = descriptor.reader(rawValue);
- let oldValue = rawOldValue;
- if (rawOldValue) {
- oldValue = descriptor.reader(rawOldValue);
- }
- changedMethod.call(this.receiver, value, oldValue);
- }
- }
- get valueDescriptors() {
- const { valueDescriptorMap } = this;
- return Object.keys(valueDescriptorMap).map(key => valueDescriptorMap[key]);
- }
- get valueDescriptorNameMap() {
- const descriptors = {};
- Object.keys(this.valueDescriptorMap).forEach(key => {
- const descriptor = this.valueDescriptorMap[key];
- descriptors[descriptor.name] = descriptor;
- });
- return descriptors;
- }
- hasValue(attributeName) {
- const descriptor = this.valueDescriptorNameMap[attributeName];
- const hasMethodName = `has${capitalize(descriptor.name)}`;
- return this.receiver[hasMethodName];
- }
-}
-
-class TargetObserver {
- constructor(context, delegate) {
- this.context = context;
- this.delegate = delegate;
- this.targetsByName = new Multimap;
- }
- start() {
- if (!this.tokenListObserver) {
- this.tokenListObserver = new TokenListObserver(this.element, this.attributeName, this);
- this.tokenListObserver.start();
- }
- }
- stop() {
- if (this.tokenListObserver) {
- this.disconnectAllTargets();
- this.tokenListObserver.stop();
- delete this.tokenListObserver;
- }
- }
- tokenMatched({ element, content: name }) {
- if (this.scope.containsElement(element)) {
- this.connectTarget(element, name);
- }
- }
- tokenUnmatched({ element, content: name }) {
- this.disconnectTarget(element, name);
- }
- connectTarget(element, name) {
- var _a;
- if (!this.targetsByName.has(name, element)) {
- this.targetsByName.add(name, element);
- (_a = this.tokenListObserver) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.targetConnected(element, name));
- }
- }
- disconnectTarget(element, name) {
- var _a;
- if (this.targetsByName.has(name, element)) {
- this.targetsByName.delete(name, element);
- (_a = this.tokenListObserver) === null || _a === void 0 ? void 0 : _a.pause(() => this.delegate.targetDisconnected(element, name));
- }
- }
- disconnectAllTargets() {
- for (const name of this.targetsByName.keys) {
- for (const element of this.targetsByName.getValuesForKey(name)) {
- this.disconnectTarget(element, name);
- }
- }
- }
- get attributeName() {
- return `data-${this.context.identifier}-target`;
- }
- get element() {
- return this.context.element;
- }
- get scope() {
- return this.context.scope;
- }
-}
-
-class Context {
- constructor(module, scope) {
- this.logDebugActivity = (functionName, detail = {}) => {
- const { identifier, controller, element } = this;
- detail = Object.assign({ identifier, controller, element }, detail);
- this.application.logDebugActivity(this.identifier, functionName, detail);
- };
- this.module = module;
- this.scope = scope;
- this.controller = new module.controllerConstructor(this);
- this.bindingObserver = new BindingObserver(this, this.dispatcher);
- this.valueObserver = new ValueObserver(this, this.controller);
- this.targetObserver = new TargetObserver(this, this);
- try {
- this.controller.initialize();
- this.logDebugActivity("initialize");
- }
- catch (error) {
- this.handleError(error, "initializing controller");
- }
- }
- connect() {
- this.bindingObserver.start();
- this.valueObserver.start();
- this.targetObserver.start();
- try {
- this.controller.connect();
- this.logDebugActivity("connect");
- }
- catch (error) {
- this.handleError(error, "connecting controller");
- }
- }
- disconnect() {
- try {
- this.controller.disconnect();
- this.logDebugActivity("disconnect");
- }
- catch (error) {
- this.handleError(error, "disconnecting controller");
- }
- this.targetObserver.stop();
- this.valueObserver.stop();
- this.bindingObserver.stop();
- }
- get application() {
- return this.module.application;
- }
- get identifier() {
- return this.module.identifier;
- }
- get schema() {
- return this.application.schema;
- }
- get dispatcher() {
- return this.application.dispatcher;
- }
- get element() {
- return this.scope.element;
- }
- get parentElement() {
- return this.element.parentElement;
- }
- handleError(error, message, detail = {}) {
- const { identifier, controller, element } = this;
- detail = Object.assign({ identifier, controller, element }, detail);
- this.application.handleError(error, `Error ${message}`, detail);
- }
- targetConnected(element, name) {
- this.invokeControllerMethod(`${name}TargetConnected`, element);
- }
- targetDisconnected(element, name) {
- this.invokeControllerMethod(`${name}TargetDisconnected`, element);
- }
- invokeControllerMethod(methodName, ...args) {
- const controller = this.controller;
- if (typeof controller[methodName] == "function") {
- controller[methodName](...args);
- }
- }
-}
-
-function readInheritableStaticArrayValues(constructor, propertyName) {
- const ancestors = getAncestorsForConstructor(constructor);
- return Array.from(ancestors.reduce((values, constructor) => {
- getOwnStaticArrayValues(constructor, propertyName).forEach(name => values.add(name));
- return values;
- }, new Set));
-}
-function readInheritableStaticObjectPairs(constructor, propertyName) {
- const ancestors = getAncestorsForConstructor(constructor);
- return ancestors.reduce((pairs, constructor) => {
- pairs.push(...getOwnStaticObjectPairs(constructor, propertyName));
- return pairs;
- }, []);
-}
-function getAncestorsForConstructor(constructor) {
- const ancestors = [];
- while (constructor) {
- ancestors.push(constructor);
- constructor = Object.getPrototypeOf(constructor);
- }
- return ancestors.reverse();
-}
-function getOwnStaticArrayValues(constructor, propertyName) {
- const definition = constructor[propertyName];
- return Array.isArray(definition) ? definition : [];
-}
-function getOwnStaticObjectPairs(constructor, propertyName) {
- const definition = constructor[propertyName];
- return definition ? Object.keys(definition).map(key => [key, definition[key]]) : [];
-}
-
-function bless(constructor) {
- return shadow(constructor, getBlessedProperties(constructor));
-}
-function shadow(constructor, properties) {
- const shadowConstructor = extend(constructor);
- const shadowProperties = getShadowProperties(constructor.prototype, properties);
- Object.defineProperties(shadowConstructor.prototype, shadowProperties);
- return shadowConstructor;
-}
-function getBlessedProperties(constructor) {
- const blessings = readInheritableStaticArrayValues(constructor, "blessings");
- return blessings.reduce((blessedProperties, blessing) => {
- const properties = blessing(constructor);
- for (const key in properties) {
- const descriptor = blessedProperties[key] || {};
- blessedProperties[key] = Object.assign(descriptor, properties[key]);
- }
- return blessedProperties;
- }, {});
-}
-function getShadowProperties(prototype, properties) {
- return getOwnKeys(properties).reduce((shadowProperties, key) => {
- const descriptor = getShadowedDescriptor(prototype, properties, key);
- if (descriptor) {
- Object.assign(shadowProperties, { [key]: descriptor });
- }
- return shadowProperties;
- }, {});
-}
-function getShadowedDescriptor(prototype, properties, key) {
- const shadowingDescriptor = Object.getOwnPropertyDescriptor(prototype, key);
- const shadowedByValue = shadowingDescriptor && "value" in shadowingDescriptor;
- if (!shadowedByValue) {
- const descriptor = Object.getOwnPropertyDescriptor(properties, key).value;
- if (shadowingDescriptor) {
- descriptor.get = shadowingDescriptor.get || descriptor.get;
- descriptor.set = shadowingDescriptor.set || descriptor.set;
- }
- return descriptor;
- }
-}
-const getOwnKeys = (() => {
- if (typeof Object.getOwnPropertySymbols == "function") {
- return (object) => [
- ...Object.getOwnPropertyNames(object),
- ...Object.getOwnPropertySymbols(object)
- ];
- }
- else {
- return Object.getOwnPropertyNames;
- }
-})();
-const extend = (() => {
- function extendWithReflect(constructor) {
- function extended() {
- return Reflect.construct(constructor, arguments, new.target);
- }
- extended.prototype = Object.create(constructor.prototype, {
- constructor: { value: extended }
- });
- Reflect.setPrototypeOf(extended, constructor);
- return extended;
- }
- function testReflectExtension() {
- const a = function () { this.a.call(this); };
- const b = extendWithReflect(a);
- b.prototype.a = function () { };
- return new b;
- }
- try {
- testReflectExtension();
- return extendWithReflect;
- }
- catch (error) {
- return (constructor) => class extended extends constructor {
- };
- }
-})();
-
-function blessDefinition(definition) {
- return {
- identifier: definition.identifier,
- controllerConstructor: bless(definition.controllerConstructor)
- };
-}
-
-class Module {
- constructor(application, definition) {
- this.application = application;
- this.definition = blessDefinition(definition);
- this.contextsByScope = new WeakMap;
- this.connectedContexts = new Set;
- }
- get identifier() {
- return this.definition.identifier;
- }
- get controllerConstructor() {
- return this.definition.controllerConstructor;
- }
- get contexts() {
- return Array.from(this.connectedContexts);
- }
- connectContextForScope(scope) {
- const context = this.fetchContextForScope(scope);
- this.connectedContexts.add(context);
- context.connect();
- }
- disconnectContextForScope(scope) {
- const context = this.contextsByScope.get(scope);
- if (context) {
- this.connectedContexts.delete(context);
- context.disconnect();
- }
- }
- fetchContextForScope(scope) {
- let context = this.contextsByScope.get(scope);
- if (!context) {
- context = new Context(this, scope);
- this.contextsByScope.set(scope, context);
- }
- return context;
- }
-}
-
-class ClassMap {
- constructor(scope) {
- this.scope = scope;
- }
- has(name) {
- return this.data.has(this.getDataKey(name));
- }
- get(name) {
- return this.getAll(name)[0];
- }
- getAll(name) {
- const tokenString = this.data.get(this.getDataKey(name)) || "";
- return tokenize(tokenString);
- }
- getAttributeName(name) {
- return this.data.getAttributeNameForKey(this.getDataKey(name));
- }
- getDataKey(name) {
- return `${name}-class`;
- }
- get data() {
- return this.scope.data;
- }
-}
-
-class DataMap {
- constructor(scope) {
- this.scope = scope;
- }
- get element() {
- return this.scope.element;
- }
- get identifier() {
- return this.scope.identifier;
- }
- get(key) {
- const name = this.getAttributeNameForKey(key);
- return this.element.getAttribute(name);
- }
- set(key, value) {
- const name = this.getAttributeNameForKey(key);
- this.element.setAttribute(name, value);
- return this.get(key);
- }
- has(key) {
- const name = this.getAttributeNameForKey(key);
- return this.element.hasAttribute(name);
- }
- delete(key) {
- if (this.has(key)) {
- const name = this.getAttributeNameForKey(key);
- this.element.removeAttribute(name);
- return true;
- }
- else {
- return false;
- }
- }
- getAttributeNameForKey(key) {
- return `data-${this.identifier}-${dasherize(key)}`;
- }
-}
-
-class Guide {
- constructor(logger) {
- this.warnedKeysByObject = new WeakMap;
- this.logger = logger;
- }
- warn(object, key, message) {
- let warnedKeys = this.warnedKeysByObject.get(object);
- if (!warnedKeys) {
- warnedKeys = new Set;
- this.warnedKeysByObject.set(object, warnedKeys);
- }
- if (!warnedKeys.has(key)) {
- warnedKeys.add(key);
- this.logger.warn(message, object);
- }
- }
-}
-
-function attributeValueContainsToken(attributeName, token) {
- return `[${attributeName}~="${token}"]`;
-}
-
-class TargetSet {
- constructor(scope) {
- this.scope = scope;
- }
- get element() {
- return this.scope.element;
- }
- get identifier() {
- return this.scope.identifier;
- }
- get schema() {
- return this.scope.schema;
- }
- has(targetName) {
- return this.find(targetName) != null;
- }
- find(...targetNames) {
- return targetNames.reduce((target, targetName) => target
- || this.findTarget(targetName)
- || this.findLegacyTarget(targetName), undefined);
- }
- findAll(...targetNames) {
- return targetNames.reduce((targets, targetName) => [
- ...targets,
- ...this.findAllTargets(targetName),
- ...this.findAllLegacyTargets(targetName)
- ], []);
- }
- findTarget(targetName) {
- const selector = this.getSelectorForTargetName(targetName);
- return this.scope.findElement(selector);
- }
- findAllTargets(targetName) {
- const selector = this.getSelectorForTargetName(targetName);
- return this.scope.findAllElements(selector);
- }
- getSelectorForTargetName(targetName) {
- const attributeName = this.schema.targetAttributeForScope(this.identifier);
- return attributeValueContainsToken(attributeName, targetName);
- }
- findLegacyTarget(targetName) {
- const selector = this.getLegacySelectorForTargetName(targetName);
- return this.deprecate(this.scope.findElement(selector), targetName);
- }
- findAllLegacyTargets(targetName) {
- const selector = this.getLegacySelectorForTargetName(targetName);
- return this.scope.findAllElements(selector).map(element => this.deprecate(element, targetName));
- }
- getLegacySelectorForTargetName(targetName) {
- const targetDescriptor = `${this.identifier}.${targetName}`;
- return attributeValueContainsToken(this.schema.targetAttribute, targetDescriptor);
- }
- deprecate(element, targetName) {
- if (element) {
- const { identifier } = this;
- const attributeName = this.schema.targetAttribute;
- const revisedAttributeName = this.schema.targetAttributeForScope(identifier);
- this.guide.warn(element, `target:${targetName}`, `Please replace ${attributeName}="${identifier}.${targetName}" with ${revisedAttributeName}="${targetName}". ` +
- `The ${attributeName} attribute is deprecated and will be removed in a future version of Stimulus.`);
- }
- return element;
- }
- get guide() {
- return this.scope.guide;
- }
-}
-
-class Scope {
- constructor(schema, element, identifier, logger) {
- this.targets = new TargetSet(this);
- this.classes = new ClassMap(this);
- this.data = new DataMap(this);
- this.containsElement = (element) => {
- return element.closest(this.controllerSelector) === this.element;
- };
- this.schema = schema;
- this.element = element;
- this.identifier = identifier;
- this.guide = new Guide(logger);
- }
- findElement(selector) {
- return this.element.matches(selector)
- ? this.element
- : this.queryElements(selector).find(this.containsElement);
- }
- findAllElements(selector) {
- return [
- ...this.element.matches(selector) ? [this.element] : [],
- ...this.queryElements(selector).filter(this.containsElement)
- ];
- }
- queryElements(selector) {
- return Array.from(this.element.querySelectorAll(selector));
- }
- get controllerSelector() {
- return attributeValueContainsToken(this.schema.controllerAttribute, this.identifier);
- }
-}
-
-class ScopeObserver {
- constructor(element, schema, delegate) {
- this.element = element;
- this.schema = schema;
- this.delegate = delegate;
- this.valueListObserver = new ValueListObserver(this.element, this.controllerAttribute, this);
- this.scopesByIdentifierByElement = new WeakMap;
- this.scopeReferenceCounts = new WeakMap;
- }
- start() {
- this.valueListObserver.start();
- }
- stop() {
- this.valueListObserver.stop();
- }
- get controllerAttribute() {
- return this.schema.controllerAttribute;
- }
- parseValueForToken(token) {
- const { element, content: identifier } = token;
- const scopesByIdentifier = this.fetchScopesByIdentifierForElement(element);
- let scope = scopesByIdentifier.get(identifier);
- if (!scope) {
- scope = this.delegate.createScopeForElementAndIdentifier(element, identifier);
- scopesByIdentifier.set(identifier, scope);
- }
- return scope;
- }
- elementMatchedValue(element, value) {
- const referenceCount = (this.scopeReferenceCounts.get(value) || 0) + 1;
- this.scopeReferenceCounts.set(value, referenceCount);
- if (referenceCount == 1) {
- this.delegate.scopeConnected(value);
- }
- }
- elementUnmatchedValue(element, value) {
- const referenceCount = this.scopeReferenceCounts.get(value);
- if (referenceCount) {
- this.scopeReferenceCounts.set(value, referenceCount - 1);
- if (referenceCount == 1) {
- this.delegate.scopeDisconnected(value);
- }
- }
- }
- fetchScopesByIdentifierForElement(element) {
- let scopesByIdentifier = this.scopesByIdentifierByElement.get(element);
- if (!scopesByIdentifier) {
- scopesByIdentifier = new Map;
- this.scopesByIdentifierByElement.set(element, scopesByIdentifier);
- }
- return scopesByIdentifier;
- }
-}
-
-class Router {
- constructor(application) {
- this.application = application;
- this.scopeObserver = new ScopeObserver(this.element, this.schema, this);
- this.scopesByIdentifier = new Multimap;
- this.modulesByIdentifier = new Map;
- }
- get element() {
- return this.application.element;
- }
- get schema() {
- return this.application.schema;
- }
- get logger() {
- return this.application.logger;
- }
- get controllerAttribute() {
- return this.schema.controllerAttribute;
- }
- get modules() {
- return Array.from(this.modulesByIdentifier.values());
- }
- get contexts() {
- return this.modules.reduce((contexts, module) => contexts.concat(module.contexts), []);
- }
- start() {
- this.scopeObserver.start();
- }
- stop() {
- this.scopeObserver.stop();
- }
- loadDefinition(definition) {
- this.unloadIdentifier(definition.identifier);
- const module = new Module(this.application, definition);
- this.connectModule(module);
- }
- unloadIdentifier(identifier) {
- const module = this.modulesByIdentifier.get(identifier);
- if (module) {
- this.disconnectModule(module);
- }
- }
- getContextForElementAndIdentifier(element, identifier) {
- const module = this.modulesByIdentifier.get(identifier);
- if (module) {
- return module.contexts.find(context => context.element == element);
- }
- }
- handleError(error, message, detail) {
- this.application.handleError(error, message, detail);
- }
- createScopeForElementAndIdentifier(element, identifier) {
- return new Scope(this.schema, element, identifier, this.logger);
- }
- scopeConnected(scope) {
- this.scopesByIdentifier.add(scope.identifier, scope);
- const module = this.modulesByIdentifier.get(scope.identifier);
- if (module) {
- module.connectContextForScope(scope);
- }
- }
- scopeDisconnected(scope) {
- this.scopesByIdentifier.delete(scope.identifier, scope);
- const module = this.modulesByIdentifier.get(scope.identifier);
- if (module) {
- module.disconnectContextForScope(scope);
- }
- }
- connectModule(module) {
- this.modulesByIdentifier.set(module.identifier, module);
- const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
- scopes.forEach(scope => module.connectContextForScope(scope));
- }
- disconnectModule(module) {
- this.modulesByIdentifier.delete(module.identifier);
- const scopes = this.scopesByIdentifier.getValuesForKey(module.identifier);
- scopes.forEach(scope => module.disconnectContextForScope(scope));
- }
-}
-
-const defaultSchema = {
- controllerAttribute: "data-controller",
- actionAttribute: "data-action",
- targetAttribute: "data-target",
- targetAttributeForScope: identifier => `data-${identifier}-target`
-};
-
-class Application {
- constructor(element = document.documentElement, schema = defaultSchema) {
- this.logger = console;
- this.debug = false;
- this.logDebugActivity = (identifier, functionName, detail = {}) => {
- if (this.debug) {
- this.logFormattedMessage(identifier, functionName, detail);
- }
- };
- this.element = element;
- this.schema = schema;
- this.dispatcher = new Dispatcher(this);
- this.router = new Router(this);
- }
- static start(element, schema) {
- const application = new Application(element, schema);
- application.start();
- return application;
- }
- async start() {
- await domReady();
- this.logDebugActivity("application", "starting");
- this.dispatcher.start();
- this.router.start();
- this.logDebugActivity("application", "start");
- }
- stop() {
- this.logDebugActivity("application", "stopping");
- this.dispatcher.stop();
- this.router.stop();
- this.logDebugActivity("application", "stop");
- }
- register(identifier, controllerConstructor) {
- if (controllerConstructor.shouldLoad) {
- this.load({ identifier, controllerConstructor });
- }
- }
- load(head, ...rest) {
- const definitions = Array.isArray(head) ? head : [head, ...rest];
- definitions.forEach(definition => this.router.loadDefinition(definition));
- }
- unload(head, ...rest) {
- const identifiers = Array.isArray(head) ? head : [head, ...rest];
- identifiers.forEach(identifier => this.router.unloadIdentifier(identifier));
- }
- get controllers() {
- return this.router.contexts.map(context => context.controller);
- }
- getControllerForElementAndIdentifier(element, identifier) {
- const context = this.router.getContextForElementAndIdentifier(element, identifier);
- return context ? context.controller : null;
- }
- handleError(error, message, detail) {
- var _a;
- this.logger.error(`%s\n\n%o\n\n%o`, message, error, detail);
- (_a = window.onerror) === null || _a === void 0 ? void 0 : _a.call(window, message, "", 0, 0, error);
- }
- logFormattedMessage(identifier, functionName, detail = {}) {
- detail = Object.assign({ application: this }, detail);
- this.logger.groupCollapsed(`${identifier} #${functionName}`);
- this.logger.log("details:", Object.assign({}, detail));
- this.logger.groupEnd();
- }
-}
-function domReady() {
- return new Promise(resolve => {
- if (document.readyState == "loading") {
- document.addEventListener("DOMContentLoaded", () => resolve());
- }
- else {
- resolve();
- }
- });
-}
-
-function ClassPropertiesBlessing(constructor) {
- const classes = readInheritableStaticArrayValues(constructor, "classes");
- return classes.reduce((properties, classDefinition) => {
- return Object.assign(properties, propertiesForClassDefinition(classDefinition));
- }, {});
-}
-function propertiesForClassDefinition(key) {
- return {
- [`${key}Class`]: {
- get() {
- const { classes } = this;
- if (classes.has(key)) {
- return classes.get(key);
- }
- else {
- const attribute = classes.getAttributeName(key);
- throw new Error(`Missing attribute "${attribute}"`);
- }
- }
- },
- [`${key}Classes`]: {
- get() {
- return this.classes.getAll(key);
- }
- },
- [`has${capitalize(key)}Class`]: {
- get() {
- return this.classes.has(key);
- }
- }
- };
-}
-
-function TargetPropertiesBlessing(constructor) {
- const targets = readInheritableStaticArrayValues(constructor, "targets");
- return targets.reduce((properties, targetDefinition) => {
- return Object.assign(properties, propertiesForTargetDefinition(targetDefinition));
- }, {});
-}
-function propertiesForTargetDefinition(name) {
- return {
- [`${name}Target`]: {
- get() {
- const target = this.targets.find(name);
- if (target) {
- return target;
- }
- else {
- throw new Error(`Missing target element "${name}" for "${this.identifier}" controller`);
- }
- }
- },
- [`${name}Targets`]: {
- get() {
- return this.targets.findAll(name);
- }
- },
- [`has${capitalize(name)}Target`]: {
- get() {
- return this.targets.has(name);
- }
- }
- };
-}
-
-function ValuePropertiesBlessing(constructor) {
- const valueDefinitionPairs = readInheritableStaticObjectPairs(constructor, "values");
- const propertyDescriptorMap = {
- valueDescriptorMap: {
- get() {
- return valueDefinitionPairs.reduce((result, valueDefinitionPair) => {
- const valueDescriptor = parseValueDefinitionPair(valueDefinitionPair);
- const attributeName = this.data.getAttributeNameForKey(valueDescriptor.key);
- return Object.assign(result, { [attributeName]: valueDescriptor });
- }, {});
- }
- }
- };
- return valueDefinitionPairs.reduce((properties, valueDefinitionPair) => {
- return Object.assign(properties, propertiesForValueDefinitionPair(valueDefinitionPair));
- }, propertyDescriptorMap);
-}
-function propertiesForValueDefinitionPair(valueDefinitionPair) {
- const definition = parseValueDefinitionPair(valueDefinitionPair);
- const { key, name, reader: read, writer: write } = definition;
- return {
- [name]: {
- get() {
- const value = this.data.get(key);
- if (value !== null) {
- return read(value);
- }
- else {
- return definition.defaultValue;
- }
- },
- set(value) {
- if (value === undefined) {
- this.data.delete(key);
- }
- else {
- this.data.set(key, write(value));
- }
- }
- },
- [`has${capitalize(name)}`]: {
- get() {
- return this.data.has(key) || definition.hasCustomDefaultValue;
- }
- }
- };
-}
-function parseValueDefinitionPair([token, typeDefinition]) {
- return valueDescriptorForTokenAndTypeDefinition(token, typeDefinition);
-}
-function parseValueTypeConstant(constant) {
- switch (constant) {
- case Array: return "array";
- case Boolean: return "boolean";
- case Number: return "number";
- case Object: return "object";
- case String: return "string";
- }
-}
-function parseValueTypeDefault(defaultValue) {
- switch (typeof defaultValue) {
- case "boolean": return "boolean";
- case "number": return "number";
- case "string": return "string";
- }
- if (Array.isArray(defaultValue))
- return "array";
- if (Object.prototype.toString.call(defaultValue) === "[object Object]")
- return "object";
-}
-function parseValueTypeObject(typeObject) {
- const typeFromObject = parseValueTypeConstant(typeObject.type);
- if (typeFromObject) {
- const defaultValueType = parseValueTypeDefault(typeObject.default);
- if (typeFromObject !== defaultValueType) {
- throw new Error(`Type "${typeFromObject}" must match the type of the default value. Given default value: "${typeObject.default}" as "${defaultValueType}"`);
- }
- return typeFromObject;
- }
-}
-function parseValueTypeDefinition(typeDefinition) {
- const typeFromObject = parseValueTypeObject(typeDefinition);
- const typeFromDefaultValue = parseValueTypeDefault(typeDefinition);
- const typeFromConstant = parseValueTypeConstant(typeDefinition);
- const type = typeFromObject || typeFromDefaultValue || typeFromConstant;
- if (type)
- return type;
- throw new Error(`Unknown value type "${typeDefinition}"`);
-}
-function defaultValueForDefinition(typeDefinition) {
- const constant = parseValueTypeConstant(typeDefinition);
- if (constant)
- return defaultValuesByType[constant];
- const defaultValue = typeDefinition.default;
- if (defaultValue !== undefined)
- return defaultValue;
- return typeDefinition;
-}
-function valueDescriptorForTokenAndTypeDefinition(token, typeDefinition) {
- const key = `${dasherize(token)}-value`;
- const type = parseValueTypeDefinition(typeDefinition);
- return {
- type,
- key,
- name: camelize(key),
- get defaultValue() { return defaultValueForDefinition(typeDefinition); },
- get hasCustomDefaultValue() { return parseValueTypeDefault(typeDefinition) !== undefined; },
- reader: readers[type],
- writer: writers[type] || writers.default
- };
-}
-const defaultValuesByType = {
- get array() { return []; },
- boolean: false,
- number: 0,
- get object() { return {}; },
- string: ""
-};
-const readers = {
- array(value) {
- const array = JSON.parse(value);
- if (!Array.isArray(array)) {
- throw new TypeError("Expected array");
- }
- return array;
- },
- boolean(value) {
- return !(value == "0" || value == "false");
- },
- number(value) {
- return Number(value);
- },
- object(value) {
- const object = JSON.parse(value);
- if (object === null || typeof object != "object" || Array.isArray(object)) {
- throw new TypeError("Expected object");
- }
- return object;
- },
- string(value) {
- return value;
- }
-};
-const writers = {
- default: writeString,
- array: writeJSON,
- object: writeJSON
-};
-function writeJSON(value) {
- return JSON.stringify(value);
-}
-function writeString(value) {
- return `${value}`;
-}
-
-class Controller {
- constructor(context) {
- this.context = context;
- }
- static get shouldLoad() {
- return true;
- }
- get application() {
- return this.context.application;
- }
- get scope() {
- return this.context.scope;
- }
- get element() {
- return this.scope.element;
- }
- get identifier() {
- return this.scope.identifier;
- }
- get targets() {
- return this.scope.targets;
- }
- get classes() {
- return this.scope.classes;
- }
- get data() {
- return this.scope.data;
- }
- initialize() {
- }
- connect() {
- }
- disconnect() {
- }
- dispatch(eventName, { target = this.element, detail = {}, prefix = this.identifier, bubbles = true, cancelable = true } = {}) {
- const type = prefix ? `${prefix}:${eventName}` : eventName;
- const event = new CustomEvent(type, { detail, bubbles, cancelable });
- target.dispatchEvent(event);
- return event;
- }
-}
-Controller.blessings = [ClassPropertiesBlessing, TargetPropertiesBlessing, ValuePropertiesBlessing];
-Controller.targets = [];
-Controller.values = {};
-
-export { Application, AttributeObserver, Context, Controller, ElementObserver, IndexedMultimap, Multimap, StringMapObserver, TokenListObserver, ValueListObserver, add, defaultSchema, del, fetch, prune };
diff --git a/src/static/styles/dashboard.css b/src/static/styles/dashboard.css
index 78cec7c..5d3f8e5 100644
--- a/src/static/styles/dashboard.css
+++ b/src/static/styles/dashboard.css
@@ -6,6 +6,7 @@
--yellow-color: #f8a911;
--yellow-alt-color: #f6c463;
--green-color: #13ce65;
+ --red-color: #ff4d44;
}
html {
@@ -93,6 +94,23 @@ button:hover {
background-color: var(--yellow-alt-color);
}
+.action-button--warning {
+ background-color: var(--red-color);
+}
+
+.action-link {
+ font-family: inherit;
+ font-weight: bold;
+ font-size: 1rem;
+ color: var(--yellow-color);
+ text-decoration: none;
+ cursor: pointer;
+}
+
+.action-link--warning {
+ color: var(--red-color);
+}
+
figure {
margin: 0;
padding: 0;
@@ -276,13 +294,13 @@ main article {
background-color: var(--gray-color);
}
.order__status--unfulfilled {
- background-color: var(--yellow-alt-color);
+ background-color: var(--red-color);
}
.order__status--partially_returned {
- background-color: var(--gray-color);
+ background-color: var(--yellow-alt-color);
}
.order__status--partially_fulfilled {
- background-color: var(--gray-color);
+ background-color: var(--yellow-alt-color);
}
.order__status--returned {
background-color: var(--gray-color);
From 4000cab0de27afb007fdd8da8fb05b1bc15c67c6 Mon Sep 17 00:00:00 2001
From: Nathan Chapman
Date: Tue, 22 Mar 2022 15:44:47 -0600
Subject: [PATCH 2/2] Add product update form and update config
---
Pipfile.lock | 112 +++++++--------
src/core/models.py | 9 ++
src/core/weight.py | 12 +-
.../templates/dashboard/order_detail.html | 13 +-
.../templates/dashboard/product_detail.html | 4 +-
.../dashboard/product_update_form.html | 18 +++
src/dashboard/urls.py | 2 +-
src/dashboard/views.py | 11 +-
src/ptcoffee/config.py | 8 +-
src/static/styles/dashboard.css | 130 +++++++++++++++++-
src/static/styles/main.css | 4 +-
src/storefront/cart.py | 6 +-
src/storefront/forms.py | 1 -
.../templates/storefront/cart_detail.html | 11 +-
.../templates/storefront/product_detail.html | 4 +-
src/storefront/views.py | 2 -
src/templates/base.html | 4 +-
17 files changed, 253 insertions(+), 98 deletions(-)
create mode 100644 src/dashboard/templates/dashboard/product_update_form.html
diff --git a/Pipfile.lock b/Pipfile.lock
index 7956781..d83b1c7 100644
--- a/Pipfile.lock
+++ b/Pipfile.lock
@@ -152,28 +152,28 @@
},
"cryptography": {
"hashes": [
- "sha256:0a817b961b46894c5ca8a66b599c745b9a3d9f822725221f0e0fe49dc043a3a3",
- "sha256:2d87cdcb378d3cfed944dac30596da1968f88fb96d7fc34fdae30a99054b2e31",
- "sha256:30ee1eb3ebe1644d1c3f183d115a8c04e4e603ed6ce8e394ed39eea4a98469ac",
- "sha256:391432971a66cfaf94b21c24ab465a4cc3e8bf4a939c1ca5c3e3a6e0abebdbcf",
- "sha256:39bdf8e70eee6b1c7b289ec6e5d84d49a6bfa11f8b8646b5b3dfe41219153316",
- "sha256:4caa4b893d8fad33cf1964d3e51842cd78ba87401ab1d2e44556826df849a8ca",
- "sha256:53e5c1dc3d7a953de055d77bef2ff607ceef7a2aac0353b5d630ab67f7423638",
- "sha256:596f3cd67e1b950bc372c33f1a28a0692080625592ea6392987dba7f09f17a94",
- "sha256:5d59a9d55027a8b88fd9fd2826c4392bd487d74bf628bb9d39beecc62a644c12",
- "sha256:6c0c021f35b421ebf5976abf2daacc47e235f8b6082d3396a2fe3ccd537ab173",
- "sha256:73bc2d3f2444bcfeac67dd130ff2ea598ea5f20b40e36d19821b4df8c9c5037b",
- "sha256:74d6c7e80609c0f4c2434b97b80c7f8fdfaa072ca4baab7e239a15d6d70ed73a",
- "sha256:7be0eec337359c155df191d6ae00a5e8bbb63933883f4f5dffc439dac5348c3f",
- "sha256:94ae132f0e40fe48f310bba63f477f14a43116f05ddb69d6fa31e93f05848ae2",
- "sha256:bb5829d027ff82aa872d76158919045a7c1e91fbf241aec32cb07956e9ebd3c9",
- "sha256:ca238ceb7ba0bdf6ce88c1b74a87bffcee5afbfa1e41e173b1ceb095b39add46",
- "sha256:ca28641954f767f9822c24e927ad894d45d5a1e501767599647259cbf030b903",
- "sha256:e0344c14c9cb89e76eb6a060e67980c9e35b3f36691e15e1b7a9e58a0a6c6dc3",
- "sha256:ebc15b1c22e55c4d5566e3ca4db8689470a0ca2babef8e3a9ee057a8b82ce4b1",
- "sha256:ec63da4e7e4a5f924b90af42eddf20b698a70e58d86a72d943857c4c6045b3ee"
+ "sha256:0a3bf09bb0b7a2c93ce7b98cb107e9170a90c51a0162a20af1c61c765b90e60b",
+ "sha256:1f64a62b3b75e4005df19d3b5235abd43fa6358d5516cfc43d87aeba8d08dd51",
+ "sha256:32db5cc49c73f39aac27574522cecd0a4bb7384e71198bc65a0d23f901e89bb7",
+ "sha256:4881d09298cd0b669bb15b9cfe6166f16fc1277b4ed0d04a22f3d6430cb30f1d",
+ "sha256:4e2dddd38a5ba733be6a025a1475a9f45e4e41139d1321f412c6b360b19070b6",
+ "sha256:53e0285b49fd0ab6e604f4c5d9c5ddd98de77018542e88366923f152dbeb3c29",
+ "sha256:70f8f4f7bb2ac9f340655cbac89d68c527af5bb4387522a8413e841e3e6628c9",
+ "sha256:7b2d54e787a884ffc6e187262823b6feb06c338084bbe80d45166a1cb1c6c5bf",
+ "sha256:7be666cc4599b415f320839e36367b273db8501127b38316f3b9f22f17a0b815",
+ "sha256:8241cac0aae90b82d6b5c443b853723bcc66963970c67e56e71a2609dc4b5eaf",
+ "sha256:82740818f2f240a5da8dfb8943b360e4f24022b093207160c77cadade47d7c85",
+ "sha256:8897b7b7ec077c819187a123174b645eb680c13df68354ed99f9b40a50898f77",
+ "sha256:c2c5250ff0d36fd58550252f54915776940e4e866f38f3a7866d92b32a654b86",
+ "sha256:ca9f686517ec2c4a4ce930207f75c00bf03d94e5063cbc00a1dc42531511b7eb",
+ "sha256:d2b3d199647468d410994dbeb8cec5816fb74feb9368aedf300af709ef507e3e",
+ "sha256:da73d095f8590ad437cd5e9faf6628a218aa7c387e1fdf67b888b47ba56a17f0",
+ "sha256:e167b6b710c7f7bc54e67ef593f8731e1f45aa35f8a8a7b72d6e42ec76afd4b3",
+ "sha256:ea634401ca02367c1567f012317502ef3437522e2fc44a3ea1844de028fa4b84",
+ "sha256:ec6597aa85ce03f3e507566b8bcdf9da2227ec86c4266bd5e6ab4d9e0cc8dab2",
+ "sha256:f64b232348ee82f13aac22856515ce0195837f6968aeaa94a3d0353ea2ec06a6"
],
- "version": "==36.0.1"
+ "version": "==36.0.2"
},
"defusedxml": {
"hashes": [
@@ -497,10 +497,10 @@
},
"pytz": {
"hashes": [
- "sha256:3672058bc3453457b622aab7a1c3bfd5ab0bdae451512f6cf25f64ed37f5b87c",
- "sha256:acad2d8b20a1af07d4e4c9d2e9285c5ed9104354062f275f3fcd88dcef4f1326"
+ "sha256:1e760e2fe6a8163bc0b3d9a19c4f84342afa0a2affebfaa84b01b978a02ecaa7",
+ "sha256:e68985985296d9a66a881eb3193b0906246245294a881e7c8afe623866ac6a5c"
],
- "version": "==2021.3"
+ "version": "==2022.1"
},
"pyyaml": {
"hashes": [
@@ -641,19 +641,19 @@
},
"sympy": {
"hashes": [
- "sha256:2009368e862cd29f1b568dc6572786371a2faa1cd8eb4d313e11a90195d6ee36",
- "sha256:6cf85a5cfe8fff69553e745b05128de6fc8de8f291965c63871c79701dc6efc9"
+ "sha256:5939eeffdf9e152172601463626c022a2c27e75cf6278de8d401d50c9d58787b",
+ "sha256:df75d738930f6fe9ebe7034e59d56698f29e85f443f743e51e47df0caccc2130"
],
"markers": "python_version >= '3.7'",
- "version": "==1.10"
+ "version": "==1.10.1"
},
"urllib3": {
"hashes": [
- "sha256:000ca7f471a233c2251c6c7023ee85305721bfdf18621ebff4fd17a8653427ed",
- "sha256:0e7c33d9a63e7ddfcb86780aac87befc2fbddf46c58dbb487e0855f7ceec283c"
+ "sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14",
+ "sha256:aabaf16477806a5e1dd19aa41f8c2b7950dd3c746362d7e3223dbe6de6ac448e"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_full_version < '4.0.0'",
- "version": "==1.26.8"
+ "version": "==1.26.9"
},
"vine": {
"hashes": [
@@ -830,28 +830,28 @@
},
"cryptography": {
"hashes": [
- "sha256:0a817b961b46894c5ca8a66b599c745b9a3d9f822725221f0e0fe49dc043a3a3",
- "sha256:2d87cdcb378d3cfed944dac30596da1968f88fb96d7fc34fdae30a99054b2e31",
- "sha256:30ee1eb3ebe1644d1c3f183d115a8c04e4e603ed6ce8e394ed39eea4a98469ac",
- "sha256:391432971a66cfaf94b21c24ab465a4cc3e8bf4a939c1ca5c3e3a6e0abebdbcf",
- "sha256:39bdf8e70eee6b1c7b289ec6e5d84d49a6bfa11f8b8646b5b3dfe41219153316",
- "sha256:4caa4b893d8fad33cf1964d3e51842cd78ba87401ab1d2e44556826df849a8ca",
- "sha256:53e5c1dc3d7a953de055d77bef2ff607ceef7a2aac0353b5d630ab67f7423638",
- "sha256:596f3cd67e1b950bc372c33f1a28a0692080625592ea6392987dba7f09f17a94",
- "sha256:5d59a9d55027a8b88fd9fd2826c4392bd487d74bf628bb9d39beecc62a644c12",
- "sha256:6c0c021f35b421ebf5976abf2daacc47e235f8b6082d3396a2fe3ccd537ab173",
- "sha256:73bc2d3f2444bcfeac67dd130ff2ea598ea5f20b40e36d19821b4df8c9c5037b",
- "sha256:74d6c7e80609c0f4c2434b97b80c7f8fdfaa072ca4baab7e239a15d6d70ed73a",
- "sha256:7be0eec337359c155df191d6ae00a5e8bbb63933883f4f5dffc439dac5348c3f",
- "sha256:94ae132f0e40fe48f310bba63f477f14a43116f05ddb69d6fa31e93f05848ae2",
- "sha256:bb5829d027ff82aa872d76158919045a7c1e91fbf241aec32cb07956e9ebd3c9",
- "sha256:ca238ceb7ba0bdf6ce88c1b74a87bffcee5afbfa1e41e173b1ceb095b39add46",
- "sha256:ca28641954f767f9822c24e927ad894d45d5a1e501767599647259cbf030b903",
- "sha256:e0344c14c9cb89e76eb6a060e67980c9e35b3f36691e15e1b7a9e58a0a6c6dc3",
- "sha256:ebc15b1c22e55c4d5566e3ca4db8689470a0ca2babef8e3a9ee057a8b82ce4b1",
- "sha256:ec63da4e7e4a5f924b90af42eddf20b698a70e58d86a72d943857c4c6045b3ee"
+ "sha256:0a3bf09bb0b7a2c93ce7b98cb107e9170a90c51a0162a20af1c61c765b90e60b",
+ "sha256:1f64a62b3b75e4005df19d3b5235abd43fa6358d5516cfc43d87aeba8d08dd51",
+ "sha256:32db5cc49c73f39aac27574522cecd0a4bb7384e71198bc65a0d23f901e89bb7",
+ "sha256:4881d09298cd0b669bb15b9cfe6166f16fc1277b4ed0d04a22f3d6430cb30f1d",
+ "sha256:4e2dddd38a5ba733be6a025a1475a9f45e4e41139d1321f412c6b360b19070b6",
+ "sha256:53e0285b49fd0ab6e604f4c5d9c5ddd98de77018542e88366923f152dbeb3c29",
+ "sha256:70f8f4f7bb2ac9f340655cbac89d68c527af5bb4387522a8413e841e3e6628c9",
+ "sha256:7b2d54e787a884ffc6e187262823b6feb06c338084bbe80d45166a1cb1c6c5bf",
+ "sha256:7be666cc4599b415f320839e36367b273db8501127b38316f3b9f22f17a0b815",
+ "sha256:8241cac0aae90b82d6b5c443b853723bcc66963970c67e56e71a2609dc4b5eaf",
+ "sha256:82740818f2f240a5da8dfb8943b360e4f24022b093207160c77cadade47d7c85",
+ "sha256:8897b7b7ec077c819187a123174b645eb680c13df68354ed99f9b40a50898f77",
+ "sha256:c2c5250ff0d36fd58550252f54915776940e4e866f38f3a7866d92b32a654b86",
+ "sha256:ca9f686517ec2c4a4ce930207f75c00bf03d94e5063cbc00a1dc42531511b7eb",
+ "sha256:d2b3d199647468d410994dbeb8cec5816fb74feb9368aedf300af709ef507e3e",
+ "sha256:da73d095f8590ad437cd5e9faf6628a218aa7c387e1fdf67b888b47ba56a17f0",
+ "sha256:e167b6b710c7f7bc54e67ef593f8731e1f45aa35f8a8a7b72d6e42ec76afd4b3",
+ "sha256:ea634401ca02367c1567f012317502ef3437522e2fc44a3ea1844de028fa4b84",
+ "sha256:ec6597aa85ce03f3e507566b8bcdf9da2227ec86c4266bd5e6ab4d9e0cc8dab2",
+ "sha256:f64b232348ee82f13aac22856515ce0195837f6968aeaa94a3d0353ea2ec06a6"
],
- "version": "==36.0.1"
+ "version": "==36.0.2"
},
"django": {
"hashes": [
@@ -951,7 +951,7 @@
"sha256:670a52d3115d0e879e1ac838a4eb999af32f858163e3a704fe4839de2a676070",
"sha256:fb2d48e4eab0dfb786a472cd514aaadc71e3445b203bc300bad93daa75d77c1a"
],
- "markers": "python_full_version >= '3.7.0'",
+ "markers": "python_version >= '3.7'",
"version": "==0.20.0"
},
"trio-websocket": {
@@ -964,18 +964,18 @@
},
"urllib3": {
"hashes": [
- "sha256:000ca7f471a233c2251c6c7023ee85305721bfdf18621ebff4fd17a8653427ed",
- "sha256:0e7c33d9a63e7ddfcb86780aac87befc2fbddf46c58dbb487e0855f7ceec283c"
+ "sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14",
+ "sha256:aabaf16477806a5e1dd19aa41f8c2b7950dd3c746362d7e3223dbe6de6ac448e"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_full_version < '4.0.0'",
- "version": "==1.26.8"
+ "version": "==1.26.9"
},
"wsproto": {
"hashes": [
"sha256:2218cb57952d90b9fca325c0dcfb08c3bda93e8fd8070b0a17f048e2e47a521b",
"sha256:a2e56bfd5c7cd83c1369d83b5feccd6d37798b74872866e62616e0ecf111bda8"
],
- "markers": "python_full_version >= '3.7.0'",
+ "markers": "python_version >= '3.7'",
"version": "==1.1.0"
}
}
diff --git a/src/core/models.py b/src/core/models.py
index 8e5e10e..93441f8 100644
--- a/src/core/models.py
+++ b/src/core/models.py
@@ -58,6 +58,9 @@ class Product(models.Model):
def __str__(self):
return self.name
+ def get_absolute_url(self):
+ return reverse('dashboard:product-detail', kwargs={'pk': self.pk})
+
class ProductPhoto(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
@@ -125,6 +128,12 @@ class OrderManager(models.Manager):
total_quantity_ordered=models.Sum('lines__quantity')
)
+ def with_fulfillment_and_filter(self, query=None):
+ return self.annotate(
+ total_quantity_fulfilled=models.Sum('lines__quantity_fulfilled'),
+ total_quantity_ordered=models.Sum('lines__quantity')
+ ).filter(pk=query)
+
class Order(models.Model):
customer = models.ForeignKey(
diff --git a/src/core/weight.py b/src/core/weight.py
index 27a01e5..baf9dd0 100644
--- a/src/core/weight.py
+++ b/src/core/weight.py
@@ -1,16 +1,16 @@
from measurement.measures import Weight
class WeightUnits:
- KILOGRAM = "kg"
- POUND = "lb"
+ # KILOGRAM = "kg"
+ # POUND = "lb"
OUNCE = "oz"
- GRAM = "g"
+ # GRAM = "g"
CHOICES = [
- (KILOGRAM, "kg"),
- (POUND, "lb"),
+ # (KILOGRAM, "kg"),
+ # (POUND, "lb"),
(OUNCE, "oz"),
- (GRAM, "g"),
+ # (GRAM, "g"),
]
diff --git a/src/dashboard/templates/dashboard/order_detail.html b/src/dashboard/templates/dashboard/order_detail.html
index 0f60d10..02aaf88 100644
--- a/src/dashboard/templates/dashboard/order_detail.html
+++ b/src/dashboard/templates/dashboard/order_detail.html
@@ -5,7 +5,16 @@
diff --git a/src/dashboard/templates/dashboard/product_detail.html b/src/dashboard/templates/dashboard/product_detail.html
index 6c31064..50aebfa 100644
--- a/src/dashboard/templates/dashboard/product_detail.html
+++ b/src/dashboard/templates/dashboard/product_detail.html
@@ -5,9 +5,9 @@
-
+
diff --git a/src/dashboard/templates/dashboard/product_update_form.html b/src/dashboard/templates/dashboard/product_update_form.html
new file mode 100644
index 0000000..0737b9d
--- /dev/null
+++ b/src/dashboard/templates/dashboard/product_update_form.html
@@ -0,0 +1,18 @@
+{% extends "dashboard.html" %}
+
+{% block content %}
+
+
+
+
+{% endblock %}
diff --git a/src/dashboard/urls.py b/src/dashboard/urls.py
index 24d3ef8..25bd9f6 100644
--- a/src/dashboard/urls.py
+++ b/src/dashboard/urls.py
@@ -16,7 +16,7 @@ urlpatterns = [
path('products/new/', views.ProductCreateView.as_view(), name='product-create'),
path('/', include([
path('', views.ProductDetailView.as_view(), name='product-detail'),
- # path('update/', views.ProductUpdateView.as_view(), name='product-update'),
+ path('update/', views.ProductUpdateView.as_view(), name='product-update'),
# path('delete/', views.ProductDeleteView.as_view(), name='product-delete'),
])),
diff --git a/src/dashboard/views.py b/src/dashboard/views.py
index 906ae3a..2549d27 100644
--- a/src/dashboard/views.py
+++ b/src/dashboard/views.py
@@ -75,10 +75,8 @@ class OrderDetailView(DetailView):
template_name = 'dashboard/order_detail.html'
def get_object(self):
- queryset = Order.objects.filter(
- pk=self.kwargs.get(self.pk_url_kwarg)
- ).annotate(
- total_quantity_fulfilled=Sum('lines__quantity_fulfilled'),
+ queryset = Order.objects.with_fulfillment_and_filter(
+ self.kwargs.get(self.pk_url_kwarg)
).select_related(
'customer',
'billing_address',
@@ -124,6 +122,11 @@ class ProductDetailView(DetailView):
model = Product
template_name = 'dashboard/product_detail.html'
+class ProductUpdateView(UpdateView):
+ model = Product
+ template_name = 'dashboard/product_update_form.html'
+ fields = '__all__'
+
class ProductCreateView(CreateView):
model = Product
template_name = "dashboard/product_create_form.html"
diff --git a/src/ptcoffee/config.py b/src/ptcoffee/config.py
index b6f1d52..0dc9ad2 100644
--- a/src/ptcoffee/config.py
+++ b/src/ptcoffee/config.py
@@ -29,7 +29,7 @@ ANYMAIL_CONFIG = {
SERVER_EMAIL = os.environ.get('SERVER_EMAIL', '')
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', '')
-SECURE_HSTS_SECONDS = os.environ.get('SECURE_HSTS_SECONDS', False)
-SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', False)
-SESSION_COOKIE_SECURE = os.environ.get('SESSION_COOKIE_SECURE', False)
-CSRF_COOKIE_SECURE = os.environ.get('CSRF_COOKIE_SECURE', False)
+SECURE_HSTS_SECONDS = os.environ.get('SECURE_HSTS_SECONDS', 3600)
+SECURE_SSL_REDIRECT = os.environ.get('SECURE_SSL_REDIRECT', 'True') == 'True'
+SESSION_COOKIE_SECURE = os.environ.get('SESSION_COOKIE_SECURE', 'True') == 'True'
+CSRF_COOKIE_SECURE = os.environ.get('CSRF_COOKIE_SECURE', 'True') == 'True'
diff --git a/src/static/styles/dashboard.css b/src/static/styles/dashboard.css
index 5d3f8e5..2942e86 100644
--- a/src/static/styles/dashboard.css
+++ b/src/static/styles/dashboard.css
@@ -7,6 +7,8 @@
--yellow-alt-color: #f6c463;
--green-color: #13ce65;
--red-color: #ff4d44;
+
+ --default-border: 2px solid var(--gray-color);
}
html {
@@ -63,13 +65,60 @@ small {
}
label {
- display: block;
}
+input[type=text],
+input[type=email],
+input[type=number],
+input[type=password],
+select[multiple=multiple],
+textarea {
+ font: inherit;
+ text-align: left;
+ color: var(--fg-color);
+ border: var(--default-border);
+ padding: 0.5rem;
+ outline: 0;
+ line-height: 1;
+}
+
+input:focus,
+textarea:focus {
+ border-color: var(--yellow-color);
+}
+
+select {
+ text-align: left;
+ font: inherit;
+ font-size: 1rem;
+ border: var(--default-border);
+ padding: 0.5em;
+ outline: 0;
+ line-height: 1;
+}
+
+input[type=number] + select {
+ margin-left: 1rem;
+}
+
+input[type=text],
+input[type=email],
+input[type=password],
+select[multiple=multiple],
+textarea {
+ display: block;
+}
input[type=number] {
- max-width: 4rem;
+ max-width: 6rem;
+}
+
+input[type=checkbox],
+input[type=radio] {
+ width: 2rem;
+ height: 2rem;
+ vertical-align: middle;
}
@@ -215,7 +264,7 @@ main article {
.object__header {
display: flex;
- align-items: baseline;
+ align-items: center;
justify-content: space-between;
margin-bottom: 1rem;
}
@@ -264,6 +313,7 @@ main article {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 2rem;
+ padding: 1rem;
}
.product__image {
@@ -272,16 +322,90 @@ main article {
}
+.object__menu {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+}
.order__fulfill {
grid-column: 8;
}
+
+.dropdown {
+ position: relative;
+ display: inline-block;
+}
+
+.dropdown__menu {
+ background-color: var(--bg-alt-color);
+ padding: 0.5rem 1rem;
+ border-radius: 0.5rem;
+ font-size: 1.25rem;
+ margin-right: 1rem;
+ font-weight: bold;
+ display: inline-block;
+ cursor: pointer;
+}
+
+.dropdown__child {
+ display: none;
+ cursor: pointer;
+ background-color: var(--bg-color);
+ position: absolute;
+ right: 1rem;
+ border-radius: 0.5rem;
+ box-shadow: 0 0 3rem var(--gray-color);
+}
+
+.dropdown__child a {
+ text-decoration: none;
+ padding: 0.5rem 1rem;
+ display: block;
+ font-size: 1.25rem;
+ font-weight: bold;
+ white-space: nowrap;
+ /*border-radius: 0.5rem;*/
+}
+.dropdown__child a:hover {
+ background-color: var(--bg-alt-color);
+}
+
+.dropdown__child a:first-child {
+ border-radius: 0.5rem 0.5rem 0 0;
+}
+
+.dropdown__child a:last-child {
+ border-radius: 0 0 0.5rem 0.5rem;
+}
+
+.dropdown:hover .dropdown__child {
+ display: block;
+}
+
+.dropdown__icon {
+ background-color: var(--bg-alt-color);
+ cursor: pointer;
+ border-radius: 0.5rem;
+ fill: currentColor;
+ width: 3em;
+ height: 3em;
+ display: inline-block;
+ line-height: 1.75;
+ transition: fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
+}
+
+.dropdown__icon:hover {
+ background-color: var(--yellow-alt-color);
+}
+
.order__status {
padding: 0.5rem 1rem;
border-radius: 0.5rem;
font-size: 1.25rem;
font-weight: bold;
+ display: inline-block;
}
.order__status--display {
diff --git a/src/static/styles/main.css b/src/static/styles/main.css
index 4a32366..59b44f4 100644
--- a/src/static/styles/main.css
+++ b/src/static/styles/main.css
@@ -1,6 +1,6 @@
:root {
--fg-color: #333;
- --bg-color: #f9f9f9;
+ --bg-color: #f6ebd3;
--gray-color: #9d9d9d;
--yellow-color: #f8a911;
--yellow-alt-color: #ffce6f;
@@ -275,7 +275,7 @@ nav {
.cart__length {
font-size: 1.5rem;
font-weight: bold;
- font-family: 'Eczar';
+ font-family: 'Inter', sans-serif;
}
.cart__item {
diff --git a/src/storefront/cart.py b/src/storefront/cart.py
index 59f8e5e..c9827e7 100644
--- a/src/storefront/cart.py
+++ b/src/storefront/cart.py
@@ -14,19 +14,17 @@ class Cart:
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
- def add(self, product, quantity=1, roast='', other='', update_quantity=False):
+ def add(self, product, quantity=1, roast='', update_quantity=False):
product_id = str(product.id)
if product_id not in self.cart:
self.cart[product_id] = {
'quantity': 0,
'roast': roast,
- 'other': other,
'price': str(product.price)
}
elif product_id in self.cart:
self.cart[product_id].update({
'roast': roast,
- 'other': other,
})
if update_quantity:
self.cart[product_id]['quantity'] = quantity
@@ -88,7 +86,7 @@ class Cart:
bulk_list = [OrderLine(
order=order,
product=item['product'],
- customer_note=f'{item["roast"]} {item["other"]}',
+ customer_note=item['roast'],
unit_price=item['price'],
quantity=item['quantity'],
tax_rate=2,
diff --git a/src/storefront/forms.py b/src/storefront/forms.py
index 82e8994..ec4df2f 100644
--- a/src/storefront/forms.py
+++ b/src/storefront/forms.py
@@ -30,7 +30,6 @@ class AddToCartForm(forms.Form):
]
quantity = forms.IntegerField(min_value=1, initial=1)
roast = forms.ChoiceField(choices=ROAST_CHOICES)
- other = forms.CharField(required=False)
update = forms.BooleanField(required=False, initial=False, widget=forms.HiddenInput)
diff --git a/src/storefront/templates/storefront/cart_detail.html b/src/storefront/templates/storefront/cart_detail.html
index 23c786b..5e962d4 100644
--- a/src/storefront/templates/storefront/cart_detail.html
+++ b/src/storefront/templates/storefront/cart_detail.html
@@ -2,9 +2,8 @@
{% block content %}
+ Shopping Cart
- {{user}}
-
{% for item in cart %}
{% with product=item.product %}
@@ -13,16 +12,14 @@
{% endwith %}
diff --git a/src/storefront/templates/storefront/product_detail.html b/src/storefront/templates/storefront/product_detail.html
index ff9a9ad..0b84bde 100644
--- a/src/storefront/templates/storefront/product_detail.html
+++ b/src/storefront/templates/storefront/product_detail.html
@@ -14,7 +14,9 @@
diff --git a/src/storefront/views.py b/src/storefront/views.py
index 4fd7fbe..de47e30 100644
--- a/src/storefront/views.py
+++ b/src/storefront/views.py
@@ -37,7 +37,6 @@ class CartView(TemplateView):
initial={
'quantity': item['quantity'],
'roast': item['roast'],
- 'other': item['other'],
'update': True
}
)
@@ -58,7 +57,6 @@ class CartAddProductView(SingleObjectMixin, FormView):
cart.add(
product=self.get_object(),
roast=form.cleaned_data['roast'],
- other=form.cleaned_data['other'],
quantity=form.cleaned_data['quantity'],
update_quantity=form.cleaned_data['update']
)
diff --git a/src/templates/base.html b/src/templates/base.html
index 4b13bd1..32f448a 100644
--- a/src/templates/base.html
+++ b/src/templates/base.html
@@ -31,7 +31,7 @@
Hi {{user.first_name}} {{user.last_name}}!
{% if user.is_staff %}
- DASHBOARD
+ Dashboard
{% endif %}
Log Out
{% else %}
@@ -45,7 +45,7 @@
Wholesale
Subscribe
Cafe
- Enjoy
+ Fair Trade
About
Contact