Skip to content

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

Switch from django-registration to simplified own registration

Marek Brzóska953e7e559c56
diff --git a/registration/__init__.py b/registration/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/registration/backends.py b/registration/backends.py
new file mode 100644
index 0000000000..b52bf14e87
--- /dev/null
+++ b/registration/backends.py
@@ -0,0 +1,21 @@
+from django.contrib.auth import get_user_model
+
+User = get_user_model()
+
+
+class EmailPasswordBackend(object):
+ """Authentication backend that expects an email in username parameter"""
+
+ def get_user(self, user_id):
+ try:
+ return User.objects.get(pk=user_id)
+ except User.DoesNotExist:
+ return None
+
+ def authenticate(self, username=None, password=None, **kwargs):
+ try:
+ user = User.objects.get(email=username)
+ except User.DoesNotExist:
+ return None
+ if user.check_password(password):
+ return user
diff --git a/registration/forms.py b/registration/forms.py
new file mode 100644
index 0000000000..7ff2ec0315
--- /dev/null
+++ b/registration/forms.py
@@ -0,0 +1,26 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth import get_user_model
+
+User = get_user_model()
+
+
+class LoginForm(AuthenticationForm):
+
+ username = forms.EmailField(label=_("Email"), max_length=75)
+
+
+class RegisterForm(forms.Form):
+
+ email = forms.EmailField()
+ password = forms.CharField(widget=forms.PasswordInput())
+
+ def clean(self):
+ email = self.cleaned_data.get('email')
+ password = self.cleaned_data.get('password')
+
+ if email and password:
+ if User.objects.filter(email=email).exists():
+ raise forms.ValidationError('Email already registered')
+ return self.cleaned_data
diff --git a/registration/models.py b/registration/models.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/templates/registration/login.html b/registration/templates/registration/login.html
similarity index 64%
rename from saleor/templates/registration/login.html
rename to registration/templates/registration/login.html
index 80b79dd56e..54ea4fb91b 100644
--- a/saleor/templates/registration/login.html
+++ b/registration/templates/registration/login.html
@@ -1,4 +1,4 @@
-{% extends "registration/registration_base.html" %}
+{% extends "base.html" %}
{% block title %}Login{% endblock %}
{% block content %}
@@ -6,7 +6,7 @@
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
-<form method="post" action="{% url 'auth_login' %}">{% csrf_token %}
+<form method="post" action="{% url 'registration:login' %}">{% csrf_token %}
<table>
<tr>
<td>{{ form.username.label_tag }}</td>
@@ -17,7 +17,7 @@
<td>{{ form.password }}</td>
</tr>
</table>
-<p><a href="{% url 'auth_password_reset' %}">Forgot</a> your password? <a href="{% url 'registration_register' %}">Need an account</a>?</p>
+<p><a href="{% url 'registration:register' %}">Create an account</a></p>
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
diff --git a/saleor/templates/registration/logout.html b/registration/templates/registration/logout.html
similarity index 66%
rename from saleor/templates/registration/logout.html
rename to registration/templates/registration/logout.html
index 39a6e99af1..03f08744f4 100644
--- a/saleor/templates/registration/logout.html
+++ b/registration/templates/registration/logout.html
@@ -1,4 +1,4 @@
-{% extends "registration/registration_base.html" %}
+{% extends "base.html" %}
{% block title %}Logged out{% endblock %}
{% block content %}
Successfully logged out!
diff --git a/registration/templates/registration/register.html b/registration/templates/registration/register.html
new file mode 100644
index 0000000000..eeb3c2921e
--- /dev/null
+++ b/registration/templates/registration/register.html
@@ -0,0 +1,9 @@
+{% extends "base.html" %}
+{% block title %}Register for an account{% endblock %}
+{% block content %}
+ <form method='post' action='{% url 'registration:register' %}'>
+ {% csrf_token %}
+ {{ form }}
+ <input type="submit" value="Register">
+ </form>
+{% endblock %}
diff --git a/registration/urls.py b/registration/urls.py
new file mode 100644
index 0000000000..b512253a2b
--- /dev/null
+++ b/registration/urls.py
@@ -0,0 +1,9 @@
+from django.conf.urls import patterns, url
+
+
+urlpatterns = patterns(
+ '',
+ url(r'^login/$', 'registration.views.login', name='login'),
+ url(r'^logout/$', 'registration.views.logout', name='logout'),
+ url(r'^register/$', 'registration.views.register', name='register'),
+)
diff --git a/registration/views.py b/registration/views.py
new file mode 100644
index 0000000000..23b83b6dfd
--- /dev/null
+++ b/registration/views.py
@@ -0,0 +1,36 @@
+from django.contrib.auth.views import (
+ login as django_login,
+ logout as django_logout,
+)
+from django.contrib.auth import get_user_model
+from django.template.response import TemplateResponse
+from django.contrib import messages
+from django.shortcuts import redirect
+
+from .forms import LoginForm, RegisterForm
+
+User = get_user_model()
+
+
+def login(request):
+ return django_login(request, authentication_form=LoginForm)
+
+
+def logout(request):
+ return django_logout(request, template_name='registration/logout.html')
+
+
+def register(request):
+ if request.method == 'POST':
+ form = RegisterForm(request.POST)
+ if form.is_valid():
+ User.objects.create_user(
+ form.cleaned_data['email'], form.cleaned_data['password'])
+ messages.success(
+ request,
+ "You have been successfully registered. You may login now.")
+ return redirect("registration:login")
+ else:
+ form = RegisterForm()
+ return TemplateResponse(request, 'registration/register.html',
+ {'form': form})
diff --git a/saleor/settings.py b/saleor/settings.py
index 0aedd960aa..6a1ae7dced 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -48,7 +48,6 @@ TEMPLATE_DIRS = [
os.path.join(PROJECT_ROOT, 'saleor', 'templates'),
]
TEMPLATE_LOADERS = [
- 'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# TODO: this one is slow, but for now need for mptt?
'django.template.loaders.eggs.Loader',
@@ -92,7 +91,6 @@ INSTALLED_APPS = [
'south',
'django_prices',
'mptt',
- 'registration',
# Local apps
'saleor',
@@ -100,6 +98,7 @@ INSTALLED_APPS = [
'cart',
'order',
'userprofile',
+ 'registration',
]
LOGGING = {
@@ -145,6 +144,8 @@ LOGGING = {
}
}
+AUTHENTICATION_BACKENDS = ('userprofile.backends.EmailPasswordBackend',)
+
AUTH_USER_MODEL = 'userprofile.User'
WARN_ABOUT_INVALID_HTML5_OUTPUT = True
@@ -152,3 +153,5 @@ WARN_ABOUT_INVALID_HTML5_OUTPUT = True
SATCHLESS_DEFAULT_CURRENCY = 'USD'
ACCOUNT_ACTIVATION_DAYS = 3
+
+LOGIN_REDIRECT_URL = "home"
diff --git a/saleor/templates/base.html b/saleor/templates/base.html
index 36236b6265..51549634b7 100644
--- a/saleor/templates/base.html
+++ b/saleor/templates/base.html
@@ -53,15 +53,15 @@
</li>
{% if user.is_authenticated %}
<li class="dropdown pull-right"> {# TODO pull-right not working #}
- <a data-toggle="dropdown" class="dropdown-toggle" href="{% url "home" %}">{{ user }}</a>{# TODO url #}
+ <a data-toggle="dropdown" class="dropdown-toggle" href=".">{{ user }}</a>
<ul class="dropdown-menu" role="menu">
- <li><a href="{% url "home" %}">Profile edit</a></li> {# TODO url #}
- <li><a href="{% url "auth_logout" %}">Log out</a></li>
+ <li><a href="{% url "profile:details" %}">Profile edit</a></li>
+ <li><a href="{% url "registration:logout" %}">Log out</a></li>
</ul>
</li>
{% else %}
<li>
- <a href="{% url "auth_login" %}">Log in</a>
+ <a href="{% url "registration:login" %}">Log in</a>
</li>
{% endif %}
</ul>
diff --git a/saleor/templates/registration/activate.html b/saleor/templates/registration/activate.html
deleted file mode 100644
index b9a929ce56..0000000000
--- a/saleor/templates/registration/activate.html
+++ /dev/null
@@ -1,10 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}{% if account %}Activation complete{% else %}Activation problem{% endif %}{% endblock %}
-{% block content %}
-{% if account %}
-Thanks {{ account }}, activation complete! You may now <a href='{% url 'auth_login' %}'>login</a> using the username and password you set at registration.
-{% else %}
-Oops &ndash; it seems that your activation key is invalid. Please check the url again.
-{% endif %}
-{% endblock %}
-
diff --git a/saleor/templates/registration/activation_complete.html b/saleor/templates/registration/activation_complete.html
deleted file mode 100644
index 4a6c61aa6b..0000000000
--- a/saleor/templates/registration/activation_complete.html
+++ /dev/null
@@ -1,7 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Activation complete{% endblock %}
-{% block content %}
-Thanks, activation complete! You may now <a href='{% url 'auth_login' %}'>login</a> using the username and password you set at registration.
-{% endblock %}
-
-
diff --git a/saleor/templates/registration/activation_email.txt b/saleor/templates/registration/activation_email.txt
deleted file mode 100644
index 7234f33807..0000000000
--- a/saleor/templates/registration/activation_email.txt
+++ /dev/null
@@ -1,11 +0,0 @@
-You (or someone pretending to be you) have asked to register an account at
-{{ site.name }}. If this wasn't you, please ignore this email
-and your address will be removed from our records.
-
-To activate this account, please click the following link within the next
-{{ expiration_days }} days:
-
-http://{{site.domain}}{% url 'registration:registration_activate' activation_key %}
-
-Sincerely,
-{{ site.name }} Management
diff --git a/saleor/templates/registration/activation_email_subject.txt b/saleor/templates/registration/activation_email_subject.txt
deleted file mode 100644
index 13912aafdd..0000000000
--- a/saleor/templates/registration/activation_email_subject.txt
+++ /dev/null
@@ -1 +0,0 @@
-Account registration for {{ site.name }}
diff --git a/saleor/templates/registration/password_change_done.html b/saleor/templates/registration/password_change_done.html
deleted file mode 100644
index c83650412d..0000000000
--- a/saleor/templates/registration/password_change_done.html
+++ /dev/null
@@ -1,6 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Password changed{% endblock %}
-{% block content %}
-Password successfully changed!
-{% endblock %}
-
diff --git a/saleor/templates/registration/password_change_form.html b/saleor/templates/registration/password_change_form.html
deleted file mode 100644
index 95939d160a..0000000000
--- a/saleor/templates/registration/password_change_form.html
+++ /dev/null
@@ -1,10 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Change password{% endblock %}
-{% block content %}
-<form method='post' action=''>{% csrf_token %}
-<table>
- {{ form }}
- <tr><td></td><td><input type='submit' value="Change password" /></td></tr>
-</table>
-</form>
-{% endblock %}
diff --git a/saleor/templates/registration/password_reset_complete.html b/saleor/templates/registration/password_reset_complete.html
deleted file mode 100644
index 0457b20873..0000000000
--- a/saleor/templates/registration/password_reset_complete.html
+++ /dev/null
@@ -1,5 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Password reset complete{% endblock %}
-{% block content %}
-Your password has been reset! You may now <a href="{{ login_url }}">log in</a>.
-{% endblock %}
diff --git a/saleor/templates/registration/password_reset_confirm.html b/saleor/templates/registration/password_reset_confirm.html
deleted file mode 100644
index 6c4e150398..0000000000
--- a/saleor/templates/registration/password_reset_confirm.html
+++ /dev/null
@@ -1,11 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Confirm password reset{% endblock %}
-{% block content %}
-Enter your new password below to reset your password:
-<form method="post" action="">{% csrf_token %}
-<table>
- {{ form.as_table }}
- <tr><td></ td><td><input type="submit" value="Set password" /></td></tr>
-</table>
-</form>
-{% endblock %}
diff --git a/saleor/templates/registration/password_reset_done.html b/saleor/templates/registration/password_reset_done.html
deleted file mode 100644
index 47a8c49ef6..0000000000
--- a/saleor/templates/registration/password_reset_done.html
+++ /dev/null
@@ -1,8 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Password reset{% endblock %}
-{% block content %}
-<p>
-We have sent you an email with a link to reset your password. Please check
-your email and click the link to continue.
-</p>
-{% endblock %}
diff --git a/saleor/templates/registration/password_reset_email.html b/saleor/templates/registration/password_reset_email.html
deleted file mode 100644
index 62977c2b01..0000000000
--- a/saleor/templates/registration/password_reset_email.html
+++ /dev/null
@@ -1,16 +0,0 @@
-Greetings {% if user.get_full_name %}{{ user.get_full_name }}{% else %}{{ user }}{% endif %},
-
-You are receiving this email because you (or someone pretending to be you)
-requested that your password be reset on the {{ domain }} site. If you do not
-wish to reset your password, please ignore this message.
-
-To reset your password, please click the following link, or copy and paste it
-into your web browser:
-
-{{ protocol }}://{{ domain }}{% url 'auth_password_reset_confirm' uid token %}
-
-Your username, in case you've forgotten: {{ user.username }}
-
-
-Best regards,
-{{ site_name }} Management
diff --git a/saleor/templates/registration/password_reset_form.html b/saleor/templates/registration/password_reset_form.html
deleted file mode 100644
index 3bf1f99a12..0000000000
--- a/saleor/templates/registration/password_reset_form.html
+++ /dev/null
@@ -1,12 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Reset password{% endblock %}
-{% block content %}
-Forgot your password? Enter your email in the form below and we'll send you
-instructions for creating a new one.
-<form method='post' action=''>{% csrf_token %}
-<table>
- {{ form }}
- <tr><td></td><td><input type='submit' value="Reset password" /></td></tr>
-</table>
-</form>
-{% endblock %}
diff --git a/saleor/templates/registration/registration_base.html b/saleor/templates/registration/registration_base.html
deleted file mode 100644
index 94d9808cc7..0000000000
--- a/saleor/templates/registration/registration_base.html
+++ /dev/null
@@ -1 +0,0 @@
-{% extends "base.html" %}
diff --git a/saleor/templates/registration/registration_complete.html b/saleor/templates/registration/registration_complete.html
deleted file mode 100644
index 0e3ceaa9ff..0000000000
--- a/saleor/templates/registration/registration_complete.html
+++ /dev/null
@@ -1,6 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Activation email sent{% endblock %}
-{% block content %}
-An activation email has been sent. Please check your email and click on the link to activate your account.
-{% endblock %}
-
diff --git a/saleor/templates/registration/registration_form.html b/saleor/templates/registration/registration_form.html
deleted file mode 100644
index 1f16a80571..0000000000
--- a/saleor/templates/registration/registration_form.html
+++ /dev/null
@@ -1,10 +0,0 @@
-{% extends "registration/registration_base.html" %}
-{% block title %}Register for an account{% endblock %}
-{% block content %}
-<table>
- <form method='post' action=''>{% csrf_token %}
- {{ form }}
- <tr><td></td><td><input type="submit" value="Send activation email" /></td>
- </form>
-</table>
-{% endblock %}
diff --git a/saleor/urls.py b/saleor/urls.py
index 25d44a7fbf..c14747fadc 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -11,6 +11,6 @@ urlpatterns = patterns(
url(r'^products/', include('product.urls', namespace='product')),
url(r'^order/', include('order.urls', namespace='order')),
url(r'^cart/', include('cart.urls', namespace='cart')),
- url(r'^account/', include('registration.backends.default.urls')),
+ url(r'^account/', include('registration.urls', namespace='registration')),
url(r'^profile/', include('userprofile.urls', namespace='profile')),
)
diff --git a/setup.py b/setup.py
index deaacef0d4..f751634992 100755
--- a/setup.py
+++ b/setup.py
@@ -40,14 +40,12 @@ setup(
'django-prices>=2013.1',
'satchless>=2013.2a',
'South>=0.7.6',
- 'unidecode',
- 'django-registration==0.8'],
+ 'unidecode'],
extras_require={
'lint': ['pylint==0.26.0', 'django-lint==dev']},
dependency_links=[
'http://github.com/mirumee/satchless/tarball/django-removal#egg=satchless-2013.2a',
- 'http://github.com/lamby/django-lint/tarball/master#egg=django-lint-dev',
- 'https://bitbucket.org/LinnTroll/django-registration-1.5/get/c71d5b8e2735.zip#egg=django-registration-0.8'],
+ 'http://github.com/lamby/django-lint/tarball/master#egg=django-lint-dev'],
entry_points={
'console_scripts': ['saleor = saleor:manage']},
cmdclass={