import yaml
from django.core.management.base import BaseCommand
from django.utils.module_loading import import_string
from ...models import Document


class Command(BaseCommand):

    help = "Imprt configuration file"

    def add_arguments(self, parser):
        parser.add_argument('config_path')

    def handle(self, verbosity, config_path, **options):
        with open(config_path) as f:
            config = yaml.load(f)
            for collection in config['collections']:
                loader_cls = import_string(collection['loader'])
                loader = loader_cls(**collection)

                for data in loader.documents():
                    (doc, created) = Document.objects.get_or_create(
                        slug=data.pop('slug'),
                        defaults=data,
                    )
                    if created:
                        print '+++', doc.slug
                    else:
                        changed = False
                        for k in data:
                            if getattr(doc, k) != data[k]:
                                setattr(doc, k, data[k])
                                changed = True
                        if changed:
                            print '%%%', doc.slug

@@ -132,16 +132,28 @@
 s import
+ Collection,
  Documen
@@ -622,39 +622,205 @@
     
-for data in loader.documents():
+(collection, _) = Collection.objects.get_or_create(%0A                    slug=collection%5B'slug'%5D)%0A%0A                for data in loader.documents():%0A                    data%5B'collection'%5D = collection
 %0A   

add documents to collection