Skip to content

Commit

Permalink
Merge pull request #62 from NumanIbnMazid/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
NumanIbnMazid authored Aug 28, 2024
2 parents f041996 + 9c19605 commit 371016f
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 32 deletions.
14 changes: 14 additions & 0 deletions backend/project/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# Django Cloudinary Storage
# going to use it only for media files though, it is django.contrib.staticfiles which has to be first
"cloudinary_storage",
"cloudinary"
]
+ THIRD_PARTY_APPS
+ LOCAL_APPS + [
Expand All @@ -71,6 +75,8 @@
# Django CORS Headers Middleware
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
# Whitenoise Middleware
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
Expand Down Expand Up @@ -274,6 +280,14 @@
X_FRAME_OPTIONS = "SAMEORIGIN"
SILENCED_SYSTEM_CHECKS = ["security.W019"]

# Django Cloudinary
CLOUDINARY_STORAGE = {
'CLOUD_NAME': os.getenv("CLOUDINARY_CLOUD_NAME"),
'API_KEY': os.getenv("CLOUDINARY_API_KEY"),
'API_SECRET': os.getenv("CLOUDINARY_API_SECRET")
}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'

# ----------------------------------------------------
# *** Configurable Values ***
# ----------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion backend/project/settings/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
MEDIA_URL = "/media/"
PUBLIC_ROOT = os.path.join(BASE_DIR, "public/")
# STATIC & MEDIA ROOT
MEDIA_ROOT = os.path.join(PUBLIC_ROOT, "media/")
# MEDIA_ROOT = os.path.join(PUBLIC_ROOT, "media/")
STATIC_ROOT = os.path.join(PUBLIC_ROOT, "static/")
# Static Files Directories
STATICFILES_DIRS = (
Expand Down
12 changes: 8 additions & 4 deletions backend/project/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# ----------------------------------------------------
ALLOWED_HOSTS = [
"nim23.com", # Frontend Host
"nim23.onrender.com",
]

# ----------------------------------------------------
Expand All @@ -13,9 +14,9 @@
# STATIC & MEDIA URL
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
PUBLIC_ROOT = os.path.join(BASE_DIR, "/home/nimcom/public_html/")
PUBLIC_ROOT = os.path.join(BASE_DIR, "public/")
# STATIC & MEDIA ROOT
MEDIA_ROOT = os.path.join(PUBLIC_ROOT, "media/")
# MEDIA_ROOT = os.path.join(PUBLIC_ROOT, "media/")
STATIC_ROOT = os.path.join(PUBLIC_ROOT, "static/")
# Static Files Directories
STATICFILES_DIRS = (
Expand Down Expand Up @@ -43,6 +44,8 @@
CORS_ORIGIN_WHITELIST = [
"https://nim23.com",
"https://www.nim23.com",
"https://nim23.onrender.com",
"https://www.nim23.onrender.com",
]
CORS_ALLOW_HEADERS = [
'accept',
Expand All @@ -68,13 +71,14 @@
# ----------------------------------------------------
# *** Site Info ***
# ----------------------------------------------------
BACKEND_BASE_URL = "https://nim23.com/backend"
# BACKEND_BASE_URL = "https://nim23.com/backend"
BACKEND_BASE_URL = "https://nim23.onrender.com"
FRONTEND_BASE_URL = "https://nim23.com"

# ----------------------------------------------------
# *** Other Definitions ***
# ----------------------------------------------------
LOGIN_URL = "/backend/admin/login/"
LOGIN_URL = "/admin/login/"

# KNOX Configuration
KNOX_TOKEN_MODEL = "knox.AuthToken"
Expand Down
2 changes: 2 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ django-cors-headers = "^4.1.0"
django-filebrowser = "^4.0.2"
django-tinymce = "^3.6.1"
beautifulsoup4 = "^4.12.2"
whitenoise = "^6.7.0"
django-cloudinary-storage = "^0.3.0"

[tool.poetry.dev-dependencies]

Expand Down
10 changes: 10 additions & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ argon2-cffi-bindings==21.2.0
asgiref==3.7.2
autopep8==1.7.0
beautifulsoup4==4.12.2
certifi==2024.7.4
cffi==1.15.1
charset-normalizer==3.3.2
cloudinary==1.41.0
cryptography==41.0.1
Django==4.0.10
django-admin-interface==0.26.0
django-cleanup==8.0.0
django-cloudinary-storage==0.3.0
django-colorfield==0.9.0
django-cors-headers==4.2.0
django-js-asset==2.1.0
django-rest-knox==4.2.0
django-tinymce==3.6.1
djangorestframework==3.14.0
drf-yasg==1.21.6
idna==3.8
inflection==0.5.1
packaging==23.1
Pillow==9.5.0
Expand All @@ -26,10 +31,15 @@ python-dotenv==0.21.1
python-slugify==8.0.1
pytz==2023.3
PyYAML==6.0.2
requests==2.32.3
setuptools==73.0.1
six==1.16.0
soupsieve==2.4.1
sqlparse==0.4.4
text-unidecode==1.3
toml==0.10.2
typing_extensions==4.7.1
uritemplate==4.1.1
urllib3==2.2.2
wheel==0.44.0
whitenoise==6.7.0
94 changes: 69 additions & 25 deletions backend/utils/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import os
import base64
from django.contrib.staticfiles import finders
from urllib.parse import urlparse
import requests


def random_string_generator(size=4, chars=string.ascii_lowercase + string.digits):
Expand Down Expand Up @@ -325,41 +327,83 @@ def get_static_file_path(static_path):

def image_as_base64(image_file):
"""
:param `image_file` for the complete path of the image.
Convert an image file or URL to a base64-encoded string.
:param image_file: The path to the image file or URL of the image.
:return: Base64-encoded image string in the format 'data:image/{extension};base64,{encoded_string}'.
"""
if not os.path.isfile(image_file):
print(f"Image file not found: {image_file}")
return

# Determine if the input is a URL or a file path
if urlparse(image_file).scheme in ['http', 'https']:
# Handle image URL
try:
response = requests.get(image_file)
response.raise_for_status() # Raise an exception for HTTP errors
image_data = response.content
except requests.RequestException as e:
print(f"Error fetching image from URL: {e}")
return
else:
# Handle file path
if not os.path.isfile(image_file):
print(f"Image file not found: {image_file}")
return
with open(image_file, "rb") as img_f:
image_data = img_f.read()

# Get the file extension dynamically
extension = os.path.splitext(image_file)[1][1:]
encoded_string = ""

with open(image_file, "rb") as img_f:
encoded_string = base64.b64encode(img_f.read()).decode("utf-8")

extension = os.path.splitext(image_file)[1][1:] if not urlparse(image_file).scheme else image_file.split('.')[-1]

# Encode the image data to base64
encoded_string = base64.b64encode(image_data).decode("utf-8")

return f"data:image/{extension};base64,{encoded_string}"


def file_as_base64(file_path):
"""
Convert a file to base64.
Convert a file or URL to base64.
:param file_path: The complete path of the file.
:return: The base64 representation of the file.
:param file_path: The path to the file or URL of the file.
:return: Base64 representation of the file in the format 'data:application/{extension};base64,{encoded_string}'.
"""
if not os.path.isfile(file_path):
print(f"File not found: {file_path}")
return

# Determine if the input is a URL or a file path
if urlparse(file_path).scheme in ['http', 'https']:
# Handle file URL
try:
response = requests.get(file_path)
response.raise_for_status() # Raise an exception for HTTP errors
file_data = response.content
except requests.RequestException as e:
print(f"Error fetching file from URL: {e}")
return
else:
# Handle local file path
if not os.path.isfile(file_path):
print(f"File not found: {file_path}")
return
with open(file_path, "rb") as file:
file_data = file.read()

# Get the file extension dynamically
extension = os.path.splitext(file_path)[1][1:]
encoded_string = ""

with open(file_path, "rb") as file:
encoded_string = base64.b64encode(file.read()).decode("utf-8")

return f"data:application/{extension};base64,{encoded_string}"
extension = os.path.splitext(file_path)[1][1:] if not urlparse(file_path).scheme else file_path.split('.')[-1]

# Define MIME type based on extension
mime_types = {
'png': 'image/png',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'gif': 'image/gif',
'pdf': 'application/pdf',
'txt': 'text/plain',
'json': 'application/json',
'xml': 'application/xml'
}
mime_type = mime_types.get(extension, 'application/octet-stream')

# Encode the file data to base64
encoded_string = base64.b64encode(file_data).decode("utf-8")

return f"data:{mime_type};base64,{encoded_string}"


def get_client_ip(request):
Expand Down
4 changes: 2 additions & 2 deletions frontend/public/feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
till now.]]></description>
<link>https://nim23.com</link>
<generator>RSS for Node</generator>
<lastBuildDate>Wed, 28 Aug 2024 18:45:00 GMT</lastBuildDate>
<lastBuildDate>Wed, 28 Aug 2024 23:07:02 GMT</lastBuildDate>
<atom:link href="https://nim23.com/feed.xml" rel="self" type="application/rss+xml"/>
<pubDate>Wed, 28 Aug 2024 18:45:00 GMT</pubDate>
<pubDate>Wed, 28 Aug 2024 23:07:02 GMT</pubDate>
<copyright><![CDATA[All rights reserved 2024, Numan Ibn Mazid]]></copyright>
<language><![CDATA[en]]></language>
</channel>
Expand Down

0 comments on commit 371016f

Please sign in to comment.