-
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate existing local remoteURL datalayers
- Loading branch information
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
umap/migrations/0019_migrate_internal_remote_datalayers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Generated by Django 4.2 on 2024-02-26 14:09 | ||
|
||
import re | ||
|
||
from django.db import migrations | ||
from django.conf import settings | ||
from django.urls import reverse, NoReverseMatch | ||
|
||
# Some users hacked uMap to use another map datalayer as a remote data source. | ||
# This script gently handles the migration for them. | ||
|
||
|
||
def migrate_datalayers(apps, schema_editor): | ||
DataLayer = apps.get_model("umap", "DataLayer") | ||
|
||
datalayers = DataLayer.objects.filter( | ||
settings__remoteData__url__icontains="datalayer" | ||
) | ||
|
||
for item in datalayers: | ||
old_url = item.settings["remoteData"]["url"] | ||
match = re.search( | ||
rf"{settings.SITE_URL}/datalayer/(?P<map_id>\d+)/(?P<datalayer_id>\d+)", | ||
old_url, | ||
) | ||
if match: | ||
remote_id = match.group("datalayer_id") | ||
map_id = match.group("map_id") | ||
try: | ||
remote_uuid = DataLayer.objects.get(id=remote_id).uuid | ||
except DataLayer.DoesNotExist: | ||
pass | ||
else: | ||
try: | ||
new_url = settings.SITE_URL + reverse( | ||
"datalayer_view", args=[map_id, remote_uuid] | ||
) | ||
except NoReverseMatch: | ||
pass | ||
else: | ||
item.settings["remoteData"]["url"] = new_url | ||
item.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("umap", "0018_datalayer_uuid"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_datalayers, reverse_code=migrations.RunPython.noop) | ||
] |