コード例 #1
0
ファイル: serializers.py プロジェクト: Kelvin-Chu/cravus
 def validate_avatar(self, avatar):
     error = 'Unable to read image, try a different image'
     request = self.context.get('request')
     if request and hasattr(request, 'data'):
         if 'crop' in request.data:
             crop = request.data['crop']
             error = check_img(avatar)
             if not error:
                 crop_image = crop_img(avatar, crop)
                 if isinstance(crop_image, str):
                     error = crop_image
                 else:
                     return crop_image
     raise ValidationError(error)
コード例 #2
0
ファイル: views.py プロジェクト: Kelvin-Chu/cravus
 def image(self, request, username):
     error = "Unable to read image, try a different image"
     if "upload" in request.data and "crop" in request.data:
         upload = request.data["upload"]
         crop = request.data["crop"]
         error = check_img(upload)
         if not error:
             crop_image = crop_img(upload, crop)
             if isinstance(crop_image, str):
                 error = crop_image
             else:
                 account = self.get_object()
                 account.avatar.delete()
                 account.avatar.save(upload.name, crop_image)
                 return Response({"upload": [account.avatar.url]}, status=status.HTTP_201_CREATED)
     return Response({"upload": [error]}, status=status.HTTP_400_BAD_REQUEST)