Skip to content

Commit 1e5bb9d

Browse files
committed
fix: drf sync website
1 parent 43dec25 commit 1e5bb9d

File tree

26 files changed

+53303
-5
lines changed

26 files changed

+53303
-5
lines changed

tests/frontendIntegration/drf_async/polls/views.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ async def login(request: Request): # type: ignore
435435
@renderer_classes([JsonTextRenderer]) # type: ignore
436436
async def login_218(request: Request): # type: ignore
437437
if request.method == "POST": # type: ignore
438-
request_json = json.loads(request.body) # type: ignore
439-
user_id = request_json["userId"]
440-
payload = request_json["payload"]
438+
request_json = request.data # type: ignore
439+
user_id = request_json["userId"] # type: ignore
440+
payload = request_json["payload"] # type: ignore
441441

442442
querier = Querier.get_instance()
443443
Querier.api_version = "2.18"
@@ -614,7 +614,6 @@ def refresh_attempted_time(request: Request): # type: ignore
614614
@custom_decorator_for_test()
615615
@verify_session()
616616
async def refresh(request: Request): # type: ignore
617-
print("HIIIIIII")
618617
return Response("refresh success") # type: ignore
619618

620619

@@ -750,7 +749,7 @@ async def check_rid(request: Request): # type: ignore
750749
@renderer_classes([JSONRenderer]) # type: ignore
751750
async def check_allow_credentials(request: Request): # type: ignore
752751
if request.method == "GET": # type: ignore
753-
return Response(json.dumps("allow-credentials" in request.headers)) # type: ignore
752+
return Response("allow-credentials" in request.headers) # type: ignore
754753
else:
755754
return send_options_api_response() # type: ignore
756755

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Django3 example
2+
3+
This is an example to show how to use supertokens-python library with django3 framework. This example works with django version 3.2.12 and python version 3.8.
4+
5+
## Installation
6+
7+
Before installing, use the script to create a virtual environment and to install all the required packages.
8+
```bash
9+
source create_env.sh
10+
```
11+
12+
## Start web app
13+
14+
```bash
15+
python manage.py runsever
16+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
pip install virtualenv
4+
5+
#Create a virtualenv
6+
virtualenv django3x_example
7+
# shellcheck disable=SC2164
8+
source django3x_example/bin/activate
9+
10+
touch requirements.txt
11+
echo "django==3.2.12" >> requirements.txt
12+
pip install -r requirements.txt
13+
pip install ../../package/supertokens_python-2.0.0-py2.py3-none-any.whl
14+

tests/frontendIntegration/drf_sync/db.sqlite3

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This is a sample Python script.
2+
3+
# Press ⌃R to execute it or replace it with your code.
4+
# Press Double ⇧ to search everywhere for classes, files, tool windows,
5+
# actions, and settings.
6+
7+
8+
def print_hi(_):
9+
pass
10+
11+
12+
# Press the green button in the gutter to run the script.
13+
if __name__ == "__main__":
14+
pass
15+
16+
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == "__main__":
21+
main()

tests/frontendIntegration/drf_sync/mysite/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for django3x project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
15+
16+
application = get_asgi_application()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import uvicorn # type: ignore
2+
3+
if __name__ == "__main__":
4+
uvicorn.run( # type: ignore
5+
"asgi:application", host="localhost.org", port=8080, log_level="info"
6+
)
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
"""
2+
Django settings for mysite project.
3+
4+
Generated by 'django-admin startproject' using Django 2.2.23.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.2/ref/settings/
11+
"""
12+
13+
import os
14+
from typing import List
15+
16+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
17+
from corsheaders.defaults import default_headers
18+
19+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20+
21+
# Quick-start development settings - unsuitable for production
22+
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
23+
24+
# SECURITY WARNING: keep the secret key used in production secret!
25+
SECRET_KEY = "f_d6ar@t2n+e@&7b^i^**kzo68w^e*1kn9%40#sp@0v2t#=vs2"
26+
27+
# SECURITY WARNING: don't run with debug turned on in production!
28+
DEBUG = True
29+
30+
ALLOWED_HOSTS = ["localhost.org", "0.0.0.0"]
31+
32+
CORS_ORIGIN_WHITELIST = [
33+
"http://localhost.org:8080",
34+
]
35+
CORS_ALLOW_ALL_ORIGINS = True
36+
CORS_ALLOW_CREDENTIALS = True
37+
CORS_ALLOWED_ORIGINS = [
38+
"http://localhost.org:8080",
39+
]
40+
CORS_ALLOWED_ORIGIN_REGEXES = [
41+
"http://localhost.org:8080",
42+
]
43+
44+
CORS_ALLOW_METHODS = [
45+
"DELETE",
46+
"GET",
47+
"OPTIONS",
48+
"PATCH",
49+
"POST",
50+
"PUT",
51+
]
52+
53+
CORS_ALLOW_HEADERS: List[str] = list(default_headers) + [
54+
"Content-Type",
55+
"rid",
56+
"fdi-version",
57+
"anti-csrf",
58+
"st-auth-mode",
59+
]
60+
61+
# Application definition
62+
63+
INSTALLED_APPS = [
64+
"corsheaders",
65+
"django.contrib.admin",
66+
"django.contrib.auth",
67+
"django.contrib.contenttypes",
68+
"django.contrib.sessions",
69+
"django.contrib.messages",
70+
"django.contrib.staticfiles",
71+
"rest_framework",
72+
]
73+
74+
MIDDLEWARE = [
75+
"corsheaders.middleware.CorsMiddleware",
76+
"django.middleware.common.CommonMiddleware",
77+
"django.contrib.sessions.middleware.SessionMiddleware",
78+
"django.contrib.auth.middleware.AuthenticationMiddleware",
79+
"django.contrib.messages.middleware.MessageMiddleware",
80+
"supertokens_python.framework.django.django_middleware.middleware",
81+
]
82+
83+
ROOT_URLCONF = "mysite.urls"
84+
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
85+
86+
TEMPLATES = [
87+
{
88+
"BACKEND": "django.template.backends.django.DjangoTemplates",
89+
"DIRS": [os.path.join(os.path.dirname(__file__), "../")],
90+
"APP_DIRS": True,
91+
"OPTIONS": {
92+
"context_processors": [
93+
"django.template.context_processors.debug",
94+
"django.template.context_processors.request",
95+
"django.contrib.auth.context_processors.auth",
96+
"django.contrib.messages.context_processors.messages",
97+
],
98+
},
99+
},
100+
]
101+
102+
ASGI_APPLICATION = "asgi.application"
103+
104+
# Database
105+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
106+
107+
DATABASES = {
108+
"default": {
109+
"ENGINE": "django.db.backends.sqlite3",
110+
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
111+
}
112+
}
113+
114+
# Password validation
115+
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
116+
117+
AUTH_PASSWORD_VALIDATORS = [
118+
{
119+
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
120+
},
121+
{
122+
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
123+
},
124+
{
125+
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
126+
},
127+
{
128+
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
129+
},
130+
]
131+
132+
# Internationalization
133+
# https://docs.djangoproject.com/en/2.2/topics/i18n/
134+
135+
LANGUAGE_CODE = "en-us"
136+
137+
TIME_ZONE = "UTC"
138+
139+
USE_I18N = True
140+
141+
USE_L10N = True
142+
143+
USE_TZ = True
144+
145+
# Static files (CSS, JavaScript, Images)
146+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
147+
148+
STATIC_URL = "/static/"
149+
150+
from supertokens_python import InputAppInfo, SupertokensConfig, init
151+
from supertokens_python.recipe import session
152+
from supertokens_python.recipe.session.interfaces import APIInterface, RecipeInterface
153+
from typing import Dict, Union, Any
154+
import sys
155+
156+
157+
def get_app_port():
158+
argvv = sys.argv
159+
for i in range(0, len(argvv)):
160+
if argvv[i] == "--port":
161+
return argvv[i + 1]
162+
163+
return "8080"
164+
165+
166+
def apis_override_session(param: APIInterface):
167+
param.disable_refresh_post = True
168+
return param
169+
170+
171+
def functions_override_session(param: RecipeInterface):
172+
original_create_new_session = param.create_new_session
173+
174+
async def create_new_session_custom(
175+
user_id: str,
176+
access_token_payload: Union[Dict[str, Any], None],
177+
session_data_in_database: Union[Dict[str, Any], None],
178+
disable_anti_csrf: Union[bool, None],
179+
tenant_id: str,
180+
user_context: Dict[str, Any],
181+
):
182+
if access_token_payload is None:
183+
access_token_payload = {}
184+
access_token_payload = {**access_token_payload, "customClaim": "customValue"}
185+
return await original_create_new_session(
186+
user_id,
187+
access_token_payload,
188+
session_data_in_database,
189+
disable_anti_csrf,
190+
tenant_id,
191+
user_context,
192+
)
193+
194+
param.create_new_session = create_new_session_custom
195+
196+
return param
197+
198+
199+
init(
200+
supertokens_config=SupertokensConfig("http://localhost:9000"),
201+
app_info=InputAppInfo(
202+
app_name="SuperTokens Python SDK",
203+
api_domain="0.0.0.0:" + get_app_port(),
204+
website_domain="http://localhost.org:8080",
205+
),
206+
framework="django",
207+
recipe_list=[
208+
session.init(
209+
override=session.InputOverrideConfig(
210+
apis=apis_override_session,
211+
functions=functions_override_session,
212+
),
213+
)
214+
],
215+
telemetry=False,
216+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""mysite URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.2/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.urls import path, include
17+
18+
urlpatterns = [
19+
path("", include("polls.urls")),
20+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for mysite project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
15+
16+
application = get_wsgi_application()

tests/frontendIntegration/drf_sync/polls/__init__.py

Whitespace-only changes.

tests/frontendIntegration/drf_sync/polls/admin.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.apps import AppConfig
2+
3+
4+
class PollsConfig(AppConfig):
5+
name = "polls"
6+
7+
def ready(self):
8+
pass

tests/frontendIntegration/drf_sync/polls/migrations/__init__.py

Whitespace-only changes.

tests/frontendIntegration/drf_sync/polls/models.py

Whitespace-only changes.

tests/frontendIntegration/drf_sync/polls/tests.py

Whitespace-only changes.

0 commit comments

Comments
 (0)