Ejemplo n.º 1
0
    def handle(self, *args, **kwargs):

        # Generate base of posts to json file for loaddata
        posts_base = []
        for n in range(1, POSTS_NUMBER + 1):
            post = generate_post()
            post_instance = {
                "model": "posts.post",
                "pk": n,
                "fields": {
                    "title": post['title'],
                    "short_description": post['short_description'],
                    "image": post['image'],
                    "full_description": post['full_description'],
                    "user": post['user'],
                    "posted": post['posted'],
                }
            }
            posts_base.append(post_instance)
        try:
            with open('posts/fixtures/posts_post.json', 'w') as posts:
                json.dump(posts_base, posts)
        except Exception as err:
            raise CommandError('Some error occurred:', str(err))

        # Generate base of comments to json file for loaddata
        comments_base = []
        for n in range(1, COMMENTS_NUMBER + 1):
            comment = generate_comment()
            comment_instance = {
                "model": "posts.comment",
                "pk": n,
                "fields": {
                    "username": comment['username'],
                    "text": comment['text'],
                    "post": comment['post'],
                    "moderated": comment['moderated'],
                }
            }
            comments_base.append(comment_instance)
        try:
            with open('posts/fixtures/posts_comment.json', 'w') as comments:
                json.dump(comments_base, comments)
        except Exception as err:
            raise CommandError('Some error occurred:', str(err))

        # Load to base
        management.call_command(loaddata.Command(), 'posts_post.json', verbosity=0)
        management.call_command(loaddata.Command(), 'posts_comment.json', verbosity=0)
    def install_fixture_data(self):
        """
        Delete all Accounts, Banks, SelfConfigurations, Users, and Validators
        Load in fixture data (same models as above)
        """

        global FIXTURES_DIR

        self.stdout.write(self.style.SUCCESS('Installing fixture data...'))

        Account.objects.all().delete()
        Bank.objects.all().delete()
        SelfConfiguration.objects.all().delete()
        User.objects.all().delete()
        Validator.objects.all().delete()

        fixture_files = [
            'validator.json', 'account.json', 'bank.json',
            'self_configuration.json', 'user.json'
        ]

        for fixture_file in fixture_files:
            fixtures = os.path.join(FIXTURES_DIR, fixture_file)
            management.call_command(loaddata.Command(), fixtures, verbosity=1)

        self.stdout.write(
            self.style.SUCCESS('Fixture data installed successfully'))
Ejemplo n.º 3
0
    def __call__(self, apps, schema_editor):
        def _get_model(model_identifier):
            """
            Helper to look up a model from an "app_label.model_name" string.
            """
            try:
                return apps.get_model(model_identifier)
            except (LookupError, TypeError):
                raise base.DeserializationError(
                    "Invalid model identifier: '%s'" % model_identifier)

        get_model_tmp = python._get_model  # pylint: disable=W0212
        try:
            python._get_model = _get_model
            file = os.path.join(self.fixture_dir, self.fixture_filename)
            if not os.path.isfile(file):
                raise IOError('File "%s" does not exists' % file)
            loaddata.Command().handle(file,
                                      ignore=True,
                                      database=DEFAULT_DB_ALIAS,
                                      app_label=None,
                                      verbosity=0,
                                      exclude=[],
                                      format='json')
        finally:
            python._get_model = get_model_tmp  # pylint: disable=W0212
    def handle(self, **options):
        if options["model"] not in ALL_MODELS:
            raise CommandError(
                f"Unknown model {options['model']}. \n\nRefer help for supported values"
            )

        obj = json.loads(self._get_json_object(options["csvfile"]))
        # As an `auto_now` field cannot be null, but `loaddata` (which is used by this command)
        # bypasses both `Model.save()` and 'QuerySet.update()` by going directly to the database,
        # data ingestion causes an IntegrityError unless the `last_modified` field's value is explicitly set.
        # The value used is `datetime.now()` to replicate the normal behaviour
        # when an object with an `auto_now` field is created.
        for row in obj:
            if "last_modified" not in row:
                row["last_modified"] = datetime.now().strftime(
                    r"%Y-%m-%dT%H:%M:%S.%f")
        json_obj = self._format_to_django_object(options["model"], obj)

        with NamedTemporaryFile(suffix=".json", mode="w") as fp:
            json.dump(json_obj, fp)
            fp.seek(0)
            management.call_command(loaddata.Command(), fp.name, format="json")
            self._save_objects(options["model"])
            self.stdout.write(
                self.style.SUCCESS(
                    f"Successfully ingested data into {options['model']}"))
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        try:
            fixtures = options['names']
            verbosity = options['verbosity']

            for nm in fixtures:
                management.call_command(loaddata.Command(),
                                        nm,
                                        verbosity=verbosity)

        except Exception as e:
            raise CommandError(e)
Ejemplo n.º 6
0
    def handle_noargs(self, **options):
        print "---running modified syncdb---"
        verbosity = int(options.get('verbosity', 1))

        tables = connection.introspection.table_names()
        if not 'django_content_type' in tables:
            db.create_table('django_content_type', (
                ('id', models.AutoField(primary_key=True)),
                ('name', models.CharField(max_length=100)),
                ('app_label', models.CharField(max_length=100)),
                ('model',
                 models.CharField(_('python model class name'),
                                  max_length=100)),
            ))
            db.create_unique('django_content_type', ('app_label', 'model'))

        loaddata.Command().execute(ct_fixture_name, verbosity=verbosity)
        options['migrate'], options['interactive'] = True, False
        super(Command, self).handle_noargs(**options)

        def ct_match_exist(content_types, id, name, app_label, model):
            for item in content_types:
                if item['model'] == 'contenttypes.contenttype' and \
                   item['pk'] == id and \
                   item['fields']['name'] == name and \
                   item['fields']['app_label'] == app_label and \
                   item['fields']['model'] == model:
                    return True
            return False

        content_types = json.load(open(ct_fixture_name))

        # check that no unexpected content types have been added
        for ct in ContentType.objects.all():
            if not ct_match_exist(content_types, ct.id, ct.name, ct.app_label,
                                  ct.model):
                ctdict = {
                    'id': ct.id,
                    'name': ct.name,
                    'app_label': ct.app_label,
                    'model': ct.model
                }
                print """{
                                    "pk": %(id)-s,
                                    "model": "contenttypes.contenttype",
                                    "fields": {
                                         "model": "%(model)-s",
                                         "name": "%(name)-s",
                                         "app_label": "%(app_label)-s"
                                    }
                                },
                                """ % (ctdict)
Ejemplo n.º 7
0
def load_data(apps, schema_editor):

    c = loaddata.Command()
    c.handle('widgets/fixtures/users.json', **{
        'database': DEFAULT_DB_ALIAS,
        'verbosity': 0,
        'ignore': False
    })
    c.handle('widgets/fixtures/widgets.json', **{
        'database': DEFAULT_DB_ALIAS,
        'verbosity': 0,
        'ignore': False
    })
Ejemplo n.º 8
0
def init_evolutions(app, created_models, **kwargs):
    """
    Attempts to initialize the Django Evolution schema signatures to sane
    values. This works around the issue where a first syncdb with Django
    Evolution (even on existing databases) will cause Django Evolution to
    assume the database is the most up to date, even if it's not. This will
    break the database. Our workarounds prevent this by starting off with
    sane values and doing some smart checks.
    """
    if FileDiff in created_models:
        # This is a new install. Let it continue through. The database will
        # be created with an up-to-date schema.
        return

    try:
        latest_version = django_evolution.Version.objects.latest('when')
    except django_evolution.Version.DoesNotExist:
        # This install didn't previously have Django Evolution. We might need
        # to prefill it with the schema from before the first db mutation.
        # However, we only want to do this if this is an existing database,
        # or users will have to evolve after the first install, which is
        # bad.
        latest_version = None

    if latest_version:
        # There's an existing Django Evolution install. Check to see if it's
        # broken, as it may be from the time just after the addition of
        # Django Evolution where it wouldn't migrate databases and instead
        # marked the schemas as being up to date in the stored signature.
        try:
            # If this succeeds, we're good.
            FileDiff.objects.filter(parent_diff64="")

            return
        except:
            # If that failed, then most likely it's due to the
            # parent_diff_base64 column not existing in the database, which
            # means that Django Evolution's view of the database and the
            # database itself are out of match from an early install during
            # the breakage period.
            #
            # We can feel free to nuke the Django Evolution tables so that
            # we can apply our own schema in order to kickstart a proper
            # evolution.
            django_evolution.Version.objects.all().delete()
            django_evolution.Evolution.objects.all().delete()

    # Load the Django Evolution fixture describing the database at the time
    # of the Django Evolution addition.
    loaddata.Command().handle("admin/fixtures/initial_evolution_schema.json",
                              verbosity=0)
    def handle(self, *args, **options):
        management.call_command(loaddata.Command(), "categories", verbosity=0)
        person_count = Person.objects.count()
        organization_count = Organization.objects.count()
        payment_count = Payment.objects.count()
        payment_strategy_count = PaymentStrategy.objects.count()
        quota_count = Quota.objects.count()

        q = options["count"]

        people = PersonFactory.create_batch(q)
        orgs = OrganizationFactory.create_batch(q)
        for person in people:
            strategy = PaymentStrategyFactory(patron=person.membership.patron)
            payment = PaymentFactory.create_batch(q, strategy=strategy)
            for p in payment:
                QuotaFactory(payment=p)
        for org in orgs:
            strategy = PaymentStrategyFactory(patron=org.membership.patron)
            payment = PaymentFactory.create_batch(q, strategy=strategy)
            for p in payment:
                QuotaFactory(payment=p)

        person_count = Person.objects.count() - person_count
        organization_count = Organization.objects.count() - organization_count
        payment_count = Payment.objects.count() - payment_count
        payment_strat_count = PaymentStrategy.objects.count(
        ) - payment_strategy_count
        quota_count = Quota.objects.count() - quota_count

        self.stdout.write(
            self.style.SUCCESS(
                "Successfully created Person instances (count %d)" %
                person_count))
        self.stdout.write(
            self.style.SUCCESS(
                "Successfully created Organization  instances (count %s)" %
                organization_count))
        self.stdout.write(
            self.style.SUCCESS(
                "Successfully created PaymentStrategy instances (count %d)" %
                payment_strat_count))
        self.stdout.write(
            self.style.SUCCESS(
                "Successfully created Payment instances (count %d)" %
                payment_count))
        self.stdout.write(
            self.style.SUCCESS(
                "Successfully created Quota instances (count %d)" %
                quota_count))
Ejemplo n.º 10
0
def load_seed(request: HttpRequest):
    if str(request.user).__eq__("AnonymousUser"):
        return HttpResponseBadRequest()

    if not request.user.is_superuser:
        message_error(request, "You're not allowed to do that, little burglar!")
        return redirect('/api/admin/')

    try:
        call_command(loaddata.Command(), 'data.json')
        message_info(request, 'Infromaţiile au fost încărcate în baza de date!')
    except Exception as err:
        message_error(request, f'A apărut o eroare! Datele nu au fost încarcate în baza de date. Error stack: {err}')

    return redirect('/api/admin/')
Ejemplo n.º 11
0
    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Process started'))

        User.objects.filter(email='*****@*****.**').delete()
        User.objects.create_superuser('admin', '*****@*****.**', '12345678')
        self.stdout.write(self.style.SUCCESS('Created superuser for testing'))

        # Person.initialize()
        # MedicalRecord.initialize()
        # DispensaryRegistration.initialize()
        # Anamnesis.initialize()
        api_fixtures = os.listdir(os.path.join(APPS_DIR, 'api/fixtures/'))

        management.call_command(loaddata.Command(), *api_fixtures, verbosity=0)
        self.stdout.write(self.style.SUCCESS('Successfully loading fixtures for api'))
Ejemplo n.º 12
0
    def install_fixture_data(self):
        """
        Delete all Accounts, Banks, SelfConfigurations, Users, and Validators

        Load in fixture data (same models as above)
        """
        self.stdout.write(self.style.SUCCESS('Installing fixture data...'))

        Account.objects.all().delete()
        Bank.objects.all().delete()
        SelfConfiguration.objects.all().delete()
        User.objects.all().delete()
        Validator.objects.all().delete()

        fixtures = ['validator', 'self_configuration', 'user']

        management.call_command(loaddata.Command(), *fixtures, verbosity=1)

        self.stdout.write(
            self.style.SUCCESS('Fixture data installed successfully'))
Ejemplo n.º 13
0
def load_sample_data():
    management.call_command(loaddata.Command(),
                            'sample_data.json',
                            verbosity=0)
Ejemplo n.º 14
0
def load_data(*args):
    management.call_command(
        loaddata.Command(),
        os.path.join('treasurer', 'fixtures', 'category.json'))
Ejemplo n.º 15
0
    def handle(self, *args, **options):
        fixtures = os.listdir(os.path.join(APPS_DIR, 'api/fixtures/'))

        management.call_command(loaddata.Command(), *fixtures, verbosity=0)
        self.stdout.write(
            self.style.SUCCESS('Successfully loading fixtures for api'))
Ejemplo n.º 16
0
def apply_fixtures(apps, schema_editor):
    # XXX: This is more or less a hack! Please refrain from using this.
    cmd = loaddata.Command()
    cmd.run_from_argv(['./manage.py', 'loaddata', 'ossm/fixtures/social.json'])
Ejemplo n.º 17
0
# Create your tests here.
import django
from django.core import management
from django.core.management.commands import loaddata

import os

from django.core.wsgi import get_wsgi_application

#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lmnop.settings")

#application = get_wsgi_application()

#print(application)

# needs environment variable for database password set:  LMNOP_DB_Pw=xxxx
#print(os.environ['LMNOP_DB_PW'])

#settings.configure(default_settings=settings, DEBUG=True)
django.setup()

# need to load JSON files in this order for foreign key dependencies
# user artist venue show note
# set verbosity=0 (no displays) for production deployment
management.call_command(loaddata.Command(), 'user', verbosity=1)
management.call_command(loaddata.Command(), 'artist', verbosity=1)
management.call_command(loaddata.Command(), 'venue', verbosity=1)
management.call_command(loaddata.Command(), 'show', verbosity=1)
management.call_command(loaddata.Command(), 'note', verbosity=1)
Ejemplo n.º 18
0
def load_data(*args):
    management.call_command(
        loaddata.Command(),
        os.path.join('authentication', 'fixtures', 'user.json'))