-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
121 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ pgbackups | |
|
||
# static | ||
static | ||
media | ||
|
||
# cache | ||
.pytest_cache | ||
|
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
106 changes: 106 additions & 0 deletions
106
admin_panel/bd_models/migrations/0002_move_upload_files.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,106 @@ | ||
# Generated by Django 5.1.4 on 2025-01-15 13:27 | ||
|
||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from bd_models.models import Ball, Economy, Regime, Special | ||
from django.db import migrations | ||
from django.db.models import Case, ImageField, When | ||
from django.db.models.expressions import F, Value | ||
from django.db.models.functions import Concat, Replace | ||
|
||
if TYPE_CHECKING: | ||
from django.apps.registry import Apps | ||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
from django.db.models import Expression | ||
|
||
MEDIA = Path("./media") | ||
OLD_STATIC = Path("../static/uploads") | ||
CORE_SRC = Path("../ballsdex/core/image_generator/src") | ||
|
||
DEFAULT_ASSETS = [ | ||
"capitalist.png", | ||
"communist.png", | ||
"democracy.png", | ||
"dictatorship.png", | ||
"shiny.png", | ||
"union.png", | ||
] | ||
|
||
|
||
def _replace_text(column: str, reverse: bool = False) -> "dict[str, Expression]": | ||
if reverse: | ||
r = Case( | ||
When( | ||
**{f"{column}__in": DEFAULT_ASSETS}, | ||
then=Concat( | ||
Value("/ballsdex/core/image_generator/src/", output_field=ImageField()), | ||
F(column), | ||
), | ||
), | ||
default=Concat(Value("/static/uploads/", output_field=ImageField()), F(column)), | ||
) | ||
else: | ||
r = Replace( | ||
Replace(F(column), Value("/ballsdex/core/image_generator/src/"), Value("")), | ||
Value("/static/uploads/", output_field=ImageField()), | ||
Value("", output_field=ImageField()), | ||
) | ||
return {column: r} | ||
|
||
|
||
def _check_reserved_names(): | ||
for file in OLD_STATIC.glob("*"): | ||
if file.name in DEFAULT_ASSETS: | ||
raise ValueError( | ||
f"The file {file.absolute()} has a reserved name and will conflict with Ballsdex " | ||
"prodivded assets. You need to delete it or move it before proceeding. " | ||
"Once this is done, reupload the asset via the new admin panel." | ||
) | ||
|
||
|
||
def move_forwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"): | ||
assert MEDIA.is_dir(follow_symlinks=False) | ||
assert OLD_STATIC.is_dir(follow_symlinks=False) | ||
assert CORE_SRC.is_dir(follow_symlinks=False) | ||
_check_reserved_names() | ||
|
||
Ball.objects.update(**_replace_text("wild_card"), **_replace_text("collection_card")) | ||
Economy.objects.update(**_replace_text("icon")) | ||
Regime.objects.update(**_replace_text("background")) | ||
Special.objects.update(**_replace_text("background")) | ||
|
||
for file in OLD_STATIC.glob("*"): | ||
if file.name == ".gitkeep": | ||
continue | ||
file.rename(MEDIA / file.name) | ||
# the files in /ballsdex/core/image_generator/src/ will be moved by git | ||
|
||
|
||
def move_backwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"): | ||
assert MEDIA.is_dir(follow_symlinks=False) | ||
assert OLD_STATIC.is_dir(follow_symlinks=False) | ||
|
||
Ball.objects.update( | ||
**_replace_text("wild_card", reverse=True), | ||
**_replace_text("collection_card", reverse=True), | ||
) | ||
Economy.objects.update(**_replace_text("icon", reverse=True)) | ||
Regime.objects.update(**_replace_text("background", reverse=True)) | ||
Special.objects.update(**_replace_text("background", reverse=True)) | ||
|
||
for file in MEDIA.glob("*"): | ||
if file.name in DEFAULT_ASSETS: | ||
continue | ||
if file.name == ".gitkeep": | ||
continue | ||
file.rename(OLD_STATIC / file.name) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("bd_models", "0001_initial"), | ||
] | ||
|
||
operations = [migrations.RunPython(move_forwards, move_backwards, atomic=True)] |
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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