Skip to content

Commit ad1a97e

Browse files
committed
Merge branch 'main' into staging
2 parents 15d3605 + 8c94e8e commit ad1a97e

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

kirovy/urls.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,40 @@
1818
from django.conf.urls.static import static
1919
from django.contrib import admin
2020
from django.urls import path, include
21+
from django.db import connection
2122
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView, SpectacularRedocView
2223

2324
from kirovy.models import CncGame
2425
from kirovy.views import test, cnc_map_views, permission_views, admin_views, map_upload_views
2526
from kirovy import typing as t, constants
2627

28+
def _get_games_url_patterns() -> list[path]:
29+
"""Return URLs compatible with legacy CnCNet clients.
30+
31+
- URLs are loaded when the :mod:`kirovy.urls` module is loaded, which happens when Django starts.
32+
- Checking the game slugs requires migration ``0002`` to have been run.
33+
34+
These conditions caused a crash when running migrations for the first time, so now we return
35+
nothing if we detect that migrations haven't been run yet.
36+
37+
.. codeauthor:: rohsyl aka wu-shaolin
38+
39+
:return:
40+
A list of URLs that are backwards compatible with MapDB 1.0. Returns an empty list if migrations
41+
haven't been run yet.
42+
"""
43+
44+
if 'CncGame' not in connection.introspection.table_names():
45+
return []
46+
47+
return [
48+
path("upload", map_upload_views.CncNetBackwardsCompatibleUploadView.as_view()),
49+
*(
50+
# Make e.g. /yr/map_hash, /ra2/map_hash, etc
51+
path(f"{g.slug}/<str:sha1_hash>", cnc_map_views.BackwardsCompatibleMapView.as_view(), {"game_id": g.id})
52+
for g in CncGame.objects.filter(slug__in=constants.BACKWARDS_COMPATIBLE_GAMES)
53+
),
54+
]
2755

2856
def _get_url_patterns() -> list[path]:
2957
"""Return the root level url patterns.
@@ -40,14 +68,7 @@ def _get_url_patterns() -> list[path]:
4068
path("api/schema/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"),
4169
]
4270

43-
backwards_compatible_urls = [
44-
path("upload", map_upload_views.CncNetBackwardsCompatibleUploadView.as_view()),
45-
*(
46-
# Make e.g. /yr/map_hash, /ra2/map_hash, etc
47-
path(f"{g.slug}/<str:sha1_hash>", cnc_map_views.BackwardsCompatibleMapView.as_view(), {"game_id": g.id})
48-
for g in CncGame.objects.filter(slug__in=constants.BACKWARDS_COMPATIBLE_GAMES)
49-
),
50-
]
71+
backwards_compatible_urls = _get_games_url_patterns()
5172

5273
return (
5374
[

0 commit comments

Comments
 (0)