Skip to content

Instantly share code, notes, and snippets.

@jagamts1
Created January 11, 2018 08:54
Show Gist options
  • Save jagamts1/ce26310665198ca55104825ef4866bc0 to your computer and use it in GitHub Desktop.
Save jagamts1/ce26310665198ca55104825ef4866bc0 to your computer and use it in GitHub Desktop.
sqlalchmey image upload method for django project
@permission_classes((IsAuthenticated,))
@authentication_classes([JSONWebTokenAuthentication, ])
class ImageUploadAPI(APIView):
def post(self, request):
serializer = ImageSerilaizer(data=request.data)
if serializer.is_valid():
try:
company = UserCompany.objects.get(user=request.user).company
db_name = company.db_name
except UserCompany.DoesNotExist:
return Response({"data": null, "message": "user company does not exist", "requestStatus": 0}, status=status.HTTP_400_BAD_REQUEST)
# print(request.data)
directory = '/tmp/%s/' % db_name
up_file = request.data["user_image"]
image_name = up_file.name
# if open(directory + up_file.name, 'r'):
# image_name = up_file.name
if not os.path.exists(directory):
os.makedirs(directory)
file_path = directory + image_name
destination = open(file_path, 'wb+')
for chunk in up_file.chunks():
print
destination.write(chunk)
destination.close()
# set path to one coloum of sqlalchmey table
#img=file_path
return Response({"data": null, "message": "List of debit notes ", "requestStatus": 1}, status=status.HTTP_200_OK)
return Response({"data": serializer.errors, "message": "no data ", "requestStatus": 2}, status=status.HTTP_400_BAD_REQUEST)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment