def create_all_data_exports(sender, **kwargs): update_all_contenttypes() create_html_export_format(sender) create_csv_export_format(sender) created_models = kwargs['created_models'] for model in created_models: try_create_export_for_model(model)
def init_customers_quotas(apps, schema_editor): Customer = apps.get_model('structure', 'Customer') Quota = apps.get_model("quotas", 'Quota') User = get_user_model() # sometimes django does not initiate customer content type, so we need update content types manually update_all_contenttypes() customer_ct = ContentType.objects.get_for_model(Customer) for customer in Customer.objects.all(): # projects customer_kwargs = { 'content_type_id': customer_ct.id, 'object_id': customer.id } if not Quota.objects.filter(name=PROJECT_COUNT_QUOTA, **customer_kwargs).exists(): Quota.objects.create(uuid=uuid4().hex, name=PROJECT_COUNT_QUOTA, usage=customer.projects.count(), **customer_kwargs) # users if not Quota.objects.filter(name=USER_COUNT_QUOTA, ** customer_kwargs).exists(): users_count = (User.objects.filter( Q(groups__projectrole__project__customer=customer) | Q(groups__projectgrouprole__project_group__customer=customer) | Q(groups__customerrole__customer=customer)).distinct().count( )) Quota.objects.create(uuid=uuid4().hex, name=USER_COUNT_QUOTA, usage=users_count, **customer_kwargs)
def create_fresh_posts(apps, schema_editor): update_all_contenttypes() Fresh = apps.get_model("feeds", "Fresh") Composition = apps.get_model("compositions", "Composition") Bucket = apps.get_model("buckets", "Bucket") ContentType = apps.get_model("contenttypes", "ContentType") ctype_composition = ContentType.objects.get(app_label='compositions', model='composition') ctype_bucket = ContentType.objects.get(app_label='buckets', model='bucket') for composition in Composition.objects.all(): post = Fresh() post.created = composition.created post.feed_type = 'AR' post.content_object = composition post.object_id = composition.id post.content_type = ctype_composition post.save() for bucket in Bucket.objects.all(): post = Fresh() post.created = bucket.created post.feed_type = 'BK' post.object_id = bucket.id post.content_type = ctype_bucket post.content_object = bucket post.save()
def handle(self, *args, **options): # Add any missing content types update_all_contenttypes() # Add any missing permissions for app in get_apps(): create_permissions(app, None, 2)
def add_basic_staff_permission(apps, schema_editor): update_all_contenttypes() # Fixes tests ContentType = apps.get_model('contenttypes.ContentType') Permission = apps.get_model('auth.Permission') content_type = ContentType.objects.get(app_label='auth', model='user') Permission.objects.create(content_type=content_type, codename='basic_staff', name='Can access Basic Admin Site')
def handle(self, *args, **options): setup_environ(Poem.settings) # Add any missing content types from django.contrib.contenttypes.management \ import update_all_contenttypes update_all_contenttypes() logger.info('Content types were successfully updated.')
def add_view_cvn_reports_permission(apps, schema_editor): update_all_contenttypes() # Fixes tests ContentType = apps.get_model('contenttypes.ContentType') Permission = apps.get_model('auth.Permission') content_type = ContentType.objects.get(app_label='cvn', model='cvn') Permission.objects.create(content_type=content_type, codename='read_cvn_reports', name='Can read cvn reports')
def create_notifications(apps, schema_editor): update_all_contenttypes() sql=""" INSERT INTO notifications_watched (object_id, created_date, content_type_id, user_id, project_id) SELECT userstory_id AS object_id, now() AS created_date, {content_type_id} AS content_type_id, user_id, project_id FROM userstories_userstory_watchers INNER JOIN userstories_userstory ON userstories_userstory_watchers.userstory_id = userstories_userstory.id""".format(content_type_id=ContentType.objects.get(model='userstory').id) cursor = connection.cursor() cursor.execute(sql)
def create_notifications(apps, schema_editor): update_all_contenttypes(verbosity=0) sql=""" INSERT INTO notifications_watched (object_id, created_date, content_type_id, user_id, project_id) SELECT task_id AS object_id, now() AS created_date, {content_type_id} AS content_type_id, user_id, project_id FROM tasks_task_watchers INNER JOIN tasks_task ON tasks_task_watchers.task_id = tasks_task.id""".format(content_type_id=ContentType.objects.get(model='task').id) cursor = connection.cursor() cursor.execute(sql)
def add_staff_menu_permission(apps, schema_editor): update_all_contenttypes() # Fixes tests ContentType = apps.get_model('contenttypes.ContentType') Permission = apps.get_model('auth.Permission') content_type = ContentType.objects.get(app_label='auth', model='user') Permission.objects.create(content_type=content_type, codename='read_admin_menu', name='Can read Admin Menu')
def create_notifications(apps, schema_editor): update_all_contenttypes(verbosity=0) sql=""" INSERT INTO notifications_watched (object_id, created_date, content_type_id, user_id, project_id) SELECT wikipage_id AS object_id, now() AS created_date, {content_type_id} AS content_type_id, user_id, project_id FROM wiki_wikipage_watchers INNER JOIN wiki_wikipage ON wiki_wikipage_watchers.wikipage_id = wiki_wikipage.id""".format(content_type_id=ContentType.objects.get(model='wikipage').id) cursor = connection.cursor() cursor.execute(sql)
def forwards(self, orm): # Start transaction db.start_transaction() # Rename the table for data backup db.rename_table('notes_note', 'notes_note_old') # create the table notes_note with the right layout db.create_table('notes_note', ( ('historylistitem_ptr', self.gf('django.db.models.fields.related.OneToOneField')(to=orm['utils.HistoryListItem'], unique=True, primary_key=True)), ('created', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, blank=True)), ('modified', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, blank=True)), ('deleted', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now, blank=True)), ('is_deleted', self.gf('django.db.models.fields.BooleanField')(default=False)), ('content', self.gf('django.db.models.fields.TextField')()), ('author', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['users.CustomUser'])), ('content_type', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['contenttypes.ContentType'])), ('object_id', self.gf('django.db.models.fields.PositiveIntegerField')()), )) db.send_create_signal('notes', ['Note']) # Get all notes from the old table old_notes = db.execute('SELECT * FROM notes_note_old') # Delete notes_note_old db.delete_table('notes_note_old') # Commit transaction db.commit_transaction() # Get the content type id for the Note model from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() ctype_id = ContentType.objects.get(app_label='notes', model='note').pk # Create new notes for note in old_notes: created = note[1] modified = note[2] deleted = note[3] is_deleted = note[4] content = note[5] author_id = note[6] content_type_id = note[7] object_id = note[8] orm.Note.objects.create( created=created, modified=modified, deleted=deleted, is_deleted=is_deleted, content=content, author_id=author_id, content_type_id=content_type_id, object_id=object_id, polymorphic_ctype_id=ctype_id )
def create_all_data_exports(sender, **kwargs): StubModel().save() # only do it for 1 sender, so that these lines don't get repeated for every app if sender.__name__ == 'django.contrib.auth.models': update_all_contenttypes() create_csv_export_format(sender) for app_label in settings.INSTALLED_PTREE_APPS: if ptree.common.is_experiment_app(app_label): print 'Creating data exports for {}'.format(app_label) create_export(app_label)
def add_impersonate_permission(apps, schema_editor): update_all_contenttypes() # Fixes tests ContentType = apps.get_model('contenttypes.ContentType') Permission = apps.get_model('auth.Permission') content_type = ContentType.objects.using( schema_editor.connection.alias).get(app_label='auth', model='user') Permission.objects.using(schema_editor.connection.alias).create( content_type=content_type, codename='impersonate', name='Can impersonate other user' )
def _flush_db(self): from django import VERSION as DJANGO_VERSION from django.conf import settings from django.core.management import call_command call_command('flush', verbosity=0, interactive=False) # In Django <1.2 Depending on the order of certain post-syncdb # signals, ContentTypes can be removed accidentally. Manually delete and re-add all # and recreate ContentTypes if we're using the contenttypes app # See: http://code.djangoproject.com/ticket/9207 # See: http://code.djangoproject.com/ticket/7052 if DJANGO_VERSION[0] <= 1 and DJANGO_VERSION[1] < 2 \ and 'django.contrib.contenttypes' in settings.INSTALLED_APPS: # TODO: Only mysql actually needs this from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes from django.db import models from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Permission ContentType.objects.all().delete() ContentType.objects.clear_cache() update_all_contenttypes(verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new contenttypes start at 1 next_pk = 1 content_types = list(ContentType.objects.all().order_by('pk')) ContentType.objects.all().delete() for ct in content_types: ct.pk = next_pk ct.save() next_pk += 1 # Because of the same problems with ContentTypes, we can get # busted permissions Permission.objects.all().delete() for app in models.get_apps(): create_permissions(app=app, created_models=None, verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new permissions start at 1 next_pk = 1 permissions = list(Permission.objects.all().order_by('pk')) Permission.objects.all().delete() for perm in permissions: perm.pk = next_pk perm.save() next_pk += 1 logger.debug("Flushing database") self._num_flush_calls += 1
def forwards(self, orm): from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes ## Update ContentType table. Otherwise pki tables are missing update_all_contenttypes() for m in ('certificateauthority', 'certificate',): ct = ContentType.objects.get(model=m) if not Permission.objects.filter(content_type=ct, codename="can_download"): Permission(name="Can download", content_type=ct, codename="can_download").save()
def migrate_therapy(apps, schema_editor): update_all_contenttypes(interactive=False) ct = ContentType.objects.get_by_natural_key("services", "Therapy") ct_wt = ContentType.objects.get_by_natural_key("services", "WorkTherapy") ct_wtm = ContentType.objects.get_by_natural_key("services", "WorkTherapyMeeting") ct_cw = ContentType.objects.get_by_natural_key("services", "CommunityWork") Therapy = apps.get_model("services", "Therapy") WorkTherapy = apps.get_model("services", "WorkTherapy") WorkTherapyMeeting = apps.get_model("services", "WorkTherapyMeeting") CommunityWork = apps.get_model("services", "CommunityWork") count = 0 for service in WorkTherapy.objects.all(): if service.content_type_id == ct_wt.id: s = Therapy() s.work_therapy = True s.encounter = service.encounter s.title = service.title s.created = service.created s.modified = service.modified s.content_type_id = ct.id s.save() service.delete() count += 1 for service in WorkTherapyMeeting.objects.all(): if service.content_type_id == ct_wtm.id: s = Therapy() s.therapy_meeting = True s.encounter = service.encounter s.title = service.title s.created = service.created s.modified = service.modified s.content_type_id = ct.id s.save() service.delete() count += 1 for service in CommunityWork.objects.all(): if service.content_type_id == ct_cw.id: s = Therapy() s.community_work = True s.encounter = service.encounter s.title = service.title s.created = service.created s.modified = service.modified s.content_type_id = ct.id s.save() service.delete() count += 1 print 'Successfully migrated %d services of type Therapy' % count
def get_old_bookmarks(apps, schema_editor): update_all_contenttypes() BookMark = apps.get_model("bookmarks", "BookMark") User = apps.get_model("accounts", "User") ContentType = apps.get_model("contenttypes", "ContentType") ctype = ContentType.objects.get(app_label='compositions', model='composition') for user in User.objects.all(): for bookmark in user.bookmarks.all(): new_bookmark = BookMark.objects.create( owner = user, bookmark_type='AR', content_type = ctype, object_id=bookmark.id)
def add_default_approach_time(apps, schema_editor): TimeDotation = apps.get_model('services', 'TimeDotation') # create ct for IndirectService now (otherwise would be created in post_migrate signal, which is too late) update_all_contenttypes(interactive=False) data = ((apps.get_model('services', 'Approach')), 60) print 'Adding dotation for ct: %s' % data[0]._meta.object_name ct = ContentType.objects.get_by_natural_key( data[0]._meta.app_label, data[0]._meta.object_name.lower()) td, _ = TimeDotation.objects.get_or_create(content_type_id=ct.id, default_minutes=data[1], defaults={'minutes': data[1]})
def add_default_therapy_time(apps, schema_editor): TimeDotation = apps.get_model('services', 'TimeDotation') Therapy = apps.get_model('services', 'Therapy') work_therapy_ct = ContentType.objects.get_by_natural_key("services", "WorkTherapy") therapy_ct = ContentType.objects.get_by_natural_key('services', 'Therapy') # create ct for IndirectService now (otherwise would be created in post_migrate signal, which is too late) update_all_contenttypes(interactive=False) wt_td = TimeDotation.objects.get(content_type_id=work_therapy_ct.id) print 'Adding dotation for ct: %s' % Therapy._meta.object_name t_td, _ = TimeDotation.objects.get_or_create(content_type_id=therapy_ct.id, default_minutes=wt_td.default_minutes, defaults={'minutes': wt_td.minutes})
def add_default_approach_time(apps, schema_editor): TimeDotation = apps.get_model('services', 'TimeDotation') address_ct = ContentType.objects.get_by_natural_key("services", "address") # create ct for IndirectService now (otherwise would be created in post_migrate signal, which is too late) update_all_contenttypes(interactive=False) td = TimeDotation.objects.get(content_type_id=address_ct.id) data = ((apps.get_model('services', 'Approach')), td.minutes) print 'Adding dotation for ct: %s' % data[0]._meta.object_name ct = ContentType.objects.get_by_natural_key(data[0]._meta.app_label, data[0]._meta.object_name.lower()) td, _ = TimeDotation.objects.get_or_create(content_type_id=ct.id, default_minutes=60, defaults={'minutes': data[1]})
def add_access_to_all_objects_in_edition(apps, schema_editor): from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes from django.contrib.auth.models import Permission update_all_contenttypes() for model in ('collection', 'module', 'womi'): content_type = ContentType.objects.get(model=model, app_label='common') perm, created = Permission.objects.get_or_create(content_type=content_type, codename='can_access_all_' + model) if created: perm.name = u'Can access all %ss in online edition' % model perm.save() print('created permission: %s' % perm.name) else: print('permission already exists: %s' % perm.name)
def handle(self, *args, **options): try: import labgeeks.settings except ImportError: import sys sys.stderr.write("Couldn't find the settings.py module.") sys.exit(1) setup_environ(labgeeks.settings) # Add any missing content types update_all_contenttypes() # Add any missing permissions for app in get_apps(): create_permissions(app, None, 2)
def create_permissions(sender, **kwargs): update_all_contenttypes() # make sure all content types exist account_content = ContentType.objects.get(app_label='accounts', model='account') guest_content = ContentType.objects.get(app_label='accounts', model='guest') verification_content = ContentType.objects.get(app_label='accounts', model='verification') Permission.objects.get_or_create(codename='profile_change', name='Can change user profiles', content_type=account_content) Permission.objects.get_or_create(codename='guest_reviews', name='Can review guest applications', content_type=guest_content) Permission.objects.get_or_create(codename='account_verify', name='Can verifiy accounts', content_type=verification_content)
def create_posts(apps, schema_editor): update_all_contenttypes() Post = apps.get_model("posts", "Post") Interpretation = apps.get_model("interpretations", "Interpretation") User = apps.get_model("accounts", "User") ContentType = apps.get_model("contenttypes", "ContentType") ctype = ContentType.objects.get(app_label='interpretations', model='interpretation') for interpretation in Interpretation.objects.all(): post = Post() post.composition = interpretation.composition post.creator = User.objects.get(pk=interpretation.user.id) post.created = interpretation.created post.public = interpretation.public post.content_type = ctype post.object_id = interpretation.id post.content_object = interpretation post.save()
def forwards(self, orm): from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes ## Update ContentType table. Otherwise pki tables are missing update_all_contenttypes() for m in ( 'certificateauthority', 'certificate', ): ct = ContentType.objects.get(model=m) if not Permission.objects.filter(content_type=ct, codename="can_download"): Permission(name="Can download", content_type=ct, codename="can_download").save()
def migrate_up(manager): """ If the auth tables don't exist, we shouldn't try to set the permissions. See migration 059 """ if db_utils.auth_tables_exist(manager): management.setup_environ(settings) # These have to be imported after the environment is set up from django.contrib.contenttypes import management as content_management from django.contrib.auth import management as auth_management from django.db import models as db_models content_management.update_all_contenttypes() for app in db_models.get_apps(): auth_management.create_permissions(app, None, 2) manager.execute_script(migration_059.UP_SQL)
def add_more_user_roles_in_edition(apps, schema_editor): from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes from django.contrib.auth.models import Permission update_all_contenttypes() for permission in ('author', 'editor', 'publisher', 'reviewer'): for model in ('collection', 'module', 'womi'): content_type = ContentType.objects.get(model=model, app_label='common') # model_class = content_type.model_class() perm, created = Permission.objects.get_or_create(content_type=content_type, codename='has_%s_for_%s' % (permission, model)) if created: perm.name = u'Has role %s for %s' % (permission, model) perm.save() print('created permission: %s' % perm.name) else: print('permission already exists: %s' % perm.name)
def create_migration_options(apps, schema_editor): update_all_contenttypes() AdmirationOption = apps.get_model("admirations", "AdmirationOption") options = ['', 'Beautiful', 'Unusual', 'Thought-Provoking', 'Repulsive', 'Soothing', 'Saddening', 'Dark', 'Touching', 'Entertaining' ] for option in options: AdmirationOption.objects.create(word=option)
def copy_data(apps, schema_editor): Address = apps.get_model("services", "Address") Approach = apps.get_model("services", "Approach") update_all_contenttypes(interactive=False) approach_ct = ContentType.objects.get_by_natural_key("services", "Approach") address_ct = ContentType.objects.get_by_natural_key("services", "address") count = 0 for address in Address.objects.all().order_by('encounter__id').distinct(): if address.content_type_id == address_ct.id: approach = Approach() approach.encounter = address.encounter approach.title = u'%s (%s)' % (address.title, approach.number_of_addressed) approach.content_type_id = approach_ct.id approach.created = address.created approach.modified = address.modified approach.save() count += 1 print 'Successfully migrated %d services of type Address' % count
def handle_noargs(self, **options): from django.core.management import setup_environ try: import settings except ImportError: import sys sys.stderr.write("Couldn't find the settings.py module.") sys.exit(1) setup_environ(settings) # Add any missing content types from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() # Add any missing permissions from django.contrib.auth.management import create_permissions from django.db.models import get_apps for app in get_apps(): create_permissions(app, None, 2)
def install_models(app_name): app_module = load_app(get_plugin_module_name(app_name)) if have_south(app_name): lang = get_language() # invalidate south cache to avoid very weird bugs (see #2025) migration.Migrations.invalidate_all_modules() migration.Migrations.calculate_dependencies(force=True) # migrate plugin with south call_command('migrate', app=app_name) # call_command activates a default 'en-us' locale in thread. we restore it activate(lang) else: style = no_style() cursor = connection.cursor() sql_commands = sql_all(app_module, style, connection) for sql_command in sql_commands: cursor.execute(sql_command) # update all content types update_all_contenttypes() transaction.commit()
def init_customers_nc_instances_quota(apps, schema_editor): Customer = apps.get_model('structure', 'Customer') Quota = apps.get_model('quotas', 'Quota') ContentType = apps.get_model('contenttypes', 'ContentType') # sometimes django does not initiate customer content type, so we need update content types manually update_all_contenttypes() customer_ct = ContentType.objects.get(app_label='structure', model='customer') customer_qs = Customer.objects.all() customer_qs = customer_qs.annotate(instance_count=Count( 'projects__cloudprojectmembership__instances', distinct=True), ) for customer in customer_qs.iterator(): Quota.objects.create( # We need to add UUID explicitly, because django ignores auto=True parameter in migration UUID field uuid=uuid4().hex, name=RESOURCE_COUNT_QUOTA, content_type=customer_ct, object_id=customer.pk, usage=customer.instance_count, )
def add_default_times(apps, schema_editor): TimeDotation = apps.get_model('services', 'TimeDotation') # create ct for IndirectService now (otherwise would be created in post_migrate signal, which is too late) update_all_contenttypes(interactive=False) DATA = [ ((apps.get_model('services', 'Address')), 60), ((apps.get_model('services', 'ContactWork')), 10), ((apps.get_model('services', 'IncomeFormFillup')), 60), ((apps.get_model('services', 'IndividualCounselling')), 30), ((apps.get_model('services', 'GroupCounselling')), 120), ((apps.get_model('services', 'CrisisIntervention')), 30), ((apps.get_model('services', 'WorkTherapy')), 60), ((apps.get_model('services', 'SocialWork')), 30), ((apps.get_model('services', 'UtilityWork')), 30), ((apps.get_model('services', 'AsistService')), 30), ((apps.get_model('services', 'WorkWithFamily')), 30), ((apps.get_model('services', 'BasicMedicalTreatment')), 10), ((apps.get_model('services', 'PostUsage')), 20), ((apps.get_model('services', 'InformationService')), 5), ((apps.get_model('services', 'HarmReduction')), 5), ((apps.get_model('services', 'HygienicService')), 20), ((apps.get_model('services', 'FoodService')), 20), ((apps.get_model('services', 'DiseaseTest')), 30), ((apps.get_model('services', 'UrineTest')), 20), ((apps.get_model('services', 'IndirectService')), 10), ] for data in DATA: print 'Adding dotation for ct: %s' % data[0]._meta.object_name ct = ContentType.objects.get_by_natural_key( data[0]._meta.app_label, data[0]._meta.object_name.lower()) td, _ = TimeDotation.objects.get_or_create( content_type_id=ct.id, default_minutes=data[1], defaults={'minutes': data[1]})
def afterTest(self, test): """ Clean up any changes to the test database. """ # Restore transaction support on tests from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.db import connection, transaction from django.core.management import call_command from django.test.utils import setup_test_environment, teardown_test_environment from django import VERSION as DJANGO_VERSION if self._should_rebuild_schema(test): connection.creation.destroy_test_db( self.old_db, verbosity=self.verbosity) teardown_test_environment() setup_test_environment() connection.creation.create_test_db(verbosity=self.verbosity) return use_transaction_isolation = self._should_use_transaction_isolation( test, settings) using_django_testcase_management = self._should_use_django_testcase_management(test) if use_transaction_isolation \ and not using_django_testcase_management: self.restore_transaction_support(transaction) transaction.rollback() if transaction.is_managed(): transaction.leave_transaction_management() # If connection is not closed Postgres can go wild with # character encodings. connection.close() elif not use_transaction_isolation: # Have to clear the db even if we're using django because django # doesn't properly flush the database after a test. It relies on # flushing before a test, so we want to avoid the case where a django # test doesn't flush and then a normal test runs, because it will # expect the db to already be flushed ContentType.objects.clear_cache() # Otherwise django.contrib.auth.Permissions will depend on deleted ContentTypes call_command('flush', verbosity=0, interactive=False) # In Django <1.2 Depending on the order of certain post-syncdb # signals, ContentTypes can be removed accidentally. Manually delete and re-add all # and recreate ContentTypes if we're using the contenttypes app # See: http://code.djangoproject.com/ticket/9207 # See: http://code.djangoproject.com/ticket/7052 if DJANGO_VERSION[0] <= 1 and DJANGO_VERSION[1] < 2 \ and 'django.contrib.contenttypes' in settings.INSTALLED_APPS: from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes from django.db import models from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Permission ContentType.objects.all().delete() ContentType.objects.clear_cache() update_all_contenttypes(verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new contenttypes start at 1 next_pk = 1 content_types = list(ContentType.objects.all().order_by('pk')) ContentType.objects.all().delete() for ct in content_types: ct.pk = next_pk ct.save() next_pk += 1 # Because of the same problems with ContentTypes, we can get # busted permissions Permission.objects.all().delete() for app in models.get_apps(): create_permissions(app=app, created_models=None, verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new permissions start at 1 next_pk = 1 permissions = list(Permission.objects.all().order_by('pk')) Permission.objects.all().delete() for perm in permissions: perm.pk = next_pk perm.save() next_pk += 1 self.call_plugins_method('afterRollback', settings)
def afterTest(self, test): """ Clean up any changes to the test database. """ # Restore transaction support on tests from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.db import connection, transaction from django.core.management import call_command from django.test.utils import setup_test_environment, teardown_test_environment from django import VERSION as DJANGO_VERSION if self._should_rebuild_schema(test): connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity) teardown_test_environment() setup_test_environment() connection.creation.create_test_db(verbosity=self.verbosity) return use_transaction_isolation = self._should_use_transaction_isolation( test, settings) using_django_testcase_management = self._should_use_django_testcase_management( test) if use_transaction_isolation \ and not using_django_testcase_management: self.restore_transaction_support(transaction) transaction.rollback() if transaction.is_managed(): transaction.leave_transaction_management() # If connection is not closed Postgres can go wild with # character encodings. connection.close() elif not use_transaction_isolation: # Have to clear the db even if we're using django because django # doesn't properly flush the database after a test. It relies on # flushing before a test, so we want to avoid the case where a django # test doesn't flush and then a normal test runs, because it will # expect the db to already be flushed ContentType.objects.clear_cache( ) # Otherwise django.contrib.auth.Permissions will depend on deleted ContentTypes call_command('flush', verbosity=0, interactive=False) # In Django <1.2 Depending on the order of certain post-syncdb # signals, ContentTypes can be removed accidentally. Manually delete and re-add all # and recreate ContentTypes if we're using the contenttypes app # See: http://code.djangoproject.com/ticket/9207 # See: http://code.djangoproject.com/ticket/7052 if DJANGO_VERSION[0] <= 1 and DJANGO_VERSION[1] < 2 \ and 'django.contrib.contenttypes' in settings.INSTALLED_APPS: from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.management import update_all_contenttypes from django.db import models from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Permission ContentType.objects.all().delete() ContentType.objects.clear_cache() update_all_contenttypes(verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new contenttypes start at 1 next_pk = 1 content_types = list(ContentType.objects.all().order_by('pk')) ContentType.objects.all().delete() for ct in content_types: ct.pk = next_pk ct.save() next_pk += 1 # Because of the same problems with ContentTypes, we can get # busted permissions Permission.objects.all().delete() for app in models.get_apps(): create_permissions(app=app, created_models=None, verbosity=0) # Because of various ways of handling auto-increment, we need to # make sure the new permissions start at 1 next_pk = 1 permissions = list(Permission.objects.all().order_by('pk')) Permission.objects.all().delete() for perm in permissions: perm.pk = next_pk perm.save() next_pk += 1
#!/usr/bin/env python from django.core.management import setup_environ try: import settings except ImportError: import sys sys.stderr.write("Couldn't find the settings.py module.") sys.exit(1) setup_environ(settings) # Add any missing content types from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() # Add any missing permissions from django.contrib.auth.management import create_permissions from django.db.models import get_apps for app in get_apps(): create_permissions(app, None, 2) print('done!')
from django.core.management import setup_environ try: import config.settings_dev except ImportError: import sys sys.stderr.write("Couldn't find the settings.py module.") sys.exit(1) setup_environ(config.settings_dev) # Add any missing content types from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() # Add any missing permissions from django.contrib.auth.management import create_permissions from django.db.models import get_apps for app in get_apps(): create_permissions(app, None, 2)
class JumoTestSuiteRunner(CITestSuiteRunner): def setup_databases(self, **kwargs): from django.db import connections, DEFAULT_DB_ALIAS # First pass -- work out which databases actually need to be created, # and which ones are test mirrors or duplicate entries in DATABASES mirrored_aliases = {} test_databases = {} dependencies = {} for alias in connections: connection = connections[alias] if connection.settings_dict['TEST_MIRROR']: # If the database is marked as a test mirror, save # the alias. mirrored_aliases[alias] = connection.settings_dict[ 'TEST_MIRROR'] else: # Store the (engine, name) pair. If we have two aliases # with the same pair, we only need to create the test database # once. test_databases.setdefault(( connection.settings_dict['HOST'], connection.settings_dict['PORT'], connection.settings_dict['ENGINE'], connection.settings_dict['NAME'], ), []).append(alias) if 'TEST_DEPENDENCIES' in connection.settings_dict: dependencies[alias] = connection.settings_dict[ 'TEST_DEPENDENCIES'] else: if alias != 'default': dependencies[alias] = connection.settings_dict.get( 'TEST_DEPENDENCIES', ['default']) # Second pass -- actually create the databases. old_names = [] mirrors = [] db_schemas = settings.DATABASE_CREATE_SCHEMAS for (host, port, engine, db_name), aliases in dependency_ordered(test_databases.items(), dependencies): # Actually create the database for the first connection connection = connections[aliases[0]] old_names.append((connection, db_name, True)) #test_db_name = connection.creation._create_test_db(self.verbosity, autoclobber=not self.interactive) test_db_name = create_test_db(connection, self.verbosity, not self.interactive) #Create Tables Via Schema File try: schema_file = db_schemas[aliases[0]] schema_string = "" with open(schema_file) as fh: schema_string = fh.read() print "Building Tables For %s from %s" % (test_db_name, schema_file) cursor = connection.cursor() connection.autocommit = True cursor.execute(schema_string) cursor.close() except Exception, e: sys.stderr.write( "Got an loading the schema file database: %s\n" % e) print "Tests Canceled" sys.exit(1) for alias in aliases[1:]: connection = connections[alias] if db_name: old_names.append((connection, db_name, False)) connection.settings_dict['NAME'] = test_db_name else: # If settings_dict['NAME'] isn't defined, we have a backend where # the name isn't important -- e.g., SQLite, which uses :memory:. # Force create the database instead of assuming it's a duplicate. old_names.append((connection, db_name, True)) connection.creation.create_test_db( self.verbosity, autoclobber=not self.interactive) from django.core.management import call_command from django.contrib.contenttypes.management import update_all_contenttypes update_all_contenttypes() call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=DEFAULT_DB_ALIAS) for alias, mirror_alias in mirrored_aliases.items(): mirrors.append((alias, connections[alias].settings_dict['NAME'])) connections[alias].settings_dict['NAME'] = connections[ mirror_alias].settings_dict['NAME'] return old_names, mirrors