1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.apps import apps
5 from django.db.models import Prefetch
6 from django.http import Http404
7 from django.urls import reverse
8 from django.utils.formats import localize_input
9 from django.contrib.auth.decorators import login_required
10 from django.contrib.auth.models import User
11 from django.shortcuts import get_object_or_404, redirect
12 from django.views.decorators.http import require_POST
13 from django.views.generic import DetailView, TemplateView
16 import documents.models
17 from rest_framework.generics import ListAPIView
18 from rest_framework.filters import SearchFilter
19 from rest_framework.permissions import IsAdminUser
20 from rest_framework.response import Response
21 from rest_framework.views import APIView
22 from rest_framework import serializers
25 class CatalogueView(TemplateView):
26 template_name = "catalogue/catalogue.html"
28 def get_context_data(self):
29 ctx = super().get_context_data()
30 ctx["authors"] = models.Author.objects.all().prefetch_related('book_set__book_set', 'translated_book_set__book_set')
35 class AuthorView(TemplateView):
37 template_name = "catalogue/author_detail.html"
39 def get_context_data(self, slug):
40 ctx = super().get_context_data()
41 authors = models.Author.objects.filter(slug=slug).prefetch_related(
43 Prefetch("translated_book_set"),
45 ctx["author"] = authors.first()
49 class BookView(DetailView):
53 class TermSearchFilter(SearchFilter):
57 class Terms(ListAPIView):
58 filter_backends = [TermSearchFilter]
59 search_fields = ['name']
61 class serializer_class(serializers.Serializer):
62 label = serializers.CharField(source='name')
65 class EpochTerms(Terms):
66 queryset = models.Epoch.objects.all()
67 class KindTerms(Terms):
68 queryset = models.Kind.objects.all()
69 class GenreTerms(Terms):
70 queryset = models.Genre.objects.all()
72 class AuthorTerms(Terms):
73 search_fields = ['first_name', 'last_name']
74 queryset = models.Author.objects.all()
76 class EditorTerms(Terms):
77 search_fields = ['first_name', 'last_name', 'username']
78 queryset = User.objects.all()
80 class serializer_class(serializers.Serializer):
81 label = serializers.SerializerMethodField()
83 def get_label(self, obj):
84 return f'{obj.last_name}, {obj.first_name}'
86 class BookTitleTerms(Terms):
87 queryset = models.Book.objects.all()
88 search_fields = ['title', 'slug']
90 class serializer_class(serializers.Serializer):
91 label = serializers.CharField(source='title')
93 class WLURITerms(Terms):
94 queryset = models.Book.objects.all()
95 search_fields = ['title', 'slug']
97 class serializer_class(serializers.Serializer):
98 label = serializers.CharField(source='wluri')
101 class WikidataView(APIView):
102 permission_classes = [IsAdminUser]
104 def get_object(self, model, qid, save):
106 Model = apps.get_model('catalogue', model)
109 if not issubclass(Model, models.WikidataModel):
112 obj = Model.objects.filter(wikidata=qid).first()
114 obj = Model(wikidata=qid)
115 if not obj.pk and save:
118 obj.wikidata_populate(save=False)
122 for attname in dir(Model.Wikidata):
123 if attname.startswith("_"):
125 for fieldname, lang in obj.wikidata_fields_for_attribute(attname):
126 d[fieldname] = getattr(obj, fieldname)
128 if isinstance(d[fieldname], models.WikidataModel):
130 "model": type(d[fieldname])._meta.model_name,
131 "wd": d[fieldname].wikidata,
132 "label": str(d[fieldname]) or d[fieldname]._wikidata_label,
134 elif hasattr(d[fieldname], 'all'):
136 {"model": type(item)._meta.model_name,
138 "label": str(item) or item._wikidata_label
139 } for item in d[attname].all()
142 d[fieldname] = localize_input(d[fieldname])
145 def get(self, request, model, qid):
146 return self.get_object(model, qid, save=False)
148 def post(self, request, model, qid):
149 return self.get_object(model, qid, save=True)
154 def publish_author(request, pk):
155 author = get_object_or_404(models.Author, pk=pk)
157 "description_pl": author.generate_description(),
159 apiclient.api_call(request.user, f"authors/{author.slug}/", data)
160 return redirect(reverse('admin:catalogue_author_change', args=[author.pk]))