|
| 1 | +from django.core.checks import Error, Tags, Warning, register |
| 2 | + |
| 3 | + |
| 4 | +@register(Tags.compatibility) |
| 5 | +def reactpy_warnings(app_configs, **kwargs): |
| 6 | + from django.conf import settings |
| 7 | + from django.urls import reverse |
| 8 | + |
| 9 | + warnings = [] |
| 10 | + |
| 11 | + # REACTPY_DATABASE is not an in-memory database. |
| 12 | + if ( |
| 13 | + getattr(settings, "DATABASES", {}) |
| 14 | + .get(getattr(settings, "REACTPY_DATABASE", "default"), {}) |
| 15 | + .get("NAME", None) |
| 16 | + == ":memory:" |
| 17 | + ): |
| 18 | + warnings.append( |
| 19 | + Warning( |
| 20 | + "Using ReactPy with an in-memory database can cause unexpected " |
| 21 | + "behaviors.", |
| 22 | + hint="Configure settings.py:DATABASES[REACTPY_DATABASE], to use a " |
| 23 | + "multiprocessing and thread safe database.", |
| 24 | + id="reactpy_django.W001", |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + # REACTPY_CACHE is not an in-memory cache. |
| 29 | + if getattr(settings, "CACHES", {}).get( |
| 30 | + getattr(settings, "REACTPY_CACHE", "default"), {} |
| 31 | + ).get("BACKEND", None) in { |
| 32 | + "django.core.cache.backends.dummy.DummyCache", |
| 33 | + "django.core.cache.backends.locmem.LocMemCache", |
| 34 | + }: |
| 35 | + warnings.append( |
| 36 | + Warning( |
| 37 | + "Using ReactPy with an in-memory cache can cause unexpected " |
| 38 | + "behaviors.", |
| 39 | + hint="Configure settings.py:CACHES[REACTPY_CACHE], to use a " |
| 40 | + "multiprocessing and thread safe cache.", |
| 41 | + id="reactpy_django.W002", |
| 42 | + ) |
| 43 | + ) |
| 44 | + |
| 45 | + # ReactPy URLs exist |
| 46 | + try: |
| 47 | + reverse("reactpy:web_modules", kwargs={"file": "example"}) |
| 48 | + reverse("reactpy:view_to_component", kwargs={"view_path": "example"}) |
| 49 | + except Exception: |
| 50 | + warnings.append( |
| 51 | + Warning( |
| 52 | + "ReactPy URLs have not been registered.", |
| 53 | + hint="""Add 'path("reactpy/", include("reactpy_django.http.urls"))' """ |
| 54 | + "to your application's urlpatterns.", |
| 55 | + id="reactpy_django.W003", |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + return warnings |
| 60 | + |
| 61 | + |
| 62 | +@register(Tags.compatibility) |
| 63 | +def reactpy_errors(app_configs, **kwargs): |
| 64 | + from django.conf import settings |
| 65 | + |
| 66 | + errors = [] |
| 67 | + |
| 68 | + # Make sure ASGI is enabled |
| 69 | + if not getattr(settings, "ASGI_APPLICATION", None): |
| 70 | + errors.append( |
| 71 | + Error( |
| 72 | + "ASGI_APPLICATION is not defined." |
| 73 | + " ReactPy requires ASGI to be enabled.", |
| 74 | + hint="Add ASGI_APPLICATION to settings.py.", |
| 75 | + id="reactpy_django.E001", |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + # DATABASE_ROUTERS is properly configured when REACTPY_DATABASE is defined |
| 80 | + if getattr( |
| 81 | + settings, "REACTPY_DATABASE", None |
| 82 | + ) and "reactpy_django.database.Router" not in getattr( |
| 83 | + settings, "DATABASE_ROUTERS", [] |
| 84 | + ): |
| 85 | + errors.append( |
| 86 | + Error( |
| 87 | + "ReactPy database has been changed but the database router is " |
| 88 | + "not configured.", |
| 89 | + hint="Set settings.py:DATABASE_ROUTERS to " |
| 90 | + "['reactpy_django.database.Router', ...]", |
| 91 | + id="reactpy_django.E002", |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | + # All settings in reactpy_django.conf are the correct data type |
| 96 | + if not isinstance(getattr(settings, "REACTPY_WEBSOCKET_URL", ""), str): |
| 97 | + errors.append( |
| 98 | + Error( |
| 99 | + "Invalid type for REACTPY_WEBSOCKET_URL.", |
| 100 | + hint="REACTPY_WEBSOCKET_URL should be a string.", |
| 101 | + obj=settings.REACTPY_WEBSOCKET_URL, |
| 102 | + id="reactpy_django.E003", |
| 103 | + ) |
| 104 | + ) |
| 105 | + if not isinstance(getattr(settings, "REACTPY_RECONNECT_MAX", 0), int): |
| 106 | + errors.append( |
| 107 | + Error( |
| 108 | + "Invalid type for REACTPY_RECONNECT_MAX.", |
| 109 | + hint="REACTPY_RECONNECT_MAX should be an integer.", |
| 110 | + obj=settings.REACTPY_RECONNECT_MAX, |
| 111 | + id="reactpy_django.E004", |
| 112 | + ) |
| 113 | + ) |
| 114 | + if not isinstance(getattr(settings, "REACTPY_CACHE", ""), str): |
| 115 | + errors.append( |
| 116 | + Error( |
| 117 | + "Invalid type for REACTPY_CACHE.", |
| 118 | + hint="REACTPY_CACHE should be a string.", |
| 119 | + obj=settings.REACTPY_CACHE, |
| 120 | + id="reactpy_django.E005", |
| 121 | + ) |
| 122 | + ) |
| 123 | + if not isinstance(getattr(settings, "REACTPY_DATABASE", ""), str): |
| 124 | + errors.append( |
| 125 | + Error( |
| 126 | + "Invalid type for REACTPY_DATABASE.", |
| 127 | + hint="REACTPY_DATABASE should be a string.", |
| 128 | + obj=settings.REACTPY_DATABASE, |
| 129 | + id="reactpy_django.E006", |
| 130 | + ) |
| 131 | + ) |
| 132 | + if not isinstance( |
| 133 | + getattr(settings, "REACTPY_DEFAULT_QUERY_POSTPROCESSOR", ""), str |
| 134 | + ): |
| 135 | + errors.append( |
| 136 | + Error( |
| 137 | + "Invalid type for REACTPY_DEFAULT_QUERY_POSTPROCESSOR.", |
| 138 | + hint="REACTPY_DEFAULT_QUERY_POSTPROCESSOR should be a string.", |
| 139 | + obj=settings.REACTPY_DEFAULT_QUERY_POSTPROCESSOR, |
| 140 | + id="reactpy_django.E007", |
| 141 | + ) |
| 142 | + ) |
| 143 | + if not isinstance(getattr(settings, "REACTPY_AUTH_BACKEND", ""), str): |
| 144 | + errors.append( |
| 145 | + Error( |
| 146 | + "Invalid type for REACTPY_AUTH_BACKEND.", |
| 147 | + hint="REACTPY_AUTH_BACKEND should be a string.", |
| 148 | + obj=settings.REACTPY_AUTH_BACKEND, |
| 149 | + id="reactpy_django.E008", |
| 150 | + ) |
| 151 | + ) |
| 152 | + |
| 153 | + return errors |
0 commit comments