From 626cc1cd070fb35f667afa0f67327c1da789e915 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 25 May 2016 02:45:42 -0400 Subject: [PATCH] Move the organizations.managment module to .utils to avoid class with /management/ folder. --- mayan/apps/organizations/utils.py | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 mayan/apps/organizations/utils.py diff --git a/mayan/apps/organizations/utils.py b/mayan/apps/organizations/utils.py new file mode 100644 index 0000000000..ef36f269c3 --- /dev/null +++ b/mayan/apps/organizations/utils.py @@ -0,0 +1,44 @@ +from __future__ import unicode_literals + +from django.apps import apps +from django.core.management.color import no_style +from django.db import DEFAULT_DB_ALIAS, connections, router +from django.db.models import signals + +from .literals import DEFAULT_ORGANIZATION_LABEL + + +def create_default_organization(verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, **kwargs): + """ + Creates the default Organization object. + """ + + try: + Organization = apps.get_model('organizations', 'Organization') + except LookupError: + return + + if not router.allow_migrate(using, Organization): + return + + if not Organization.objects.using(using).exists(): + # The default settings set ORGANIZATION_ID = 1, and some tests in Django's test + # suite rely on this value. However, if database sequences are reused + # (e.g. in the test suite after flush/syncdb), it isn't guaranteed that + # the next id will be 1, so we coerce it. See #15573 and #16353. This + # can also crop up outside of tests - see #15346. + if verbosity >= 2: + print("Creating default Organization object") + Organization(pk=1, label=DEFAULT_ORGANIZATION_LABEL).save(using=using) + + # We set an explicit pk instead of relying on auto-incrementation, + # so we need to reset the database sequence. See #17415. + sequence_sql = connections[using].ops.sequence_reset_sql(no_style(), [Organization]) + if sequence_sql: + if verbosity >= 2: + print('Resetting sequence') + with connections[using].cursor() as cursor: + for command in sequence_sql: + cursor.execute(command) + + Organization.objects.clear_cache()