10
10
from .serializers import NavbarItemSerializer
11
11
from .utils .recaptcha import verify_recaptcha
12
12
13
+ import os
14
+ from django .conf import settings
15
+ from django .core .management import call_command
16
+ from django .contrib .auth import get_user_model
17
+ from django .db import transaction
18
+ from users .models import Profile # Import the Profile model
19
+ from users .managers import CustomUserManager # Import the custom manager
20
+
21
+ User = get_user_model ()
13
22
14
23
@ensure_csrf_cookie
15
24
def set_csrf_token (request ):
@@ -22,7 +31,6 @@ class NavbarItemList(generics.ListAPIView):
22
31
queryset = NavbarItem .objects .all ()
23
32
serializer_class = NavbarItemSerializer
24
33
25
-
26
34
class ReCaptchaLoginView (APIView ):
27
35
def post (self , request , * args , ** kwargs ):
28
36
username = request .data .get ('username' )
@@ -40,4 +48,51 @@ def post(self, request, *args, **kwargs):
40
48
return JsonResponse ({'detail' : 'Successfully logged in.' })
41
49
else :
42
50
return JsonResponse ({'detail' : 'Invalid login credentials.' }, status = status .HTTP_400_BAD_REQUEST )
43
-
51
+
52
+ class SetupView (APIView ):
53
+ def post (self , request , * args , ** kwargs ):
54
+ db_host = request .data .get ('db_host' )
55
+ db_port = request .data .get ('db_port' )
56
+ db_name = request .data .get ('db_name' )
57
+ db_user = request .data .get ('db_user' )
58
+ db_password = request .data .get ('db_password' )
59
+ admin_username = request .data .get ('admin_username' )
60
+ admin_email = request .data .get ('admin_email' )
61
+ admin_password = request .data .get ('admin_password' )
62
+
63
+ # Write database settings to .env file
64
+ env_path = os .path .join (settings .BASE_DIR , '.env' )
65
+ with open (env_path , 'a' ) as f :
66
+ f .write (f'DATABASE_URL=postgres://{ db_user } :{ db_password } @{ db_host } :{ db_port } /{ db_name } \n ' )
67
+
68
+ try :
69
+ # Run migrations
70
+ call_command ('migrate' )
71
+
72
+ with transaction .atomic ():
73
+ # Use the custom manager to create the superuser
74
+ User .objects .create_superuser (
75
+ username = admin_username ,
76
+ email = admin_email ,
77
+ password = admin_password
78
+ )
79
+
80
+ # Create setup complete flag in the media directory
81
+ setup_flag_path = os .path .join (settings .MEDIA_ROOT , 'setup_complete.txt' )
82
+ with open (setup_flag_path , 'w' ) as f :
83
+ f .write ('Setup complete' )
84
+
85
+ return JsonResponse ({'detail' : 'Setup complete' }, status = status .HTTP_200_OK )
86
+
87
+ except Exception as e :
88
+ # If an error occurs, remove the added database settings
89
+ with open (env_path , 'r' ) as f :
90
+ lines = f .readlines ()
91
+ with open (env_path , 'w' ) as f :
92
+ f .writelines (lines [:- 1 ]) # Remove the last line
93
+
94
+ # Log the full error for debugging
95
+ import logging
96
+ logging .error (f"Error during setup: { str (e )} " , exc_info = True )
97
+
98
+ return JsonResponse ({'error' : str (e )}, status = status .HTTP_500_INTERNAL_SERVER_ERROR )
0 commit comments