diff --git a/pinaxcon/account_hooks.py b/pinaxcon/account_hooks.py new file mode 100644 index 0000000000000000000000000000000000000000..6f2b0dde7857bfd9c6ce5f393cc4f7e88a828134 --- /dev/null +++ b/pinaxcon/account_hooks.py @@ -0,0 +1,25 @@ +from account import hooks +from django.contrib.auth.models import User + + +class BetterAccountHookSet(hooks.AccountDefaultHookSet): + + def get_user_credentials(self, form, identifier_field): + username = form.cleaned_data[identifier_field] + + # Find an actual username so we can authenticate + print username, + if identifier_field == "email": + username = self.get_username_by_email(username) + print username, + + return { + "username": username, + "password": form.cleaned_data["password"], + } + + def get_username_by_email(self, email): + try: + return User.objects.get(email=email).username + except User.DoesNotExist: + return None diff --git a/pinaxcon/settings.py b/pinaxcon/settings.py index 1ee7dde2594326e88d5e76a802977ec037fd670e..0e39def4c8b79e65c99f67eb02a96ededd43c613 100644 --- a/pinaxcon/settings.py +++ b/pinaxcon/settings.py @@ -261,6 +261,8 @@ EMAIL_HOST_PASSWORD = os.environ.get("DJANGO_EMAIL_HOST_PASSWORD", "") EMAIL_USE_TLS = bool(int(os.environ.get("DJANGO_EMAIL_USE_TLS", "0"))) EMAIL_USE_SSL = bool(int(os.environ.get("DJANGO_EMAIL_USE_SSL", "0"))) +ACCOUNT_LOGIN_URL = "nbpy_login" +LOGIN_URL = "nbpy_login" # We need to explicitly switch on signups. ACCOUNT_OPEN_SIGNUP = bool(int(os.environ.get("DJANGO_ACCOUNT_OPEN_SIGNUP", "0"))) @@ -270,6 +272,7 @@ ACCOUNT_LOGIN_REDIRECT_URL = "home" ACCOUNT_LOGOUT_REDIRECT_URL = "home" ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 2 ACCOUNT_USE_AUTH_AUTHENTICATE = True +ACCOUNT_HOOKSET = "pinaxcon.account_hooks.BetterAccountHookSet" AUTHENTICATION_BACKENDS = [ "symposion.teams.backends.TeamPermissionsBackend", diff --git a/pinaxcon/templates/account_login.html b/pinaxcon/templates/account_login.html new file mode 100644 index 0000000000000000000000000000000000000000..a058d0c1e3e726e2392ca73f51d5402435bb0469 --- /dev/null +++ b/pinaxcon/templates/account_login.html @@ -0,0 +1,59 @@ +{% extends "page_with_title_and_lede.html" %} + +{% load bootstrap %} +{% load i18n %} + +{% block head_title %}Log In{% endblock %} + +{% block heading %}Log In or Sign Up{% endblock %} + +{% block body_class %}login{% endblock %} + +{% block content %} + +
+
+
+ +
+
+
+

+ Log into an existing account +

+
+
+ {% csrf_token %} + {{ login_form|bootstrap }} +
+ +
+
+ +
+ {% if signup_open %} +
+
+
+
+

+ Sign up for a new account +

+
+
+ {% csrf_token %} + {{ signup_form|bootstrap }} +
+ +
+
+
+ {% endif %} +
+
+ +{% endblock %} diff --git a/pinaxcon/urls.py b/pinaxcon/urls.py index 6f454a87254105804a59d8d28cdc7a81bdad52e5..eed7a6a070fea1f905923fce648a7a25a8e0bd81 100644 --- a/pinaxcon/urls.py +++ b/pinaxcon/urls.py @@ -52,6 +52,9 @@ urlpatterns = [ url(r"^admin/", include(admin.site.urls)), + url(r"^login$", views.account_login, name="nbpy_login"), + # Override the default account_login view with one that takes email addys + url(r"^account/login/$", views.EmailLoginView.as_view(), name="account_login"), url(r"^account/", include("account.urls")), url(r"^dashboard/", symposion.views.dashboard, name="dashboard"), diff --git a/pinaxcon/views.py b/pinaxcon/views.py index 433e3684b49a6d76a2d4f230a143b6b873bed7ea..63401ead9e0ba97a1ca65190ac29ea7f2a53b6a9 100644 --- a/pinaxcon/views.py +++ b/pinaxcon/views.py @@ -1,9 +1,30 @@ +from django.conf import settings from django.http import HttpResponseServerError +from django.shortcuts import render from django.template import RequestContext from django.template import Template from django.template.loader import get_template from django.views import defaults +from account.forms import LoginEmailForm, LoginUsernameForm, SignupForm +from account.views import LoginView + def server_error(request, template_name=defaults.ERROR_500_TEMPLATE_NAME): t = Template("{%% include '%s' %%}" % template_name) return HttpResponseServerError(t.render(RequestContext(request))) + + +def account_login(request): + + d = { + "login_form": LoginEmailForm(), + "signup_form": SignupForm(), + "signup_open": getattr(settings, "ACCOUNT_OPEN_SIGNUP", True), + } + + print d["signup_open"], settings.ACCOUNT_OPEN_SIGNUP + return render(request, "account_login.html", d) + + +class EmailLoginView(LoginView): + form_class = LoginEmailForm