File tree 4 files changed +33
-8
lines changed
4 files changed +33
-8
lines changed Original file line number Diff line number Diff line change 1
- import contextlib
2
1
import sys
3
2
4
3
from django .apps import AppConfig
@@ -11,8 +10,6 @@ class TestAppConfig(AppConfig):
11
10
name = "test_app"
12
11
13
12
def ready (self ):
14
- from django .contrib .auth .models import User
15
-
16
13
register_iframe ("test_app.views.view_to_iframe_sync_func" )
17
14
register_iframe (views .view_to_iframe_async_func )
18
15
register_iframe (views .ViewToIframeSyncClass )
@@ -22,8 +19,3 @@ def ready(self):
22
19
23
20
if "test" in sys .argv :
24
21
return
25
-
26
- with contextlib .suppress (Exception ):
27
- User .objects .create_superuser (
28
- username = "admin" , email = "admin@example.com" , password = "password"
29
- )
Original file line number Diff line number Diff line change
1
+ import contextlib
2
+
3
+ from asgiref .sync import iscoroutinefunction , markcoroutinefunction
4
+
5
+
6
+ class AutoCreateAdminMiddleware :
7
+ async_capable = True
8
+ sync_capable = True
9
+
10
+ def __init__ (self , get_response ):
11
+ from django .contrib .auth .models import User
12
+
13
+ # One-time configuration and initialization.
14
+ self .get_response = get_response
15
+ with contextlib .suppress (Exception ):
16
+ User .objects .create_superuser (
17
+ username = "admin" , email = "admin@example.com" , password = "password"
18
+ )
19
+
20
+ if iscoroutinefunction (self .get_response ):
21
+ markcoroutinefunction (self )
22
+
23
+ def __call__ (self , request ):
24
+ if iscoroutinefunction (self .get_response ):
25
+
26
+ async def async_call ():
27
+ return await self .get_response (request )
28
+
29
+ return async_call ()
30
+
31
+ return self .get_response (request )
Original file line number Diff line number Diff line change 33
33
MIDDLEWARE = [
34
34
"django.middleware.security.SecurityMiddleware" ,
35
35
"servestatic.middleware.ServeStaticMiddleware" ,
36
+ "test_app.middleware.AutoCreateAdminMiddleware" ,
36
37
"django.contrib.sessions.middleware.SessionMiddleware" ,
37
38
"django.middleware.common.CommonMiddleware" ,
38
39
"django.middleware.csrf.CsrfViewMiddleware" ,
Original file line number Diff line number Diff line change 33
33
MIDDLEWARE = [
34
34
"django.middleware.security.SecurityMiddleware" ,
35
35
"servestatic.middleware.ServeStaticMiddleware" ,
36
+ "test_app.middleware.AutoCreateAdminMiddleware" ,
36
37
"django.contrib.sessions.middleware.SessionMiddleware" ,
37
38
"django.middleware.common.CommonMiddleware" ,
38
39
"django.middleware.csrf.CsrfViewMiddleware" ,
You can’t perform that action at this time.
0 commit comments