# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later from hashlib import md5 import json from django.conf import settings from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.contenttypes.models import ContentType from django.core.exceptions import PermissionDenied from django.db import transaction from django.db.models import F, Q from django.http import HttpResponse from django.urls import reverse_lazy from django.utils import timezone from django.utils.decorators import method_decorator from django.utils.translation import gettext_lazy as _ from django.views import View from django.views.decorators.cache import cache_page from django.views.generic import DetailView, TemplateView, UpdateView from django.views.generic.list import ListView from django_tables2.views import MultiTableMixin, SingleTableMixin, SingleTableView from api.viewsets import is_regex from note.models import Alias, NoteSpecial, NoteUser from permission.backends import PermissionBackend from permission.views import ProtectQuerysetMixin, ProtectedCreateView from .models import Wrapped from .tables import WrappedTable class WrappedListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): """ Display all Wrapped, and classify by year """ model = Wrapped table_class = WrappedTable template_name = 'wrapped/wrapped_list.html' extra_context = {'title': _("List of wrapped")} def get_queryset(self, **kwargs): return super().get_queryset(**kwargs).distinct() def get_table_data(self): return Wrapped.objects.filter(PermissionBackend.filter_queryset( self.request, Wrapped, "change", field='public')).distinct().order_by("-bde__date_start") def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) W = self.object_list.filter(note__noteclub__club__pk__gte=-1, public=False) if W: context['club_not_public'] = 'true' else: context['club_not_public'] = 'false' return context class WrappedDetailView(ProtectQuerysetMixin, DetailView): """ View a wrapped """ model = Wrapped template_name = 'wrapped/0/wrapped_view.html' #by default def get(self, *args, **kwargs): bde_id = Wrapped.objects.get(pk=kwargs['pk']).bde.id note_type = 'user' if 'user' in Wrapped.objects.get(pk=kwargs['pk']).note.__dir__() else 'club' self.template_name = 'wrapped/' + str(bde_id) + '/wrapped_view_' + note_type + '.html' return super().get(*args, **kwargs) def get_context_data(self, **kwargs): context = super().get_context_data( **kwargs) d = json.loads(self.object.data_json) for key in d: context[key] = d[key] context['title'] = str(self.object) return context