diff --git a/events/management/__init__.py b/events/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/events/management/commands/__init__.py b/events/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/events/management/commands/import.py b/events/management/commands/import.py new file mode 100644 index 0000000..4e6e1b3 --- /dev/null +++ b/events/management/commands/import.py @@ -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!") diff --git a/events/models/search.py b/events/models/search.py index 2cf639e..2a4bf05 100644 --- a/events/models/search.py +++ b/events/models/search.py @@ -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)