Add first pass at federating searchable data
This commit is contained in:
parent
6ad92b4c4b
commit
98a89ab143
4 changed files with 30 additions and 0 deletions
0
events/management/__init__.py
Normal file
0
events/management/__init__.py
Normal file
0
events/management/commands/__init__.py
Normal file
0
events/management/commands/__init__.py
Normal file
23
events/management/commands/import.py
Normal file
23
events/management/commands/import.py
Normal 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!")
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue