Ver código fonte

Added new checkout gateway page similar to Amazon's.

master
David Winterbottom 13 anos atrás
pai
commit
a0515aa5d6

+ 22
- 0
oscar/apps/checkout/forms.py Ver arquivo

@@ -1,5 +1,6 @@
1 1
 from django import forms
2 2
 from django.db.models import get_model
3
+from django.contrib.auth.forms import AuthenticationForm
3 4
 
4 5
 
5 6
 class ShippingAddressForm(forms.ModelForm):
@@ -16,4 +17,25 @@ class ShippingAddressForm(forms.ModelForm):
16 17
         exclude = ('user', 'search_text')
17 18
 
18 19
 
20
+class GatewayForm(AuthenticationForm):
21
+    username = forms.EmailField(label="My email address is")
22
+    NEW, EXISTING = 'new', 'existing'
23
+    CHOICES = ((NEW, 'No, I am a new customer'),
24
+               (EXISTING, 'Yes, I have a password'))
25
+    options = forms.ChoiceField(widget=forms.widgets.RadioSelect,
26
+                                choices=CHOICES)
27
+
28
+    def clean(self):
29
+        cleaned_data = self.cleaned_data
30
+        if self.is_guest_checkout():
31
+            del self.errors['password']
32
+        else:
33
+            return super(GatewayForm, self).clean()
34
+        return cleaned_data
35
+
36
+    def is_guest_checkout(self):
37
+        return self.cleaned_data.get('options', None) == self.NEW
38
+
39
+
40
+
19 41
 # The BillingAddress form is in oscar.apps.payment.forms

+ 24
- 7
oscar/apps/checkout/views.py Ver arquivo

@@ -10,6 +10,7 @@ from django.forms import ModelForm
10 10
 from django.contrib import messages
11 11
 from django.core.urlresolvers import resolve
12 12
 from django.core.exceptions import ObjectDoesNotExist
13
+from django.contrib.auth import login
13 14
 from django.utils.translation import ugettext as _
14 15
 from django.template.response import TemplateResponse
15 16
 from django.core.mail import EmailMessage
@@ -18,7 +19,7 @@ from django.views.generic import DetailView, TemplateView, FormView, \
18 19
 
19 20
 from oscar.apps.shipping.methods import Free
20 21
 from oscar.core.loading import import_module
21
-import_module('checkout.forms', ['ShippingAddressForm'], locals())
22
+import_module('checkout.forms', ['ShippingAddressForm', 'GatewayForm'], locals())
22 23
 import_module('checkout.calculators', ['OrderTotalCalculator'], locals())
23 24
 import_module('checkout.utils', ['CheckoutSessionData'], locals())
24 25
 import_module('checkout.signals', ['pre_payment', 'post_payment'], locals())
@@ -40,15 +41,31 @@ import_module('basket.models', ['Basket'], locals())
40 41
 logger = logging.getLogger('oscar.checkout')
41 42
 
42 43
 
43
-class IndexView(AccountAuthView):
44
+class IndexView(FormView):
44 45
     """
45
-    First page of the checkout.  If the user is signed in then we forward
46
-    straight onto the next step.  Otherwise, we provide options to login, register and
47
-    (if the option is enabled) proceed anonymously.
46
+    First page of the checkout.  We prompt user to either sign in, or
47
+    to proceed as a guest (where we still collect their email address).
48 48
     """
49 49
     template_name = 'checkout/gateway.html'
50
-    
51
-    def get_logged_in_redirect(self):
50
+    form_class = GatewayForm
51
+
52
+    def get(self, request, *args, **kwargs):
53
+        if request.user.is_authenticated():
54
+            return self.get_success_response()
55
+        return super(IndexView, self).get(request, *args, **kwargs)
56
+
57
+    def form_valid(self, form):
58
+        if form.is_guest_checkout():
59
+            email = form.cleaned_data['username']
60
+        else:
61
+            user = form.get_user()
62
+            login(self.request, user)
63
+        return self.get_success_response()
64
+
65
+    def get_success_response(self):
66
+        return HttpResponseRedirect(self.get_success_url())
67
+
68
+    def get_success_url(self):
52 69
         return reverse('checkout:shipping-address')
53 70
 
54 71
 

+ 2
- 2
oscar/core/validators.py Ver arquivo

@@ -8,7 +8,6 @@ from django.utils.translation import ugettext_lazy as _
8 8
 
9 9
 from django.db.models import get_model
10 10
 
11
-
12 11
 class ExtendedURLValidator(validators.URLValidator):
13 12
     def __call__(self, value):
14 13
         try:
@@ -29,7 +28,8 @@ class ExtendedURLValidator(validators.URLValidator):
29 28
                 resolve(value)
30 29
             self.is_local_url = True
31 30
         except Http404:
32
-            # FlatPages is None if not installed
31
+            # We load flatpages here as it causes a circular reference problem
32
+            # sometimes.  FlatPages is None if not installed
33 33
             FlatPage = get_model('flatpages', 'FlatPage')
34 34
             if FlatPage is not None:
35 35
                 for page in FlatPage.objects.all().only(('url')):

+ 21
- 0
oscar/static/oscar/js/oscar/checkout.js Ver arquivo

@@ -0,0 +1,21 @@
1
+var oscar = oscar || {};
2
+oscar.checkout = {
3
+    gateway: {
4
+        init: function() {
5
+            var radioWidgets = $('form input[name=options]');
6
+            oscar.checkout.gateway.handleRadioSelection(radioWidgets.val());
7
+            radioWidgets.change(oscar.checkout.gateway.handleRadioChange);
8
+        },
9
+        handleRadioChange: function() {
10
+            oscar.checkout.gateway.handleRadioSelection($(this).val());
11
+        },
12
+        handleRadioSelection: function(value) {
13
+            var pwInput = $('#id_password');
14
+            if (value == 'new') {
15
+                pwInput.attr('disabled', 'disabled');
16
+            } else {
17
+                pwInput.removeAttr('disabled');
18
+            }
19
+        }
20
+    }
21
+};

+ 30
- 31
oscar/templates/checkout/gateway.html Ver arquivo

@@ -1,43 +1,42 @@
1 1
 {% extends "checkout/layout.html" %}
2
-
3 2
 {% load currency_filters %}
4 3
 
4
+{% block title %}
5
+Checkout gateway | {{ block.super }}
6
+{% endblock %}
7
+
5 8
 {% block header %}
6 9
 <div class="page-header">
7
-    <h1>Checkout / Gateway</h1>
10
+    <h1>Checkout gateway</h1>
8 11
 </div>
9 12
 {% endblock header %}
10 13
 
11
-
12 14
 {% block content %}
13 15
 
14
-<div class="row-fluid">
15
-    <div class="span6 login_form">
16
-        <form action="{{ request.get_full_path }}" method="post" class="form-stacked" />
17
-            <h2>Log In</h2>
18
-            {% csrf_token %}
19
-            {% include "partials/form_fields.html" with form=login_form %}
20
-            <input name="login_submit" type="submit" value="Log In" class="btn" />
21
-        </form>
22
-    </div>
23
-    <div class="span6 register_form">
24
-        <form action="{{ request.get_full_path }}" method="post" class="form-stacked" />
25
-            <h2>Register Here</h2>
26
-            {% csrf_token %}
27
-            {% include "partials/form_fields.html" with form=registration_form %}
28
-            <input name="registration_submit" type="submit" value="Register" class="btn" />
29
-        </form>
30
-    </div>
31
-</div>
16
+<form action="." method="post">
17
+	{% csrf_token %}
18
+	{{ form.non_field_errors }}
19
+	<div>
20
+		<h4>What is your email address?</h4>
21
+		<label for="id_username">My email address is:</label>
22
+		{{ form.username }}
23
+		{{ form.username.errors }}
24
+	</div>
25
+	<div>
26
+		<h4>Do you have an account already?</h4>
27
+		{{ form.options.errors }}
28
+		<label><input type="radio" id="id_options_0" name="options" value="new" {% if not form.password.errors %}checked="checked"{% endif %} /> No, I want to use guest checkout</label><br/>
29
+		<label><input type="radio" id="id_options_1" name="options" value="existing" {% if form.password.errors %}checked="checked"{% endif %}e> Yes, I have a password</label>
30
+		{{ form.password }}
31
+		{{ form.password.errors }}
32
+	</div>
33
+	<div>
34
+		<button type="submit" class="btn">Go!</button>
35
+	</div>
36
+</form>
32 37
 
33
-{% if anon_checkout_allowed %}
34
-<div class="sub-header">
35
-    <h2>Checkout Anonymously</h2>
36
-</div>
37
-<p>You can checkout without signing in.  You will be given the opportunity to register when checkout is complete.</p>
38
-<div class="form-actions">
39
-    <a href="{% url checkout:shipping-address %}" class="btn">Checkout anonymously</a>
40
-</div>
41
-{% endif %}
38
+{% endblock content %}
42 39
 
43
-{% endblock content %}
40
+{% block onbodyload %}
41
+oscar.checkout.gateway.init();
42
+{% endblock %}

+ 1
- 0
oscar/templates/checkout/layout.html Ver arquivo

@@ -90,4 +90,5 @@
90 90
 
91 91
 {# Block for additional scripts #}
92 92
 {% block extrascripts %}
93
+    <script src="{{ STATIC_URL }}oscar/js/oscar/checkout.js" type="text/javascript" charset="utf-8"></script>
93 94
 {% endblock %}

Carregando…
Cancelar
Salvar