Beispiel #1
0
    def import_yaml(self, yaml_filename):
        with open(yaml_filename, 'r') as f:
            contents = yaml.load(f, Loader=yaml.FullLoader)

            Author.objects.all().delete()

            for author in contents:
                self.profiles_nb += 1

                filename = author['bio']

                if not os.path.isabs(filename):
                    base_dir = os.path.abspath(os.path.dirname(yaml_filename))
                    filename = os.path.join(base_dir, filename)

                if not os.path.isfile(filename):
                    raise CommandError(f'In file {yaml_filename}, '
                                       f'file {filename} does not exist')

                header_img = None
                if 'header_img' in author:
                    header_img = author['header_img']

                db_author = Author(pseudo=author['pseudo'],
                                   name=author['name'],
                                   email=author['email'],
                                   bio=md_convert(filename))

                if header_img is not None:
                    db_author.header_img = header_img

                db_author.save()