dnmapper/apiv2/serializers.py

33 lines
1007 B
Python

import json
from rest_framework import serializers
from bgpdata.models import CrawlRun, ASLastSeen
class CrawlRunSerializer(serializers.ModelSerializer):
graph = serializers.ReadOnlyField()
class Meta:
model = CrawlRun
fields = ('id', 'startTime', 'endTime', 'asCount', 'asOnlineCount', 'asOfflineCount', 'peeringCount', 'graph')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if "with_graph" not in self.context['request'].query_params:
self.fields.pop("graph")
def to_representation(self, instance):
data = super().to_representation(instance)
for elem in data:
if "graph" in data and isinstance(data['graph'], str):
data['graph'] = json.loads(data['graph'])
return data
class ASLastSeenSerializer(serializers.ModelSerializer):
class Meta:
model = ASLastSeen
fields = ('id', 'asn', 'directlyCrawled', 'online', 'lastSeen', 'crawlLastSeen')