@@ -0,0 +1,1517 @@
+from xml.etree import ElementTree%0Afrom django.core.management.base import BaseCommand, CommandError%0Afrom django.db import transaction%0Aimport requests%0A%0Afrom apps.curia_vista.models import *%0A%0A%0Aclass Command(BaseCommand):%0A    help = 'Import departments from parlament.ch'%0A%0A    @transaction.atomic%0A    def handle(self, *args, **options):%0A        source = 'http://ws.parlament.ch/Departments?format=xml&lang=de'%0A        headers = %7B'User-Agent': 'Mozilla'%7D%0A%0A        self.stdout.write(%22Importing: %7B%7D%22.format(source))%0A%0A        try:%0A            response = requests.get(source, headers=headers)%0A        except Exception as e:%0A            raise CommandError(%22Could not fetch file from %7B%7D%22.format(source))%0A%0A        departments = ElementTree.fromstring(response.content)%0A%0A        if not departments:%0A            raise CommandError(%22Not a valid XML file: %7B%7D%22.format(source))%0A%0A        for department in departments:%0A            department_id = department.find('id').text%0A            department_updated = department.find('updated').text%0A            department_code = department.find('code').text%0A            department_model, created = Department.objects.update_or_create(id=department_id,%0A                                                                            defaults=%7B'updated': department_updated,%0A                                                                                      'code': department_code%7D)%0A            department_model.full_clean()%0A            department_model.save()%0A            print(department_model)%0A

Add import script for department data