index.community/backend/apiv1/serializers.py

106 lines
3.1 KiB
Python
Raw Normal View History

2018-08-26 00:32:55 +00:00
from rest_framework import serializers
2018-09-03 18:15:28 +00:00
import math
2018-08-28 22:22:29 +00:00
from collections import OrderedDict
2018-09-03 18:15:28 +00:00
from scraper.models import Instance, Edge
2018-08-26 22:12:24 +00:00
class InstanceListSerializer(serializers.ModelSerializer):
2018-09-03 19:30:11 +00:00
"""
Minimal instance details used in the full list of instances.
"""
2018-08-26 22:12:24 +00:00
class Meta:
model = Instance
2018-08-28 22:22:29 +00:00
fields = ('name', 'user_count')
def to_representation(self, instance):
"""
Object instance -> Dict of primitive datatypes.
We use a custom to_representation function to exclude empty fields in the serialized JSON.
2018-08-28 22:22:29 +00:00
"""
ret = super(InstanceListSerializer, self).to_representation(instance)
ret = OrderedDict(list(filter(lambda x: x[1], ret.items())))
return ret
2018-08-26 22:12:24 +00:00
class InstanceDetailSerializer(serializers.ModelSerializer):
2018-09-03 19:30:11 +00:00
"""
Detailed instance view.
"""
2018-09-01 17:24:05 +00:00
userCount = serializers.SerializerMethodField()
statusCount = serializers.SerializerMethodField()
domainCount = serializers.SerializerMethodField()
lastUpdated = serializers.SerializerMethodField()
2018-08-26 22:12:24 +00:00
peers = InstanceListSerializer(many=True, read_only=True)
2018-09-01 17:24:05 +00:00
def get_userCount(self, obj):
return obj.user_count
def get_statusCount(self, obj):
return obj.status_count
def get_domainCount(self, obj):
return obj.domain_count
def get_lastUpdated(self, obj):
return obj.last_updated
2018-08-26 00:32:55 +00:00
class Meta:
model = Instance
2018-09-03 19:30:11 +00:00
fields = ('name', 'description', 'version', 'userCount',
'statusCount', 'domainCount', 'peers', 'lastUpdated',
'status')
class EdgeSerializer(serializers.ModelSerializer):
2018-09-03 19:30:11 +00:00
"""
Used for displaying the graph.
"""
id = serializers.SerializerMethodField('get_pk')
2018-09-03 18:15:28 +00:00
size = serializers.SerializerMethodField('get_weight')
class Meta:
2018-09-03 18:15:28 +00:00
model = Edge
fields = ('source', 'target', 'id', 'size')
def get_pk(self, obj):
return obj.pk
2018-09-03 18:15:28 +00:00
def get_weight(self, obj):
return obj.weight
class NodeSerializer(serializers.ModelSerializer):
2018-09-03 19:30:11 +00:00
"""
Used for displaying the graph.
"""
id = serializers.SerializerMethodField('get_name')
label = serializers.SerializerMethodField('get_name')
size = serializers.SerializerMethodField()
2018-09-03 14:10:44 +00:00
x = serializers.SerializerMethodField()
y = serializers.SerializerMethodField()
class Meta:
model = Instance
2018-09-03 14:10:44 +00:00
fields = ('id', 'label', 'size', 'x', 'y')
def get_name(self, obj):
return obj.name
def get_size(self, obj):
return math.log(obj.user_count) if (obj.user_count and (obj.user_count > 1)) else 1
2018-09-03 14:10:44 +00:00
def get_x(self, obj):
return obj.x_coord
def get_y(self, obj):
return obj.y_coord
def to_representation(self, instance):
"""
Object instance -> Dict of primitive datatypes.
We use a custom to_representation function to exclude empty fields in the serialized JSON.
"""
ret = super(NodeSerializer, self).to_representation(instance)
ret = OrderedDict(list(filter(lambda x: x[1], ret.items())))
return ret