123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import yaml
- from datetime import datetime
- from pprint import pprint
- from django.core.management.base import BaseCommand
- from django.utils.text import slugify
- from backoffice.models import Reference, ReferenceType, Authority
- class Command(BaseCommand):
- help = 'Importe dans la base les données du yml'
- def add_arguments(self, parser):
- pass
- def handle(self, *args, **options):
- print('Removing all entries...')
- Reference.objects.all().delete()
- ReferenceType.objects.all().delete()
- Authority.objects.all().delete()
- print('Done')
- raw_data = None
- with open('../data.yml', 'r', encoding='utf-8') as stream:
- raw_data = yaml.load(stream)
- ref_list = list(raw_data.values())[0]
- for ref in ref_list:
- print('Parsing %s' % ref['id'])
- reference = Reference()
- if 'authority' in ref:
- authority_slug = slugify(ref['authority'])
- print('Searching authority : %s' % authority_slug)
- authority_object = Authority.objects.filter(slug=authority_slug).first()
- if authority_object is None:
- print('Not found, creating')
- authority_object = Authority(name=ref['authority'], slug=authority_slug)
- authority_object.save()
- reference.authority = authority_object
- if 'type' in ref and ref['type'] is not None:
- type_slug = ref['type']
- type_object = ReferenceType.objects.filter(slug=type_slug).first()
- if type_object is None:
- type_name = type_slug.replace('_', ' ').title()
- print('Creating %s' % type_name)
- type_object = ReferenceType(name=type_name, slug=type_slug)
- type_object.save()
- reference.reference_type = type_object
- if 'id' in ref and ref['id'] is not None:
- reference.identifier = ref['id']
- if 'section' in ref and ref['section'] is not None:
- reference.section = ref['section'].replace('^e^', 'ieme')
- if 'title' in ref:
- reference.title = ref['title']
- if 'title-short' in ref:
- reference.title_short = ref['title-short']
- if 'number' in ref and ref['number'] is not None:
- reference.number = ref['number'].replace('^o^', '°')
- if 'ECLI' in ref and ref['ECLI'] is not None:
- reference.ecli = ref['ECLI']
- if 'URL' in ref and ref['URL'] is not None:
- reference.url = ref['URL']
- if 'comments' in ref and ref['comments'] is not None:
- reference.comments = ref['comments']
- if 'issued' in ref and ref['issued']['year'] is not None:
- year = ref['issued']['year']
- month = ref['issued']['month']
- day = ref['issued']['day']
- if isinstance(day, str):
- day = int(day, base=10)
- if isinstance(month, str):
- month = int(month, base=10)
- if isinstance(year, str):
- year = int(year, base=10)
- reference.issued_date = datetime(year, month, day)
- reference.save()
- print('---------')
|