class CompanyImageUploadVi ew APIView parser_classes FileUploadParser

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class CompanyImageUploadView(APIView):
parser_classes = (FileUploadParser, MultiPartParser)
permission_classes = (IsAuthenticated,)
max_size = 5000000
def post(self, request):
file_obj = request.FILES['file']
if file_obj.size > self.max_size:
return Response(data={'detail': 'The size of the uploaded file cannot be more than 5MB'}, status=status.HTTP_400_BAD_REQUEST)
file_path = os.path.join(settings.MEDIA_ROOT, 'company_office', file_obj.name)
file_url = "company_office/%s" % file_obj.name
absolute_url = get_thumbnail(file_url, "92x92", crop='center').url
with open(file_path, "wb+") as fl:
for ch in file_obj.chunks():
fl.write(ch)
return Response({'thumbnail_url': absolute_url, 'name': file_obj.name, 'image': file_url},
status=status.HTTP_201_CREATED)