diff --git a/backend/devpro/base/admin.py b/backend/devpro/base/admin.py index 341bbbc..fb9887e 100644 --- a/backend/devpro/base/admin.py +++ b/backend/devpro/base/admin.py @@ -1,15 +1,9 @@ from django.contrib import admin -from django.contrib.admin import ModelAdmin from django_min_custom_user.admin import MinUserAdmin -from devpro.base.models import User, Arquivo +from devpro.base.models import User @admin.register(User) class UserAdmin(MinUserAdmin): pass - - -@admin.register(Arquivo) -class ArquivoAdmin(ModelAdmin): - pass diff --git a/backend/devpro/base/migrations/0002_arquivo.py b/backend/devpro/base/migrations/0002_arquivo.py deleted file mode 100644 index f17c61f..0000000 --- a/backend/devpro/base/migrations/0002_arquivo.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 5.0.7 on 2024-09-04 23:25 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('base', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Arquivo', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('arq', models.FileField(upload_to='public/')), - ('privado', models.FileField(upload_to='private/')), - ], - ), - ] diff --git a/backend/devpro/base/models.py b/backend/devpro/base/models.py index f3021da..9278cdf 100644 --- a/backend/devpro/base/models.py +++ b/backend/devpro/base/models.py @@ -1,11 +1,5 @@ -from django.db import models from django_min_custom_user.models import MinAbstractUser class User(MinAbstractUser): pass - - -class Arquivo(models.Model): - arq = models.FileField(upload_to='public/') - privado = models.FileField(upload_to='private/') diff --git a/backend/devpro/s3_file_handlers.py b/backend/devpro/s3_file_handlers.py deleted file mode 100644 index 5efd3ec..0000000 --- a/backend/devpro/s3_file_handlers.py +++ /dev/null @@ -1,40 +0,0 @@ -from django.core.files.storage import FileSystemStorage -from storages.backends.s3 import S3Storage - -_PUBLIC_PREFIX = 'public/' -_PRIVATE_PREFIX = 'private/' - - -class FileConfigurationError(Exception): - pass - - -class S3FileStorage(S3Storage): - def url(self, name, parameters=None, expire=None, http_method=None): - url = super().url(name, parameters, expire, http_method) - if name.startswith(_PUBLIC_PREFIX): - url = url.split('?')[0] - return url - - def get_object_parameters(self, name): - parameters = super().get_object_parameters(name) - self._validate(name) - if self._startswith(name, _PUBLIC_PREFIX): - parameters['ACL'] = 'public-read' - else: - parameters['ACL'] = 'private' - return parameters - - def _validate(self, name: str): - if not (self._startswith(name, _PUBLIC_PREFIX) or self._startswith(name, _PRIVATE_PREFIX)): - raise FileConfigurationError(f'property "upload_to" must start with "public" or "private" and it is {name}') - - def _startswith(self, name, prefix): - return name.startswith(f'{self.location}/{prefix}') - - -class FileSystemWithValidationStorage(FileSystemStorage): - def _save(self, name, content): - if not (name.startswith(_PUBLIC_PREFIX) or name.startswith(_PRIVATE_PREFIX)): - raise FileConfigurationError(f'property "upload_to" must start with "public" or "private" and it is {name}') - return super()._save(name, content) diff --git a/backend/devpro/settings.py b/backend/devpro/settings.py index fe4fca7..f5cde63 100644 --- a/backend/devpro/settings.py +++ b/backend/devpro/settings.py @@ -11,9 +11,9 @@ """ from pathlib import Path -from decouple import config, Csv import dj_database_url +from decouple import config, Csv # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -134,7 +134,7 @@ # Muda a configuração de upload de arquivos locais para bater com produção STORAGES = { "default": { - "BACKEND": "devpro.s3_file_handlers.FileSystemWithValidationStorage", + "BACKEND": "devpro_s3_storages.handlers.FileSystemWithValidationStorage", }, "staticfiles": { "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", @@ -147,7 +147,7 @@ AWS_S3_SECRET_ACCESS_KEY = config('AWS_S3_SECRET_ACCESS_KEY') STORAGES = { "default": { - "BACKEND": "devpro.s3_file_handlers.S3FileStorage", + "BACKEND": "devpro_s3_storages.handlers.S3FileStorage", "OPTIONS": { 'default_acl': 'private', 'location': 'media', diff --git a/backend/poetry.lock b/backend/poetry.lock index 3457d9e..99f8169 100644 --- a/backend/poetry.lock +++ b/backend/poetry.lock @@ -63,6 +63,21 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "devpro-s3-storages" +version = "0.1.0" +description = "Django Project to handle Static and Upload Files on s3 the right way" +optional = false +python-versions = "<4.0,>=3.12" +files = [ + {file = "devpro_s3_storages-0.1.0-py3-none-any.whl", hash = "sha256:99eab1b444864e3e675bc0e5d00706846c10f8faba3db3c68007bd3037f17413"}, + {file = "devpro_s3_storages-0.1.0.tar.gz", hash = "sha256:ec273c8685aa2387ca588fb563908824aa420093965a3ca856219d9dd88fae53"}, +] + +[package.dependencies] +django = ">=5.1.1,<6.0.0" +django-storages = {version = ">=1.14.4,<2.0.0", extras = ["s3"]} + [[package]] name = "dj-database-url" version = "2.2.0" @@ -80,17 +95,17 @@ typing_extensions = ">=3.10.0.0" [[package]] name = "django" -version = "5.0.7" +version = "5.1.1" description = "A high-level Python web framework that encourages rapid development and clean, pragmatic design." optional = false python-versions = ">=3.10" files = [ - {file = "Django-5.0.7-py3-none-any.whl", hash = "sha256:f216510ace3de5de01329463a315a629f33480e893a9024fc93d8c32c22913da"}, - {file = "Django-5.0.7.tar.gz", hash = "sha256:bd4505cae0b9bd642313e8fb71810893df5dc2ffcacaa67a33af2d5cd61888f2"}, + {file = "Django-5.1.1-py3-none-any.whl", hash = "sha256:71603f27dac22a6533fb38d83072eea9ddb4017fead6f67f2562a40402d61c3f"}, + {file = "Django-5.1.1.tar.gz", hash = "sha256:021ffb7fdab3d2d388bc8c7c2434eb9c1f6f4d09e6119010bbb1694dda286bc2"}, ] [package.dependencies] -asgiref = ">=3.7.0,<4" +asgiref = ">=3.8.1,<4" sqlparse = ">=0.3.1" tzdata = {version = "*", markers = "sys_platform == \"win32\""} @@ -500,4 +515,4 @@ zstd = ["zstandard (>=0.18.0)"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "f9b78727085831dd1777bdf88708cc9fb32f152a9ed2316f842ee7eb219b923c" +content-hash = "23776fbffd16c6a7b67f80016a271505670ea709c00e206c13549718e6c7990e" diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 64a52d8..160ad38 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -15,6 +15,7 @@ python-decouple = "^3.8" django-min-custom-user = "^0.2.0" django-storages = {extras = ["s3"], version = "^1.14.4"} pillow = "^10.4.0" +devpro-s3-storages = "^0.1.0" [tool.poetry.group.dev]