def add_place_subscriptions(apps, schema_editor): """Adds place subscribers Only adds users who signed up for activities three weeks ago or later """ app_config = apps.get_app_config('places') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) User = apps.get_model('users', 'User') Place = apps.get_model('places', 'Place') PlaceSubscription = apps.get_model('places', 'PlaceSubscription') Conversation = apps.get_model('conversations', 'Conversation') ConversationParticipant = apps.get_model('conversations', 'ConversationParticipant') ContentType = apps.get_model('contenttypes', 'ContentType') ct = ContentType.objects.get(app_label='places', model='place') for place in Place.objects.all(): conversation, _ = Conversation.objects.get_or_create( target_type=ct, target_id=place.id) some_time_ago = timezone.now() - relativedelta(days=21) recent_participants = User.objects.filter( activities__date__startswith__gte=some_time_ago, activities__place_id=place.id, ).distinct() for user in recent_participants: PlaceSubscription.objects.create(user=user, place=place) ConversationParticipant.objects.create(user=user, conversation=conversation)
def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): if not app_config.models_module: return # Ensure that contenttypes are created for this app. Needed if # 'django.contrib.auth' is in INSTALLED_APPS before # 'django.contrib.contenttypes'. create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs) app_label = app_config.label try: app_config = apps.get_app_config(app_label) ContentType = apps.get_model('contenttypes', 'ContentType') Permission = apps.get_model('auth', 'Permission') except LookupError: return if not router.allow_migrate_model(using, Permission): return # This will hold the permissions we're looking for as # (content_type, (codename, name)) searched_perms = [] # The codenames and ctypes that should exist. ctypes = set() for klass in app_config.get_models(): # Force looking up the content types in the current database # before creating foreign keys to them. ctype = ContentType.objects.db_manager(using).get_for_model(klass) ctypes.add(ctype) for perm in _get_all_permissions(klass._meta): searched_perms.append((ctype, perm)) # Find all the Permissions that have a content_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set( Permission.objects.using(using).filter( content_type__in=ctypes, ).values_list("content_type", "codename")) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] Permission.objects.using(using).bulk_create(perms) if verbosity >= 2: for perm in perms: print("Adding permission '%s'" % perm)
def test_unavailable_content_type_model(self): """ A ContentType shouldn't be created if the model isn't available. """ apps = Apps() with self.assertNumQueries(0): contenttypes_management.create_contenttypes(self.app_config, interactive=False, verbosity=0, apps=apps) self.assertEqual(ContentType.objects.count(), self.before_count + 1)
def add(apps, editor): app_config = apps.get_app_config('thesis') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) create_permissions(app_config) Group.objects.get(name='student').permissions.add( Permission.objects.get_by_natural_key('view_reservation', 'thesis', 'reservation'))
def forward(apps, schema_editor): from django.apps import apps as django_apps from django.contrib.contenttypes.management import create_contenttypes apps = django_apps.get_app_configs() for app in apps: create_contenttypes(app) call_command("loaddata", "default_admin_index.json")
def forwards_func(apps, schema_editor): Group = apps.get_model('auth', 'Group') Perm = apps.get_model('auth', 'Permission') ContentType = apps.get_model('contenttypes', 'ContentType') for custom in MODELS: ct = ContentType.objects.filter( model=custom.replace(" ", "") ) if ct.exists(): ct.delete() for app_config in apps.get_app_configs(): app_config.models_module = True create_permissions(app_config, verbosity=0) app_config.models_module = None for app_config in apps.get_app_configs(): app_config.models_module = True create_contenttypes(app_config, verbosity=0) app_config.models_module = None for custom in MODELS: ContentType.objects.get_or_create( app_label='api', model=custom.replace(" ", "") ) for group in GROUPS: new_group, created = Group.objects.get_or_create(name=group) for model in MODELS: for permission in PERMISSIONS: name = 'Can {} {}'.format(permission, model) print("Creating {}".format(name)) model_comb = model value = model_comb.replace(" ", "") codename = '{}_{}'.format(permission, value) try: content_type = \ ContentType.objects.get(model=value) model_add_perm, created = \ Perm.objects.get_or_create(codename=codename, name=name, content_type= content_type) except Perm.DoesNotExist: logging.warning("Permission not found with name '{}'.". format(name)) continue new_group.permissions.add(model_add_perm) print("Moved permissions to api app.")
def update_admin_index(sender, **kwargs): from django_admin_index.models import AppGroup AppGroup.objects.all().delete() for app in settings.INSTALLED_APPS: if app.startswith("archiefvernietigingscomponent"): app_config = apps.get_app_config(app.split(".")[-1]) create_contenttypes(app_config) call_command("loaddata", "admin_index", verbosity=0)
def update_admin_index(sender, **kwargs): from django_admin_index.models import AppGroup AppGroup.objects.all().delete() # Make sure Open Personen models are registered. for app in settings.INSTALLED_APPS: if app.startswith("openpersonen"): app_config = apps.get_app_config(app.split(".")[-1]) create_contenttypes(app_config) call_command("loaddata", "default_admin_index", verbosity=0)
def create_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): if not app_config.models_module: return # Ensure that contenttypes are created for this app. Needed if # 'django.contrib.auth' is in INSTALLED_APPS before # 'django.contrib.contenttypes'. create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs) app_label = app_config.label try: app_config = apps.get_app_config(app_label) ContentType = apps.get_model('contenttypes', 'ContentType') Permission = apps.get_model('auth', 'Permission') except LookupError: return if not router.allow_migrate_model(using, Permission): return # This will hold the permissions we're looking for as # (content_type, (codename, name)) searched_perms = [] # The codenames and ctypes that should exist. ctypes = set() for klass in app_config.get_models(): # Force looking up the content types in the current database # before creating foreign keys to them. ctype = ContentType.objects.db_manager(using).get_for_model(klass, for_concrete_model=False) ctypes.add(ctype) for perm in _get_all_permissions(klass._meta): searched_perms.append((ctype, perm)) # Find all the Permissions that have a content_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set(Permission.objects.using(using).filter( content_type__in=ctypes, ).values_list( "content_type", "codename" )) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] Permission.objects.using(using).bulk_create(perms) if verbosity >= 2: for perm in perms: print("Adding permission '%s'" % perm)
def add_primary_interests(apps, schema_editor): Category = apps.get_model('exams', 'Category') ContentType = apps.get_model('contenttypes', 'ContentType') Exam = apps.get_model('exams', 'Exam') PrimaryInterest = apps.get_model('accounts', 'PrimaryInterest') Profile = apps.get_model('accounts', 'Profile') exam_app = apps.app_configs['exams'] exam_app.models_module = exam_app.models_module or True create_contenttypes(exam_app, apps=apps) exam_content_type = ContentType.objects.get(app_label='exams', model='exam') category_content_type = ContentType.objects.get(app_label='exams', model='category') if Exam.objects.filter(name="SMLE").exists(): smle, was_created = PrimaryInterest.objects.get_or_create( name="SMLE", object_id=Exam.objects.get(name="SMLE").pk, content_type=exam_content_type) Profile.objects.filter(main_interest="SMLE").update( primary_interest=smle) if Exam.objects.filter(name="SNLE").exists(): snle, was_created = PrimaryInterest.objects.get_or_create( name="SNLE", object_id=Exam.objects.get(name="SNLE").pk, content_type=exam_content_type) Profile.objects.filter(main_interest="SNLE").update( primary_interest=snle) if Exam.objects.filter(name="Pediatrics Saudi Board").exists(): residencies, was_created = PrimaryInterest.objects.get_or_create( name="Residency programs") PrimaryInterest.objects.get_or_create( name="Pediatric Saudi Board", parent=residencies, object_id=Exam.objects.get(name="Pediatrics Saudi Board").pk, content_type=exam_content_type) colleges, was_created = PrimaryInterest.objects.get_or_create( name="College programs") ksau_hs, was_created = PrimaryInterest.objects.get_or_create( name="King Saud bin Abdulaziz University for Health Sciences", parent=colleges, object_id=Category.objects.get(name="KSAU-HS").pk, content_type=category_content_type) Profile.objects.filter(main_interest="COLLEGES").update( primary_interest=ksau_hs)
def create_spam_filter_example(apps, schema_editor): # Hackery for ensuring all the apps are available from django.apps.registry import apps as dapps apps = dapps sf_config = apps.get_app_config('spam_filtering') sf_config.models_module = sf_config.models_module or True create_contenttypes(sf_config) sl_config = apps.get_app_config('supervised_learning') sl_config.models_module = sl_config.models_module or True create_contenttypes(sl_config) # SpamFilter = apps.get_model("spam_filtering", "SpamFilter") SVC = apps.get_model("supervised_learning", "SVC") DataColumn = apps.get_model("base", "DataColumn") ContentType = apps.get_model("contenttypes", "ContentType") svm = SVC( name="SVM for Spam (example)", kernel="linear", penalty_parameter=0.1, ) svm.save() sf = SpamFilter( name="Spam Filter for Comments (example)", classifier="supervised_learning.SVC|SVM for Spam (example)", counter_threshold=5, threshold_actions=":recalculate", spam_model_is_enabled=True, spam_model_model="examples.CommentOfMySite", labels_column="examples.commentofmysite.is_spam", pretraining="examples.SFPTYoutube", cv_is_enabled=True, cv_folds=10, cv_metric="average_precision", bow_is_enabled=True, bow_analyzer="word", bow_ngram_range_min=1, bow_ngram_range_max=3, bow_max_df=0.9, bow_min_df=0.001) sf.save() dc = DataColumn(content_type=ContentType.objects.get( model="spamfilter", app_label="spam_filtering"), object_id=sf.id, ref_model=ContentType.objects.get(model="commentofmysite", app_label="examples"), ref_column="comment", position=0) dc.save()
def handle(self, *args, **options): if options['apps']: app_names = options['apps'].split(',') apps = [django_apps.get_app_config(x) for x in app_names] else: apps = django_apps.get_app_configs() if options['create_only']: do_create, do_update = True, False elif options['update_only']: do_create, do_update = False, True else: do_create, do_update = True, True for app in apps: if DJANGO_VERSION < (2, 2): # see https://github.com/django/django/commit/bec651a427fc032d9115d30c8c5d0e702d754f6c # Ensure that contenttypes are created for this app. Needed if # 'django.contrib.auth' is in INSTALLED_APPS before # 'django.contrib.contenttypes'. from django.contrib.contenttypes.management import create_contenttypes create_contenttypes(app, verbosity=options['verbosity']) if do_create: # create permissions if they do not exist create_permissions(app, options['verbosity']) if do_update: # update permission name's if changed for model in app.get_models(): content_type = ContentType.objects.get_for_model(model) for codename, name in _get_all_permissions(model._meta): try: permission = Permission.objects.get( codename=codename, content_type=content_type) except Permission.DoesNotExist: continue if permission.name != name: old_str = str(permission) permission.name = name if options['verbosity'] >= 2: self.stdout.write( self.style.SUCCESS( "Update permission '%s' to '%s'" % (old_str, permission))) permission.save()
def create_import_permission(apps, schema_editor): app_config = apps.get_app_config("wagtail_transfer") # Ensure content types from previous migrations are created. This is normally done # in a post_migrate signal, see # https://github.com/django/django/blob/3.2/django/contrib/contenttypes/apps.py#L21 app_config.models_module = getattr(app_config, 'models_module', None) or True create_contenttypes(app_config) ContentType = apps.get_model("contenttypes", "ContentType") content_type = ContentType.objects.get( app_label="wagtail_transfer", model="idmapping" ) Permission = apps.get_model("auth", "Permission") Permission.objects.get_or_create( content_type=content_type, codename="wagtailtransfer_can_import", name="Can import pages and snippets from other sites", )
def forward(apps, schema_editor): from django.apps import apps as django_apps from django.contrib.contenttypes.management import create_contenttypes app = django_apps.get_app_config("django_auth_adfs_db") create_contenttypes(app) AppGroup = apps.get_model("admin_index.AppGroup") ContentType = apps.get_model("admin_index.ContentTypeProxy") ADFSConfig = apps.get_model("django_auth_adfs_db.ADFSConfig") max_order = AppGroup.objects.aggregate(max=Max("order"))["max"] or 0 config, _ = AppGroup.objects.get_or_create(slug="configuration", defaults={ "name": "Configuratie", "order": max_order + 1, }) ct = ContentType.objects.get_for_model(ADFSConfig) config.models.add(ct)
def migrate_old_content(apps, schema_editor): try: Comment = apps.get_model(comments_app_name, 'XtdComment') except LookupError: # django_comments_xtd isn't installed. return create_contenttypes(apps.app_configs['contenttypes']) JobReviewComment = apps.get_model('jobs', 'JobReviewComment') Job = apps.get_model('jobs', 'Job') ContentType = apps.get_model('contenttypes', 'ContentType') db_alias = schema_editor.connection.alias try: # 'ContentType.name' is now a property in Django 1.8 so we # can't use it to query a ContentType anymore. job_contenttype = ContentType.objects.using(db_alias).get( model=content_type) except ContentType.DoesNotExist: return old_comments = Comment.objects.using(db_alias).filter( content_type=job_contenttype.pk, is_public=True, is_removed=False, ) found_jobs = {} comments = [] for comment in old_comments: try: job = found_jobs[comment.object_pk] except KeyError: try: job = Job.objects.using(db_alias).get(pk=comment.object_pk) found_jobs[comment.object_pk] = job except Job.DoesNotExist: continue review_comment = JobReviewComment( job=job, comment=MARKER + comment.comment, creator=comment.user, created=comment.submit_date, updated=now(), ) comments.append(review_comment) JobReviewComment.objects.using(db_alias).bulk_create(comments)
def handle(self, *args, **options): if options['apps']: app_names = options['apps'].split(',') apps = [django_apps.get_app_config(x) for x in app_names] else: apps = django_apps.get_app_configs() if options['create_only']: do_create, do_update = True, False elif options['update_only']: do_create, do_update = False, True else: do_create, do_update = True, True for app in apps: if DJANGO_VERSION < (2, 2): # see https://github.com/django/django/commit/bec651a427fc032d9115d30c8c5d0e702d754f6c # Ensure that contenttypes are created for this app. Needed if # 'django.contrib.auth' is in INSTALLED_APPS before # 'django.contrib.contenttypes'. from django.contrib.contenttypes.management import create_contenttypes create_contenttypes(app, verbosity=options['verbosity']) if do_create: # create permissions if they do not exist create_permissions(app, options['verbosity']) if do_update: # update permission name's if changed for model in app.get_models(): content_type = ContentType.objects.get_for_model(model) for codename, name in _get_all_permissions(model._meta): try: permission = Permission.objects.get(codename=codename, content_type=content_type) except Permission.DoesNotExist: continue if permission.name != name: old_str = str(permission) permission.name = name if options['verbosity'] >= 2: self.stdout.write(self.style.SUCCESS("Update permission '%s' to '%s'" % (old_str, permission))) permission.save()
def test_migration(self): apps = self.apps # Below forces content types to be created for the migrated items from django.contrib.contenttypes.management import create_contenttypes app_config = apps.get_app_config('aristotle_dse') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) DataSetSpecification = self.apps.get_model('aristotle_dse', 'DataSetSpecification') CustomField = self.apps.get_model('aristotle_mdr_custom_fields', 'CustomField') ContentType = self.apps.get_model('contenttypes', 'ContentType') CustomValue = self.apps.get_model('aristotle_mdr_custom_fields', 'CustomValue') ctype = ContentType.objects.get( app_label='aristotle_dse', model='datasetspecification', ) self.dss = DataSetSpecification.objects.get(pk=self.dss.pk) isd_field = CustomField.objects.get( name="Implementation Start Date", allowed_model=ctype, ) ied_field = CustomField.objects.get( name="Implementation End Date", allowed_model=ctype, ) self.assertEqual('date', isd_field.type) self.assertEqual('date', ied_field.type) isd_value = CustomValue.objects.get(field=isd_field, concept=self.dss) ied_value = CustomValue.objects.get(field=ied_field, concept=self.dss) self.assertEqual(isd_value.content, self.implementation_start_date) self.assertEqual(ied_value.content, self.implementation_end_date)
def post_migrate_create_user_permissions( app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): # Ensure that contenttypes are created for this app. create_contenttypes( app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs) try: app_config = apps.get_app_config(app_config.label) ContentType = apps.get_model('contenttypes', 'ContentType') Permission = apps.get_model('auth', 'Permission') User = apps.get_model('auth_uuid', 'UserUUID') ctype = ContentType.objects.get_for_model(User, for_concrete_model=False) all_perms = set(Permission.objects.filter(content_type=ctype).values_list( 'content_type', 'codename' )) searched_perms = [] for perm in settings.AUTH_USER_ALL_PERMISSIONS: searched_perms.append((ctype, perm)) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] Permission.objects.bulk_create(perms) except LookupError: pass
def database_forwards(self, app_label, schema_editor, from_state, to_state): apps = from_state.apps ContentType = apps.get_model('contenttypes', 'ContentType') if ContentType.objects.count() == 0: # Below forces content types to be created for the migrated items # In production, contenttypes should already be loaded from django.contrib.contenttypes.management import create_contenttypes app_config = apps.get_app_config(self.app_label.lower()) app_config.models_module = app_config.models_module or True create_contenttypes(app_config) # Only add custom field if there are any items MigratedModel = apps.get_model(self.app_label, self.model_name) if MigratedModel.objects.count() == 0: return CustomField = apps.get_model('aristotle_mdr_custom_fields', 'CustomField') CustomValue = apps.get_model('aristotle_mdr_custom_fields', 'CustomValue') ctype = ContentType.objects.get( app_label=self.app_label.lower(), model=self.model_name.lower(), ) custom_field, c = CustomField.objects.get_or_create( name=self.custom_field_name, type=self.custom_field_type, allowed_model=ctype, defaults=self.custom_field_kwargs) for obj in MigratedModel.objects.all(): if getattr(obj, self.field_name): CustomValue.objects.create(field=custom_field, concept=obj, content=getattr( obj, self.field_name))
def migrate_old_content(apps, schema_editor): try: Comment = apps.get_model(comments_app_name, 'XtdComment') except LookupError: # django_comments_xtd isn't installed. return create_contenttypes(apps.app_configs['contenttypes']) JobReviewComment = apps.get_model('jobs', 'JobReviewComment') Job = apps.get_model('jobs', 'Job') ContentType = apps.get_model('contenttypes', 'ContentType') db_alias = schema_editor.connection.alias try: # 'ContentType.name' is now a property in Django 1.8 so we # can't use it to query a ContentType anymore. job_contenttype = ContentType.objects.using(db_alias).get(model=content_type) except ContentType.DoesNotExist: return old_comments = Comment.objects.using(db_alias).filter( content_type=job_contenttype.pk, is_public=True, is_removed=False, ) found_jobs = {} comments = [] for comment in old_comments: try: job = found_jobs[comment.object_pk] except KeyError: try: job = Job.objects.using(db_alias).get(pk=comment.object_pk) found_jobs[comment.object_pk] = job except Job.DoesNotExist: continue review_comment = JobReviewComment( job=job, comment=MARKER + comment.comment, creator=comment.user, created=comment.submit_date, updated=now(), ) comments.append(review_comment) JobReviewComment.objects.using(db_alias).bulk_create(comments)
def setUpBeforeMigration(self, apps): # Below forces content types to be created for the migrated items from django.contrib.contenttypes.management import create_contenttypes app_config = apps.get_app_config('aristotle_dse') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) app_config = apps.get_app_config('aristotle_dse') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) DataSetSpecification = apps.get_model('aristotle_dse', 'DataSetSpecification') self.implementation_start_date = "2018-01-01" self.implementation_end_date = "2019-01-01" self.dss = DataSetSpecification.objects.create( name='My DSS', definition='test defn', implementation_start_date=self.implementation_start_date, implementation_end_date=self.implementation_end_date, )
def accessrightinvites_to_userinvites(apps, schema_editor): AccessRightInvite = apps.get_model("document", "AccessRightInvite") AccessRight = apps.get_model("document", "AccessRight") UserInvite = apps.get_model("user", "UserInvite") ContentType = apps.get_model("contenttypes", "ContentType") user_app_config = apps.get_app_config("user") user_app_config.models_module = True create_contenttypes(user_app_config) userinvite_ct = ContentType.objects.get( app_label="user", model="userinvite" ) for ari in AccessRightInvite.objects.all(): ui = UserInvite.objects.create( email=ari.email, key=ari.id, username=ari.email, by=ari.document.owner, ) AccessRight.objects.create( document=ari.document, holder_id=ui.id, holder_type=userinvite_ct, rights=ari.rights, )
def hydrate_models_and_permissions(app_config): """ Setup content types, base permissions and data source specific permission groups for use in the admin. """ try: create_contenttypes(app_config, interactive=False, verbosity=4) except OperationalError as e: logger.error("Error creating content-types: %s" % e) try: create_permissions(app_config, interactive=False, verbosity=4) except OperationalError as e: logger.error("Error creating permissions: %s" % e) try: build_permission_groups(app_config.name) except (OperationalError, AppRegistryNotReady) as e: logger.error("Error creating permission groups: %s" % e) try: build_tag_permission_group() except (OperationalError, AppRegistryNotReady) as e: logger.error("Error creating tagging perm group: %s" % e)
def handle(self, *args, **options): # Autoconfigure Debugger self.set_name("CODENERIX") self.set_debug() self.debug("Settings permissions for:", color='blue') # Get list of apps self.debug("Getting list of APPs", color='blue') apps_config = apps.get_app_configs() apps_total = len(apps_config) # Create missins permissions self.debug("Creating missing permissions", color='blue') idx = 1 for app_config in apps_config: self.debug(" -> {}/{} {}".format(idx, apps_total, app_config.label), color='cyan') create_permissions(app_config, apps=apps, verbosity=0) idx += 1 # Update contenttypes self.debug("Updating Content Types", color='blue') idx = 1 for app_config in apps_config: self.debug(" -> {}/{} {}".format(idx, apps_total, app_config.label), color='cyan') create_contenttypes(app_config) idx += 1 # Get all users from the system person = None for user in User.objects.all(): self.debug(" > {} ".format(user.username), color='cyan', tail=None) if hasattr(user, 'person') and user.person: self.debug("OK".format(user.username), color='green', header=None) user.person.refresh_permissions() person = user.person else: self.debug("NO PERSON".format(user.username), color='red', header=None) # Remake groups permissions if we have at least one valid user if person: self.debug( "Refreshing group permissions (it may takes over a minute)... ", color='blue', tail=None) person.__class__.group_permissions(person.__class__) self.debug("DONE", color='green', header=None) else: self.debug( "Can not refresh group permissions because I didn't find a user with a Person", color='red')
def update_environment_contenttypes(*args): environments = apps.get_app_config('environments') create_contenttypes(environments)
def add_initial_data(apps, schema_editor): chapter = apps.get_model("authentication", "chapter") Group = apps.get_model("auth", "group") Permission = apps.get_model("auth", "permission") ContentType = apps.get_model('contenttypes', 'ContentType') # Make sure contenttypes are updated app_config = apps.get_app_config('authentication') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) chapter.objects.bulk_create( [ chapter( location='ASRG-S', city='Stuttgart', country='Germany', lead='Sven Schran', foundation=datetime(2018, 7, 18, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-D', city='Detroit', country='USA', lead='Brandon Barry', foundation=datetime(2018, 5, 2, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-TLV', city='Tel Aviv', country='Israel', lead='Eli Ben Ami', foundation=datetime(2019, 2, 28, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-C', city='Cluj-Napoca', country='Romania', lead='Dan Paunescu', foundation=datetime(2019, 4, 10, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-SIN', city='Singapore', country='Singapore', lead='Alina Tan', foundation=datetime(2019, 8, 12, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-MUC', city='Munich', country='Germany', lead='Jana von Wedel', foundation=datetime(2019, 11, 4, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-CAI', city='Cairo', country='Egypt', lead='Mohamed Madbouly', foundation=datetime(2019, 11, 10, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-SHA', city='Shanghai', country='China', lead='', foundation=datetime(2020, 1, 4, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-BER', city='Berlin', country='Germany', lead='Christian Schmidt-Janssen', foundation=datetime(2020, 1, 6, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-PIT', city='Pittsburgh', country='USA', lead='Shalabh Jain', foundation=datetime(2020, 1, 26, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-SFO', city='San Francisco', country='USA', lead='', foundation=datetime(2020, 1, 27, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-FRA', city='Darmstadt', country='Germany', lead='Patric Lenhart', foundation=datetime(2020, 2, 20, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-JPN', city='Tokyo', country='Japan', lead='M. Kamel Ghali', foundation=datetime(2020, 2, 15, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-OXF', city='Oxford', country='UK', lead='Erica Yang', foundation=datetime(2020, 2, 20, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-SYD', city='Sydney', country='Australia', lead='Jasmine Rhyder', foundation=datetime(2020, 3, 11, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-IASI', city='Iasi', country='Romania', lead='Irina-Georgiana Oancea', foundation=datetime(2020, 3, 16, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-DNCR', city='Delhi', country='India', lead='Shaurya Singh', foundation=datetime(2020, 3, 31, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-DAY', city='Dayton', country='USA', lead='Bryan Fite', foundation=datetime(2020, 4, 9, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-REC', city='Recife', country='Brasil', lead='Divanilson Campelo', foundation=datetime(2020, 4, 9, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-BLR', city='Bangalore', country='India', lead='Koushik Pal', foundation=datetime(2020, 4, 10, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-LAX', city='Los Angeles', country='USA', lead='Kyle Crockett', foundation=datetime(2020, 4, 16, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-BUC', city='Bucharest', country='Romania', lead='Anas Ahammed K A', foundation=datetime(2020, 7, 21, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-QRO', city='Querétaro', country='Mexico', lead='Alonso Jáuregui Martínez', foundation=datetime(2020, 6, 29, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-CGN', city='Cologne', country='Germany', lead='Stefan Würth', foundation=datetime(2020, 6, 29, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-TOR', city='Toronto', country='Canada', lead='AJ Khan', foundation=datetime(2020, 7, 1, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-WIN', city='Windsor', country='Canada', lead='Ikjot Saini', foundation=datetime(2020, 7, 1, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-KER', city='Kochi', country='India', lead='Anas Ahammed K A', foundation=datetime(2020, 7, 21, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-VIE', city='Vienna', country='Austria', lead='Martin Schmiedecker', foundation=datetime(2020, 7, 27, 0, 0, 0, 0, pytz.UTC), ), chapter( location='ASRG-HYD', city='Hyderabad', country='India', lead='Shravan Paidipala', foundation=datetime(2020, 7, 27, 0, 0, 0, 0, pytz.UTC), ), ] ) ct = ContentType.objects.get(app_label='authentication', model='chapter') for chp in chapter.objects.all(): location = chp.location # Create permissions needed for the new chapter asrg_perm, _ = Permission.objects.get_or_create(codename=location, name=location, content_type=ct) local_lead_perm, _ = Permission.objects.get_or_create( codename=f'local-lead-{location}', name=f'Local lead {location}', content_type=ct ) # Add groups for the new chapters that will be used to granualarly add permissions asrg_goup, _ = Group.objects.get_or_create(name=location) asrg_goup.permissions.add(asrg_perm) local_lead_group, _ = Group.objects.get_or_create(name=f'local-lead-{location}') local_lead_group.permissions.add(local_lead_perm)
def update_all_contenttypes(**kwargs): for app_config in apps.get_app_configs(): create_contenttypes(app_config, **kwargs)
def migrate_wagtail_page_type(apps, schema_editor, mapping): ''' Fairly generic method to convert all instances of one direct subtype of wagtail page into another. Returns the number of converted pages. This is supposed to be called from a data migration operation function. https://docs.djangoproject.com/en/2.2/topics/migrations/#data-migrations Method: A record from the specific Page type table is copied into the new type table. But the parent record in wagtailcore_page is left intact (same id, title, slug, webpath) apart from its content_type. All fields with the same names are automatically copied. Other fields and custom transforms can be done in a custom copy function attached to the 'mapping' dictionary. An OPTIONAL 'select' entry in 'mapping' links to a function that can further filter the default django queryset of all pages to convert. IT IS RECOMMENDED TO BACK UP YOUR DATABASE BEFORE USING THIS FUNCTION. Example: def my_migration_operation(apps, schema_editor): def copy(page_from, page_to): page_to.field_a = page_from.field_b def select(qs): return qs.filter(title__icontains='banana') mapping = { 'models': { 'from': ('kdl_wagtail_page', 'RichPage'), 'to': ('kdl_wagtail_core', 'RichTextPage'), }, 'copy': copy, 'select': select, } convert_pages(apps, schema_editor, mapping) ''' PageRevision = apps.get_model('wagtailcore', 'PageRevision') PageFrom = apps.get_model(*mapping['models']['from']) PageTo = apps.get_model(*mapping['models']['to']) # see ClusterableModel.to_json() def to_json(page): from wagtail.core.models import Page return json.dumps(Page.serializable_data(page), cls=DjangoJSONEncoder) pages_to = [] pages_from = PageFrom.objects.all() select = mapping.get('select', None) if select: pages_from = select(pages_from) if pages_from.count() < 1: return pages_to copy = mapping.get('copy', None) # make sure all content_types are present in the DB # see https://stackoverflow.com/a/42791235/3748764 from django.apps import apps as global_apps create_contenttypes(global_apps.get_app_config(mapping['models']['to'][0]), verbosity=0, interactive=False) # get the content type of PageTo ContentType = apps.get_model('contenttypes', 'ContentType') content_type_to = ContentType.objects.filter( app_label=mapping['models']['to'][0], model=mapping['models']['to'][1].lower()).first() for page_from in pages_from: page_to = PageTo() # naive conversion: we copy all the fields which have a common name # this will at least copy all the fields from Page table # See wagtail.core.models.Page.copy() for field in page_to._meta.get_fields(): # Ignore reverse relations if field.auto_created: continue # Ignore m2m relations - they will be copied as child objects # if modelcluster supports them at all (as it does for tags) if field.many_to_many: continue if hasattr(page_from, field.name): setattr(page_to, field.name, getattr(page_from, field.name, None)) # particular cases page_to.id = page_from.id page_to.page_ptr_id = page_from.page_ptr_id page_to.content_type_id = content_type_to.pk # custom copy if copy: copy(page_from, page_to) pages_to.append(page_to) # now convert the latest revision (if any) page_rev = PageRevision.objects.filter(page_id=page_to.id).order_by( '-created_at', '-id').first() if page_rev: page_rev.content_json = to_json(page_to) page_rev.save() # Remove all the converted page # we use a raw statement instead of .delete() because we want to keep # the parent Page record. # TODO: for large number of ids, we might need to process this in chunk. # TODO: ANY in the the where clause may not work with other RDBMS than psql from django.db import connection with connection.cursor() as cursor: cursor.execute( 'DELETE FROM {} WHERE page_ptr_id = ANY(%s)'.format( PageFrom._meta.db_table), [[p.page_ptr_id for p in pages_to]]) # now we can save the converted pages (without duplicate values) for page_to in pages_to: page_to.save() return len(pages_to)
def update_networks_contenttypes(*args): networks = apps.get_app_config('networks') create_contenttypes(networks)
def create_bn1_example(apps, schema_editor): """ Create a Bayesian Network from the scratch. """ # Content Types Hackery for ensuring that it exists app_config = apps.get_app_config('examples') app_config.models_module = app_config.models_module or True create_contenttypes(app_config) ## BayesianNetwork = apps.get_model("bayesian_networks", "BayesianNetwork") BayesianNetworkEdge = apps.get_model("bayesian_networks", "BayesianNetworkEdge") BayesianNetworkNode = apps.get_model("bayesian_networks", "BayesianNetworkNode") BayesianNetworkNodeColumn = apps.get_model("bayesian_networks", "BayesianNetworkNodeColumn") ContentType = apps.get_model("contenttypes", "ContentType") bn1 = BayesianNetwork(name="BN1 (Example)") bn1.save() mu = BayesianNetworkNode( network=bn1, name="mu", node_type=BNN.NODE_TYPE_STOCHASTIC, is_observable=False, distribution=DIST_GAUSSIAN_ARD, distribution_params="0, 1e-6", graph_interval="-10, 20" ) tau = BayesianNetworkNode( network=bn1, name="tau", node_type=BNN.NODE_TYPE_STOCHASTIC, is_observable=False, distribution=DIST_GAMMA, distribution_params="1e-6, 1e-6", graph_interval="1e-6, 0.1" ) ui_avg1 = BayesianNetworkNode( network=bn1, name="userinfo.avg1", node_type=BNN.NODE_TYPE_STOCHASTIC, is_observable=True, distribution=DIST_GAUSSIAN_ARD, distribution_params="mu, tau", ) mu.save() tau.save() ui_avg1.save() # ui_avg1_col = BayesianNetworkNodeColumn( node=ui_avg1, ref_model=ContentType.objects.get(model="userinfo", app_label="examples"), ref_column="avg1" ) ui_avg1_col.save() # mu_to_ui_avg1 = BayesianNetworkEdge( network=bn1, description="mu -> userinfo.avg1", parent=mu, child=ui_avg1 ) tau_to_ui_avg1 = BayesianNetworkEdge( network=bn1, description="tau -> userinfo.avg1", parent=tau, child=ui_avg1 ) mu_to_ui_avg1.save() tau_to_ui_avg1.save() # Generate the image generate_bn_image(bn1)
def _fixture_setup(self): super(AskbotTestCase, self)._fixture_setup() for app_config in apps.get_app_configs(): create_contenttypes(app_config) create_permissions(app_config) create_default_site(app_config)
def _update_contenttypes(self, *args, **kwargs): for app_config in apps.get_app_configs(): create_contenttypes(app_config)
def update_guest_groups_permissions(apps, schema_editor): """ This migration adds necessary permissions to guest groups. """ Dashboard = apps.get_model('pyplan', 'Dashboard') InputTemplate = apps.get_model('pyplan', 'InputTemplate') Group = apps.get_model('auth', 'Group') Permission = apps.get_model('auth', 'Permission') ContentType = apps.get_model('contenttypes', 'ContentType') content_types, app_models = get_contenttypes_and_models( apps.app_configs['pyplan'], 'default', ContentType) # If content_types are not created yet, then it's a clean install. # In order to add permissions after, we need to force ContentType creation. if not content_types: create_contenttypes(apps.app_configs['pyplan']) # Dashboard content type ctype_dashboard = ContentType.objects.get_for_model(Dashboard) # Dashboard permissions view_dashboard_perm, view_dash_perm_created = Permission.objects.get_or_create( codename='view_dashboard', content_type=ctype_dashboard, ) if view_dash_perm_created: view_dashboard_perm.name = 'Can view dashboard' view_dashboard_perm.save() change_dashboard_perm, change_dash_perm_created = Permission.objects.get_or_create( codename='change_dashboard', content_type=ctype_dashboard, ) if change_dash_perm_created: change_dashboard_perm.name = 'Can change dashboard' change_dashboard_perm.save() # InputTemplate content type ctype_inputtemplate = ContentType.objects.get_for_model(InputTemplate) # InputTemplate permissions view_inputtemplate_perm, view_inptmp_created = Permission.objects.get_or_create( codename='view_inputtemplate', content_type=ctype_inputtemplate, ) if view_inptmp_created: view_inputtemplate_perm.name = 'Can view input template' view_inputtemplate_perm.save() change_inputtemplate_perm, change_inptmp_created = Permission.objects.get_or_create( codename='change_inputtemplate', content_type=ctype_inputtemplate, ) if change_inptmp_created: change_inputtemplate_perm.name = 'Can change input template' change_inputtemplate_perm.save() guest_groups = Group.objects.filter(name__icontains='guest') for guest_group in guest_groups: guest_group.permissions.add(view_dashboard_perm) guest_group.permissions.add(change_dashboard_perm) guest_group.permissions.add(view_inputtemplate_perm) guest_group.permissions.add(change_inputtemplate_perm) guest_group.save()
def create_report_permissions(app_config, verbosity=2, interactive=True, using=DEFAULT_DB_ALIAS, apps=global_apps, **kwargs): """ Copied from django create permissions for model, edited to create view and print for reports Reports permissions are created in the database similar to django's app.print_clientbalances (ClientBalances is the report slug (defaulting to report class name), Content Type is the base model content_type :param app_config: :param verbosity: :param interactive: :param using: :param apps: :param kwargs: :return: """ if not app_config.models_module: return # Ensure that contenttypes are created for this app. Needed if # 'django.contrib.auth' is in INSTALLED_APPS before # 'django.contrib.contenttypes'. create_contenttypes(app_config, verbosity=verbosity, interactive=interactive, using=using, apps=apps, **kwargs) app_label = app_config.label try: app_config = apps.get_app_config(app_label) ContentType = apps.get_model('contenttypes', 'ContentType') Permission = apps.get_model('auth', 'Permission') except LookupError: return if not router.allow_migrate_model(using, Permission): return # This will hold the permissions we're looking for as # (content_type, (codename, name)) all_reports = report_registry.get_all_reports() searched_perms = [] # The codenames and ctypes that should exist. ctypes = set() for report in all_reports: # for klass in report_registry.get_base_models(): # Force looking up the content types in the current database # before creating foreign keys to them. klass = report.base_model ctype = ContentType.objects.db_manager(using).get_for_model( klass, for_concrete_model=False) ctypes.add(ctype) for perm in _get_all_permissions(report): searched_perms.append((ctype, perm)) # Find all the Permissions that have a content_type for a model we're # looking for. We don't need to check for codenames since we already have # a list of the ones we're going to create. all_perms = set( Permission.objects.using(using).filter( content_type__in=ctypes, ).values_list("content_type", "codename")) perms = [ Permission(codename=codename, name=name, content_type=ct) for ct, (codename, name) in searched_perms if (ct.pk, codename) not in all_perms ] Permission.objects.using(using).bulk_create(perms) if verbosity >= 2: for perm in perms: print("Adding permission '%s'" % perm)