Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local file storage #70

Merged
merged 7 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added home/management/__init__.py
Empty file.
Empty file.
55 changes: 55 additions & 0 deletions home/management/commands/download_files.py
Original file line number Diff line number Diff line change
@@ -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}'))

360 changes: 81 additions & 279 deletions home/migrations/0001_initial.py

Large diffs are not rendered by default.

125 changes: 0 additions & 125 deletions home/migrations/0002_remove_allocationautumn22_month_and_more.py

This file was deleted.

1 change: 1 addition & 0 deletions home/models/students.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
16 changes: 11 additions & 5 deletions messWebsite/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions messWebsite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading