-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cleanup): delete old files when template is deleted or changed (#445
) Resolves #437
- Loading branch information
Showing
8 changed files
with
82 additions
and
24 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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
import os.path | ||
import shutil | ||
|
||
from django.conf import settings | ||
from django.core.files import File | ||
|
||
_data_path = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
def path(name): | ||
return os.path.join(_data_path, name) | ||
def django_file(name, mode="rb"): | ||
abspath = os.path.join(_data_path, name) | ||
new_path = f"{settings.MEDIA_ROOT}/attachments" | ||
|
||
try: | ||
os.makedirs(new_path) | ||
except FileExistsError: # pragma: no cover | ||
pass | ||
|
||
def django_file(name, mode="rb"): | ||
abspath = path(name) | ||
return File(open(abspath, mode)) | ||
shutil.copy(abspath, f"{new_path}/{name}") | ||
|
||
return File(open(abspath, mode), name=f"attachments/{name}") |
28 changes: 28 additions & 0 deletions
28
document_merge_service/api/migrations/0004_cleanup_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,28 @@ | ||
import os | ||
import logging | ||
|
||
|
||
from django.db import migrations | ||
from django.conf import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
|
||
def cleanup_files(apps, schema_editor): | ||
Template = apps.get_model("api", "Template") | ||
used_files = [template.template.path for template in Template.objects.all()] | ||
|
||
with os.scandir(settings.MEDIA_ROOT) as files: | ||
for f in files: | ||
if not f.path in used_files and os.path.isfile(f.path): | ||
os.remove(f.path) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0003_template_meta"), | ||
] | ||
|
||
operations = [migrations.RunPython(cleanup_files, migrations.RunPython.noop)] |
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
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