Translators as authors.
[wolnelektury.git] / src / catalogue / api / fields.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 from rest_framework import serializers
5 from api.fields import AbsoluteURLField
6 from catalogue.models import Book
7 from club.models import Membership
8
9
10 class BookLiked(serializers.ReadOnlyField):
11     def __init__(self, source='pk', **kwargs):
12         super(BookLiked, self).__init__(source=source, **kwargs)
13
14     def to_representation(self, value):
15         request = self.context['request']
16         if not hasattr(request, 'liked_books'):
17             if request.user.is_authenticated:
18                 request.liked_books = set(
19                     Book.tagged.with_any(request.user.tag_set.all()).values_list('id', flat=True)
20                 )
21             else:
22                 request.liked_books = None
23         if request.liked_books is not None:
24             return value in request.liked_books
25
26
27 class EmbargoURLField(AbsoluteURLField):
28     def to_representation(self, value):
29         request = self.context['request']
30         # FIXME: See #3955.
31         if True or Membership.is_active_for(request.user):
32             return super().to_representation(value)
33         else:
34             return None