diff --git a/django_nextjs/render.py b/django_nextjs/render.py index 262cdb5..0d4feb3 100644 --- a/django_nextjs/render.py +++ b/django_nextjs/render.py @@ -9,6 +9,7 @@ from django.middleware.csrf import get_token as get_csrf_token from django.template.loader import render_to_string from multidict import MultiMapping + from .app_settings import NEXTJS_SERVER_URL from .utils import filter_mapping_obj diff --git a/pyproject.toml b/pyproject.toml index eecdd2c..74aba64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.pytest.ini_options] -DJANGO_SETTINGS_MODULE = "tests.settings" +DJANGO_SETTINGS_MODULE = "tests.sample.djproject.settings" norecursedirs = ".git" django_find_project = false pythonpath = ["."] diff --git a/sample/project/__init__.py b/sample/project/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/sample/project/settings.py b/sample/project/settings.py deleted file mode 100644 index 6f88006..0000000 --- a/sample/project/settings.py +++ /dev/null @@ -1,126 +0,0 @@ -""" -Django settings for sample project. - -Generated by 'django-admin startproject' using Django 4.2.1. - -For more information on this file, see -https://docs.djangoproject.com/en/4.2/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/4.2/ref/settings/ -""" - -from pathlib import Path - -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = "django-insecure-u#zp5#px+9ec#!u47e3*73gjk%o%mt92u5aaf1*idntzw60^1m" - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - "daphne", - "django.contrib.admin", - "django.contrib.auth", - "django.contrib.contenttypes", - "django.contrib.sessions", - "django.contrib.messages", - "django.contrib.staticfiles", - "django_nextjs.apps.DjangoNextJSConfig", -] - -MIDDLEWARE = [ - "django.middleware.security.SecurityMiddleware", - "django.contrib.sessions.middleware.SessionMiddleware", - "django.middleware.common.CommonMiddleware", - "django.middleware.csrf.CsrfViewMiddleware", - "django.contrib.auth.middleware.AuthenticationMiddleware", - "django.contrib.messages.middleware.MessageMiddleware", - "django.middleware.clickjacking.XFrameOptionsMiddleware", -] - -ROOT_URLCONF = "project.urls" - -TEMPLATES = [ - { - "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [BASE_DIR / "project" / "templates"], - "APP_DIRS": True, - "OPTIONS": { - "context_processors": [ - "django.template.context_processors.debug", - "django.template.context_processors.request", - "django.contrib.auth.context_processors.auth", - "django.contrib.messages.context_processors.messages", - ], - }, - }, -] - -WSGI_APPLICATION = "project.wsgi.application" -ASGI_APPLICATION = "project.asgi.application" - - -# Database -# https://docs.djangoproject.com/en/4.2/ref/settings/#databases - -# DATABASES = { -# "default": { -# "ENGINE": "django.db.backends.sqlite3", -# "NAME": BASE_DIR / "db.sqlite3", -# } -# } - - -# Password validation -# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", - }, - { - "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", - }, - { - "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", - }, - { - "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/4.2/topics/i18n/ - -LANGUAGE_CODE = "en-us" - -TIME_ZONE = "UTC" - -USE_I18N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/4.2/howto/static-files/ - -STATIC_URL = "static/" - -# Default primary key field type -# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field - -DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/sample/project/templates/index.html b/sample/project/templates/index.html deleted file mode 100644 index 4b2b1f7..0000000 --- a/sample/project/templates/index.html +++ /dev/null @@ -1,17 +0,0 @@ -{% extends "django_nextjs/document_base.html" %} - - -{% block head %} - head - {{ block.super }} - -{% endblock %} - - -{% block body %} - ... the content you want to place at the beginning of "body" tag ... - ... e.g. include the navbar template ... - {{ block.super }} - ... the content you want to place at the end of "body" tag ... - ... e.g. include the footer template ... -{% endblock %} diff --git a/setup.py b/setup.py index 6a9bfbc..ace94fb 100644 --- a/setup.py +++ b/setup.py @@ -16,6 +16,7 @@ "pytest-cov", "pytest-django", "pytest-asyncio", + "daphne", # used in tests "black", "isort", ] diff --git a/sample/project/asgi.py b/tests/sample/djproject/asgi.py similarity index 89% rename from sample/project/asgi.py rename to tests/sample/djproject/asgi.py index 1a798d1..babcdac 100644 --- a/sample/project/asgi.py +++ b/tests/sample/djproject/asgi.py @@ -1,17 +1,17 @@ import os from django.core.asgi import get_asgi_application -from django.urls import re_path, path +from django.urls import path, re_path -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djproject.settings") django_asgi_app = get_asgi_application() from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter -from django_nextjs.proxy import NextJSProxyHttpConsumer, NextJSProxyWebsocketConsumer - from django.conf import settings +from django_nextjs.proxy import NextJSProxyHttpConsumer, NextJSProxyWebsocketConsumer + # put your custom routes here if you need http_routes = [re_path(r"", django_asgi_app)] websocket_routers = [] diff --git a/tests/settings.py b/tests/sample/djproject/settings.py similarity index 78% rename from tests/settings.py rename to tests/sample/djproject/settings.py index 827d5cd..7c11861 100644 --- a/tests/settings.py +++ b/tests/sample/djproject/settings.py @@ -1,17 +1,20 @@ -import os +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) SECRET_KEY = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" DEBUG = True USE_TZ = False INSTALLED_APPS = [ + "daphne", "django.contrib.admin", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.sessions", "django.contrib.messages", + "django_nextjs.apps.DjangoNextJSConfig", ] MIDDLEWARE = [ @@ -22,12 +25,12 @@ "django.contrib.messages.middleware.MessageMiddleware", ] -# ROOT_URLCONF = "tests.urls" +ROOT_URLCONF = "djproject.urls" TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [], + "DIRS": [BASE_DIR / "djproject" / "templates"], "APP_DIRS": True, "OPTIONS": { "context_processors": [ @@ -40,12 +43,8 @@ }, ] -DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), - } -} +WSGI_APPLICATION = "djproject.wsgi.application" +ASGI_APPLICATION = "djproject.asgi.application" STATIC_URL = "/static/" diff --git a/tests/sample/djproject/templates/index.html b/tests/sample/djproject/templates/index.html new file mode 100644 index 0000000..b18bcbb --- /dev/null +++ b/tests/sample/djproject/templates/index.html @@ -0,0 +1,15 @@ +{% extends "django_nextjs/document_base.html" %} + + +{% block head %} + head + {{ block.super }} + +{% endblock %} + + +{% block body %} + pre_body_{{ request.path_info }} + {{ block.super }} + post_body_{{ request.path_info }} +{% endblock %} diff --git a/sample/project/urls.py b/tests/sample/djproject/urls.py similarity index 93% rename from sample/project/urls.py rename to tests/sample/djproject/urls.py index d75eced..5409563 100644 --- a/sample/project/urls.py +++ b/tests/sample/djproject/urls.py @@ -1,5 +1,5 @@ from django.contrib import admin -from django.urls import path, include +from django.urls import include, path from django_nextjs.render import render_nextjs_page diff --git a/sample/project/wsgi.py b/tests/sample/djproject/wsgi.py similarity index 82% rename from sample/project/wsgi.py rename to tests/sample/djproject/wsgi.py index 2c4459d..81d48cc 100644 --- a/sample/project/wsgi.py +++ b/tests/sample/djproject/wsgi.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djproject.settings") application = get_wsgi_application() diff --git a/sample/manage.py b/tests/sample/manage.py similarity index 88% rename from sample/manage.py rename to tests/sample/manage.py index e170f6b..6d38bec 100755 --- a/sample/manage.py +++ b/tests/sample/manage.py @@ -6,7 +6,7 @@ def main(): """Run administrative tasks.""" - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djproject.settings") try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/sample/next13/.gitignore b/tests/sample/next/.gitignore similarity index 100% rename from sample/next13/.gitignore rename to tests/sample/next/.gitignore diff --git a/sample/next13/app/app/page.js b/tests/sample/next/app/app/page.js similarity index 63% rename from sample/next13/app/app/page.js rename to tests/sample/next/app/app/page.js index 7777645..25349f0 100644 --- a/sample/next13/app/app/page.js +++ b/tests/sample/next/app/app/page.js @@ -4,7 +4,9 @@ export default function Home() { return (
/app
- Go To /app/second + + /app/second +
); } diff --git a/sample/next13/app/app/second/page.js b/tests/sample/next/app/app/second/page.js similarity index 69% rename from sample/next13/app/app/second/page.js rename to tests/sample/next/app/app/second/page.js index 8ccbb1d..335ba46 100644 --- a/sample/next13/app/app/second/page.js +++ b/tests/sample/next/app/app/second/page.js @@ -4,7 +4,9 @@ export default function Home() { return (
/app/second
- Go To /app + + /app +
); } diff --git a/sample/next13/app/layout.js b/tests/sample/next/app/layout.js similarity index 100% rename from sample/next13/app/layout.js rename to tests/sample/next/app/layout.js diff --git a/sample/next13/app/page.js b/tests/sample/next/app/page.js similarity index 100% rename from sample/next13/app/page.js rename to tests/sample/next/app/page.js diff --git a/sample/next13/jsconfig.json b/tests/sample/next/jsconfig.json similarity index 100% rename from sample/next13/jsconfig.json rename to tests/sample/next/jsconfig.json diff --git a/sample/next13/next.config.js b/tests/sample/next/next.config.js similarity index 100% rename from sample/next13/next.config.js rename to tests/sample/next/next.config.js diff --git a/sample/next13/package-lock.json b/tests/sample/next/package-lock.json similarity index 100% rename from sample/next13/package-lock.json rename to tests/sample/next/package-lock.json diff --git a/sample/next13/package.json b/tests/sample/next/package.json similarity index 100% rename from sample/next13/package.json rename to tests/sample/next/package.json diff --git a/sample/next13/pages/_document.js b/tests/sample/next/pages/_document.js similarity index 100% rename from sample/next13/pages/_document.js rename to tests/sample/next/pages/_document.js diff --git a/sample/next13/pages/page/index.js b/tests/sample/next/pages/page/index.js similarity index 100% rename from sample/next13/pages/page/index.js rename to tests/sample/next/pages/page/index.js diff --git a/sample/next13/pages/page/second.js b/tests/sample/next/pages/page/second.js similarity index 100% rename from sample/next13/pages/page/second.js rename to tests/sample/next/pages/page/second.js diff --git a/tests/test_live.py b/tests/test_live.py new file mode 100644 index 0000000..b56f2b6 --- /dev/null +++ b/tests/test_live.py @@ -0,0 +1,36 @@ +# import pytest +# from playwright.async_api import Page, expect +# from pytest_django.live_server_helper import LiveServer + +# def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page): +# page.goto("https://playwright.dev/") + +# # Expect a title "to contain" a substring. +# expect(page).to_have_title(re.compile("Playwright")) + +# # create a locator +# get_started = page.get_by_role("link", name="Get started") + +# # Expect an attribute "to be strictly equal" to the value. +# expect(get_started).to_have_attribute("href", "/docs/intro") + +# # Click the get started link. +# get_started.click() + +# # Expects the URL to contain intro. +# expect(page).to_have_url(re.compile(".*intro")) + + +# @pytest.mark.asyncio +# async def test_dispatch_raises_exception_when_not_in_debug_mode(page: Page, live_server: LiveServer): +# await page.goto(live_server.url + "/app") + +# expect(page).to_have_title("pre_body_/app") +# expect(page).to_have_title("post_body_/app") + +# btn_goto_2 = page.get_by_role("link", name="/app/second") +# expect(btn_goto_2).to_have_attribute("href", "/app/second") + +# btn_goto_2.click() + +# expect() diff --git a/tests/test_proxy.py b/tests/test_proxy.py index fde51d6..8c4f958 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -1,9 +1,9 @@ import pytest +from django.conf import settings from django.test import RequestFactory -from django_nextjs.proxy import NextJSProxyView + from django_nextjs.exceptions import NextJSImproperlyConfigured -from django.test import RequestFactory -from django.conf import settings +from django_nextjs.proxy import NextJSProxyView def test_dispatch_raises_exception_when_not_in_debug_mode(rf: RequestFactory): @@ -13,12 +13,3 @@ def test_dispatch_raises_exception_when_not_in_debug_mode(rf: RequestFactory): view = NextJSProxyView.as_view() with pytest.raises(NextJSImproperlyConfigured): view(request) - - -def test_(rf: RequestFactory): - settings.DEBUG = True - - request = rf.get("/test") - view = NextJSProxyView.as_view() - - # TODO: