Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add dashboard related methods

Artur Smęt0e0f779586d3
diff --git a/saleor/order/__init__.py b/saleor/order/__init__.py
index b3d1c46303..798d6f458a 100644
--- a/saleor/order/__init__.py
+++ b/saleor/order/__init__.py
@@ -27,12 +27,3 @@ def order_status_change(sender, instance, **kwargs):
order.change_status('fully-paid')
instance.send_confirmation_email()
analytics.report_order(order.tracking_client_id, order)
-
-
-def get_ip(request):
- ip = request.META.get("HTTP_X_FORWARDED_FOR", None)
- if ip:
- ip = ip.split(", ")[0]
- else:
- ip = request.META.get("REMOTE_ADDR", "")
- return ip
diff --git a/saleor/order/admin.py b/saleor/order/admin.py
index 3ba17484bb..ad42cb6676 100644
--- a/saleor/order/admin.py
+++ b/saleor/order/admin.py
@@ -22,7 +22,7 @@ class OrderModelAdmin(ModelAdmin):
if order:
inlines.extend([
DeliveryInlineAdmin(self.model, self.admin_site, group)
- for group in order])
+ for group in order.groups.all()])
return inlines
diff --git a/saleor/order/models.py b/saleor/order/models.py
index 8640daf689..6712505f1d 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -68,11 +68,15 @@ class Order(models.Model, ItemSet):
if not type(self).objects.filter(token=token).exists():
self.token = token
break
- return super(Order, self).save(*args, **kwargs)
+ super(Order, self).save(*args, **kwargs)
+ if not self.status_history.exists():
+ self.status_history.create(status=self.status)
def change_status(self, status):
- self.status = status
- self.save()
+ if status != self.status:
+ self.status = status
+ self.save()
+ self.status_history.create(status=status)
def get_items(self):
return OrderedItem.objects.filter(delivery_group__order=self)
@@ -115,6 +119,15 @@ class Order(models.Model, ItemSet):
context = {'payment_url': payment_url}
send_email(email, 'order/emails/confirm_email.txt', context)
+ def get_last_payment_status(self):
+ last_payment = self.payments.last()
+ if last_payment:
+ return last_payment.get_status_display()
+ return None
+
+ def is_pre_authorized(self):
+ return self.payments.filter(status='preauth').exists()
+
class DeliveryGroup(models.Model, ItemSet):
STATUS_CHOICES = (
@@ -157,14 +170,14 @@ class DeliveryGroup(models.Model, ItemSet):
def add_items_from_partition(self, partition):
for item_line in partition:
- product_variant = item_line.product
price = item_line.get_price_per_item()
+ product = item_line.product
self.items.create(
- product=product_variant.product,
+ product=product,
quantity=item_line.get_quantity(),
unit_price_net=price.net,
- product_name=smart_text(product_variant),
- product_sku=product_variant.sku,
+ product_name=smart_text(product),
+ product_sku=product.sku,
unit_price_gross=price.gross)
@@ -247,3 +260,34 @@ class Payment(BasePayment):
currency=settings.DEFAULT_CURRENCY)
for item in self.order.get_items()]
return items
+
+
+@python_2_unicode_compatible
+class OrderStatusChange(models.Model):
+ date = models.DateTimeField(
+ pgettext_lazy('Order field', 'last status change'),
+ default=now, editable=False)
+ order = models.ForeignKey(Order, related_name='status_history')
+ status = models.CharField(
+ pgettext_lazy('Order field', 'order status'),
+ max_length=32, choices=Order.STATUS_CHOICES)
+ comment = models.CharField(max_length=100, null=True, blank=True)
+
+ def __str__(self):
+ return 'Order #{0} status changed: {1}'.format(
+ self.order.pk, self.status
+ )
+
+ class Meta:
+ ordering = ['date']
+
+
+@python_2_unicode_compatible
+class OrderNote(models.Model):
+ user = models.ForeignKey(User)
+ date = models.DateTimeField(auto_now_add=True)
+ order = models.ForeignKey(Order, related_name='notes')
+ content = models.CharField(max_length=250)
+
+ def __str__(self):
+ return 'OrderNote for Order {0}'.format(self.order.pk)
diff --git a/saleor/order/templates/order/details.html b/saleor/order/templates/order/details.html
index 72271aa730..53a3f88b18 100644
--- a/saleor/order/templates/order/details.html
+++ b/saleor/order/templates/order/details.html
@@ -118,7 +118,7 @@
{{ waiting_payment_form|as_vertical_form }}
<button type="submit" class="btn btn-danger">{% trans "I understand, allow me to pay again" %}</button>
</form>
- {% else %}
+ {% elif payment_form %}
<form method="post" action="{% url "order:details" token=order.token %}">
{% csrf_token %}
<legend>{% trans "Pay for your order" %}</legend>
@@ -131,6 +131,8 @@
{% endblock %}
</div>
</form>
+ {% else %}
+ <p>{% trans "You've authorized transaction. Order is in processing" %}</p>
{% endif %}
</div>
</div>
diff --git a/saleor/order/templates/order/payment/default.html b/saleor/order/templates/order/payment/default.html
index 010ccde4da..93c3b3be85 100644
--- a/saleor/order/templates/order/payment/default.html
+++ b/saleor/order/templates/order/payment/default.html
@@ -7,16 +7,21 @@
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
- <form method="{{ form.method }}"{% if form.action %} action="{{ form.action }}"{% endif %}>
+ <form id="payment-form" method="{{ form.method }}"{% if form.action %} action="{{ form.action }}"{% endif %}>
{% csrf_token %}
{{ form.media }}
{{ form|as_vertical_form }}
- <div class="form-group">
- <button type="submit" class="btn btn-primary">
- {% trans "Save changes" %}
- </button>
- </div>
+ <div class="form-group">
+ <button type="submit" class="btn btn-primary">
+ {% trans "Save changes" %}
+ </button>
+ </div>
</form>
+ {% if "TermUrl" in form.fields %}
+ <script>
+ document.getElementById('payment-form').submit();
+ </script>
+ {% endif %}
</div>
</div>
</div>
diff --git a/saleor/order/views.py b/saleor/order/views.py
index ed496ce34e..a3e54654b9 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -9,7 +9,7 @@ from django.utils.translation import ugettext as _
from django.template.response import TemplateResponse
from payments import RedirectNeeded
-from . import check_order_status, get_ip
+from . import check_order_status
from .forms import PaymentDeleteForm, PaymentMethodsForm
from .models import Order, Payment
@@ -32,11 +32,16 @@ def details(request, token):
None, order=order, initial={'payment_id': waiting_payment.id})
if order.is_fully_paid():
form_data = None
- payment_form = PaymentMethodsForm(form_data)
- if payment_form.is_valid():
- payment_method = payment_form.cleaned_data['method']
- return redirect('order:payment', token=order.token,
- variant=payment_method)
+ if order.is_pre_authorized():
+ payment_form = None
+ else:
+ payment_form = PaymentMethodsForm(form_data)
+
+ if payment_form.is_valid():
+ payment_method = payment_form.cleaned_data['method']
+ return redirect('order:payment', token=order.token,
+ variant=payment_method)
+
return TemplateResponse(request, 'order/details.html',
{'order': order, 'groups': groups,
'payment_form': payment_form,
@@ -52,19 +57,24 @@ def start_payment(request, order, variant):
return redirect('order:details', token=order.token)
billing = order.billing_address
total = order.get_total()
+ x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
+ if x_forwarded_for:
+ customer_ip = x_forwarded_for.split(',')[0]
+ else:
+ customer_ip = request.META.get('REMOTE_ADDR')
+
defaults = {'total': total.gross,
- 'tax': total.tax, 'currency': total.currency,
+ 'tax': total.tax,
+ 'currency': total.currency,
'delivery': order.get_delivery_total().gross,
- 'billing_first_name': billing.first_name,
- 'billing_last_name': billing.last_name,
- 'billing_address_1': billing.street_address_1,
- 'billing_address_2': billing.street_address_2,
+ 'billing_first_name': billing.name,
+ 'billing_last_name': billing.name,
+ 'billing_address_1': billing.street_address,
'billing_city': billing.city,
'billing_postcode': billing.postal_code,
'billing_country_code': billing.country,
- 'description': _('Order %(order_number)s' % {'order_number': order}),
- 'billing_country_area': billing.country_area,
- 'customer_ip_address': get_ip(request)}
+ 'billing_email': order.user.email,
+ 'customer_ip_address': customer_ip}
if not variant in [v for v, n in settings.CHECKOUT_PAYMENT_CHOICES]:
raise Http404('%r is not a valid payment variant' % (variant,))
with transaction.atomic():