Skip to content

Commit

Permalink
Add a limit for the file size
Browse files Browse the repository at this point in the history
  • Loading branch information
tudoramariei committed Oct 29, 2024
1 parent c89fab5 commit d74c50c
Show file tree
Hide file tree
Showing 7 changed files with 499 additions and 214 deletions.
5 changes: 5 additions & 0 deletions backend/civil_society_vote/common/contants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Constants for memory sizes
KIBIBYTE = 1024
MEBIBYTE = KIBIBYTE * 1024
GIBIBYTE = MEBIBYTE * 1024
TEBIBYTE = GIBIBYTE * 1024
21 changes: 21 additions & 0 deletions backend/civil_society_vote/common/formatting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from civil_society_vote.common.contants import GIBIBYTE, KIBIBYTE, MEBIBYTE, TEBIBYTE


def get_human_readable_size(num_bytes: int):
if num_bytes >= TEBIBYTE:
size_unit = "TB"
size_in_unit = num_bytes / TEBIBYTE
elif num_bytes >= GIBIBYTE:
size_unit = "GB"
size_in_unit = num_bytes / GIBIBYTE
elif num_bytes >= MEBIBYTE:
size_unit = "MB"
size_in_unit = num_bytes / MEBIBYTE
elif num_bytes >= KIBIBYTE:
size_unit = "KB"
size_in_unit = num_bytes / KIBIBYTE
else:
size_unit = "B"
size_in_unit = num_bytes

return {"size": size_in_unit, "unit": size_unit}
19 changes: 10 additions & 9 deletions backend/civil_society_vote/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
import sentry_sdk
from django.urls import reverse_lazy # noqa


# Constants for memory sizes
KIBIBYTE = 1024
MEBIBYTE = KIBIBYTE * 1024
GIBIBYTE = MEBIBYTE * 1024
TEBIBYTE = GIBIBYTE * 1024

from civil_society_vote.common.contants import MEBIBYTE
from civil_society_vote.common.formatting import get_human_readable_size

# Environment parameters
root = Path(__file__).resolve().parent.parent.parent
Expand Down Expand Up @@ -73,7 +68,7 @@
TIME_ZONE=(str, "Europe/Bucharest"),
AUDITLOG_EXPIRY_DAYS=(int, 45),
DATA_UPLOAD_MAX_MEMORY_SIZE=(int, 3 * MEBIBYTE),
MAX_DOCUMENT_SIZE=(int, 2 * MEBIBYTE),
MAX_DOCUMENT_SIZE=(int, 50 * MEBIBYTE),
IMPERSONATE_READ_ONLY=(bool, False),
# db settings
# DATABASE_ENGINE=(str, "sqlite3"),
Expand Down Expand Up @@ -425,12 +420,18 @@ def show_toolbar(request):
STATIC_ROOT = os.path.abspath(os.path.join(os.sep, "var", "www", "votong", "backend", "static"))
MEDIA_ROOT = os.path.abspath(os.path.join(os.sep, "var", "www", "votong", "backend", "media"))

# Maximum request size excludind the uploaded files
# Maximum request size excluding the uploaded files
DATA_UPLOAD_MAX_MEMORY_SIZE = env.int("DATA_UPLOAD_MAX_MEMORY_SIZE")

# Maximum single file size for uploaded files
MAX_DOCUMENT_SIZE = env.int("MAX_DOCUMENT_SIZE")

MAX_DOCUMENT_READABLE_SIZE = get_human_readable_size(MAX_DOCUMENT_SIZE)

MAX_DOCUMENT_SIZE_UNIT = MAX_DOCUMENT_READABLE_SIZE["unit"]
MAX_DOCUMENT_SIZE_IN_UNIT = MAX_DOCUMENT_READABLE_SIZE["size"]


STATICFILES_DIRS = (os.path.abspath(os.path.join(BASE_DIR, "static_extras")),)


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Generated by Django 4.2.16 on 2024-10-29 14:33

from django.db import migrations, models
import hub.models


class Migration(migrations.Migration):

dependencies = [
("hub", "0067_remove_organization_user"),
]

operations = [
migrations.AlterField(
model_name="candidate",
name="criminal_record",
field=models.FileField(
blank=True,
help_text="(Optional) Criminal record, valid at the time of submitting the candidacy",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Criminal record",
),
),
migrations.AlterField(
model_name="candidate",
name="cv",
field=models.FileField(
blank=True,
help_text="Europass format CV",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="CV",
),
),
migrations.AlterField(
model_name="candidate",
name="declaration_of_interests",
field=models.FileField(
blank=True,
help_text="Official format Declaration of interests",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Declaration of interests",
),
),
migrations.AlterField(
model_name="candidate",
name="fiscal_record",
field=models.FileField(
blank=True,
help_text="Fiscal record, valid at the time of submitting the candidacy",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Fiscal record",
),
),
migrations.AlterField(
model_name="candidate",
name="letter_of_intent",
field=models.FileField(
blank=True,
help_text="Letter of intent (with the mention of the domain to be represented in the CES)",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Letter of intent",
),
),
migrations.AlterField(
model_name="candidate",
name="mandate",
field=models.FileField(
blank=True,
help_text="Mandate from the organization (signed in original + electronic) with the highlighting of the domain for which it is running",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Mandate",
),
),
migrations.AlterField(
model_name="candidate",
name="photo",
field=models.ImageField(
blank=True,
default="",
max_length=300,
storage=hub.models.select_public_storage,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Candidate photo",
),
),
migrations.AlterField(
model_name="candidate",
name="statement",
field=models.FileField(
blank=True,
help_text="Declaration of the designated representative stating that he/she is not a member of the leadership of a political party, has not been elected to a public office and is not a dignitary of the Romanian state.",
max_length=300,
null=True,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Representative statement",
),
),
migrations.AlterField(
model_name="organization",
name="fiscal_certificate_anaf",
field=models.FileField(
blank=True,
default="",
help_text="Certificat fiscal emis de ANAF",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Fiscal certificate ANAF",
),
),
migrations.AlterField(
model_name="organization",
name="fiscal_certificate_local",
field=models.FileField(
blank=True,
default="",
help_text="Certificat fiscal emis de Direcția de Impozite și Taxe Locale",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Fiscal certificate local",
),
),
migrations.AlterField(
model_name="organization",
name="report_2021",
field=models.FileField(
blank=True,
default="",
help_text="Rapoartele anuale trebuie să includă sursele de finanțare din care să rezulte că organizația dispune de resurse financiare şi umane pentru îndeplinirea mandatului de membru în Comisia Electorală a VotONG.",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Yearly report 2021",
),
),
migrations.AlterField(
model_name="organization",
name="report_2022",
field=models.FileField(
blank=True,
default="",
help_text="Rapoartele anuale trebuie să includă sursele de finanțare din care să rezulte că organizația dispune de resurse financiare şi umane pentru îndeplinirea mandatului de membru în Comisia Electorală a VotONG.",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Yearly report 2022",
),
),
migrations.AlterField(
model_name="organization",
name="report_2023",
field=models.FileField(
blank=True,
default="",
help_text="Rapoartele anuale trebuie să includă sursele de finanțare din care să rezulte că organizația dispune de resurse financiare şi umane pentru îndeplinirea mandatului de membru în Comisia Electorală a VotONG.",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Yearly report 2023",
),
),
migrations.AlterField(
model_name="organization",
name="statement_discrimination",
field=models.FileField(
blank=True,
default="",
help_text="Declarație pe proprie răspundere prin care declară că nu realizează activități sau susține cauze de natură politică sau care discriminează pe considerente legate de etnie, rasă, sex, orientare sexuală, religie, capacități fizice sau psihice sau de apartenența la una sau mai multe categorii sociale sau economice.",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Non-discrimination statement",
),
),
migrations.AlterField(
model_name="organization",
name="statement_political",
field=models.FileField(
blank=True,
default="",
help_text="Declarație pe propria răspundere prin care declar că ONG-ul nu are între membrii conducerii organizației (Președinte sau Consiliul Director) membri ai conducerii unui partid politic sau persoane care au fost alese într-o funcție publică.",
max_length=300,
upload_to="",
validators=[hub.models.file_validator],
verbose_name="Non-political statement",
),
),
]
Loading

0 comments on commit d74c50c

Please sign in to comment.