-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom SecureFileField class in FileField.
- Loading branch information
1 parent
5cd19b3
commit 1986d35
Showing
14 changed files
with
197 additions
and
71 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
api/migrations/0214_alter_generaldocument_document_and_more.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,30 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 10:12 | ||
|
||
from django.db import migrations | ||
|
||
import api.models | ||
import main.base_class | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0213_merge_20240807_1001"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="generaldocument", | ||
name="document", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to=api.models.general_document_path, verbose_name="document" | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="situationreport", | ||
name="document", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to=api.models.sitrep_document_path, verbose_name="document" | ||
), | ||
), | ||
] |
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
28 changes: 28 additions & 0 deletions
28
country_plan/migrations/0008_alter_countryplan_internal_plan_file.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 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 10:12 | ||
|
||
import django.core.validators | ||
from django.db import migrations | ||
|
||
import country_plan.models | ||
import main.base_class | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("country_plan", "0007_alter_membershipcoordination_sector_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="countryplan", | ||
name="internal_plan_file", | ||
field=main.base_class.SecureFileField( | ||
blank=True, | ||
null=True, | ||
upload_to=country_plan.models.pdf_upload_to, | ||
validators=[django.core.validators.FileExtensionValidator(["pdf"])], | ||
verbose_name="Internal Plan", | ||
), | ||
), | ||
] |
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
41 changes: 41 additions & 0 deletions
41
dref/migrations/0075_alter_dref_budget_file_preview_alter_dreffile_file_and_more.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,41 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 10:12 | ||
|
||
from django.db import migrations | ||
|
||
import main.base_class | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("dref", "0074_auto_20240129_0909"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="dref", | ||
name="budget_file_preview", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to="dref/images/", verbose_name="budget file preview" | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="dreffile", | ||
name="file", | ||
field=main.base_class.SecureFileField(upload_to="dref/images/", verbose_name="file"), | ||
), | ||
migrations.AlterField( | ||
model_name="dreffinalreport", | ||
name="financial_report_preview", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to="dref/images/", verbose_name="financial preview" | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name="drefoperationalupdate", | ||
name="budget_file_preview", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to="dref-op-update/images/", verbose_name="budget file preview" | ||
), | ||
), | ||
] |
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
19 changes: 0 additions & 19 deletions
19
flash_update/migrations/0013_alter_flashgraphicmap_file.py
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
flash_update/migrations/0013_alter_flashgraphicmap_file_and_more.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,27 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 10:12 | ||
|
||
from django.db import migrations | ||
|
||
import main.base_class | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("flash_update", "0012_auto_20230410_0720"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="flashgraphicmap", | ||
name="file", | ||
field=main.base_class.SecureFileField(upload_to="flash_update/images", verbose_name="file"), | ||
), | ||
migrations.AlterField( | ||
model_name="flashupdate", | ||
name="extracted_file", | ||
field=main.base_class.SecureFileField( | ||
blank=True, null=True, upload_to="flash_update/pdf/", verbose_name="extracted file" | ||
), | ||
), | ||
] |
19 changes: 0 additions & 19 deletions
19
flash_update/migrations/0014_alter_flashupdate_extracted_file.py
This file was deleted.
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
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,29 @@ | ||
import datetime | ||
import posixpath | ||
from uuid import uuid4 | ||
|
||
from django.core.files.utils import validate_file_name | ||
from django.db.models.fields.files import FileField | ||
|
||
|
||
class SecureFileField(FileField): | ||
def generate_filename(self, instance, filename): | ||
""" | ||
Apply (if callable) or prepend (if a string) upload_to to the filename, | ||
then delegate further processing of the name to the storage backend. | ||
Until the storage layer, all file paths are expected to be Unix style | ||
(with forward slashes). | ||
""" | ||
extension = filename.split(".")[-1] | ||
old_file_name = filename.split(".")[0] | ||
# Create a unique filename using uuid4 | ||
filename = f"{old_file_name}-{uuid4().hex}.{extension}" | ||
|
||
if callable(self.upload_to): | ||
filename = self.upload_to(instance, filename) | ||
else: | ||
dirname = datetime.datetime.now().strftime(str(self.upload_to)) | ||
filename = posixpath.join(dirname, filename) | ||
filename = validate_file_name(filename, allow_relative_path=True) | ||
|
||
return self.storage.generate_filename(filename) |
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
25 changes: 25 additions & 0 deletions
25
per/migrations/0121_alter_perdocumentupload_file_alter_perfile_file.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,25 @@ | ||
# Generated by Django 4.2.15 on 2024-09-10 10:12 | ||
|
||
from django.db import migrations | ||
|
||
import main.base_class | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("per", "0120_alter_formcomponent_status"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="perdocumentupload", | ||
name="file", | ||
field=main.base_class.SecureFileField(upload_to="per/documents/", verbose_name="file"), | ||
), | ||
migrations.AlterField( | ||
model_name="perfile", | ||
name="file", | ||
field=main.base_class.SecureFileField(upload_to="per/images/", verbose_name="file"), | ||
), | ||
] |
Oops, something went wrong.