Add first pass at federating searchable data

This commit is contained in:
Michael Hall 2017-12-26 17:01:09 -05:00
parent 6ad92b4c4b
commit 98a89ab143
4 changed files with 30 additions and 0 deletions

View file

View file

View file

@ -0,0 +1,23 @@
from django.core.management.base import BaseCommand, CommandError
from events.models.search import Searchable, SearchableSerializer
from rest_framework.parsers import JSONParser
import urllib
class Command(BaseCommand):
help = 'Imports searchable data from another node'
def add_arguments(self, parser):
parser.add_argument('url', type=str)
def handle(self, *args, **options):
if 'url' in options:
resp = urllib.request.urlopen(options['url'])
json_data = JSONParser().parse(resp)
serializer = SearchableSerializer(data=json_data, many=True)
if serializer.is_valid():
serializer.save(federation_node=options['url'])
else:
print("Serialized data not valid: %s" % serializer.errors)
else:
print("No URL in options!")

View file

@ -43,3 +43,10 @@ class SearchableSerializer(serializers.ModelSerializer):
'origin_node'
)
def save(self, federation_node):
self.federation_node = federation_node
super().save()
def update(self, instance, validated_data):
validated_data['federation_node'] = self.federation_node
return super().update(instance, validated_data)