from django.db import models
from django.utils.translation import ugettext_lazy as _
+from django import forms
import datetime
import os
from importlib import import_module
from mezzanine.conf import settings
+from cartridge.shop import checkout
from cartridge.shop.models import Product, Category, Cart, Order, ProductVariation, DiscountCode
+from cartridge.shop.utils import set_shipping
+
+from django_countries.data import COUNTRIES
+COUNTRY_CHOICES = tuple((k,v) for k,v in COUNTRIES.items())
+
+from operator import itemgetter
+
+COUNTRY_CHOICES = tuple(sorted(COUNTRY_CHOICES, key=itemgetter(1)))
+
+from payments.multipayments.forms.base import CallbackUUIDOrderForm
+
from paypal.standard.ipn.signals import valid_ipn_received
+
def payment_complete(sender, **kwargs):
"""Performs the same logic as the code in
cartridge.shop.models.Order.complete(), but fetches the session,
pass
valid_ipn_received.connect(payment_complete)
+
+# See https://github.com/SmileyChris/django-countries/blob/master/django_countries/data.py#L46
+EURO_ZONE = [
+ ## Zone 1
+ 'DE', ## Allemagne,
+ 'BE', ## Belgique,
+ 'LU', ## Luxembourg,
+ 'NL', ## Pays-Bas.
+ ## Zone 2 :
+ 'AT', ## Autriche,
+ 'DK', ## Danemark,
+ 'ES', ## Espagne,
+ 'FI', ## Finlande,
+ 'UK', ## Grande-Bretagne,
+ 'GR', ## Grèce,
+ 'IE', ## Irlande,
+ 'IT', ## Italie,
+ 'PT', ## Portugal,
+ 'SE', ## Suède.
+ ## Zone 3 :
+ 'BG', ## Bulgarie,
+ 'CY', ## Chypre,
+ 'HR', ## Croatie,
+ 'EE', ## Estonie,
+ 'HU', ## Hongrie,
+ 'LV', ## Lettonie,
+ 'LT', ## Lituanie,
+ 'MT', ## Malte,
+ 'PL', ## Pologne,
+ 'CZ', ## République Tchèque,
+ 'RO', ## Roumanie,
+ 'SK', ## Slovaquie,
+ 'SI', ## Slovénie. +
+ ## Hors Zone
+ 'NO', ## Norvege
+ 'CH', ## Suisse
+ ]
+
+
+def custom_billship_handler(request, order_form):
+ """
+ Default billing/shipping handler - called when the first step in
+ the checkout process with billing/shipping address fields is
+ submitted. Implement your own and specify the path to import it
+ from via the setting ``SHOP_HANDLER_BILLING_SHIPPING``.
+ This function will typically contain any shipping calculation
+ where the shipping amount can then be set using the function
+ ``cartridge.shop.utils.set_shipping``. The Cart object is also
+ accessible via ``request.cart``
+ """
+
+ shipping_type = "shipping rate"
+ shipping_total = 15
+
+ if order_form is not None:
+ shipping_detail_country = order_form.cleaned_data['shipping_detail_country']
+ if shipping_detail_country == 'FR':
+ if (request.cart.total_price() <= 100) & (request.cart.total_quantity()< 3):
+ shipping_type = _("Colissimo shipping")
+ shipping_total = 7
+ else:
+ shipping_type = _("Colissimo recommande shipping")
+ shipping_total = 10
+
+ elif shipping_detail_country in EURO_ZONE:
+ if (request.cart.total_price() <= 50) & (request.cart.total_quantity()< 6):
+ shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping")
+ shipping_total = 12
+ elif (request.cart.total_price() <= 150) & (request.cart.total_quantity()< 6):
+ shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping")
+ shipping_total = 14.50
+ else:
+ shipping_type = _("INTERNATIONAL COLISSIMO (INSURED) shipping")
+ shipping_total = 18
+
+ else:
+ if (request.cart.total_price() <= 50) & (request.cart.total_quantity()< 6):
+ shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping")
+ shipping_total = 13
+ elif (request.cart.total_price() <= 150) & (request.cart.total_quantity()< 6):
+ shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping")
+ shipping_total = 16
+ else:
+ shipping_type = _("INTERNATIONAL COLISSIMO (INSURED) shipping")
+ shipping_total = 25
+
+
+
+ if not request.session.get("free_shipping"):
+ settings.use_editable()
+ print shipping_type, shipping_total
+ set_shipping(request, shipping_type, shipping_total)
+
+
+SHIPPING_CHOICES = ( (0,"SIMPLE REGISTERED PRIORITY PARCEL"),
+ (1,"INTERNATIONAL COLISSIMO (INSURED)"),
+ (2,"COLISSIMO (France only)"),
+ (3,"COLISSIMO RECOMMANDE (France only)"),
+ )
+
+
+
+class CustomOrderForm(CallbackUUIDOrderForm):
+ ## shipping_method = forms.ChoiceField(choices=SHIPPING_CHOICES,
+ ## label=_("Shipping method"))
+
+ def __init__(self, request, step, data=None, initial=None, errors=None,
+ **kwargs):
+ """
+ Setup for each order form step which does a few things:
+ """
+ super(CustomOrderForm, self).__init__(request, step, data=data, initial=initial, **kwargs)
+ billing_country = forms.Select(choices=COUNTRY_CHOICES)
+ shipping_country = forms.Select(choices=COUNTRY_CHOICES)
+ self.fields['billing_detail_country'].widget = billing_country
+ self.fields['shipping_detail_country'].widget = shipping_country
+
+
+
+ ## if step == checkout.CHECKOUT_STEP_PAYMENT:
+ ## print "Payment step"
+ ## if self.is_valid():
+ ## data = self.cleaned_data
+ ## if data["shipping_detail_country"] == 'France':
+ ## print ' ----- FRANCE ----'
+ ## choices = SHIPPING_CHOICES[2:3]
+ ## self.fields["shipping_method"].choices = choices
+ ## else:
+ ## print ' ----- PAS FRANCE ---------'
+ ## print self.fields["shipping_detail_country"]
+ ## else:
+ ## print "Pas VALIDE"
+
+
+
+ ## class Meta(CallbackUUIDOrderForm.Meta):
+ ##
+ ## fields = CallbackUUIDOrderForm.Meta.fields + ["shipping_method"]
+ ## print fields