Exemplo n.º 1
0
from settings import *
import autofixture
autofixture.autodiscover()


INSTALLED_APPS += (
    'autofixture',
    )

Exemplo n.º 2
0
    def handle(self, *attrs, **options):
        error_option = None
        #
        # follow options
        #
        if options["no_follow_fk"] is None:
            follow_fk = None
        else:
            follow_fk = False
        if options["no_follow_m2m"] is None:
            follow_m2m = None
            # this is the only chance for the follow_m2m options to be parsed
            if options["follow_m2m"]:
                try:
                    value = options["follow_m2m"].split(",")
                    if len(value) == 1 and value[0].count(":") == 1:
                        follow_m2m = [int(i) for i in value[0].split(":")]
                    else:
                        follow_m2m = {}
                        for field in value:
                            key, minval, maxval = field.split(":")
                            follow_m2m[key] = int(minval), int(maxval)
                except ValueError:
                    error_option = "--follow-m2m={0}".format(options["follow_m2m"])
        else:
            follow_m2m = False
        #
        # generation options
        #
        if options["generate_fk"] is None:
            generate_fk = None
        else:
            generate_fk = options["generate_fk"].split(",")
        generate_m2m = None
        if options["generate_m2m"]:
            try:
                value = [v for v in options["generate_m2m"].split(",") if v]
                if len(value) == 1 and value[0].count(":") == 1:
                    generate_m2m = [int(i) for i in value[0].split(":")]
                else:
                    generate_m2m = {}
                    for field in value:
                        key, minval, maxval = field.split(":")
                        generate_m2m[key] = int(minval), int(maxval)
            except ValueError:
                error_option = "--generate-m2m={0}".format(options["generate_m2m"])

        if error_option:
            raise CommandError(
                u"Invalid option {0}\nExpected: {1}=field:min:max,field2:min:max... (min and max must be numbers)".format(
                    error_option, error_option.split("=", 1)[0]
                )
            )

        use = options["use"]
        if use:
            use = use.split(".")
            use = getattr(importlib.import_module(".".join(use[:-1])), use[-1])

        overwrite_defaults = options["overwrite_defaults"]
        self.verbosity = int(options["verbosity"])

        models = []
        for attr in attrs:
            try:
                app_label, model_label = attr.split(".")
                model_label, count = model_label.split(":")
                count = int(count)
            except ValueError:
                raise CommandError(
                    u"Invalid argument: {0}\nExpected: app_label.ModelName:count (count must be a number)".format(attr)
                )
            model = get_model(app_label, model_label)
            if not model:
                raise CommandError(u"Unknown model: {0}.{1}".format(app_label, model_label))
            models.append((model, count))

        signals.instance_created.connect(self.print_instance)

        autofixture.autodiscover()

        kwargs = {
            "overwrite_defaults": overwrite_defaults,
            "follow_fk": follow_fk,
            "generate_fk": generate_fk,
            "follow_m2m": follow_m2m,
            "generate_m2m": generate_m2m,
        }

        for model, count in models:
            if use:
                fixture = use(model, **kwargs)
                fixture.create(count)
            else:
                autofixture.create(model, count, **kwargs)
Exemplo n.º 3
0
    def handle(self, *attrs, **options):
        error_option = None
        #
        # follow options
        #
        if options['no_follow_fk'] is None:
            follow_fk = None
        else:
            follow_fk = False
        if options['no_follow_m2m'] is None:
            follow_m2m = None
            # this is the only chance for the follow_m2m options to be parsed
            if options['follow_m2m']:
                try:
                    value = options['follow_m2m'].split(',')
                    if len(value) == 1 and value[0].count(':') == 1:
                        follow_m2m = [int(i) for i in value[0].split(':')]
                    else:
                        follow_m2m = {}
                        for field in value:
                            key, minval, maxval = field.split(':')
                            follow_m2m[key] = int(minval), int(maxval)
                except ValueError:
                    error_option = '--follow-m2m={0}'.format(options['follow_m2m'])
        else:
            follow_m2m = False
        #
        # generation options
        #
        if options['generate_fk'] is None:
            generate_fk = None
        else:
            generate_fk = options['generate_fk'].split(',')
        generate_m2m = None
        if options['generate_m2m']:
            try:
                value = [v for v in options['generate_m2m'].split(',') if v]
                if len(value) == 1 and value[0].count(':') == 1:
                    generate_m2m = [int(i) for i in value[0].split(':')]
                else:
                    generate_m2m = {}
                    for field in value:
                        key, minval, maxval = field.split(':')
                        generate_m2m[key] = int(minval), int(maxval)
            except ValueError:
                error_option = '--generate-m2m={0}'.format(options['generate_m2m'])

        if error_option:
            raise CommandError(
                u'Invalid option {0}\n'
                u'Expected: {1}=field:min:max,field2:min:max... (min and max must be numbers)'.format(
                    error_option,
                    error_option.split('=', 1)[0]))

        use = options['use']
        if use:
            use = use.split('.')
            use = getattr(importlib.import_module('.'.join(use[:-1])), use[-1])

        overwrite_defaults = options['overwrite_defaults']
        self.verbosity = int(options['verbosity'])

        models = []
        for attr in attrs:
            try:
                app_label, model_label = attr.split('.')
                model_label, count = model_label.split(':')
                count = int(count)
            except ValueError:
                raise CommandError(
                    u'Invalid argument: {0}\n'
                    u'Expected: app_label.ModelName:count '
                    u'(count must be a number)'.format(attr))
            model = get_model(app_label, model_label)
            if not model:
                raise CommandError(
                    u'Unknown model: {0}.{1}'.format(app_label, model_label))
            models.append((model, count))

        signals.instance_created.connect(
            self.print_instance)

        autofixture.autodiscover()

        kwargs = {
            'overwrite_defaults': overwrite_defaults,
            'follow_fk': follow_fk,
            'generate_fk': generate_fk,
            'follow_m2m': follow_m2m,
            'generate_m2m': generate_m2m,
        }

        for model, count in models:
            if use:
                fixture = use(model, **kwargs)
                fixture.create(count)
            else:
                autofixture.create(model, count, **kwargs)
Exemplo n.º 4
0
"""example URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
import autofixture
from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

autofixture.autodiscover()
Exemplo n.º 5
0
    def handle(self, *attrs, **options):
        from django.db.models import get_model

        error_option = None
        #
        # follow options
        #
        if options['no_follow_fk'] is None:
            follow_fk = None
        else:
            follow_fk = False
        if options['no_follow_m2m'] is None:
            follow_m2m = None
            # this is the only chance for the follow_m2m options to be parsed
            if options['follow_m2m']:
                try:
                    value = options['follow_m2m'].split(',')
                    if len(value) == 1 and value[0].count(':') == 1:
                        follow_m2m = [int(i) for i in value[0].split(':')]
                    else:
                        follow_m2m = {}
                        for field in value:
                            key, minval, maxval = field.split(':')
                            follow_m2m[key] = int(minval), int(maxval)
                except ValueError:
                    error_option = '--follow-m2m={0}'.format(
                        options['follow_m2m'])
        else:
            follow_m2m = False
        #
        # generation options
        #
        if options['generate_fk'] is None:
            generate_fk = None
        else:
            generate_fk = options['generate_fk'].split(',')
        generate_m2m = None
        if options['generate_m2m']:
            try:
                value = [v for v in options['generate_m2m'].split(',') if v]
                if len(value) == 1 and value[0].count(':') == 1:
                    generate_m2m = [int(i) for i in value[0].split(':')]
                else:
                    generate_m2m = {}
                    for field in value:
                        key, minval, maxval = field.split(':')
                        generate_m2m[key] = int(minval), int(maxval)
            except ValueError:
                error_option = '--generate-m2m={0}'.format(
                    options['generate_m2m'])

        if error_option:
            raise CommandError(
                u'Invalid option {0}\n'
                u'Expected: {1}=field:min:max,field2:min:max... (min and max must be numbers)'
                .format(error_option,
                        error_option.split('=', 1)[0]))

        use = options['use']
        if use:
            use = use.split('.')
            use = getattr(import_module('.'.join(use[:-1])), use[-1])

        overwrite_defaults = options['overwrite_defaults']
        self.verbosity = int(options['verbosity'])

        models = []
        for attr in attrs:
            try:
                app_label, model_label = attr.split('.')
                model_label, count = model_label.split(':')
                count = int(count)
            except ValueError:
                raise CommandError(u'Invalid argument: {0}\n'
                                   u'Expected: app_label.ModelName:count '
                                   u'(count must be a number)'.format(attr))
            model = get_model(app_label, model_label)
            if not model:
                raise CommandError(u'Unknown model: {0}.{1}'.format(
                    app_label, model_label))
            models.append((model, count))

        signals.instance_created.connect(self.print_instance)

        autofixture.autodiscover()

        kwargs = {
            'overwrite_defaults': overwrite_defaults,
            'follow_fk': follow_fk,
            'generate_fk': generate_fk,
            'follow_m2m': follow_m2m,
            'generate_m2m': generate_m2m,
        }

        for model, count in models:
            if use:
                fixture = use(model, **kwargs)
                fixture.create(count)
            else:
                autofixture.create(model, count, **kwargs)