64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
|
|
from rest_framework import serializers
|
||
|
|
from .models import Region, ModeratorApplication, ModeratorPermission
|
||
|
|
from users.serializers import UserSerializer
|
||
|
|
|
||
|
|
|
||
|
|
class RegionSerializer(serializers.ModelSerializer):
|
||
|
|
parent = serializers.PrimaryKeyRelatedField(read_only=True)
|
||
|
|
children_count = serializers.SerializerMethodField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = Region
|
||
|
|
fields = [
|
||
|
|
'id', 'name', 'level', 'parent', 'code', 'description',
|
||
|
|
'is_active', 'created_at', 'children_count'
|
||
|
|
]
|
||
|
|
read_only_fields = ['created_at']
|
||
|
|
|
||
|
|
def get_children_count(self, obj):
|
||
|
|
return obj.children.count()
|
||
|
|
|
||
|
|
|
||
|
|
class RegionDetailSerializer(serializers.ModelSerializer):
|
||
|
|
parent = RegionSerializer(read_only=True)
|
||
|
|
children = RegionSerializer(many=True, read_only=True)
|
||
|
|
articles_count = serializers.SerializerMethodField()
|
||
|
|
services_count = serializers.SerializerMethodField()
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = Region
|
||
|
|
fields = [
|
||
|
|
'id', 'name', 'level', 'parent', 'children', 'code', 'description',
|
||
|
|
'is_active', 'created_at', 'articles_count', 'services_count'
|
||
|
|
]
|
||
|
|
|
||
|
|
def get_articles_count(self, obj):
|
||
|
|
return obj.articles.filter(publish_status='published').count()
|
||
|
|
|
||
|
|
def get_services_count(self, obj):
|
||
|
|
return obj.featured_services.filter(publish_status='published').count()
|
||
|
|
|
||
|
|
|
||
|
|
class ModeratorApplicationSerializer(serializers.ModelSerializer):
|
||
|
|
applicant = UserSerializer(read_only=True)
|
||
|
|
region = RegionSerializer(read_only=True)
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = ModeratorApplication
|
||
|
|
fields = [
|
||
|
|
'id', 'applicant', 'region', 'application_reason',
|
||
|
|
'support_count', 'required_support', 'deadline',
|
||
|
|
'status', 'created_at'
|
||
|
|
]
|
||
|
|
read_only_fields = ['applicant', 'support_count', 'status', 'created_at']
|
||
|
|
|
||
|
|
|
||
|
|
class ModeratorPermissionSerializer(serializers.ModelSerializer):
|
||
|
|
moderator = UserSerializer(read_only=True)
|
||
|
|
region = RegionSerializer(read_only=True)
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = ModeratorPermission
|
||
|
|
fields = ['id', 'moderator', 'region', 'rank', 'status', 'created_at']
|
||
|
|
read_only_fields = ['created_at']
|