from django.contrib.auth import get_user_model from baserow_premium.license.models import License from drf_spectacular.openapi import OpenApiTypes from drf_spectacular.utils import extend_schema_field from rest_framework import serializers User = get_user_model() class LicenseSerializer(serializers.ModelSerializer): license_id = serializers.CharField(help_text="Unique identifier of the license.") is_active = serializers.BooleanField( help_text="Indicates if the backend deems the license valid." ) valid_from = serializers.DateTimeField( help_text="From which timestamp the license becomes active." ) valid_through = serializers.DateTimeField( help_text="Until which timestamp the license is active." ) free_users_count = serializers.SerializerMethodField( help_text="The amount of free users that are currently using the license." ) seats_taken = serializers.SerializerMethodField( help_text="The amount of users that are currently using the license." ) seats = serializers.IntegerField( help_text="The maximum amount of users that can use the license." ) product_code = serializers.CharField( help_text="The product code that indicates what the license unlocks." ) issued_on = serializers.DateTimeField( help_text="The date when the license was issued. It could be that a new " "license is issued with the same `license_id` because it was updated. In that " "case, the one that has been issued last should be used." ) issued_to_email = serializers.EmailField( help_text="Indicates to which email address the license has been issued." ) issued_to_name = serializers.CharField( help_text="Indicates to whom the license has been issued." ) class Meta: model = License fields = ( "id", "license_id", "is_active", "last_check", "valid_from", "valid_through", "free_users_count", "seats_taken", "seats", "product_code", "issued_on", "issued_to_email", "issued_to_name", ) @extend_schema_field(OpenApiTypes.INT) def get_seats_taken(self, obj): return obj.license_type.get_seats_taken(obj) @extend_schema_field(OpenApiTypes.INT) def get_free_users_count(self, obj): return obj.license_type.get_free_users_count(obj) class RegisterLicenseSerializer(serializers.Serializer): license = serializers.CharField(help_text="The license that you want to register.") class LicenseUserSerializer(serializers.ModelSerializer): class Meta: model = User fields = ("id", "first_name", "email") class LicenseWithUsersSerializer(LicenseSerializer): users = serializers.SerializerMethodField() class Meta(LicenseSerializer.Meta): fields = LicenseSerializer.Meta.fields + ("users",) @extend_schema_field(LicenseUserSerializer(many=True)) def get_users(self, object): users = [user.user for user in object.users.all()] return LicenseUserSerializer(users, many=True).data class LicenseUserLookupSerializer(serializers.ModelSerializer): value = serializers.SerializerMethodField( help_text="The name and the email " "address of the user." ) class Meta: model = User fields = ("id", "value") @extend_schema_field(OpenApiTypes.STR) def get_value(self, object): return f"{object.first_name} ({object.email})"