more polish

This commit is contained in:
Barunes Padhy 2024-06-14 19:39:02 +03:00
parent d8292de446
commit 60beb4d387
3 changed files with 31 additions and 6 deletions

View File

@ -7,6 +7,8 @@ from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser, FormParser from rest_framework.parsers import MultiPartParser, FormParser
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
from django.conf import settings from django.conf import settings
from PIL import Image
import io
from .serializers import ( from .serializers import (
MediaSerializer MediaSerializer
@ -15,6 +17,23 @@ from .serializers import (
class MediaUpload(APIView): class MediaUpload(APIView):
parser_classes = (MultiPartParser, FormParser) parser_classes = (MultiPartParser, FormParser)
def reduce_image_quality(self, image, quality=20):
try:
img = Image.open(image)
img_io = io.BytesIO()
# Handle different image modes
if img.mode in ("RGBA", "P"):
img = img.convert("RGB")
img_format = img.format if img.format else 'JPEG'
img.save(img_io, format=img_format, quality=quality)
img_io.seek(0)
return img_io
except Exception as e:
print(f"Error reducing image quality: {e}")
return image
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
file_serializer = MediaSerializer(data=request.data) file_serializer = MediaSerializer(data=request.data)
if file_serializer.is_valid(): if file_serializer.is_valid():
@ -29,12 +48,20 @@ class MediaUpload(APIView):
file_path = f"{file_path_base}/{resource_type}/{resource_id}/media/{file_unique_slug+resource_id+f.name}" file_path = f"{file_path_base}/{resource_type}/{resource_id}/media/{file_unique_slug+resource_id+f.name}"
else: else:
file_path = f"{file_path_base}/{resource_type}/media/{file_unique_slug+resource_id+f.name}" file_path = f"{file_path_base}/{resource_type}/media/{file_unique_slug+resource_id+f.name}"
# Reduce image quality if the file is an image
if f.content_type.startswith('image'):
reduced_image = self.reduce_image_quality(f, quality=65)
default_storage.save(file_path, reduced_image)
else:
# Save non-image files directly
default_storage.save(file_path, f) default_storage.save(file_path, f)
return Response(file_serializer.data, status=status.HTTP_201_CREATED) return Response(file_serializer.data, status=status.HTTP_201_CREATED)
else: else:
return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ListMedia(APIView): class ListMedia(APIView):
def get(self, request, resource_type, resource_id, format=None): def get(self, request, resource_type, resource_id, format=None):
if resource_id != resource_type: if resource_id != resource_type:

View File

@ -50,9 +50,10 @@ def github_deploy():
git_commands = {} git_commands = {}
git_commands["git_init"] = ['git', 'init'] git_commands["git_init"] = ['git', 'init']
git_commands["git_add"] = ['git', 'add', '.'] git_commands["git_add"] = ['git', 'add', '.']
git_commands["git_pull"] = ['git', 'pull']
git_commands["git_config_email"] = ['git', 'config', '--local', 'user.email'] git_commands["git_config_email"] = ['git', 'config', '--local', 'user.email']
git_commands["git_config_name"] = ['git', 'config', '--local', 'user.name'] git_commands["git_config_name"] = ['git', 'config', '--local', 'user.name']
git_commands["git_commit"] = ['git', 'commit', '-m', '"Update website"'] git_commands["git_commit"] = ['git', 'commit', '-m', 'Update website']
git_commands["git_branch"] = ['git', 'branch', '-m', 'main'] git_commands["git_branch"] = ['git', 'branch', '-m', 'main']
git_commands["git_add_url"] = ['git', 'remote', 'add', 'origin'] git_commands["git_add_url"] = ['git', 'remote', 'add', 'origin']
git_commands["git_push"] = ['git', 'push', '-u', 'origin', 'main'] git_commands["git_push"] = ['git', 'push', '-u', 'origin', 'main']
@ -60,9 +61,6 @@ def github_deploy():
data_location = f'{settings.BASE_DIR}/deploy/' data_location = f'{settings.BASE_DIR}/deploy/'
deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages' deploy_location = settings.DEPLOY_CONFIG["DEPLOY_LOCATION"]+'/ghpages'
run_gh_build = ['npm', 'run', 'build:ghpages']
subprocess.run(run_gh_build, cwd=settings.DEPLOY_CONFIG["DEPLOY_LOCATION"].replace('/dist', ''), check=True, text=True, capture_output=True)
create_404_page(deploy_location) create_404_page(deploy_location)
copyData(data_location, deploy_location) copyData(data_location, deploy_location)

View File

@ -43,7 +43,7 @@ function HomePage(props) {
</Row> </Row>
<Row className={`mb-5 mt-2 ${ThemeConfig[GlobalTheme].textColor}`}> <Row className={`mb-5 mt-2 ${ThemeConfig[GlobalTheme].textColor}`}>
<Col xs='3' className='d-none d-md-block'></Col> <Col xs='3' className='d-none d-md-block'></Col>
<Col className='p-4 blogContent'> <Col className='p-2 blogContent'>
<div className={`blogContent ${ThemeConfig[GlobalTheme].textColor}`}>{introContent}</div> <div className={`blogContent ${ThemeConfig[GlobalTheme].textColor}`}>{introContent}</div>
</Col> </Col>
<Col xs='3' className='d-none d-md-block'></Col> <Col xs='3' className='d-none d-md-block'></Col>