# Path: src/moviesapp/views/mixins.py
# class VkAjaxView(AjaxView):
#     def dispatch(self, request, *args, **kwargs):
#         if not request.user.is_vk_user:
#             return self.no_permissions_fail(request)
#         return super().dispatch(request, *args, **kwargs)


import os
import tempfile
import requests
from requests_toolbelt import MultipartEncoder
from moviesapp.models import Record
from .mixins import VkAjaxView



class UploadPosterToWallView(VkAjaxView):
    tmp_file = None

    def _get_filepath(self, record_id):
        movie = Record.objects.get(pk=record_id).movie
        file_contents = requests.get(movie.poster_big).content
        # We need to make sure that file is not deleted that is why we use `self`.
        # TODO: remove pylint warning. Need to test this properly.
        self.tmp_file = tempfile.NamedTemporaryFile(suffix=".jpg")  # pylint: disable=consider-using-with
        self.tmp_file.write(file_contents)
        os.chmod(self.tmp_file.name, 0o666)  # nosec

    @staticmethod
    def _upload_file(url, filepath):
        # TODO: need to test this:
        with open(filepath, "rb") as f:
            data = MultipartEncoder(fields={"photo": (f.name, f, "image/jpg")})
        return requests.post(url, data=data, headers={"Content-Type": data.content_type}).text

    def post(self, request, **kwargs):
        try:
            record_id = int(kwargs["id"])
        except ValueError:
            return self.render_bad_request_response()
        try:
            url = request.POST["url"]
        except KeyError:

# Path: src/moviesapp/views/mixins.py
# class VkAjaxView(AjaxView):
#     def dispatch(self, request, *args, **kwargs):
#         if not request.user.is_vk_user:
#             return self.no_permissions_fail(request)
#         return super().dispatch(request, *args, **kwargs)


# Path: src/moviesapp/views/vk.py
import os
import tempfile
import requests
from requests_toolbelt import MultipartEncoder
from moviesapp.models import Record
from .mixins import VkAjaxView




class UploadPosterToWallView(VkAjaxView):
    tmp_file = None

    def _get_filepath(self, record_id):
        movie = Record.objects.get(pk=record_id).movie
        file_contents = requests.get(movie.poster_big).content
        # We need to make sure that file is not deleted that is why we use `self`.
        # TODO: remove pylint warning. Need to test this properly.
        self.tmp_file = tempfile.NamedTemporaryFile(suffix=".jpg")  # pylint: disable=consider-using-with
        self.tmp_file.write(file_contents)
        os.chmod(self.tmp_file.name, 0o666)  # nosec

    @staticmethod
    def _upload_file(url, filepath):
        # TODO: need to test this:
        with open(filepath, "rb") as f:
            data = MultipartEncoder(fields={"photo": (f.name, f, "image/jpg")})
        return requests.post(url, data=data, headers={"Content-Type": data.content_type}).text

    def post(self, request, **kwargs):
        try:
            record_id = int(kwargs["id"])
        except ValueError:
            return self.render_bad_request_response()
        try:
            url = request.POST["url"]
        except KeyError:

            return self.render_bad_request_response()