From 99bc330bf50232e6302a12ea86c62745f5d87fef Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Thu, 3 Oct 2019 15:08:07 +0100 Subject: Initial Allauth dependency and settings/urls --- pydis_site/settings.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'pydis_site/settings.py') diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 4534f54b..189c35e6 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -82,6 +82,13 @@ INSTALLED_APPS = [ 'django.contrib.sites.apps.SitesConfig', 'django.contrib.staticfiles', + 'allauth', + 'allauth.account', + 'allauth.socialaccount', + + 'allauth.socialaccount.providers.discord', + 'allauth.socialaccount.providers.github', + 'crispy_forms', 'django_crispy_bulma', 'django_hosts', @@ -347,3 +354,13 @@ WIKI_MARKDOWN_HTML_ATTRIBUTES = { WIKI_MARKDOWN_HTML_WHITELIST = [ 'article', 'section', 'button' ] + +# Django Allauth stuff + +AUTHENTICATION_BACKENDS = ( + # Needed to login by username in Django admin, regardless of `allauth` + 'django.contrib.auth.backends.ModelBackend', + + # `allauth` specific authentication methods, such as login by e-mail + 'allauth.account.auth_backends.AuthenticationBackend', +) -- cgit v1.2.3 From 01c059877bc0d82a72558194e4c8ddb30f1c8b33 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Thu, 3 Oct 2019 18:53:04 +0100 Subject: Disable Allauth email verification --- pydis_site/settings.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'pydis_site/settings.py') diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 189c35e6..21841e15 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -364,3 +364,5 @@ AUTHENTICATION_BACKENDS = ( # `allauth` specific authentication methods, such as login by e-mail 'allauth.account.auth_backends.AuthenticationBackend', ) + +ACCOUNT_EMAIL_VERIFICATION = "none" # No verification required; we don't use emails for anything -- cgit v1.2.3 From 23f7f9e5f09e7b5b52810d19ffe7ad31eeabd054 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sat, 5 Oct 2019 14:49:29 +0100 Subject: Add MessageRedirectView and show messages on the index page --- pydis_site/settings.py | 10 ++++++++++ pydis_site/static/css/base/base.css | 8 ++++++++ pydis_site/templates/home/index.html | 16 ++++++++++++++++ pydis_site/utils/views.py | 25 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 pydis_site/utils/views.py (limited to 'pydis_site/settings.py') diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 21841e15..919547de 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -272,6 +272,16 @@ LOGGING = { } } +# Django Messages framework config + +MESSAGE_TAGS = { + messages.DEBUG: 'primary', + messages.INFO: 'info', + messages.SUCCESS: 'success', + messages.WARNING: 'warning', + messages.ERROR: 'danger', +} + # Custom settings for Crispyforms CRISPY_ALLOWED_TEMPLATE_PACKS = ( "bootstrap", diff --git a/pydis_site/static/css/base/base.css b/pydis_site/static/css/base/base.css index ce1503a3..283e89cb 100644 --- a/pydis_site/static/css/base/base.css +++ b/pydis_site/static/css/base/base.css @@ -70,3 +70,11 @@ div.card.has-equal-height { #pydis-text { font-weight: bold; } + +button.is-size-navbar-menu { + font-size: 14px; +} + +section.message-section { + padding: 1.5rem 1.5rem 0 1.5rem; +} diff --git a/pydis_site/templates/home/index.html b/pydis_site/templates/home/index.html index 205e92ff..a367fe70 100644 --- a/pydis_site/templates/home/index.html +++ b/pydis_site/templates/home/index.html @@ -9,6 +9,22 @@ {% block content %} {% include "base/navbar.html" %} + {% if messages %} +
+
+
+ {% for message in messages %} +
+ + + {{ message }} +
+ {% endfor %} +
+
+
+ {% endif %} +
{# Who are we? #} diff --git a/pydis_site/utils/views.py b/pydis_site/utils/views.py new file mode 100644 index 00000000..c9803bd6 --- /dev/null +++ b/pydis_site/utils/views.py @@ -0,0 +1,25 @@ +from django.contrib import messages +from django.http import HttpRequest +from django.views.generic import RedirectView + + +class MessageRedirectView(RedirectView): + """ + Redirects to another URL, also setting a message using the Django Messages framework. + + This is based on Django's own `RedirectView` and works the same way, but takes two additional + parameters. + + * `message`: Set to the message content you wish to display. + * `message_level`: Set to one of the message levels from the Django messages framework. This + parameter defaults to `messages.INFO`. + """ + + message: str = "" + message_level: int = messages.INFO + + def get(self, request: HttpRequest, *args, **kwargs) -> None: + """Called upon a GET request.""" + messages.add_message(request, self.message_level, self.message) + + return super().get(request, *args, **kwargs) -- cgit v1.2.3 From 0ebaa16646ad3723a6c365ebe4208586216b609c Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sat, 5 Oct 2019 14:50:24 +0100 Subject: Set up needed Allauth URLs and add login/logout to navbar --- pydis_site/apps/home/urls.py | 24 +++++++++++++++++++++++- pydis_site/settings.py | 1 + pydis_site/templates/base/navbar.html | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) (limited to 'pydis_site/settings.py') diff --git a/pydis_site/apps/home/urls.py b/pydis_site/apps/home/urls.py index b9e7341b..dbb53cb6 100644 --- a/pydis_site/apps/home/urls.py +++ b/pydis_site/apps/home/urls.py @@ -1,15 +1,37 @@ +from allauth.account.views import LogoutView +from allauth.socialaccount.views import ConnectionsView from django.conf import settings from django.conf.urls.static import static from django.contrib import admin +from django.contrib.messages import ERROR from django.urls import include, path +from pydis_site.utils.views import MessageRedirectView from .views import HomeView app_name = 'home' urlpatterns = [ path('', HomeView.as_view(), name='home'), path('pages/', include('wiki.urls')), - path('accounts/', include('allauth.urls'), name='auth'), + + path('accounts/', include('allauth.socialaccount.providers.discord.urls')), + path('accounts/', include('allauth.socialaccount.providers.github.urls')), + + path( + 'accounts/login/cancelled', MessageRedirectView.as_view( + pattern_name="home", message="Login cancelled." + ), name='socialaccount_login_cancelled' + ), + path( + 'accounts/login/error', MessageRedirectView.as_view( + pattern_name="home", message="Login failed due to an error, please try again.", + message_level=ERROR + ), name='socialaccount_login_error' + ), + + path('connections', ConnectionsView.as_view()), + path('logout', LogoutView.as_view(), name="logout"), + path('admin/', admin.site.urls), path('notifications/', include('django_nyt.urls')), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 919547de..cc741117 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -376,3 +376,4 @@ AUTHENTICATION_BACKENDS = ( ) ACCOUNT_EMAIL_VERIFICATION = "none" # No verification required; we don't use emails for anything +LOGIN_REDIRECT_URL = "home" diff --git a/pydis_site/templates/base/navbar.html b/pydis_site/templates/base/navbar.html index ee68852a..e73864fe 100644 --- a/pydis_site/templates/base/navbar.html +++ b/pydis_site/templates/base/navbar.html @@ -1,3 +1,5 @@ +{% load account %} +{% load socialaccount %} {% load static %}