Skip to content

Commit

Permalink
increase worker timeout for sending email (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
mittal-ishaan authored Oct 1, 2024
1 parent 71a3b5f commit 249ecdd
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[flake8]
max-line-length = 88
# Remove F811 from admin.py
ignore = E501, E722, W503, F811
ignore = E501, E722, W503, F811, E203
per-file-ignores =
home/models/students.py: E402
home/models/admin.py: F811
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
rev: 24.8.0
hooks:
- id: black
language_version: python3.10
language_version: python3.12
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
Expand Down
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ python manage.py migrate --noinput
python manage.py collectstatic --noinput

# Start server
gunicorn messWebsite.wsgi:application --bind 0.0.0.0:8000 --workers=16 --preload
gunicorn messWebsite.wsgi:application --bind 0.0.0.0:8000 --workers=16 --preload --timeout 300
10 changes: 9 additions & 1 deletion home/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,15 @@ class about_Admin(ImportExportModelAdmin, admin.ModelAdmin):
"email__degree",
"email__department",
)
list_display = ("student_id", "name", "email", "period", "caterer", "jain", "registration_time",)
list_display = (
"student_id",
"name",
"email",
"period",
"caterer",
"jain",
"registration_time",
)
fieldsets = (
(
None,
Expand Down
46 changes: 29 additions & 17 deletions home/management/commands/download_files.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import mimetypes
import os

import cloudinary
import cloudinary.uploader
import cloudinary.api
import cloudinary.uploader
import requests
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:
Expand All @@ -15,41 +18,50 @@ def download_file(url, save_path):
# Check if the request was successful
response.raise_for_status()

content_type = response.headers.get('Content-Type')
content_type = response.headers.get("Content-Type")
if content_type:
extension = mimetypes.guess_extension(content_type)
file_extension = extension if extension else '.bin'
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:
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'
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'],
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/")):]
file_name = obj.file.name[len(str("media/documents/")) :]
try:
url = cloudinary.CloudinaryResource(obj.file.url[len(str("/media/")) : ]).build_url()
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))
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}'))

self.stdout.write(
self.style.ERROR(
f"Error fetching URL for public ID {obj.file}: {e}"
)
)
4 changes: 2 additions & 2 deletions home/models/allocation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models
from django.utils.translation import gettext as _
from django.utils.timezone import now
from django.utils.translation import gettext as _

from .caterer import Caterer

Expand Down Expand Up @@ -124,7 +124,7 @@ class Allocation(models.Model):
registration_time = models.DateTimeField(
_("Registration time"),
default=now,
blank=True,
blank=True,
null=True,
help_text="This contains the date and time of registration",
editable=True,
Expand Down
8 changes: 5 additions & 3 deletions home/resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging

from import_export import fields, resources
from django.utils import timezone
from import_export import fields, resources

from .models import (
Allocation,
Expand Down Expand Up @@ -79,12 +79,14 @@ class AllocationResource(resources.ModelResource):
attribute="second_pref", column_name="Second Preferences"
)
third_pref = fields.Field(attribute="third_pref", column_name="Third Preferences")
registration_time = fields.Field(attribute="registration_time", column_name="Registration Date Time")
registration_time = fields.Field(
attribute="registration_time", column_name="Registration Date Time"
)

def dehydrate_registration_time(self, allocation):
# Convert the registration_time to local time
local_time = timezone.localtime(allocation.registration_time)
return local_time.strftime('%Y-%m-%d %H:%M:%S')
return local_time.strftime("%Y-%m-%d %H:%M:%S")

class Meta:
model = Allocation
Expand Down
4 changes: 2 additions & 2 deletions messWebsite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
path("accounts/google/login/", oauth2_login, name="google_login"),
path("accounts/google/login/callback/", oauth2_callback, name="google_callback"),
path("", include("home.urls")),
path('media/<path:path>/', serve, {'document_root': settings.MEDIA_ROOT}),
path('static/<path:path>/', serve, {'document_root': settings.STATIC_ROOT}),
path("media/<path:path>/", serve, {"document_root": settings.MEDIA_ROOT}),
path("static/<path:path>/", serve, {"document_root": settings.STATIC_ROOT}),
]

0 comments on commit 249ecdd

Please sign in to comment.