From 8478e4328387a8e1c486d36dbb4cf15b535b382f Mon Sep 17 00:00:00 2001 From: Chad Bentz <1760475+felickz@users.noreply.github.com> Date: Sat, 23 Nov 2024 10:49:14 -0500 Subject: [PATCH] init --- my_django_project/README.md | 57 ++++++++++ my_django_project/manage.py | 18 ++++ .../my_django_project/__init__.py | 1 + .../my_django_project/settings.py | 101 ++++++++++++++++++ my_django_project/my_django_project/urls.py | 6 ++ my_django_project/my_django_project/views.py | 39 +++++++ my_django_project/my_django_project/wsgi.py | 6 ++ my_django_project/requirements.txt | 1 + 8 files changed, 229 insertions(+) create mode 100644 my_django_project/README.md create mode 100644 my_django_project/manage.py create mode 100644 my_django_project/my_django_project/__init__.py create mode 100644 my_django_project/my_django_project/settings.py create mode 100644 my_django_project/my_django_project/urls.py create mode 100644 my_django_project/my_django_project/views.py create mode 100644 my_django_project/my_django_project/wsgi.py create mode 100644 my_django_project/requirements.txt diff --git a/my_django_project/README.md b/my_django_project/README.md new file mode 100644 index 00000000..1e34ed99 --- /dev/null +++ b/my_django_project/README.md @@ -0,0 +1,57 @@ +# My Django Project + +This is a simple Django project designed to test the `handle_post` view function, which handles file downloads based on specified subdirectory and filename. + +## Project Structure + +``` +my_django_project +├── my_django_project +│ ├── __init__.py +│ ├── settings.py +│ ├── urls.py +│ ├── views.py +│ └── wsgi.py +├── manage.py +└── README.md +``` + +## Requirements + +- Python 3.x +- Django + +## Setup Instructions + +1. **Clone the repository:** + ``` + git clone + cd my_django_project + ``` + +2. **Create a virtual environment:** + ``` + python -m venv venv + source venv/bin/activate # On Windows use `venv\Scripts\activate` + ``` + +3. **Install dependencies:** + ``` + pip install django + ``` + +4. **Run the development server:** + ``` + python manage.py runserver + ``` + +5. **Access the application:** + Open your web browser and navigate to `http://127.0.0.1:8000/`. + +## Usage + +To test the `handle_post` view, send a POST request to the appropriate URL with the required parameters (`subdir` and `filename`). The view will attempt to return the specified file as a downloadable attachment. + +## License + +This project is licensed under the MIT License. \ No newline at end of file diff --git a/my_django_project/manage.py b/my_django_project/manage.py new file mode 100644 index 00000000..6f210e05 --- /dev/null +++ b/my_django_project/manage.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python +import os +import sys + +def main(): + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_django_project.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/my_django_project/my_django_project/__init__.py b/my_django_project/my_django_project/__init__.py new file mode 100644 index 00000000..82789f20 --- /dev/null +++ b/my_django_project/my_django_project/__init__.py @@ -0,0 +1 @@ +# This file is intentionally left blank. \ No newline at end of file diff --git a/my_django_project/my_django_project/settings.py b/my_django_project/my_django_project/settings.py new file mode 100644 index 00000000..9688a2b1 --- /dev/null +++ b/my_django_project/my_django_project/settings.py @@ -0,0 +1,101 @@ +# settings.py + +import os +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'your-secret-key' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +# Application definition +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'my_django_project.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'my_django_project.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + +# Password validation +# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + +# Internationalization +# https://docs.djangoproject.com/en/4.0/topics/i18n/ +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.0/howto/static-files/ +STATIC_URL = '/static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' \ No newline at end of file diff --git a/my_django_project/my_django_project/urls.py b/my_django_project/my_django_project/urls.py new file mode 100644 index 00000000..70a6e507 --- /dev/null +++ b/my_django_project/my_django_project/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from .views import handle_post + +urlpatterns = [ + path('handle_post/', handle_post, name='handle_post'), +] \ No newline at end of file diff --git a/my_django_project/my_django_project/views.py b/my_django_project/my_django_project/views.py new file mode 100644 index 00000000..d2fd1a77 --- /dev/null +++ b/my_django_project/my_django_project/views.py @@ -0,0 +1,39 @@ +from django.http import JsonResponse +from django.views.decorators.csrf import csrf_exempt +from django import http +import os +from pathlib import Path + +@csrf_exempt +def handle_post(request): + if request.method == 'POST': + """Get a downloadable attachment given path to a file""" + local_volume_path = Path("/root/my_django_project/files") + subdir = request.POST['subdir'] + filename = request.POST['filename'] + + if filename is None or filename == '': + return JsonResponse({'error': 'filename is a required field.'}, status=400) + + if subdir is None or subdir == '': + return JsonResponse({'error': 'subdir is a required field.'}, status=400) + + # Remove fwd slash in the front + subdir = subdir.lstrip('/') + + if not os.path.isdir(local_volume_path / subdir): + return JsonResponse({'error': 'This subdirectory does not exist.'}, status=404) + + if not (local_volume_path / subdir / filename).exists(): + return JsonResponse({'error': 'File does not exist.'}, status=404) + + try: + return http.FileResponse( + open(local_volume_path / subdir / filename, 'rb'), + filename=filename, + as_attachment=True) + + except Exception as e: + return JsonResponse({'error': str(e)}, status=500) + + return JsonResponse({'error': 'Invalid method'}, status=405) \ No newline at end of file diff --git a/my_django_project/my_django_project/wsgi.py b/my_django_project/my_django_project/wsgi.py new file mode 100644 index 00000000..98a0c34f --- /dev/null +++ b/my_django_project/my_django_project/wsgi.py @@ -0,0 +1,6 @@ +import os +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_django_project.settings') + +application = get_wsgi_application() \ No newline at end of file diff --git a/my_django_project/requirements.txt b/my_django_project/requirements.txt new file mode 100644 index 00000000..deceaddf --- /dev/null +++ b/my_django_project/requirements.txt @@ -0,0 +1 @@ +Django>=3.2,<4.0 \ No newline at end of file