From 20e0bee33aa7ea542f3801ae1f4bef58ded45919 Mon Sep 17 00:00:00 2001 From: Rubikium <30288416+Rubikium@users.noreply.github.com> Date: Fri, 23 Sep 2022 19:32:35 +0800 Subject: [PATCH 1/5] Use django's FileField for file handling --- .../cms/polls/templates/cms_polls/menu.html | 1 - django_material_demo/cms/polls/urls.py | 3 +- django_material_demo/cms/polls/views/file.py | 13 --- .../cms/polls/views/question.py | 9 ++- .../django_material_demo/settings.py | 2 + django_material_demo/polls/admin.py | 19 +---- .../polls/migrations/0012_add_temp_fields.py | 68 ++++++++++++++++ ...ttachment_file_alter_question_thumbnail.py | 23 ++++++ .../0014_remove_file_model_and_more.py | 81 +++++++++++++++++++ django_material_demo/polls/models.py | 19 +---- django_material_demo/requirements.txt | 1 + 11 files changed, 187 insertions(+), 52 deletions(-) delete mode 100644 django_material_demo/cms/polls/views/file.py create mode 100644 django_material_demo/polls/migrations/0012_add_temp_fields.py create mode 100644 django_material_demo/polls/migrations/0013_alter_attachment_file_alter_question_thumbnail.py create mode 100644 django_material_demo/polls/migrations/0014_remove_file_model_and_more.py diff --git a/django_material_demo/cms/polls/templates/cms_polls/menu.html b/django_material_demo/cms/polls/templates/cms_polls/menu.html index a07ef12..c5f1022 100644 --- a/django_material_demo/cms/polls/templates/cms_polls/menu.html +++ b/django_material_demo/cms/polls/templates/cms_polls/menu.html @@ -1,7 +1,6 @@ {% comment %} https://materializecss.com/sidenav.html {% endcomment %}
Home @@ -9,7 +12,14 @@ {% if latest_question_list %}
Home > @@ -8,7 +11,13 @@
From 5ce7de4564d2f5ebdb1ab4604eaedcd7aa45a2fc Mon Sep 17 00:00:00 2001 From: Rubikium <30288416+Rubikium@users.noreply.github.com> Date: Mon, 26 Sep 2022 17:35:18 +0800 Subject: [PATCH 3/5] Add AWS S3 support --- django_material_demo/.env.example | 10 +++++++++ .../django_material_demo/settings.py | 21 +++++++++++++++++++ django_material_demo/requirements.txt | 6 ++++++ 3 files changed, 37 insertions(+) diff --git a/django_material_demo/.env.example b/django_material_demo/.env.example index 900e367..cb4642c 100644 --- a/django_material_demo/.env.example +++ b/django_material_demo/.env.example @@ -7,3 +7,13 @@ POSTGRES_USER=postgres POSTGRES_PASSWORD=GENERATE_SECRET[postgres_password] POSTGRES_HOST=db POSTGRES_PORT=5432 + +FILE_STORAGE_IMPL=local_storage + +AWS_STORAGE_BUCKET_NAME=django-material-demo +AWS_S3_REGION_NAME=[AWS_S3_REGION_NAME] +AWS_S3_ACCESS_KEY_ID=[AWS_S3_ACCESS_KEY_ID] +AWS_S3_SECRET_ACCESS_KEY=[AWS_S3_SECRET_ACCESS_KEY] + +AWS_S3_FILE_OVERWRITE=true +AWS_LOCATION=media diff --git a/django_material_demo/django_material_demo/settings.py b/django_material_demo/django_material_demo/settings.py index e1c7e28..caafa4c 100644 --- a/django_material_demo/django_material_demo/settings.py +++ b/django_material_demo/django_material_demo/settings.py @@ -52,6 +52,7 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'django_filters', + 'storages', ] MIDDLEWARE = [ @@ -166,6 +167,26 @@ MEDIA_ROOT = 'media/' MEDIA_URL = 'media/' +FILE_STORAGE_IMPL = str(os.getenv('FILE_STORAGE_IMPL')) + +if FILE_STORAGE_IMPL.lower() == 's3': + DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' +else: + # use django.core.files.storage.FileSystemStorage + # as defined in django/conf/global_settings.py + pass + +# Amazon S3 settings +# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html + +AWS_STORAGE_BUCKET_NAME = str(os.getenv('AWS_STORAGE_BUCKET_NAME')) +AWS_S3_REGION_NAME = str(os.getenv('AWS_S3_REGION_NAME')) +AWS_S3_ACCESS_KEY_ID = str(os.getenv('AWS_S3_ACCESS_KEY_ID')) +AWS_S3_SECRET_ACCESS_KEY = str(os.getenv('AWS_S3_SECRET_ACCESS_KEY')) +AWS_S3_FILE_OVERWRITE = (str(os.getenv('AWS_S3_FILE_OVERWRITE')).lower() + in ['true', 'yes', '1']) +AWS_LOCATION = str(os.getenv('AWS_LOCATION')) + # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field diff --git a/django_material_demo/requirements.txt b/django_material_demo/requirements.txt index e424b01..342d173 100644 --- a/django_material_demo/requirements.txt +++ b/django_material_demo/requirements.txt @@ -1,10 +1,16 @@ asgiref==3.5.2 +boto3==1.24.80 +botocore==1.27.80 Django==4.1 django-filter==22.1 django-material==1.10.0 +django-storages==1.13.1 +jmespath==1.0.1 Pillow==9.2.0 psycopg2==2.9.3 +python-dateutil==2.8.2 python-dotenv==0.20.0 PyYAML==6.0 +s3transfer==0.6.0 six==1.16.0 sqlparse==0.4.2 From bf796e658028d51160e79b394cb417bb92f1a1bb Mon Sep 17 00:00:00 2001 From: Rubikium <30288416+Rubikium@users.noreply.github.com> Date: Mon, 26 Sep 2022 19:08:26 +0800 Subject: [PATCH 4/5] Add TODOs on html templates for data render --- django_material_demo/cms/polls/views/question.py | 6 ++++++ django_material_demo/cms/utils/views.py | 1 + 2 files changed, 7 insertions(+) diff --git a/django_material_demo/cms/polls/views/question.py b/django_material_demo/cms/polls/views/question.py index abb8cb4..602cdfc 100644 --- a/django_material_demo/cms/polls/views/question.py +++ b/django_material_demo/cms/polls/views/question.py @@ -286,20 +286,26 @@ def get_object_data(self): if item[0] == thumbnail_name: # Skip if no image if item[1]: + #TODO: replace with template image_html = mark_safe( f"") yield (item[0], image_html) + else: + yield (item[0], 'None') else: yield item attachments = question.attachment_set.all() if len(attachments): + #TODO: replace with template attachments = [ mark_safe(f"{x.file.name}") for x in attachments] html_list = get_html_list(attachments) yield ('Attachments', html_list) + else: + yield ('Attachments', 'None') class QuestionViewSet(ModelViewSet): diff --git a/django_material_demo/cms/utils/views.py b/django_material_demo/cms/utils/views.py index 6dca189..a1ff1ef 100644 --- a/django_material_demo/cms/utils/views.py +++ b/django_material_demo/cms/utils/views.py @@ -16,6 +16,7 @@ def get_html_list(arr): if len(arr) == 0: return '' + #TODO: replace with template value_list = [conditional_escape(x) for x in arr] value_list_html = mark_safe( '