mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-15 18:07:02 +00:00
added description for api, changes in upload image and file to alibaba method
This commit is contained in:
@ -43,18 +43,41 @@ def upload_image_to_alibaba(image, filename):
|
||||
# Save the image object to a temporary file
|
||||
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||
"""write image in temporary file"""
|
||||
if type(image) == bytes:
|
||||
temp_file.write(image)
|
||||
else:
|
||||
temp_file.write(image.read())
|
||||
"""auth of bucket"""
|
||||
auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET)
|
||||
"""fetch bucket details"""
|
||||
bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME)
|
||||
# Upload the temporary file to Alibaba OSS
|
||||
bucket.put_object_from_file(filename, temp_file.name)
|
||||
"""create perfect url for image"""
|
||||
new_filename = filename.replace(' ', '%20')
|
||||
temp_file.write(image.read())
|
||||
return upload_file_to_alibaba(temp_file, filename)
|
||||
|
||||
|
||||
def upload_base64_image_to_alibaba(image, filename):
|
||||
"""
|
||||
upload image on oss alibaba bucket
|
||||
"""
|
||||
# Save the image object to a temporary file
|
||||
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||
# write image in temporary file
|
||||
temp_file.write(image)
|
||||
return upload_file_to_alibaba(temp_file, filename)
|
||||
|
||||
|
||||
def upload_excel_file_to_alibaba(response, filename):
|
||||
"""
|
||||
upload excel file on oss alibaba bucket
|
||||
"""
|
||||
# Save the image object to a temporary file
|
||||
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||
# write image in temporary file
|
||||
temp_file.write(response.content)
|
||||
return upload_file_to_alibaba(temp_file, filename)
|
||||
|
||||
|
||||
def upload_file_to_alibaba(temp_file, filename):
|
||||
"""auth of bucket"""
|
||||
auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET)
|
||||
"""fetch bucket details"""
|
||||
bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME)
|
||||
# Upload the temporary file to Alibaba OSS
|
||||
bucket.put_object_from_file(filename, temp_file.name)
|
||||
"""create perfect url for image"""
|
||||
new_filename = filename.replace(' ', '%20')
|
||||
return f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
||||
|
||||
|
||||
|
@ -56,10 +56,10 @@ class NotificationViewSet(viewsets.GenericViewSet):
|
||||
to send test notification
|
||||
:return:
|
||||
"""
|
||||
send_notification_to_guardian(TEST_NOTIFICATION, None, request.auth.payload['user_id'],
|
||||
{'task_id': None})
|
||||
send_notification_to_junior(TEST_NOTIFICATION, request.auth.payload['user_id'], None,
|
||||
{'task_id': None})
|
||||
send_notification_to_guardian.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'],
|
||||
{'task_id': None})
|
||||
send_notification_to_junior.delay(TEST_NOTIFICATION, None, request.auth.payload['user_id'],
|
||||
{'task_id': None})
|
||||
return custom_response(SUCCESS_CODE["3000"])
|
||||
|
||||
@action(methods=['get'], detail=False, url_path='list', url_name='list',
|
||||
|
@ -4,7 +4,7 @@ web_utils file
|
||||
import base64
|
||||
|
||||
from base.constants import ARTICLE_CARD_IMAGE_FOLDER
|
||||
from guardian.utils import upload_image_to_alibaba
|
||||
from guardian.utils import upload_image_to_alibaba, upload_base64_image_to_alibaba
|
||||
|
||||
|
||||
def pop_id(data):
|
||||
@ -32,7 +32,7 @@ def get_image_url(data):
|
||||
image_name = data.pop('image_name') if 'image_name' in data else f"{data['title']}.jpg"
|
||||
filename = f"{ARTICLE_CARD_IMAGE_FOLDER}/{image_name}"
|
||||
# upload image on ali baba
|
||||
image_url = upload_image_to_alibaba(base64_image, filename)
|
||||
image_url = upload_base64_image_to_alibaba(base64_image, filename)
|
||||
return image_url
|
||||
elif 'image' in data and data['image'] is not None:
|
||||
image = data.pop('image')
|
||||
|
@ -28,6 +28,7 @@ from django.http import HttpResponse
|
||||
from account.utils import custom_response
|
||||
from base.constants import PENDING, IN_PROGRESS, REJECTED, REQUESTED, COMPLETED, EXPIRED, DATE_FORMAT, TASK_STATUS
|
||||
from guardian.models import JuniorTask
|
||||
from guardian.utils import upload_excel_file_to_alibaba
|
||||
from junior.models import JuniorPoints
|
||||
from web_admin.pagination import CustomPageNumberPagination
|
||||
from web_admin.permission import AdminPermission
|
||||
@ -251,18 +252,6 @@ class AnalyticsViewSet(GenericViewSet):
|
||||
buffer.close()
|
||||
|
||||
filename = f"{'analytics'}/{'ZOD_Bank_Analytics.xlsx'}"
|
||||
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
||||
"""write image in temporary file"""
|
||||
temp_file.write(response.content)
|
||||
"""auth of bucket"""
|
||||
auth = oss2.Auth(settings.ALIYUN_OSS_ACCESS_KEY_ID, settings.ALIYUN_OSS_ACCESS_KEY_SECRET)
|
||||
"""fetch bucket details"""
|
||||
bucket = oss2.Bucket(auth, settings.ALIYUN_OSS_ENDPOINT, settings.ALIYUN_OSS_BUCKET_NAME)
|
||||
# Upload the temporary file to Alibaba OSS
|
||||
bucket.put_object_from_file(filename, temp_file.name)
|
||||
"""create perfect url for image"""
|
||||
new_filename = filename.replace(' ', '%20')
|
||||
link = f"https://{settings.ALIYUN_OSS_BUCKET_NAME}.{settings.ALIYUN_OSS_ENDPOINT}/{new_filename}"
|
||||
print(link)
|
||||
return custom_response(None, link)
|
||||
file_link = upload_excel_file_to_alibaba(response, filename)
|
||||
return custom_response(None, file_link)
|
||||
|
||||
|
@ -44,9 +44,20 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
article create api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:param request: { "title": "string", "description": "string",
|
||||
"article_cards": [
|
||||
{ "title": "string",
|
||||
"description": "string",
|
||||
"image_name": "string",
|
||||
"image_url": "string"
|
||||
} ],
|
||||
"article_survey": [
|
||||
{ "question": "string",
|
||||
"options": [
|
||||
{ "option": "string",
|
||||
"is_answer": true }
|
||||
] }
|
||||
] }
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
@ -57,9 +68,24 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
def update(self, request, *args, **kwargs):
|
||||
"""
|
||||
article update api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:param request: article_id,
|
||||
{ "title": "string", "description": "string",
|
||||
"article_cards": [
|
||||
{ "id": 0,
|
||||
"title": "string",
|
||||
"description": "string",
|
||||
"image_name": "string",
|
||||
"image_url": "string"
|
||||
} ],
|
||||
"article_survey": [
|
||||
{ "id": 0,
|
||||
"question": "string",
|
||||
"options": [
|
||||
{ "id": 0,
|
||||
"option": "string",
|
||||
"is_answer": true
|
||||
} ]
|
||||
} ] }
|
||||
:return: success message
|
||||
"""
|
||||
article = self.get_object()
|
||||
@ -72,8 +98,6 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
"""
|
||||
article list api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:return: list of article
|
||||
"""
|
||||
queryset = self.get_queryset()
|
||||
@ -86,9 +110,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
"""
|
||||
article detail api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:param request: article_id
|
||||
:return: article detail data
|
||||
"""
|
||||
queryset = self.get_object()
|
||||
@ -98,9 +120,7 @@ class ArticleViewSet(GenericViewSet, mixins.CreateModelMixin, mixins.UpdateModel
|
||||
def destroy(self, request, *args, **kwargs):
|
||||
"""
|
||||
article delete (soft delete) api method
|
||||
:param request:
|
||||
:param args:
|
||||
:param kwargs:
|
||||
:param request: article_id
|
||||
:return: success message
|
||||
"""
|
||||
article = self.get_object()
|
||||
@ -177,7 +197,10 @@ class DefaultArticleCardImagesViewSet(GenericViewSet, mixins.CreateModelMixin, m
|
||||
def create(self, request, *args, **kwargs):
|
||||
"""
|
||||
api method to upload default article card images
|
||||
:param request:
|
||||
:param request: {
|
||||
"image_name": "string",
|
||||
"image": "image_file"
|
||||
}
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
|
@ -27,6 +27,7 @@ class ForgotAndResetPasswordViewSet(GenericViewSet):
|
||||
def admin_otp(self, request):
|
||||
"""
|
||||
api method to send otp
|
||||
:param request: {"email": "string"}
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
@ -40,6 +41,7 @@ class ForgotAndResetPasswordViewSet(GenericViewSet):
|
||||
def admin_verify_otp(self, request):
|
||||
"""
|
||||
api method to verify otp
|
||||
:param request: {"email": "string", "otp": "otp"}
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
@ -52,6 +54,7 @@ class ForgotAndResetPasswordViewSet(GenericViewSet):
|
||||
def admin_create_password(self, request):
|
||||
"""
|
||||
api method to create new password
|
||||
:param request: {"email": "string", "new_password": "string", "confirm_password": "string"}
|
||||
:return: success message
|
||||
"""
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
|
Reference in New Issue
Block a user