class ChapterSerializer(ModelSerializer): """Serializer for chapters.""" full_title = CharField(source='__str__', read_only=True, help_text='The formatted title of the chapter.') views = IntegerField(min_value=0, read_only=True, help_text='The total views of the chapter.') series = SlugRelatedField(queryset=Series.objects.only('slug', 'title'), slug_field='slug', help_text='The series of the chapter.') groups = StringRelatedField( many=True, help_text='The scanlation groups of the chapter.') url = URLField(source='get_absolute_url', read_only=True, help_text='The absolute URL of the chapter.') def to_representation(self, instance: Chapter) -> Dict: rep = super().to_representation(instance) # HACK: adapt the date format based on a query param dt_format = self.context['request'] \ .query_params.get('date_format', 'iso-8601') published = instance.published rep['published'] = { 'iso-8601': published.strftime('%Y-%m-%dT%H:%M:%SZ'), 'rfc-5322': published.strftime('%a, %d %b %Y %H:%M:%S GMT'), 'timestamp': str(round(published.timestamp() * 1e3)) }.get(dt_format) return rep def __uri(self, path: str) -> str: return self.context['view'].request.build_absolute_uri(path) def _get_pages(self, obj: Chapter) -> List[str]: return [self.__uri(p.image.url) for p in obj.pages.iterator()] class Meta: model = Chapter fields = ('id', 'title', 'number', 'volume', 'published', 'views', 'final', 'series', 'groups', 'full_title', 'url', 'file') extra_kwargs = {'file': {'write_only': True}}
class SessionSerializer(serializers.HyperlinkedModelSerializer): id = serializers.CharField(source="slug") description = serializers.CharField(source="abstract") # TODO Jens: Replace 'event' and 'speakers' with proper nested relationships once their endpoints are done. event = StringRelatedField() speakers = NestedSpeakersSerializer(source="published_speakers", many=True, read_only=True) class Meta: model = Talk fields = ["id", "url", "title", "description", "speakers", "event"] lookup_field = "slug" extra_kwargs = {"url": {'lookup_field': 'slug'}} def to_representation(self, instance): ret = super().to_representation(instance) ret["actions"] = {"favourite": ret["url"] + "favourite/"} return ret
class BuildingPostSerializer(serializers.ModelSerializer): creator = StringRelatedField() class Meta: model = BuildingPost fields = ( 'building', 'creator', 'title', 'content', ) def update(self, instance, validated_data): if self.context['request'].user != instance.creator: raise PermissionDenied() new_instance = super(BuildingPostSerializer, self).update(instance, validated_data) BuildingPostHistory.objects.create(building_post=instance, building=instance.building, creator=instance.creator, title=instance.title, content=instance.content) return new_instance
class PensumSerializer(NestedHyperlinkedModelSerializer): class Meta: model = Pensum fields = [ 'url', # employee's data section: 'employee_url', 'first_name', 'last_name', 'employee', 'e_mail', 'pensum_group', # basic threshold section: 'basic_threshold', 'part_of_job_time', 'basic_threshold_factors_url', 'basic_threshold_factors', 'reduction_url', 'reduction', 'calculated_threshold', # contact hours section: 'min_for_contact_hours', 'amount_until_contact_hours_min', 'pensum_contact_hours', 'limit_for_contact_hours', 'amount_until_contact_hours_limit', 'limit_for_over_time_hours', 'amount_until_over_time_hours_limit', 'plans', # additional hours section: 'pensum_additional_hours', 'pensum_additional_hours_not_counted_into_limit', 'additional_hours_factors_url', 'additional_hours_factors', 'exams_additional_hours_url', 'exams_additional_hours', # hidden 'schedule' ] extra_kwargs = {'basic_threshold': {'min_value': 0}} parent_lookup_kwargs = {'schedule_slug': 'schedule__slug'} url = AdvNestedHyperlinkedIdentityField( view_name='pensums-detail', lookup_field=None, parent_lookup_kwargs={ **parent_lookup_kwargs, 'employee': 'employee__abbreviation' }) employee_url = AdvNestedHyperlinkedIdentityField( view_name='employees-detail', lookup_field=None, parent_lookup_kwargs={'abbreviation': 'employee__abbreviation'}) first_name = StringRelatedField(read_only=True, source='employee.first_name') last_name = StringRelatedField(read_only=True, source='employee.last_name') employee = SlugRelatedField(queryset=Employees.objects.all(), slug_field='abbreviation') e_mail = StringRelatedField(read_only=True, source='employee.e_mail') pensum_group = StringRelatedField(read_only=True, source='employee.pensum_group') part_of_job_time = StringRelatedField(read_only=True, source='employee.part_of_job_time') basic_threshold_factors_url = AdvNestedHyperlinkedIdentityField( view_name='pensum-factors-list', lookup_field=None, parent_lookup_kwargs={ **parent_lookup_kwargs, 'pensums_employee': 'employee__abbreviation' }) basic_threshold_factors = PensumBasicThresholdFactorSerializer( read_only=True, many=True) reduction_url = AdvNestedHyperlinkedIdentityField( view_name='pensum-reduction-detail', lookup_field=None, parent_lookup_kwargs={ **parent_lookup_kwargs, 'pensums_employee': 'employee__abbreviation' }) reduction = PensumReductionSerializer(read_only=True) plans = EmployeePlansSerializer(many=True, read_only=True) additional_hours_factors_url = AdvNestedHyperlinkedIdentityField( view_name='pensum-additional_hours_factors-list', lookup_field=None, parent_lookup_kwargs={ **parent_lookup_kwargs, 'pensums_employee': 'employee__abbreviation' }) additional_hours_factors = PensumAdditionalHoursFactorsSerializer( many=True, read_only=True) exams_additional_hours_url = AdvNestedHyperlinkedIdentityField( view_name='pensum-exams_additional_hours-list', lookup_field=None, parent_lookup_kwargs={ **parent_lookup_kwargs, 'pensums_employee': 'employee__abbreviation' }) exams_additional_hours = ExamsAdditionalHoursSerializer(many=True, read_only=True) schedule = ParentHiddenRelatedField( queryset=Schedules.objects.all(), parent_lookup_kwargs={'schedule_slug': 'slug'}, )
class EventSerializer(serializers.ModelSerializer): categories = StringRelatedField(many=True) class Meta: model = Event fields = '__all__'
class LegacyMembershipSerializer(ModelSerializer): class Meta: model = Membership user = StringRelatedField(read_only=True) team = StringRelatedField(read_only=True)
class CharacterSerializer(serializers.HyperlinkedModelSerializer): template = StringRelatedField() class Meta: model = Character fields = ('name', 'url', 'template', 'uuid', 'status', 'version')
class LegacyParticipationSerializer(ModelSerializer): class Meta: model = Participation participant = StringRelatedField(read_only=True) target = StringRelatedField(read_only=True)
class LegacyEventImageSerializer(ModelSerializer): class Meta: model = EventImage property = StringRelatedField(read_only=True) image = Base64ImageField(max_length=0, use_url=True)
class FishSerializer(serializers.ModelSerializer): type = StringRelatedField(many=False) class Meta: model = Fish fields = ['id', 'name', 'type', 'latin_name', 'size', 'price', 'description', 'stock']
class CityStringSerializer(serializers.ModelSerializer): region = StringRelatedField() class Meta: model = Station fields = ('title', 'region')
class LibrarySerializer(ModelSerializer): author = StringRelatedField(source='author.name') class Meta: model = Book fields = ['author', 'title', 'page_count']