Last active
September 16, 2022 10:38
-
-
Save cmaggiulli/ad59a274747676b47c1b852489b58610 to your computer and use it in GitHub Desktop.
Extending the django-admin datadump command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Author: Chris Maggiulli | |
| Rquirements: | |
| 1. Place in project/app/management/commands/datadump.py | |
| 2. Put this app first in INSTALLED_APPS | |
| """ | |
| from django.core.management.base import BaseCommand, CommandError | |
| from django.apps import apps | |
| from logging import getLogger | |
| from django.contrib.contenttypes.fields import GenericForeignKey | |
| from django.contrib.contenttypes.models import ContentType | |
| from django.apps import apps | |
| from django.core import serializers | |
| from django.core.management.base import BaseCommand,CommandError | |
| from django.core.management.utils import parse_apps_and_model_labels | |
| from django.db import DEFAULT_DB_ALIAS, router | |
| from django.utils import timezone | |
| from django.contrib.contenttypes.models import ContentType | |
| from django.contrib.auth.models import Permission | |
| from django.core.management.commands import dumpdata | |
| from django.contrib.auth.models import User | |
| class Command(dumpdata.Command): | |
| def handle(self, *args, **options): | |
| help = ( | |
| "The Django datadump command has been extended to support the creation of fixtures" | |
| "For all related person data. This command accepts all standard dattadump flags and" | |
| "also supports $ datadump --user 31,43,141,3 " | |
| ) | |
| def add_arguments(self, parser): | |
| """ | |
| Adding an argument for authenticate username. From there we sill traverse model tree and export | |
| all objects in all models with a direct or inderect relationn to this user | |
| """ | |
| parser.add_argument( | |
| '--user' , help="Authenticated Users Username" | |
| ) | |
| def handle(self, *app_labels, **options): | |
| username = options['user'] | |
| user = User.objects.get(pk=username) | |
| if not user: | |
| raise CommandError("Invalid usernmae supplied") | |
| app_label = User._meta.app_label | |
| self.stdout.write(app_label, ending='') | |
| return | |
| """ | |
| app_config for app_config in apps.get_app_configs() | |
| if app_config.models_module: | |
| print(app_config.models_module) | |
| super(Command, self).handle(*args, **options) | |
| model_name = Person._meta.app_label | |
| print(model_name) | |
| return | |
| fields = ['codename', 'content_type'] | |
| permissions = Permission.objects.filter( | |
| content_type__in=ContentType.objects.filter(app_label=model_name) | |
| ) | |
| for permission in permissions: | |
| print(permission.content_type, permission) | |
| return | |
| ################################################## | |
| permissions = Permission.objects.filter( | |
| content_type__in=ContentType.objects.filter(app_label=model_name) | |
| ) | |
| for permission in permissions: | |
| print(f'{permission.codename} - {permission.content_type.model}') | |
| permission = Permission.objects.all()[5] | |
| content_type = permission.content_type | |
| model, codename = content_type.model, content_type.name | |
| """ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment