diff --git a/home/management/__init__.py b/home/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/home/management/commands/__init__.py b/home/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/home/management/commands/download_files.py b/home/management/commands/download_files.py new file mode 100644 index 0000000..336f0b3 --- /dev/null +++ b/home/management/commands/download_files.py @@ -0,0 +1,55 @@ +import cloudinary +import cloudinary.uploader +import cloudinary.api +from django.core.management.base import BaseCommand +from home.models import LongRebate +from messWebsite.settings import CLOUDINARY_STORAGE, MEDIA_ROOT +import requests +import os +import mimetypes + +def download_file(url, save_path): + try: + # Send a GET request to the URL + response = requests.get(url, stream=True) + # Check if the request was successful + response.raise_for_status() + + content_type = response.headers.get('Content-Type') + if content_type: + extension = mimetypes.guess_extension(content_type) + file_extension = extension if extension else '.bin' + + # Open the file in write-binary mode and write the content + with open(f"{save_path}{file_extension}", 'wb') as file: + for chunk in response.iter_content(chunk_size=8192): + file.write(chunk) + + print(f"File downloaded successfully and saved to {save_path}") + + return file_extension + + except requests.RequestException as e: + print(f"An error occurred: {e}") + +class Command(BaseCommand): + help = 'Fetch Cloudinary image URLs for all entries in LongRebate' + def handle(self, *args, **options): + cloudinary.config( + cloud_name=CLOUDINARY_STORAGE['CLOUD_NAME'], + api_key=CLOUDINARY_STORAGE['API_KEY'], + api_secret=CLOUDINARY_STORAGE['API_SECRET'], + ) + + for obj in LongRebate.objects.all(): + file_name = obj.file.name[len(str("media/documents/")):] + try: + url = cloudinary.CloudinaryResource(obj.file.url[len(str("/media/")) : ]).build_url() + if url: + file_extension = download_file(url, os.path.join(MEDIA_ROOT, 'documents', file_name)) + if file_extension: + obj.file.name = f"documents/{file_name}{file_extension}" + obj.save() + except Exception as e: + self.stdout.write(self.style.ERROR(f'Error fetching URL for public ID {obj.file}: {e}')) + diff --git a/home/models/students.py b/home/models/students.py index 72faa51..5331e1b 100644 --- a/home/models/students.py +++ b/home/models/students.py @@ -174,6 +174,7 @@ class LongRebate(models.Model): date_applied = models.DateField( default=now, help_text="Date on which the rebate was applied" ) + file = models.FileField( _("File"), upload_to="documents/", default=None, null=True, blank=True ) diff --git a/messWebsite/settings.py b/messWebsite/settings.py index 0997c74..50aa874 100644 --- a/messWebsite/settings.py +++ b/messWebsite/settings.py @@ -3,11 +3,12 @@ import environ -env = environ.Env() -environ.Env.read_env() - # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent + +env = environ.Env() +environ.Env.read_env(BASE_DIR / '.env') + DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240 # Quick-start development settings - unsuitable for production @@ -192,10 +193,15 @@ "API_SECRET": env("API_SECRET_CLOUD"), } -DEFAULT_FILE_STORAGE = "cloudinary_storage.storage.RawMediaCloudinaryStorage" -DEFAULT_FILE_STORAGE = "cloudinary_storage.storage.MediaCloudinaryStorage" +MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_URL = "/media/" +DEFAULT_FILE_STORAGE = 'django.core.files.storage.FileSystemStorage' + +FILE_UPLOAD_HANDLERS = [ + 'django.core.files.uploadhandler.MemoryFileUploadHandler', + 'django.core.files.uploadhandler.TemporaryFileUploadHandler', +] # Default primary key field type # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field diff --git a/messWebsite/urls.py b/messWebsite/urls.py index 49cd75f..2a7faa1 100644 --- a/messWebsite/urls.py +++ b/messWebsite/urls.py @@ -31,6 +31,4 @@ path("accounts/google/login/", oauth2_login, name="google_login"), path("accounts/google/login/callback/", oauth2_callback, name="google_callback"), path("", include("home.urls")), -] -if settings.DEBUG: - urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) +] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)