Sfoglia il codice sorgente

adds a custom PasswordResetForm to handle emails through communication events

master
Sebastian Vetter 13 anni fa
parent
commit
63db79d63c

+ 3
- 0
oscar/app.py Vedi File

@@ -11,6 +11,8 @@ from oscar.apps.search.app import application as search_app
11 11
 from oscar.apps.offer.app import application as offer_app
12 12
 from oscar.apps.dashboard.app import application as dashboard_app
13 13
 
14
+from oscar.apps.customer import forms
15
+
14 16
 
15 17
 class Shop(Application):
16 18
     name = None
@@ -38,6 +40,7 @@ class Shop(Application):
38 40
             # we can't namespace these urls as that prevents
39 41
             # the reverse function from working.
40 42
             url(r'^password-reset/$', auth_views.password_reset,
43
+                {'password_reset_form': forms.PasswordResetForm},
41 44
                 name='password-reset'),
42 45
             url(r'^password-reset/done/$', auth_views.password_reset_done,
43 46
                 name='password-reset-done'),

+ 57
- 1
oscar/apps/customer/forms.py Vedi File

@@ -5,11 +5,19 @@ from django.contrib.auth.forms import AuthenticationForm
5 5
 from django.utils.translation import ugettext_lazy as _
6 6
 from django.core.exceptions import ObjectDoesNotExist
7 7
 from django import forms
8
+from django.db.models import get_model
8 9
 from django.contrib.auth.models import User
10
+from django.contrib.auth import forms as auth_forms
9 11
 from django.conf import settings
10 12
 from django.core import validators
13
+from django.utils.http import int_to_base36
14
+from django.contrib.sites.models import get_current_site
15
+from django.contrib.auth.tokens import default_token_generator
11 16
 
12
-from oscar.core.loading import get_profile_class
17
+from oscar.core.loading import get_profile_class, get_class
18
+
19
+Dispatcher = get_class('customer.utils', 'Dispatcher')
20
+CommunicationEventType = get_model('customer', 'communicationeventtype')
13 21
 
14 22
 
15 23
 def generate_username():
@@ -21,6 +29,54 @@ def generate_username():
21 29
         return uname
22 30
 
23 31
 
32
+class PasswordResetForm(auth_forms.PasswordResetForm):
33
+    communication_type_code = "PASSWORD_RESET"
34
+
35
+    def save(self, subject_template_name='registration/password_reset_subject.txt',
36
+             email_template_name='registration/password_reset_email.html',
37
+             use_https=False, token_generator=default_token_generator,
38
+             from_email=None, request=None, **kwargs):
39
+        """
40
+        Generates a one-use only link for resetting password and sends to the
41
+        user.
42
+        """
43
+        for user in self.users_cache:
44
+            current_site = get_current_site(request)
45
+            ctx = {
46
+                'email': user.email,
47
+                'domain': current_site.domain,
48
+                'site_name': current_site.name,
49
+                'uid': int_to_base36(user.id),
50
+                'token': token_generator.make_token(user),
51
+                'protocol': use_https and 'https' or 'http',
52
+                'site': current_site,
53
+            }
54
+            self.send_reset_email(user, ctx)
55
+
56
+    def send_reset_email(self, user, extra_context=None):
57
+        code = self.communication_type_code
58
+        ctx = {
59
+            'user': user,
60
+            'static_base_url': getattr(settings, 'OSCAR_STATIC_BASE_URL', None)
61
+        }
62
+
63
+        if extra_context:
64
+            ctx.update(extra_context)
65
+
66
+        try:
67
+            event_type = CommunicationEventType.objects.get(code=code)
68
+        except CommunicationEventType.DoesNotExist:
69
+            # No event in database, attempt to find templates for this type
70
+            messages = CommunicationEventType.objects.get_and_render(code, ctx)
71
+        else:
72
+            # Create order event
73
+            messages = event_type.get_messages(ctx)
74
+
75
+        if messages and messages['body']:
76
+            dispatcher = Dispatcher()
77
+            dispatcher.dispatch_user_messages(user, messages)
78
+
79
+
24 80
 class EmailAuthenticationForm(AuthenticationForm):
25 81
     """
26 82
     Extends the standard django AuthenticationForm, to support 75 character

oscar/templates/oscar/registration/password_reset_email.html → oscar/templates/oscar/customer/emails/commtype_password_reset_body.html Vedi File


+ 13
- 0
oscar/templates/oscar/customer/emails/commtype_password_reset_body.txt Vedi File

@@ -0,0 +1,13 @@
1
+{% load i18n %}{% load url from future %}{% autoescape off %}
2
+{% blocktrans %}You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.{% endblocktrans %}
3
+
4
+{% trans "Please go to the following page and choose a new password:" %}
5
+{% block reset_link %}
6
+{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %}
7
+{% endblock %}
8
+
9
+{% trans "Thanks for using our site!" %}
10
+
11
+{% blocktrans %}The {{ site_name }} team{% endblocktrans %}
12
+
13
+{% endautoescape %}

+ 1
- 0
oscar/templates/oscar/customer/emails/commtype_password_reset_subject.txt Vedi File

@@ -0,0 +1 @@
1
+{% load i18n %}{% blocktrans with site_name=site.name %}Resetting your password at {{ site_name }}.{% endblocktrans %}

Loading…
Annulla
Salva