# Path: src/moviesapp/views/mixins.py
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# class TemplateView(LoginRequiredMixin, TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# 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


from datetime import datetime
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from moviesapp.models import ActionRecord
from .mixins import TemplateAnonymousView, TemplateView
from .utils import paginate





class FeedView(TemplateAnonymousView):
    template_name = "social/feed.html"

    def get_context_data(self, list_name):
        FEED_TITLE = {
            "people": _("People"),
            "friends": _("Friends"),
        }

        date_to = datetime.today()
        date_from = date_to - relativedelta(days=settings.FEED_DAYS)
        actions = ActionRecord.objects.filter(date__range=(date_from, date_to)).order_by("-pk")

        users = self.request.user.get_users(friends=list_name == "friends")
        actions = actions.filter(user__in=users)
        posters = [action.movie.poster_small for action in actions]

# Path: src/moviesapp/views/mixins.py
# class TemplateAnonymousView(TemplateViewOriginal):
#     pass
# 
# class TemplateView(LoginRequiredMixin, TemplateViewOriginal):
#     pass
# 
# Path: src/moviesapp/views/utils.py
# 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


# Path: src/moviesapp/views/social.py
from datetime import datetime
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from moviesapp.models import ActionRecord
from .mixins import TemplateAnonymousView, TemplateView
from .utils import paginate






class FeedView(TemplateAnonymousView):
    template_name = "social/feed.html"

    def get_context_data(self, list_name):
        FEED_TITLE = {
            "people": _("People"),
            "friends": _("Friends"),
        }

        date_to = datetime.today()
        date_from = date_to - relativedelta(days=settings.FEED_DAYS)
        actions = ActionRecord.objects.filter(date__range=(date_from, date_to)).order_by("-pk")

        users = self.request.user.get_users(friends=list_name == "friends")
        actions = actions.filter(user__in=users)
        posters = [action.movie.poster_small for action in actions]

        posters_2x = [action.movie.poster_normal for action in actions]