diff --git a/email_change/__init__.py b/email_change/__init__.py new file mode 100644 index 00000000..1c7d5a51 --- /dev/null +++ b/email_change/__init__.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +VERSION = (0, 2, 2, 'final', 0) + +def get_version(): + version = '%d.%d.%d' % (VERSION[0], VERSION[1], VERSION[2]) + return version + diff --git a/email_change/admin.py b/email_change/admin.py new file mode 100644 index 00000000..ead9a59f --- /dev/null +++ b/email_change/admin.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.contrib import admin +from django.db.models.loading import cache + +class EmailChangeRequestAdmin(admin.ModelAdmin): + raw_id_fields = ('user',) + +admin.site.register(cache.get_model('email_change', 'EmailChangeRequest'), EmailChangeRequestAdmin) \ No newline at end of file diff --git a/email_change/forms.py b/email_change/forms.py new file mode 100644 index 00000000..d3c71f0d --- /dev/null +++ b/email_change/forms.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django import forms +from django.db.models.loading import cache + + +class EmailChangeForm(forms.Form): + + email = forms.EmailField(label='New E-mail', max_length=75) + + def __init__(self, username=None, *args, **kwargs): + """Constructor. + + **Mandatory arguments** + + ``username`` + The username of the user that requested the email change. + + """ + self.username = username + super(EmailChangeForm, self).__init__(*args, **kwargs) + + def clean_email(self): + """Checks whether the new email address differs from the user's current + email address. + + """ + email = self.cleaned_data.get('email') + + User = cache.get_model('auth', 'User') + user = User.objects.get(username__exact=self.username) + + # Check if the new email address differs from the current email address. + if user.email == email: + raise forms.ValidationError('New email address cannot be the same \ + as your current email address') + + return email + + + diff --git a/email_change/locale/el/LC_MESSAGES/django.mo b/email_change/locale/el/LC_MESSAGES/django.mo new file mode 100644 index 00000000..64ca1e9b Binary files /dev/null and b/email_change/locale/el/LC_MESSAGES/django.mo differ diff --git a/email_change/locale/el/LC_MESSAGES/django.po b/email_change/locale/el/LC_MESSAGES/django.po new file mode 100644 index 00000000..893ce6b5 --- /dev/null +++ b/email_change/locale/el/LC_MESSAGES/django.po @@ -0,0 +1,141 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2011-02-23 18:25+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: .\templates\email_change\email_change_complete.html.py:4 +#: .\templates\email_change\email_change_complete.html.py:13 +msgid "Email change complete" +msgstr "Αλλαγή διεύθυνσης email ολοκληρώθηκε" + +#: .\templates\email_change\email_change_complete.html.py:7 +#: .\templates\email_change\email_verification_sent.html.py:7 +#: .\templates\email_change\email_verify.html.py:7 +msgid "Notice" +msgstr "Σημείωση" + +#: .\templates\email_change\email_change_complete.html.py:18 +msgid "Your email address has been changed successfully." +msgstr "Η διεύθυνση email σας έχει αλλάξει επιτυχώς." + +#: .\templates\email_change\email_change_complete.html.py:19 +msgid "Your new email address is:" +msgstr "Η νέα διεύθυνση email είναι:" + +#: .\templates\email_change\email_change_complete.html.py:21 +#: .\templates\email_change\email_change_form.html.py:25 +#: .\templates\email_change\email_verification_sent.html.py:20 +#: .\templates\email_change\email_verify.html.py:21 +msgid "Back to your profile" +msgstr "Πίσω στο προφίλ σας" + +#: .\templates\email_change\email_change_form.html.py:4 +#: .\templates\email_change\email_change_form.html.py:7 +#: .\templates\email_change\email_change_form.html.py:13 +#: .\templates\email_change\email_change_form.html.py:23 +msgid "Change email address" +msgstr "Αλλαγή διεύθυνσης email" + +#: .\templates\email_change\email_change_form.html.py:18 +msgid "Your current email address is:" +msgstr "Η τωρινή διεύθυνση email σας είναι:" + +#: .\templates\email_change\email_change_form.html.py:20 +msgid "" +"To associate a new email address with your account, please enter the new " +"address in the box below." +msgstr "Για να συσχετίσεται μια νέα διεύθυνση email με το λογαριασμό σας, παρακαλούμε εισάγετε τη νέα διεύθυνση παρακάτω." + +#: .\templates\email_change\email_verification_sent.html.py:4 +#: .\templates\email_change\email_verification_sent.html.py:13 +msgid "Verification email sent" +msgstr "Email επαλήθευσης στάλθηκε" + +#: .\templates\email_change\email_verification_sent.html.py:18 +msgid "" +"An email containing a verification link has been sent to your new email " +"address." +msgstr "Ένα email που περιέχει έναν σύνδεσμο για επαλήθευση σας έχει αποσταλεί στη νέα διεύθυνση." + +#: .\templates\email_change\email_verification_sent.html.py:19 +msgid "" +"Follow the instructions in this email in order to successfully change your " +"current email address." +msgstr "Ακολουθήστε τις οδηγίες αυτού του μηνύματος προκειμένου να αλλάξετε επιτυχώς την τωρινή σας διεύθυνση." + +#: .\templates\email_change\email_verify.html.py:4 +msgid "Email confirmation" +msgstr "Επιβεβαίωση διεύθυνσης email" + +#: .\templates\email_change\email_verify.html.py:13 +msgid "Email confirmation failed" +msgstr "Επιβεβαίωση διεύθυνσης email απέτυχε" + +#: .\templates\email_change\email_verify.html.py:18 +msgid "This confirmation code has either expired or is invalid." +msgstr "Αυτός ο κωδικός επιβεβαίωσης έχει λήξει ή είναι άκυρος." + +#: .\templates\email_change\email_verify.html.py:19 +msgid "The requested email address change has failed." +msgstr "Το αίτημα για αλλαγή της διεύθυνσης email απέτυχε." + +#: .\templates\email_change\email_verify.html.py:20 +msgid "Please, try to change it again in your account settings." +msgstr "Παρακαλούμε, επιχειρήστε να το αλλάξετε ξανά στις ρυθμίσεις του λογαριασμού σας." + +#: .\templates\email_change\emails\verification_email_message.html.py:2 +msgid "Hi" +msgstr "Γεια" + +#: .\templates\email_change\emails\verification_email_message.html.py:4 +#, python-format +msgid "" +"A request to change the email address of your account has been made at " +"%(site_name)s." +msgstr "Ένα αίτημα για αλλαγή της διεύθυνσης email του λογαριασμού σας έχει γίνει στο %(site_name)s." + +#: .\templates\email_change\emails\verification_email_message.html.py:6 +msgid "" +"Before changing your current email address, verification of your new email " +"address is required." +msgstr "Πριν αλλάξετε την τρέχουσα διεύθυνση email, η επαλήθευση της νέας διεύθυνσης email είναι απαραίτητη." + +#: .\templates\email_change\emails\verification_email_message.html.py:8 +msgid "" +"To complete the email change, please visit the following URL, either by " +"clicking on it or by copying and pasting it in your browser:" +msgstr "Για να ολοκληρώσετε την αλλαγή της διεύθυνσης email, παρακαλούμε επισκευτείτε την παρακάτω διεύθυνση είτε κάνοντας κλικ πάνω της είτε αντιγράφοντάς την και επικολλώντας την στον φυλομετρητή σας:" + +#: .\templates\email_change\emails\verification_email_message.html.py:12 +msgid "You will only need to visit this URL once." +msgstr "Χρειάζετε να επισκευτείτε αυτή τη διεύθυνση μόνο μία φορά." + +#: .\templates\email_change\emails\verification_email_message.html.py:14 +msgid "Thank you" +msgstr "Ευχαριστούμε" + +#: .\templates\email_change\emails\verification_email_message.html.py:16 +#, python-format +msgid "The Team at %(site_name)s" +msgstr "Η μάδα του %(site_name)s" + +#: .\templates\email_change\emails\verification_email_subject.html.py:1 +#, python-format +msgid "Email change verification for %(site_name)s" +msgstr "Επιβεβαίωση αλλαγής διεύθυνσης email στο %(site_name)s" + diff --git a/email_change/models.py b/email_change/models.py new file mode 100644 index 00000000..0dfe99fa --- /dev/null +++ b/email_change/models.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import datetime + +from django.db import models + +from email_change import settings + + +class EmailChangeRequest(models.Model): + user = models.ForeignKey('auth.User', unique=True, related_name='%(class)s_user') + verification_key = models.CharField(max_length=40) + email = models.EmailField(max_length=75) # Contains the new email address + date_created = models.DateTimeField(auto_now_add=True) + + class Meta: + verbose_name = 'email change request' + verbose_name_plural = 'email change requests' + + def __unicode__(self): + return '(%s) %s --> %s' % (self.user.username, self.user.email, self.email) + + def has_expired(self): + dt = datetime.timedelta(days=settings.EMAIL_CHANGE_VERIFICATION_DAYS) + expiration_date = self.date_created + dt + return expiration_date <= datetime.datetime.now() + diff --git a/email_change/settings.py b/email_change/settings.py new file mode 100644 index 00000000..f5ddb93e --- /dev/null +++ b/email_change/settings.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.conf import settings + + +EMAIL_CHANGE_VERIFICATION_DAYS = getattr(settings, 'EMAIL_CHANGE_VERIFICATION_DAYS', 7) + diff --git a/email_change/signal_cb.py b/email_change/signal_cb.py new file mode 100644 index 00000000..f448902f --- /dev/null +++ b/email_change/signal_cb.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.db.models.loading import cache + + +def signal_callback(sender, **kwargs): + instance = kwargs['instance'] # app_label.ModelName instance + \ No newline at end of file diff --git a/email_change/templates/email_change/base.html b/email_change/templates/email_change/base.html new file mode 100644 index 00000000..924cfbc8 --- /dev/null +++ b/email_change/templates/email_change/base.html @@ -0,0 +1,15 @@ +{% extends "registration/registration_base.html" %} + + + +{% block doccontent %} +{% block ce_content %}{% endblock %} + + +{% endblock %} \ No newline at end of file diff --git a/email_change/templates/email_change/email_change_complete.html b/email_change/templates/email_change/email_change_complete.html new file mode 100644 index 00000000..9765384c --- /dev/null +++ b/email_change/templates/email_change/email_change_complete.html @@ -0,0 +1,16 @@ +{% extends "email_change/base.html" %} +{% load i18n %} + +{% block title %}{% trans "Email change complete" %}{% endblock %} + + +{% block ce_content %} +

{% trans "Email change complete" %}

+
+

{% trans "Your email address has been changed successfully." %}

+

{% trans "Your new email address is:" %}

+
{{ user.email }}
+
+{% endblock %} + + diff --git a/email_change/templates/email_change/email_change_form.html b/email_change/templates/email_change/email_change_form.html new file mode 100644 index 00000000..e1b5f72b --- /dev/null +++ b/email_change/templates/email_change/email_change_form.html @@ -0,0 +1,22 @@ +{% extends "email_change/base.html" %} +{% load i18n %} + +{% block title %}{% trans "Change email address" %}{% endblock %} + + +{% block ce_content %} +

{% trans "Change email address" %}

+
+

{% trans "Your current email address is:" %}

+
{{ user.email }}
+

{% trans "To associate a new email address with your account, please enter the new address in the box below." %}

+
{% csrf_token %} + {{ form.as_p }} +

+
+ +
+{% endblock %} + diff --git a/email_change/templates/email_change/email_verification_sent.html b/email_change/templates/email_change/email_verification_sent.html new file mode 100644 index 00000000..2ba18133 --- /dev/null +++ b/email_change/templates/email_change/email_verification_sent.html @@ -0,0 +1,13 @@ +{% extends "email_change/base.html" %} +{% load i18n %} + +{% block title %}{% trans "Verification email sent" %}{% endblock %} + + +{% block ce_content %} +

{% trans "Verification email sent" %}

+
+

{% trans "An email containing a verification link has been sent to your new email address." %}

+

{% trans "Follow the instructions in this email in order to successfully change your current email address." %}

+{% endblock %} + diff --git a/email_change/templates/email_change/email_verify.html b/email_change/templates/email_change/email_verify.html new file mode 100644 index 00000000..5e6cde34 --- /dev/null +++ b/email_change/templates/email_change/email_verify.html @@ -0,0 +1,15 @@ +{% extends "email_change/base.html" %} +{% load i18n %} + +{% block title %}{% trans "Email confirmation" %}{% endblock %} + + + +{% block ce_content %} +

{% trans "Email confirmation failed" %}

+
+

{% trans "This confirmation code has either expired or is invalid." %}

+

{% trans "The requested email address change has failed." %}

+
+{% endblock %} + diff --git a/email_change/templates/email_change/emails/verification_email_message.html b/email_change/templates/email_change/emails/verification_email_message.html new file mode 100644 index 00000000..1282e75f --- /dev/null +++ b/email_change/templates/email_change/emails/verification_email_message.html @@ -0,0 +1,17 @@ +{% load i18n %}{% autoescape off %} +{% trans "Hi" %} {% firstof user.get_full_name user.username %}, + +{% blocktrans %}A request to change the email address of your account has been made at {{ site_name }}.{% endblocktrans %} + +{% blocktrans %}Before changing your current email address, verification of your new email address is required.{% endblocktrans %} + +{% blocktrans %}To complete the email change, please visit the following URL, either by clicking on it or by copying and pasting it in your browser:{% endblocktrans %} + +{{ protocol }}://{{ site_domain }}{% url email_verify verification_key %} + +{% blocktrans %}You will only need to visit this URL once.{% endblocktrans %} + +{% trans "Thank you" %} +-- +{% blocktrans %}The Team at {{ site_name }}{% endblocktrans %} +{% endautoescape %} diff --git a/email_change/templates/email_change/emails/verification_email_subject.html b/email_change/templates/email_change/emails/verification_email_subject.html new file mode 100644 index 00000000..7e02b65c --- /dev/null +++ b/email_change/templates/email_change/emails/verification_email_subject.html @@ -0,0 +1 @@ +{% load i18n %}{% blocktrans %}Email change verification for {{ site_name }}{% endblocktrans %} \ No newline at end of file diff --git a/email_change/tests.py b/email_change/tests.py new file mode 100644 index 00000000..a9738b17 --- /dev/null +++ b/email_change/tests.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/email_change/urls.py b/email_change/urls.py new file mode 100644 index 00000000..cb3e6d84 --- /dev/null +++ b/email_change/urls.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.conf.urls.defaults import * +from django.views.generic.simple import direct_to_template + + +urlpatterns = patterns('', + url(r'^email/change/$', 'email_change.views.email_change_view', name='email_change'), + url(r'^email/verification/sent/$', + direct_to_template, {'template': 'email_change/email_verification_sent.html'}, + name='email_verification_sent'), + # Note taken from django-registration + # Verification keys get matched by \w+ instead of the more specific + # [a-fA-F0-9]{40} because a bad verification key should still get to the view; + # that way it can return a sensible "invalid key" message instead of a + # confusing 404. + url(r'^email/verify/(?P\w+)/$', 'email_change.views.email_verify_view', name='email_verify'), + url(r'^email/change/complete/$', + direct_to_template, {'template': 'email_change/email_change_complete.html'}, + name='email_change_complete'), +) diff --git a/email_change/utils.py b/email_change/utils.py new file mode 100644 index 00000000..c2d75820 --- /dev/null +++ b/email_change/utils.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random +from django.contrib.auth.tokens import default_token_generator +from django.utils.hashcompat import sha_constructor + + +def generate_key(user, email): + """Generates and returns unique keys. + + **Arguments** + + ``user`` + auth.User instance + ``email`` + The new email address + + """ + return sha_constructor( + default_token_generator.make_token(user) + email + str(random.random()) + ).hexdigest() + \ No newline at end of file diff --git a/email_change/views.py b/email_change/views.py new file mode 100644 index 00000000..ea6e1576 --- /dev/null +++ b/email_change/views.py @@ -0,0 +1,138 @@ +# -*- coding: utf-8 -*- +# +# This file is part of django-email-change. +# +# django-email-change adds support for email address change and confirmation. +# +# Development Web Site: +# - http://www.codetrax.org/projects/django-email-change +# Public Source Code Repository: +# - https://source.codetrax.org/hgroot/django-email-change +# +# Copyright 2010 George Notaras +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +from django.contrib.auth.decorators import login_required +from django.shortcuts import render_to_response, redirect +from django.db.models.loading import cache +from django.template import RequestContext, Context +from django.template.loader import render_to_string +from django.core.mail import send_mail + +from email_change.forms import EmailChangeForm +from email_change.utils import generate_key + + +@login_required +def email_change_view(request, extra_context={}, + success_url='email_verification_sent', + template_name='email_change/email_change_form.html', + email_message_template_name='email_change/emails/verification_email_message.html', + email_subject_template_name='email_change/emails/verification_email_subject.html'): + """Allow a user to change the email address associated with the user account. + + + """ + if request.method == 'POST': + form = EmailChangeForm(username=request.user.username, data=request.POST, files=request.FILES) + if form.is_valid(): + + EmailChangeRequest = cache.get_model('email_change', 'EmailChangeRequest') + Site = cache.get_model('sites', 'Site') + + email = form.cleaned_data.get('email') + verification_key = generate_key(request.user, email) + + current_site = Site.objects.get_current() + site_name = current_site.name + domain = current_site.domain + + protocol = 'http' + if request.is_secure(): + protocol = 'https' + + # First clean all email change requests made by this user + qs = EmailChangeRequest.objects.filter(user=request.user) + qs.delete() + + # Create an email change request + EmailChangeRequest.objects.create( + user = request.user, + verification_key = verification_key, + email = email + ) + + # Prepare context + c = { + 'email': email, + 'site_domain': domain, + 'site_name': site_name, + 'user': request.user, + 'verification_key': verification_key, + 'protocol': protocol, + } + c.update(extra_context) + context = Context(c) + + # Send success email + subject = render_to_string(email_subject_template_name, context_instance=context) + message = render_to_string(email_message_template_name, context_instance=context) + + send_mail(subject, message, None, [email]) + + # Redirect + return redirect(success_url) + + else: + form = EmailChangeForm(username=request.user.username) + + context = RequestContext(request, extra_context) + context['form'] = form + + return render_to_response(template_name, context_instance=context) + + + +@login_required +def email_verify_view(request, verification_key, extra_context={}, + success_url='email_change_complete', + template_name='email_change/email_verify.html'): + """ + """ + EmailChangeRequest = cache.get_model('email_change', 'EmailChangeRequest') + context = RequestContext(request, extra_context) + try: + ecr = EmailChangeRequest.objects.get( + user=request.user, verification_key=verification_key) + except EmailChangeRequest.DoesNotExist: + # Return failure response + return render_to_response(template_name, context_instance=context) + else: + # Check if the email change request has expired + if ecr.has_expired(): + ecr.delete() + # Return failure response + return render_to_response(template_name, context_instance=context) + + # Success. Replace the user's email with the new email + request.user.email = ecr.email + request.user.save() + + # Delete the email change request + ecr.delete() + + # Redirect to success URL + return redirect(success_url) + diff --git a/frontend/templates/registration/user_change_form.html b/frontend/templates/registration/user_change_form.html index 9deb0ff7..1b0324b6 100644 --- a/frontend/templates/registration/user_change_form.html +++ b/frontend/templates/registration/user_change_form.html @@ -12,19 +12,14 @@
-
-

Changing Your Email

-

Your current email: {{ user.email }}

-
- {% csrf_token %} - {{ emailform.as_p }} - -
-
+

Other Account Tools

Want to change your password?

-

... or manage your pledges and payment info?

+

... or manage your pledges and payment info?

+

... or change your email address?

+

... or manage your contact preferences?

+ {% endblock %} diff --git a/settings/common.py b/settings/common.py index 21e92149..7fb70aa4 100644 --- a/settings/common.py +++ b/settings/common.py @@ -139,6 +139,7 @@ INSTALLED_APPS = ( 'regluit.frontend.templatetags', 'regluit.payment.templatetags', 'notification', + 'email_change', 'ckeditor', 'storages', # this must appear *after* django.frontend or else it overrides the diff --git a/urls.py b/urls.py index f7ebdca5..45ea70d5 100755 --- a/urls.py +++ b/urls.py @@ -15,6 +15,7 @@ urlpatterns = patterns('', {'template': 'registration/welcome.html', 'extra_context': {'suppress_search_box': True,} }), + url('accounts/', include('email_change.urls')), (r'^api/', include('regluit.api.urls')), (r'', include('regluit.frontend.urls')), (r'', include('regluit.payment.urls')), @@ -22,5 +23,6 @@ urlpatterns = patterns('', url(r'^admin/', include(admin_site.urls)), (r'^comments/', include('django.contrib.comments.urls')), (r'^notification/', include(notification.urls)), + (r'^ckeditor/', include('ckeditor.urls')), )