# Path: src/moviesapp/views/mixins.py
# class AjaxAnonymousView(JsonRequestResponseMixin, View):
#     def success(self, **kwargs):
#         response = {"status": "success"}
#         response.update(kwargs)
#         return self.render_json_response(response)
# 
# class AjaxView(LoginRequiredMixin, AjaxAnonymousView):
#     raise_exception = True
# 
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# class TemplateView(LoginRequiredMixin, TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# def add_movie_to_list(movie_id, list_id, user):
#     record = user.get_records().filter(movie_id=movie_id)
#     if record.exists():
#         record = record[0]
#         if record.list_id != list_id:
#             ActionRecord(action_id=2, user=user, movie_id=movie_id, list_id=list_id).save()
#             record.list_id = list_id
#             record.date = datetime.today()
#             record.save()
#     else:
#         record = Record(movie_id=movie_id, list_id=list_id, user=user)
#         record.save()
#         ActionRecord(action_id=1, user=user, movie_id=movie_id, list_id=list_id).save()
# 
# def get_anothers_account(username):
#     if username:
#         return get_object_or_404(User, username=username)
#     return False
# 
# def get_records(list_name, user, anothers_account):
#     """Get records for certain user and list."""
#     if anothers_account:
#         user = anothers_account
#     return user.get_records().filter(list__key_name=list_name).select_related("movie")
# 
# def paginate(objects, page, objects_on_page):
#     paginator = Paginator(objects, objects_on_page)
#     try:
#         objects = paginator.page(page)
#     except PageNotAnInteger:
#         # If page is not an integer, deliver first page.
#         objects = paginator.page(1)
#     except EmptyPage:
#         # If page is out of range (e.g. 9999), deliver last page of results.
#         objects = paginator.page(paginator.num_pages)
#     return objects
# 
# def sort_by_rating(records, username, list_name):
#     if not username and list_name == "to-watch":
#         # Sorting is changing here because there is no user rating yet.
#         return records.order_by("-movie__imdb_rating", "-movie__release_date")
#     return records.order_by("-rating", "-movie__release_date")


import json
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.http import Http404
from moviesapp.models import Action, ActionRecord, List, Record, User
from .mixins import AjaxAnonymousView, AjaxView, TemplateAnonymousView, TemplateView
from .utils import add_movie_to_list, get_anothers_account, get_records, paginate, sort_by_rating
        movie_ids_and_list_ids = (
            self.request.user.get_records().filter(movie_id__in=movies).values_list("movie_id", "list_id")
        )
        movie_id_and_list_id_dict = {}
        for movie_id_and_list_id in movie_ids_and_list_ids:
            movie_id_and_list_id_dict[movie_id_and_list_id[0]] = movie_id_and_list_id[1]

        list_data = {}
        for record_id, value in record_ids_and_movies_dict.items():
            list_data[record_id] = movie_id_and_list_id_dict.get(value, 0)
        return list_data

    def _initialize_session_values(self):
        session = self.request.session
        if "sort" not in session:
            session["sort"] = "addition_date"
        if "recommendation" not in session:
            session["recommendation"] = False
        if "mode" not in session:
            session["mode"] = "full"
        self.session = session

    def get_context_data(self, list_name, username=None):
        if username is None and self.request.user.is_anonymous:
            raise Http404
        anothers_account = get_anothers_account(username)
        if anothers_account:
            if User.objects.get(username=username) not in self.request.user.get_users():
                raise PermissionDenied


# Path: src/moviesapp/views/mixins.py
# class AjaxAnonymousView(JsonRequestResponseMixin, View):
#     def success(self, **kwargs):
#         response = {"status": "success"}
#         response.update(kwargs)
#         return self.render_json_response(response)
# 
# class AjaxView(LoginRequiredMixin, AjaxAnonymousView):
#     raise_exception = True
# 
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# class TemplateView(LoginRequiredMixin, TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# def add_movie_to_list(movie_id, list_id, user):
#     record = user.get_records().filter(movie_id=movie_id)
#     if record.exists():
#         record = record[0]
#         if record.list_id != list_id:
#             ActionRecord(action_id=2, user=user, movie_id=movie_id, list_id=list_id).save()
#             record.list_id = list_id
#             record.date = datetime.today()
#             record.save()
#     else:
#         record = Record(movie_id=movie_id, list_id=list_id, user=user)
#         record.save()
#         ActionRecord(action_id=1, user=user, movie_id=movie_id, list_id=list_id).save()
# 
# def get_anothers_account(username):
#     if username:
#         return get_object_or_404(User, username=username)
#     return False
# 
# def get_records(list_name, user, anothers_account):
#     """Get records for certain user and list."""
#     if anothers_account:
#         user = anothers_account
#     return user.get_records().filter(list__key_name=list_name).select_related("movie")
# 
# def paginate(objects, page, objects_on_page):
#     paginator = Paginator(objects, objects_on_page)
#     try:
#         objects = paginator.page(page)
#     except PageNotAnInteger:
#         # If page is not an integer, deliver first page.
#         objects = paginator.page(1)
#     except EmptyPage:
#         # If page is out of range (e.g. 9999), deliver last page of results.
#         objects = paginator.page(paginator.num_pages)
#     return objects
# 
# def sort_by_rating(records, username, list_name):
#     if not username and list_name == "to-watch":
#         # Sorting is changing here because there is no user rating yet.
#         return records.order_by("-movie__imdb_rating", "-movie__release_date")
#     return records.order_by("-rating", "-movie__release_date")


# Path: src/moviesapp/views/list.py
import json
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from django.http import Http404
from moviesapp.models import Action, ActionRecord, List, Record, User
from .mixins import AjaxAnonymousView, AjaxView, TemplateAnonymousView, TemplateView
from .utils import add_movie_to_list, get_anothers_account, get_records, paginate, sort_by_rating

        movie_ids_and_list_ids = (
            self.request.user.get_records().filter(movie_id__in=movies).values_list("movie_id", "list_id")
        )
        movie_id_and_list_id_dict = {}
        for movie_id_and_list_id in movie_ids_and_list_ids:
            movie_id_and_list_id_dict[movie_id_and_list_id[0]] = movie_id_and_list_id[1]

        list_data = {}
        for record_id, value in record_ids_and_movies_dict.items():
            list_data[record_id] = movie_id_and_list_id_dict.get(value, 0)
        return list_data

    def _initialize_session_values(self):
        session = self.request.session
        if "sort" not in session:
            session["sort"] = "addition_date"
        if "recommendation" not in session:
            session["recommendation"] = False
        if "mode" not in session:
            session["mode"] = "full"
        self.session = session

    def get_context_data(self, list_name, username=None):
        if username is None and self.request.user.is_anonymous:
            raise Http404
        anothers_account = get_anothers_account(username)
        if anothers_account:
            if User.objects.get(username=username) not in self.request.user.get_users():
                raise PermissionDenied


        records = get_records(list_name, self.request.user, anothers_account)