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