From 73ed4ecaf6c3ee795c0a7bfc21d97e27fa267031 Mon Sep 17 00:00:00 2001 From: Rafi Date: Wed, 15 Mar 2023 11:13:17 +0800 Subject: [PATCH] Add "open_registration" setting option to control whether registration is enabled. --- account/urls.py | 31 +++++++++++++++++++++++++++++++ account/views.py | 19 ++++++++++++++++--- chatgpt_ui_server/urls.py | 3 +-- 3 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 account/urls.py diff --git a/account/urls.py b/account/urls.py new file mode 100644 index 0000000..4290678 --- /dev/null +++ b/account/urls.py @@ -0,0 +1,31 @@ +from django.urls import path, re_path, include +from django.views.generic import TemplateView +from dj_rest_auth.registration.views import VerifyEmailView, ResendEmailVerificationView +from .views import RegistrationView + +urlpatterns = [ + path('', include('dj_rest_auth.urls')), + path('registration/', RegistrationView.as_view(), name='rest_register'), + path('verify-email/', VerifyEmailView.as_view(), name='rest_verify_email'), + path('resend-email/', ResendEmailVerificationView.as_view(), name="rest_resend_email"), + + # This url is used by django-allauth and empty TemplateView is + # defined just to allow reverse() call inside app, for example when email + # with verification link is being sent, then it's required to render email + # content. + + # account_confirm_email - You should override this view to handle it in + # your API client somehow and then, send post to /verify-email/ endpoint + # with proper key. + # If you don't want to use API on that step, then just use ConfirmEmailView + # view from: + # django-allauth https://github.com/pennersr/django-allauth/blob/master/allauth/account/views.py + re_path( + r'^account-confirm-email/(?P[-:\w]+)/$', TemplateView.as_view(), + name='account_confirm_email', + ), + path( + 'account-email-verification-sent/', TemplateView.as_view(), + name='account_email_verification_sent', + ), +] \ No newline at end of file diff --git a/account/views.py b/account/views.py index f9de87b..aed08ab 100644 --- a/account/views.py +++ b/account/views.py @@ -1,4 +1,17 @@ -from django.shortcuts import render +from rest_framework.response import Response +from rest_framework import status +from dj_rest_auth.registration.views import RegisterView +from chat.models import Setting -# Create your views here. -from dj_rest_auth.views import LoginView \ No newline at end of file + +class RegistrationView(RegisterView): + def create(self, request, *args, **kwargs): + try: + open_registration = Setting.objects.get(name='open_registration').value == 'True' + except Setting.DoesNotExist: + open_registration = True + + if open_registration: + return super().create(request, *args, **kwargs) + else: + return Response({'detail': 'Registration is not yet open.'}, status=status.HTTP_403_FORBIDDEN) \ No newline at end of file diff --git a/chatgpt_ui_server/urls.py b/chatgpt_ui_server/urls.py index b321951..82d9320 100644 --- a/chatgpt_ui_server/urls.py +++ b/chatgpt_ui_server/urls.py @@ -21,7 +21,6 @@ path('api/chat/', include('chat.urls')), path('api/conversation/', conversation, name='conversation'), path('api/gen_title/', gen_title, name='gen_title'), - path('api/account/', include('dj_rest_auth.urls')), - path('api/account/registration/', include('dj_rest_auth.registration.urls')), + path('api/account/', include('account.urls')), path('admin/', admin.site.urls), ]