Skip to content

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

Add login and logout views

Mira Szewczyk99631b6036f9
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index 1455e0728d..08e7201ae7 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -1,4 +1,3 @@
-from allauth.account.forms import LoginForm
from django.shortcuts import redirect
from django.template.response import TemplateResponse
@@ -13,6 +12,7 @@ from .validators import (
validate_shipping_method, validate_is_shipping_required)
from ..core import load_checkout
from ..forms import ShippingMethodForm
+from ...userprofile.registration.forms import LoginForm
@load_checkout
diff --git a/saleor/order/views.py b/saleor/order/views.py
index df885859f1..aa75fc7f8c 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -1,6 +1,5 @@
import logging
-from allauth.account.forms import LoginForm
from django.conf import settings
from django.contrib import messages, auth
from django.contrib.auth.decorators import login_required
@@ -16,6 +15,7 @@ from .models import Order, Payment
from .utils import check_order_status, attach_order_to_user
from ..core.utils import get_client_ip
from ..userprofile.models import User
+from ..userprofile.registration.forms import LoginForm
from . import OrderStatus
logger = logging.getLogger(__name__)
diff --git a/saleor/urls.py b/saleor/urls.py
index 9b6befe2f5..f1df1fd16d 100644
--- a/saleor/urls.py
+++ b/saleor/urls.py
@@ -14,7 +14,7 @@ from .core.urls import urlpatterns as core_urls
from .order.urls import urlpatterns as order_urls
from .product.urls import urlpatterns as product_urls
from .search.urls import urlpatterns as search_urls
-from .userprofile.views import login as login_view
+from .userprofile.registration.urls import urlpatterns as registration_urls
from .userprofile.urls import urlpatterns as userprofile_urls
from .data_feeds.urls import urlpatterns as feed_urls
from .dashboard.urls import urlpatterns as dashboard_urls
@@ -22,8 +22,7 @@ from .dashboard.urls import urlpatterns as dashboard_urls
urlpatterns = [
url(r'^', include(core_urls)),
- url(r'^account/', include('allauth.urls')),
- url(r'^account/login', login_view, name="account_login"),
+ url(r'^account/', include(registration_urls)),
url(r'^cart/', include(cart_urls, namespace='cart')),
url(r'^checkout/', include(checkout_urls, namespace='checkout')),
url(r'^dashboard/', include(dashboard_urls, namespace='dashboard')),
diff --git a/saleor/userprofile/registration/__init__.py b/saleor/userprofile/registration/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/saleor/userprofile/registration/forms.py b/saleor/userprofile/registration/forms.py
new file mode 100644
index 0000000000..1b8115f440
--- /dev/null
+++ b/saleor/userprofile/registration/forms.py
@@ -0,0 +1,15 @@
+from django import forms
+from django.contrib.auth.forms import AuthenticationForm
+from django.utils.translation import pgettext
+
+
+class LoginForm(AuthenticationForm):
+ username = forms.EmailField(
+ label=pgettext('Form field', 'Email'), max_length=75)
+
+ def __init__(self, request=None, *args, **kwargs):
+ super(LoginForm, self).__init__(request=request, *args, **kwargs)
+ if request:
+ email = request.GET.get('email')
+ if email:
+ self.fields['username'].initial = email
diff --git a/saleor/userprofile/registration/urls.py b/saleor/userprofile/registration/urls.py
new file mode 100644
index 0000000000..adc48aa12e
--- /dev/null
+++ b/saleor/userprofile/registration/urls.py
@@ -0,0 +1,9 @@
+from django.conf.urls import url
+
+from . import views
+
+
+urlpatterns = [
+ url(r'^login/$', views.login, name="account_login"),
+ url(r'^logout', views.logout, name="account_logout"),
+]
diff --git a/saleor/userprofile/registration/views.py b/saleor/userprofile/registration/views.py
new file mode 100644
index 0000000000..f2b3510cca
--- /dev/null
+++ b/saleor/userprofile/registration/views.py
@@ -0,0 +1,26 @@
+from django.conf import settings
+from django.contrib import messages, auth
+from django.contrib.auth import views as django_views
+from django.contrib.auth.decorators import login_required
+from django.shortcuts import redirect
+from django.utils.translation import ugettext_lazy as _
+
+from ...cart.utils import find_and_assign_anonymous_cart
+from .forms import LoginForm
+
+
+@find_and_assign_anonymous_cart()
+def login(request):
+ next_url = request.GET.get('next')
+ kwargs = {
+ 'template_name': 'account/login.html', 'authentication_form': LoginForm}
+ if next_url:
+ kwargs['redirect_authenticated_user'] = next_url
+ return django_views.login(request, **kwargs)
+
+
+@login_required
+def logout(request):
+ auth.logout(request)
+ messages.success(request, _('You have been successfully logged out.'))
+ return redirect(settings.LOGIN_REDIRECT_URL)
diff --git a/tests/test_registration.py b/tests/test_registration.py
new file mode 100644
index 0000000000..3dd9cb8ca3
--- /dev/null
+++ b/tests/test_registration.py
@@ -0,0 +1,57 @@
+from django.shortcuts import reverse
+from django.contrib.auth.models import AnonymousUser
+
+from saleor.userprofile.registration.forms import LoginForm
+from .utils import get_redirect_location
+
+
+def test_login_form_valid(customer_user):
+ data = {'username': '[email protected]', 'password': 'password'}
+ form = LoginForm(data=data)
+ assert form.is_valid()
+ assert form.get_user() == customer_user
+
+
+def test_login_form_not_valid(customer_user):
+ data = {'user': '[email protected]', 'password': 'wrongpassword'}
+ form = LoginForm(data=data)
+ assert not form.is_valid()
+ assert form.get_user_id() is None
+
+
+def test_login_view_valid(client, customer_user):
+ url = reverse('account_login')
+ response = client.post(
+ url, {'username': '[email protected]', 'password': 'password'},
+ follow=True)
+ assert response.context['user'] == customer_user
+
+
+def test_login_view_not_valid(client, customer_user):
+ url = reverse('account_login')
+ response = client.post(
+ url, {'username': '[email protected]', 'password': 'wrong'},
+ follow=True)
+ assert isinstance(response.context['user'], AnonymousUser)
+
+
+def test_login_view_next(client, customer_user):
+ url = reverse('account_login') + '?next=/cart/'
+ response = client.post(
+ url, {'username': '[email protected]', 'password': 'password'})
+ redirect_location = get_redirect_location(response)
+ assert redirect_location == '/cart/'
+
+
+def test_logout_view_no_user(client):
+ url = reverse('account_logout')
+ response = client.get(url)
+ redirect_location = get_redirect_location(response)
+ location = '/account/login/?next=/account/logout'
+ assert redirect_location == location
+
+
+def test_logout_with_user(authorized_client):
+ url = reverse('account_logout')
+ response = authorized_client.get(url, follow=True)
+ assert isinstance(response.context['user'], AnonymousUser)