index.community/backend/apiv1/views.py

38 lines
1.4 KiB
Python
Raw Normal View History

2018-08-26 00:32:55 +00:00
from rest_framework import viewsets
2018-09-03 14:10:44 +00:00
from scraper.models import Instance, Edge
from apiv1.serializers import InstanceListSerializer, InstanceDetailSerializer, NodeSerializer, EdgeSerializer
2018-08-26 00:32:55 +00:00
2018-08-27 10:58:11 +00:00
class InstanceViewSet(viewsets.ReadOnlyModelViewSet):
"""API endpoint to view stats for, and the peers of, an instance"""
2018-08-26 22:12:24 +00:00
2018-08-27 10:43:16 +00:00
lookup_field = 'name'
2018-08-26 22:12:24 +00:00
lookup_value_regex = '[a-zA-Z0-9-_\.]+'
2018-08-26 00:32:55 +00:00
queryset = Instance.objects.all()
2018-08-26 22:12:24 +00:00
serializer_class = InstanceListSerializer
detail_serializer_class = InstanceDetailSerializer # this serializer also includes stats and a list of peers
2018-08-26 00:32:55 +00:00
2018-08-26 22:12:24 +00:00
def get_serializer_class(self):
if self.action == 'retrieve':
if hasattr(self, 'detail_serializer_class'):
return self.detail_serializer_class
return self.serializer_class
class EdgeView(viewsets.ReadOnlyModelViewSet):
"""
Endpoint to get a list of the graph's edges in a SigmaJS-friendly format.
"""
2018-09-03 14:10:44 +00:00
queryset = Edge.objects.all()
serializer_class = EdgeSerializer
class NodeView(viewsets.ReadOnlyModelViewSet):
"""
Endpoint to get a list of the graph's nodes in a SigmaJS-friendly format.
"""
2019-04-19 14:29:45 +00:00
queryset = Instance.objects.filter(status='success', x_coord__isnull=False, y_coord__isnull=False, user_count__isnull=False)\
.exclude(sources__isnull=True, targets__isnull=True)
serializer_class = NodeSerializer