import_references.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import yaml
  2. from datetime import datetime
  3. from pprint import pprint
  4. from django.core.management.base import BaseCommand
  5. from django.utils.text import slugify
  6. from backoffice.models import Reference, ReferenceType, Authority
  7. class Command(BaseCommand):
  8. help = 'Importe dans la base les données du yml'
  9. def add_arguments(self, parser):
  10. pass
  11. def handle(self, *args, **options):
  12. print('Removing all entries...')
  13. Reference.objects.all().delete()
  14. ReferenceType.objects.all().delete()
  15. Authority.objects.all().delete()
  16. print('Done')
  17. raw_data = None
  18. with open('../data.yml', 'r', encoding='utf-8') as stream:
  19. raw_data = yaml.load(stream)
  20. ref_list = list(raw_data.values())[0]
  21. for ref in ref_list:
  22. print('Parsing %s' % ref['id'])
  23. reference = Reference()
  24. if 'authority' in ref:
  25. authority_slug = slugify(ref['authority'])
  26. print('Searching authority : %s' % authority_slug)
  27. authority_object = Authority.objects.filter(slug=authority_slug).first()
  28. if authority_object is None:
  29. print('Not found, creating')
  30. authority_object = Authority(name=ref['authority'], slug=authority_slug)
  31. authority_object.save()
  32. reference.authority = authority_object
  33. if 'type' in ref and ref['type'] is not None:
  34. type_slug = ref['type']
  35. type_object = ReferenceType.objects.filter(slug=type_slug).first()
  36. if type_object is None:
  37. type_name = type_slug.replace('_', ' ').title()
  38. print('Creating %s' % type_name)
  39. type_object = ReferenceType(name=type_name, slug=type_slug)
  40. type_object.save()
  41. reference.reference_type = type_object
  42. if 'id' in ref and ref['id'] is not None:
  43. reference.identifier = ref['id']
  44. if 'section' in ref and ref['section'] is not None:
  45. reference.section = ref['section'].replace('^e^', 'ieme')
  46. if 'title' in ref:
  47. reference.title = ref['title']
  48. if 'title-short' in ref:
  49. reference.title_short = ref['title-short']
  50. if 'number' in ref and ref['number'] is not None:
  51. reference.number = ref['number'].replace('^o^', '°')
  52. if 'ECLI' in ref and ref['ECLI'] is not None:
  53. reference.ecli = ref['ECLI']
  54. if 'URL' in ref and ref['URL'] is not None:
  55. reference.url = ref['URL']
  56. if 'comments' in ref and ref['comments'] is not None:
  57. reference.comments = ref['comments']
  58. if 'issued' in ref and ref['issued']['year'] is not None:
  59. year = ref['issued']['year']
  60. month = ref['issued']['month']
  61. day = ref['issued']['day']
  62. if isinstance(day, str):
  63. day = int(day, base=10)
  64. if isinstance(month, str):
  65. month = int(month, base=10)
  66. if isinstance(year, str):
  67. year = int(year, base=10)
  68. reference.issued_date = datetime(year, month, day)
  69. reference.save()
  70. print('---------')