def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] app = get_app(ilabel) ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) suite = reorder_suite(suite, (TestCase,)) # signals.build_suite.send(sender=self, suite=suite) return suite
def dump_data(request,appname): app_list = SortedDict() try: if request.POST: for appname in request.POST.getlist('apps'): app = get_app(appname) app_list[app] = None appname = 'choices' else: app = get_app(appname) app_list[app] = None except ImproperlyConfigured: if appname == 'all': for app in get_apps(): app_list[app] = None if(len(app_list) > 0): objects = [] for model in sort_dependencies(app_list.items()): if not model._meta.proxy and router.allow_syncdb(DEFAULT_DB_ALIAS, model): objects.extend(model._default_manager.using(DEFAULT_DB_ALIAS).all()) serializers.get_serializer('json') json = serializers.serialize('json', objects, indent=2,use_natural_keys=True) response = HttpResponse(json, mimetype='application/json'); response['Content-Disposition'] = 'attachment; filename=%s_%s_fixture.json' % (date.today().__str__(),appname) return response return render_to_response('diagnostic/dumpdata.html',context_instance=RequestContext(request))
def handle_noargs(self, **options): orig_load_initial_data = options.get('load_initial_data') options['load_initial_data'] = False super(Command, self).handle_noargs(**options) db = options.get('database') connection= connections[db] # Create customs views for geojson_rest if 'geojson_rest' in settings.INSTALLED_APPS: app = models.get_app('geojson_rest') app_models = models.get_models(app, include_auto_created=True) tables = connection.introspection.table_names() converter = connection.introspection.table_name_converter custom_sql = sql_custom(models.get_app('geojson_rest'), no_style, connection) #self.stdout.write(custom_sql) if custom_sql: cursor = connection.cursor() try: for sql in custom_sql: cursor.execute(sql) except Exception as e: self.stdout.write(sql) self.stderr.write("Failed to install custom SQL for geojson_rest: %s\n" % e) transaction.rollback_unless_managed(using=db) else: transaction.commit_unless_managed(using=db) # Load initial_data fixtures (unless that has been disabled) if orig_load_initial_data: call_command('loaddata', 'initial_data', verbosity=verbosity, database=db, skip_validation=True)
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: appname, test = label.split('.', 1) else: appname, test = label, '' def filter_test(testcase, testprefix=label): testname = "%s.%s.%s" % (testcase.__class__.__module__, testcase.__class__.__name__, testcase) return testname.startswith(testprefix) app = get_app(appname) suite.addTest(simple.build_suite(app)) self.filter_suite(suite, filter_test) else: for appname in settings.OUR_APPS: app = get_app(appname, emptyOK=True) if app is None: continue suite.addTest(simple.build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return simple.reorder_suite(suite, (testcases.TestCase,))
def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps exclude = options.get('exclude', []) excluded_apps = [get_app(app_label) for app_label in exclude] if len(app_labels) == 0: app_list = [app for app in get_apps() if app not in excluded_apps] else: app_list = [get_app(app_label) for app_label in app_labels] for app in app_list: name = app.__name__.replace('.models', '') app_path = __import__(name, {}, {}, [name.split('.')[-1]]).__path__ try: params = imp.find_module('generator', app_path) except ImportError: pass else: generator = imp.load_module(name + '.generator', *params) print "Generating items for %s." % name.title() start = time.time() load_json(generator.generate()) now = time.time() print "Generation completed in %s seconds" % (now - start)
def handle(self, *app_labels, **options): format = options.get('format','json') indent = options.get('indent',None) exclude = options.get('exclude',[]) show_traceback = options.get('traceback', False) excluded_apps = [get_app(app_label) for app_label in exclude] if len(app_labels) == 0: app_list = [app for app in get_apps() if app not in excluded_apps] else: app_list = [get_app(app_label) for app_label in app_labels] # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. try: serializers.get_serializer(format) except KeyError: raise CommandError("Unknown serialization format: %s" % format) objects = [] collected = set() for app in app_list: #Yey for ghetto recusion objects, collected = _relational_dumpdata(app, collected) #****End New stuff try: return serializers.serialize(format, objects, indent=indent) except Exception, e: if show_traceback: raise raise CommandError("Unable to serialize database: %s" % e)
def handle(self, *args, **options): if not args: apps = [get_app(model._meta.app_label) for model in get_models()] else: apps = [get_app(arg) for arg in args] for app in apps: create_permissions(app, get_models(), options.get('verbosity', 0))
def get_apps(application_labels=[], exclude_application_labels=[]): """ - if not @application_labels and not @exclude_application_labels, it returns all applications. - if @application_labels is not None, it returns just these applications, except applications with label in exclude_application_labels. @Returns an array of `module` objects """ if application_labels: applications = [] for app_label in application_labels: if django_greater_than('1.7'): app_config = apps.get_app_config(app_label) applications.append(app_config.module) else: applications.append(models.get_app(app_label)) else: applications = models.get_apps() if exclude_application_labels: for app_label in exclude_application_labels: if app_label: if django_greater_than('1.7'): app_config = apps.get_app_config(app_label) applications.remove(app_config.models_module) else: applications.remove(models.get_app(app_label)) return applications
def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps, get_models format = options.get('format','json') indent = options.get('indent',None) exclude = options.get('exclude',[]) show_traceback = options.get('traceback', False) excluded_apps = [get_app(app_label) for app_label in exclude] if len(app_labels) == 0: app_list = [app for app in get_apps() if app not in excluded_apps] else: app_list = [get_app(app_label) for app_label in app_labels] # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. if format not in serializers.get_public_serializer_formats(): raise CommandError("Unknown serialization format: %s" % format) try: serializers.get_serializer(format) except KeyError: raise CommandError("Unknown serialization format: %s" % format) objects = [] for app in app_list: for model in get_models(app): objects.extend(model._default_manager.all()) try: return serializers.serialize(format, objects, indent=indent) except Exception, e: if show_traceback: raise raise CommandError("Unable to serialize database: %s" % e)
def clean_app_name(self): app_name = self.cleaned_data.get("app_name") try: models.get_app(app_name) except ImproperlyConfigured: raise forms.ValidationError(u'The app "{0}" could not be found'.format(app_name)) return app_name
def process_meta_data(): xl = pd.ExcelFile('migration_data/initial_data.xlsx') all_sheets = xl.sheet_names rhizome_app = get_app('rhizome') auth_app = get_app('auth') models_to_process = {} all_models = get_models(rhizome_app) + get_models(auth_app) for model in all_models: ## iterate through the models in the rhizome app and create a lookup ## for {'sheet_name': Model} .. for instance -> {'indicator': Indicator} if model._meta.db_table in all_sheets: models_to_process[model._meta.db_table] = model for sheet in all_sheets: ## if the sheet has a cooresponding model, create a data frame out of ## the sheet anf bulk insert the data using the model_df_to_data fn try: model = models_to_process[sheet] print 'processing sheet ' + sheet model_df = xl.parse(sheet) model_ids = model_df_to_data(model_df,model) except KeyError: pass ## once the locations are all created we need to ## ## cache them in the locaiton_tree table ## ltc = LocationTreeCache() ltc.main()
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] try: app = get_app(ilabel) except Exception as e: print e else: ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase,))
def handle(self, *args, **options): # To preserve users' agents add -sa True when running command sa = False if options['saveagents']: sa = True apps = [] apps.append(get_app('lrs')) apps.append(get_app('oauth_provider')) apps.append(get_app('oauth2')) # Clear app(db) data for app in apps: for model in get_models(app): if app.__name__.split('.')[0] == 'lrs' and model.__name__ == "Agent" and sa: user_emails = ["mailto:" + s for s in User.objects.all().values_list('email', flat=True)] model.objects.exclude(mbox__in=user_emails).delete() self.stdout.write("Deleted all %s objects from - %s except for the Agents associated with LRS users\n" % (model.__name__, app.__name__.split('.')[0])) else: model.objects.all().delete() self.stdout.write("Deleted all %s objects from - %s\n" % (model.__name__, app.__name__.split('.')[0])) # Clear cache data cache.clear() # Clear media folders for subdir, dirs, files in os.walk(settings.MEDIA_ROOT): for dr in dirs: for sd, ds, fs in os.walk(os.path.join(settings.MEDIA_ROOT, dr)): for f in fs: os.remove(os.path.join(sd, f)) self.stdout.write("Successfully cleared all data from the apps\n")
def test_south_migrations(self): from django.core.exceptions import ImproperlyConfigured from django.conf import settings from django.db import models from south.migration import Migrations, migrate_app from south.models import MigrationHistory from south.exceptions import NoMigrations from south.creator import changes, actions, freezer from south.management.commands.datamigration import Command as DataCommand apps = [app for app in settings.INSTALLED_APPS if app.startswith('wagtail.')] failing_apps = [] for app_name in apps: app = app_name.split('.')[-1] try: models.get_app(app) except ImproperlyConfigured: # This module fails to load, probably because it has no # models.py. Ignore it and move on continue try: migrations = Migrations(app, force_creation=False, verbose_creation=False) last_migration = migrations[-1] except (NoMigrations, IndexError): # No migrations for this app, probably doesnt have models continue if migrations.app_label() not in getattr(last_migration.migration_class(), "complete_apps", []): self.fail("Automatic migrations checking failed, since the previous migration does not have this whole app frozen.\nEither make migrations using '--freeze %s' or set 'SOUTH_AUTO_FREEZE_APP = True' in your settings.py." % migrations.app_label()) # Alright, construct two model dicts to run the differ on. old_defs = dict( (k, v) for k, v in last_migration.migration_class().models.items() if k.split(".")[0] == migrations.app_label() ) new_defs = dict( (k, v) for k, v in freezer.freeze_apps([migrations.app_label()]).items() if k.split(".")[0] == migrations.app_label() ) change_source = changes.AutoChanges( migrations = migrations, old_defs = old_defs, old_orm = last_migration.orm(), new_defs = new_defs, ) name = 'test' # Get the actions, and then insert them into the actions lists if list(change_source.get_changes()): failing_apps.append(app_name) if failing_apps: self.fail('Model changes with no South migration detected in apps: %s' % ( ', '.join(failing_apps)))
def forwards(self, orm): "Write your forwards methods here." # Note: Don't use "from appname.models import ModelName". # Use orm.ModelName to refer to models in this application, # and orm['appname.ModelName'] for models in other applications. update_contenttypes(get_app('activities'), get_models()) create_permissions(get_app('activities'), get_models(), 0)
def handle(self, *app_labels, **options): format = options.get('format','json') indent = options.get('indent',None) exclude = options.get('exclude',[]) exclude_models = options.get('excludem',[]) #includem = options.get('includem',[]) show_traceback = options.get('extrasort', False) extrasort = options.get('traceback', False) excluded_apps = [get_app(app_label) for app_label in exclude] excluded_models = [(get_app(app_label.split('.')[0]), get_model(app_label.split('.')[0], app_label.split('.')[1]) ) for app_label in exclude_models] if len(app_labels) == 0: app_list = [app for app in get_apps() if app not in excluded_apps] else: app_list = [get_app(app_label) for app_label in app_labels] # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. if format not in serializers.get_public_serializer_formats(): raise CommandError("Unknown serialization format: %s" % format) try: serializers.get_serializer(format) except KeyError: raise CommandError("Unknown serialization format: %s" % format) objects = [] models=[] for app in app_list: for model in get_models(app): if intuplelist0(excluded_models, app) and\ intuplelist1(excluded_models, model): pass else: models.append(model) if extrasort: models = sort_by_relation(models) for model in models: try: objects.extend(model._default_manager.all().order_by(pk)) except: objects.extend(model._default_manager.all()) try: return serializers.serialize(format, objects, indent=indent) except Exception, e: if show_traceback: raise raise CommandError("Unable to serialize database: %s" % e)
def handle(self, *app_labels, **options): # Activate project's default language translation.activate(settings.LANGUAGE_CODE) comment = options["comment"] batch_size = options["batch_size"] database = options.get('database') verbosity = int(options.get("verbosity", 1)) app_list = OrderedDict() # if no apps given, use all installed. if len(app_labels) == 0: for app in get_apps(): if not app in app_list: app_list[app] = [] for model_class in get_models(app): if not model_class in app_list[app]: app_list[app].append(model_class) else: for label in app_labels: try: app_label, model_label = label.split(".") try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) model_class = get_model(app_label, model_label) if model_class is None: raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) if app in app_list: if app_list[app] and model_class not in app_list[app]: app_list[app].append(model_class) else: app_list[app] = [model_class] except ValueError: # This is just an app - no model qualifier. app_label = label try: app = get_app(app_label) if not app in app_list: app_list[app] = [] for model_class in get_models(app): if not model_class in app_list[app]: app_list[app].append(model_class) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) # Create revisions. for app, model_classes in app_list.items(): for model_class in model_classes: self.create_initial_revisions(app, model_class, comment, batch_size, verbosity, database=database) # Go back to default language translation.deactivate()
def handle(self, *args, **options): if not args: apps = [] for model in get_models(): try: apps.append(get_app(model._meta.app_label)) except ImproperlyConfigured: pass else: apps = [] for arg in args: apps.append(get_app(arg)) table_dict = {} cursor = connection.cursor() for app in apps: for model in get_models(app): key = '%s.%s' %(model._meta.app_label, model.__name__) if table_dict.has_key(key): continue tbl = model._meta.db_table sql = self.STATEMENT % tbl cursor.execute(sql) row = cursor.fetchone() dbtbl = DBTable(model, row) table_dict[key] = dbtbl tables = table_dict.values() tables.sort() max = options['max'] print '\n', '-'*75 print 'Table Sizes:' print '-'*75 for i, table in enumerate(tables): if max and i >= max: break print str(table) db_dict = {} for name, dbsettings in settings.DATABASES.items(): sql = self.DB_STATEMENT % dbsettings['NAME'] cursor.execute(sql) row = cursor.fetchone() dbtbl = DBTable(name, row) db_dict[name] = dbtbl databases = db_dict.values() databases.sort() print '\n', '-'*75 print 'Database Sizes:' print '-'*75 for db in databases: print str(db)
def build_tests(self): """ Build tests for inclusion in suite from resolved packages for <= 1.8 TODO: Cleanup/simplify this method, flow too complex, too much duplication. """ from django.core.exceptions import ImproperlyConfigured from django.test.simple import build_suite, build_test try: from django.apps import apps get_app = apps.get_app_config except ImportError: from django.db.models import get_app tests = [] packages = [self.options['label'], ] if \ self.options['label'] else self.packages for package in packages: try: if not self.options['autoreload']: if self.options['label']: try: tests.append(build_test(package)) except (ImproperlyConfigured, ValueError) as e: self.handle_label_exception(e) else: app = get_app(package) tests.append(build_suite(app)) else: # Wait for exceptions to be resolved. exception = None while True: try: if self.options['label']: try: tests.append(build_test(package)) except (ImproperlyConfigured, ValueError) as e: self.handle_label_exception(e) else: app = get_app(package) tests.append(build_suite(app)) break except LabelException: raise except Exception as e: if exception != str(e): traceback.print_exc() exception = str(e) time.sleep(1) except ImproperlyConfigured as e: log.info("Warning: %s" % traceback.format_exc()) except ImportError as e: log.info("Warning: %s" % traceback.format_exc()) return tests
def _get_app_labels(apps): app_labels = [] for app in apps: try: app_label = app.split('.')[-1] get_app(app_label) app_labels.append(app_label) except: pass return app_labels
def check_db_connection(request): health_status = "success" reason = "Connected to RDS" try: app_list = [get_app("gladminds"), get_app("afterbuy"), get_app("aftersell")] for every_app in app_list: for model in get_models(every_app): model.objects.filter() except Exception as ex: reason = "Error on RDS Connection : %s" % ex health_status = "error" return {"health_status": health_status, "reason": reason}
def site(request): """Sets in the present context information about the current site.""" # Current SiteManager already handles prevention of spurious # database calls. If the user does not have the Sites framework # installed, a RequestSite object is an appropriate fallback. try: models.get_app('sites') site_obj = Site.objects.get_current() except ImproperlyConfigured: site_obj = RequestSite(request) return {'site': site_obj}
def handle(self, *args, **options): print 'Sincronizando permisos...' if not args: apps = [] for model in get_models(): apps.append(get_app(model._meta.app_label)) else: apps = [] for arg in args: apps.append(get_app(arg)) for app in apps: create_permissions(app, get_models(), options.get('verbosity', 0)) print 'Permisos sincronizados'
def handle(self, *args, **options): apps = [] apps.append(get_app('lrs')) apps.append(get_app('oauth_provider')) apps.append(get_app('oauth2')) for app in apps: for model in get_models(app): model.objects.all().delete() self.stdout.write("Deleted all %s objects from - %s\n" % (model.__name__, app.__name__.split('.')[0])) cache.clear() self.stdout.write("Successfully cleared all data from the apps\n")
def handle(self, *args, **options): if not args: apps = [] for model in get_models(): apps.append(get_app(model._meta.app_label)) else: apps = [] for arg in args: apps.append(get_app(arg)) for app in apps: update_contenttypes(app, None, options.get('verbosity', 2), interactive=True) create_permissions(app, get_models(), options.get('verbosity', 0))
def setUp(self): try: get_app('otp_static') except: self.skipTest("Requires django_otp.plugins.otp_static") try: self.alice = self.create_user('alice', 'alice') except IntegrityError: self.skipTest("Unable to create a test user") else: device = self.alice.staticdevice_set.create() device.token_set.create(token='alice1') device.token_set.create(token='alice2')
def handle(self, *args, **options): if not args: apps = [] for model in get_models(): try: apps.append(get_app(model._meta.app_label)) except ImproperlyConfigured: pass else: apps = [] for arg in args: apps.append(get_app(arg)) for app in apps: create_permissions(app, get_models(), options.get('verbosity', 0))
def skip(self, obj): # Skip ignored models. if obj.__class__ in self.excluded_models: return True if get_app(obj._meta.app_label) in self.excluded_apps: return True # Skip models not specifically being included. if (self.included_apps or self.included_models) and \ not isinstance(obj, self.primary_model): if obj.__class__ not in self.included_models: return True if get_app(obj._meta.app_label) not in self.included_apps: return True return False
def build_test(label): """Construct a test case a test with the specified label. Label should be of the form model.TestClass or model.TestClass.test_method. Returns an instantiated test or test suite corresponding to the label provided. """ parts = label.split('.') if len(parts) < 2 or len(parts) > 3: raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method" % label) app_module = get_app(parts[0]) TestClass = getattr(app_module, parts[1], None) # Couldn't find the test class in models.py; look in tests.py if TestClass is None: test_module = get_tests(app_module) if test_module: TestClass = getattr(test_module, parts[1], None) if len(parts) == 2: # label is app.TestClass try: return unittest.TestLoader().loadTestsFromTestCase(TestClass) except TypeError: raise ValueError("Test label '%s' does not refer to a test class" % label) else: # label is app.TestClass.test_method if not TestClass: raise ValueError("Test label '%s' does not refer to a test class" % label) return TestClass(parts[2])
def custom_sql_for_model(model, style): from django.db import models from django.conf import settings opts = model._meta app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) output = [] # Post-creation SQL should come before any initial SQL data is loaded. # However, this should not be done for fields that are part of a a parent # model (via model inheritance). nm = opts.init_name_map() post_sql_fields = [f for f in opts.local_fields if hasattr(f, 'post_create_sql')] for f in post_sql_fields: output.extend(f.post_create_sql(style, model._meta.db_table)) # Some backends can't execute more than one SQL statement at a time, # so split into separate statements. statements = re.compile(r";[ \t]*$", re.M) # Find custom SQL, if it's available. sql_files = [os.path.join(app_dir, "%s.%s.sql" % (opts.object_name.lower(), settings.DATABASE_ENGINE)), os.path.join(app_dir, "%s.sql" % opts.object_name.lower())] for sql_file in sql_files: if os.path.exists(sql_file): fp = open(sql_file, 'U') for statement in statements.split(fp.read().decode(settings.FILE_CHARSET)): # Remove any comments from the file statement = re.sub(r"--.*([\n\Z]|$)", "", statement) if statement.strip(): output.append(statement + ";") fp.close() return output
from django.contrib import admin from django.db.models import get_models, get_app # Register your models here. from member.models import * for model in get_models(get_app('member')): admin.site.register(model)
1. The migration_restart branch has been merged to master and deployed. 2. south_migrationhistory has been truncated. 3. The initial migrations for clients and services have been faked. """ from django.contrib.auth.management import create_permissions from django.contrib.auth.models import Permission from django.contrib.contenttypes.models import ContentType from django.core.management import call_command from django.db.models import get_models, get_app from boris.services.management import proxy_permissions_fix # First, create the missing permissions. create_permissions(get_app('services'), get_models(), 2) # Then remove the obsolete permissions. # Obsolete models contenttypes = ( ('services', 'crisisintervention'), ('clients', 'riskybehavior'), ) for app_label, model in contenttypes: try: ct = ContentType.objects.get(app_label=app_label, model=model) except ContentType.DoesNotExist: print 'ContentType for %s not found!' % model else: qset = Permission.objects.filter(content_type=ct)
def generate_dot(app_labels, **kwargs): disable_fields = kwargs.get('disable_fields', False) include_models = kwargs.get('include_models', []) exclude_models = kwargs.get('exclude_models', []) dot = head_template for app_label in app_labels: app = models.get_app(app_label) graph = Context({ 'name': '"%s"' % app.__name__, 'disable_fields': disable_fields, 'models': [] }) for appmodel in get_models(app): # consider given model name ? def consider(model_name): return (not include_models or model_name in include_models ) and (not model_name in exclude_models) if not consider(appmodel._meta.object_name): continue model = {'name': appmodel.__name__, 'fields': [], 'relations': []} # model attributes def add_attributes(): model['fields'].append({ 'name': field.name, 'type': type(field).__name__, 'blank': field.blank }) for field in appmodel._meta.fields: add_attributes() if appmodel._meta.many_to_many: for field in appmodel._meta.many_to_many: add_attributes() # relations def add_relation(extras=""): _rel = { 'target': field.rel.to.__name__, 'type': type(field).__name__, 'name': field.name, 'arrows': extras } if _rel not in model['relations'] and consider(_rel['target']): model['relations'].append(_rel) for field in appmodel._meta.fields: if isinstance(field, ForeignKey): add_relation() elif isinstance(field, OneToOneField): add_relation("[arrowhead=none arrowtail=none]") if appmodel._meta.many_to_many: for field in appmodel._meta.many_to_many: if isinstance(field, ManyToManyField): add_relation("[arrowhead=normal arrowtail=normal]") elif isinstance(field, GenericRelation): add_relation( '[style="dotted"] [arrowhead=normal arrowtail=normal]' ) graph['models'].append(model) t = Template(body_template) dot += '\n' + t.render(graph) dot += '\n' + tail_template return dot
from django.contrib import admin from django.db.models import get_models, get_app class ModAdmin(admin.ModelAdmin): list_display = ('data', 'meta') for model in get_models(get_app('website')): admin.site.register(model, ModAdmin)
from django.contrib import admin from django.db.models import get_models, get_app for model in get_models(get_app('core')): admin.site.register(model)
def handle(self, *args, **options): apps = [] if options['all_applications']: apps = list(get_apps()) for app_label in options['appnames']: app = get_app(app_label) if app not in apps: apps.append(app) self.verbose_names = options['verbose_names'] self.exclude_modules = parse_file_or_list(options['exclude_modules']) self.exclude_models = parse_file_or_list(options['exclude_models']) self.exclude_fields = parse_file_or_list(options['exclude_columns']) self.inheritance = options['inheritance'] self.sort_fields = options['sort_fields'] self.bezier = options['bezier'] ET.register_namespace('dia', 'http://www.lysator.liu.se/~alla/dia/') ns = {'dia': 'http://www.lysator.liu.se/~alla/dia/'} dom = ET.fromstring(get_empty_xml()) self.layer = dom.find('dia:layer', namespaces=ns) app_colors = {} obj_num = 0 obj_ref = [] model_list = self.get_full_model_list(apps) for model in model_list: mdata = { 'id': obj_num, 'pos': (random.random() * 80, random.random() * 80), 'name': self.get_model_name(model), 'fields': self.get_model_fields(model), 'color': get_model_color(app_colors, model), 'port_idx': 0, } self.xml_make_table(mdata) obj_ref.append((obj_num, model, mdata)) obj_num += 1 for model in model_list: for rel in self.get_model_relations(model): try: self.prepare_relation_stage2(obj_ref, rel, obj_num) self.xml_make_relation(rel) obj_num += 1 except ModelNotFoundException: pass if self.inheritance: for rel in self.get_model_inheritance(model): try: self.prepare_relation_stage2(obj_ref, rel, obj_num) self.xml_make_relation(rel) except ModelNotFoundException: pass xml = six.b('<?xml version="1.0" encoding="UTF-8"?>') + \ ET.tostring(dom, encoding='utf-8') outfile = options['outputfile'] if outfile: if outfile[-4:] != '.dia': outfile += '.dia' with gzip.open(outfile, 'wb') as f: f.write(xml) else: self.stdout.write(xml.decode('utf-8'))
def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps, get_models, get_model indent = options.get('indent', None) using = options.get('database', DEFAULT_DB_ALIAS) connection = connections[using] excludes = options.get('exclude', []) show_traceback = options.get('traceback', False) use_natural_keys = options.get('use_natural_keys', False) use_base_manager = options.get('use_base_manager', False) excluded_apps = set() excluded_models = set() for exclude in excludes: if '.' in exclude: app_label, model_name = exclude.split('.', 1) model_obj = get_model(app_label, model_name) if not model_obj: raise CommandError('Unknown model in excludes: %s' % exclude) excluded_models.add(model_obj) else: try: app_obj = get_app(exclude) excluded_apps.add(app_obj) except ImproperlyConfigured: raise CommandError('Unknown app in excludes: %s' % exclude) if len(app_labels) == 0: app_list = OrderedDict( (app, None) for app in get_apps() if app not in excluded_apps) else: app_list = OrderedDict() for label in app_labels: try: app_label, model_label = label.split('.') try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) if app in excluded_apps: continue model = get_model(app_label, model_label) if model is None: raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) if app in app_list.keys(): if app_list[app] and model not in app_list[app]: app_list[app].append(model) else: app_list[app] = [model] except ValueError: # This is just an app - no model qualifier app_label = label try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) if app in excluded_apps: continue app_list[app] = None # Now collate the objects to be serialized. objects = [] for model in sort_dependencies(app_list.items()): if model in excluded_models: continue if not model._meta.proxy and router.allow_syncdb(using, model): if use_base_manager: objects.extend(model._base_manager.using(using).all()) else: objects.extend(model._default_manager.using(using).all()) try: serializer = XMLExportSerializer() return serializer.serialize(objects, indent=indent, use_natural_keys=use_natural_keys) except Exception as e: if show_traceback: raise raise CommandError("Unable to serialize database: %s" % e)
def handle(self, *args, **options): apps = [] models_d = {} tables_list = [] for app in settings.INSTALLED_APPS: try: app_label = app.split('.')[-1] apps.append(app_label) except: # No models, no problem. pass for app_label in apps: # skip the legacy if app_label in ['legacy']: continue # skip the social_auth if not set if not getattr(settings, 'SOCIAL_AUTH_USER_MODEL', None): if app_label in ['social_auth']: continue try: app = get_app(app_label) except: app = None if app: for model in get_models(app, include_auto_created=True): models_d[model._meta.db_table] = model tables_list.append(model._meta.db_table) tables_list.remove('mig_help_files_helpfile_t4_to_t5') # django 1.4 doesn't have auth_message table if 'auth_message' in tables_list: tables_list.remove('auth_message') related_tables = {} # get a list of related tables for each table for table in tables_list: related_tables[table] = [field.rel.to._meta.db_table \ for field in models_d[table]._meta.fields \ if isinstance(field, (ForeignKey, OneToOneField))] sorted_list = [] for table in tables_list: if not related_tables[table]: sorted_list.append(table) n = 100 # avoid getting into the infinite loop - just in case #while related_tables: while n > 1: # remove tables from related_tables if already in the sorted_list for key in related_tables.keys(): for rel_table in related_tables[key]: if rel_table in sorted_list: related_tables[key].remove(rel_table) if not related_tables[key]: del related_tables[key] # add to the sorted_list if there is no # related_tables for this table for table in tables_list: # if the related_tables is gone if table not in sorted_list and ( table not in related_tables.keys()): sorted_list.append(table) # continue until all the related tables are gone if not related_tables: break n = n - 1 if related_tables: print "ERROR: Sorting not completed." # copy the list to your conf.yml file print '-', '\n- '.join(sorted_list)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ This module allows users to run tests for GIS apps that require the creation of a spatial database. Currently, this is only required for PostgreSQL as PostGIS needs extra overhead in test database creation. In order to create a PostGIS database, the DATABASE_USER (or TEST_DATABASE_USER, if defined) will require superuser priviliges. To accomplish this outside the `postgres` user, you have a few options: (A) Make your user a super user: This may be done at the time the user is created, for example: $ createuser --superuser <user_name> Or you may alter the user's role from the SQL shell (assuming this is done from an existing superuser role): postgres# ALTER ROLE <user_name> SUPERUSER; (B) Create your own PostgreSQL database as a local user: 1. Initialize database: `initdb -D /path/to/user/db` 2. If there's already a Postgres instance on the machine, it will need to use a different TCP port than 5432. Edit postgresql.conf (in /path/to/user/db) to change the database port (e.g. `port = 5433`). 3. Start this database `pg_ctl -D /path/to/user/db start` (C) On Windows platforms the pgAdmin III utility may also be used as a simple way to add superuser privileges to your database user. The TEST_RUNNER needs to be set in your settings like so: TEST_RUNNER='django.contrib.gis.tests.run_tests' Note: This test runner assumes that the PostGIS SQL files ('lwpostgis.sql' and 'spatial_ref_sys.sql') are installed in the directory specified by `pg_config --sharedir` (and defaults to /usr/local/share if that fails). This behavior is overridden if POSTGIS_SQL_PATH is set in your settings. Windows users should set POSTGIS_SQL_PATH manually because the output of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'. Finally, the tests may be run by invoking `./manage.py test`. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test from django.test.utils import setup_test_environment, teardown_test_environment # The `create_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) # Creating the test spatial database. create_spatial_db(test=True, verbosity=verbosity, autoclobber=not interactive) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
from django.contrib import admin from django.db.models import get_models, get_app # Smarter way to register all models for an app for model in get_models(get_app('notes')): # If you need to customise or # have duplicate models exclude them admin.site.register(model)
def handle_app(self, app_name): try: app = get_app(app_name) except ImproperlyConfigured, e: raise CommandError(e)
def make_model(self, app, name, data): "Makes a Model class out of the given app name, model name and pickled data." # Extract any bases out of Meta if "_ormbases" in data["Meta"]: # Make sure everything we depend on is done already; otherwise, wait. for key in data["Meta"]["_ormbases"]: key = key.lower() if key not in self.models: raise ORMBaseNotIncluded("Cannot find ORM base %s" % key) elif isinstance(self.models[key], string_types): # Then the other model hasn't been unfrozen yet. # We postpone ourselves; the situation will eventually resolve. raise UnfreezeMeLater() bases = [self.models[key.lower()] for key in data["Meta"]["_ormbases"]] # Perhaps the old style? elif "_bases" in data["Meta"]: bases = map(ask_for_it_by_name, data["Meta"]["_bases"]) # Ah, bog standard, then. else: bases = [models.Model] # Turn the Meta dict into a basic class meta = self.make_meta(app, name, data["Meta"], data.get("_stub", False)) failed_fields = {} fields = {} stub = False # Now, make some fields! for fname, params in data.items(): # If it's the stub marker, ignore it. if fname == "_stub": stub = bool(params) continue elif fname == "Meta": continue elif not params: raise ValueError( "Field '%s' on model '%s.%s' has no definition." % (fname, app, name) ) elif isinstance(params, string_types): # It's a premade definition string! Let's hope it works... code = params extra_imports = {} else: # If there's only one parameter (backwards compat), make it 3. if len(params) == 1: params = (params[0], [], {}) # There should be 3 parameters. Code is a tuple of (code, what-to-import) if len(params) == 3: code = "SouthFieldClass(%s)" % ", ".join( params[1] + ["%s=%s" % (n, v) for n, v in params[2].items()] ) extra_imports = {"SouthFieldClass": params[0]} else: raise ValueError( "Field '%s' on model '%s.%s' has a weird definition length (should be 1 or 3 items)." % (fname, app, name) ) try: # Execute it in a probably-correct context. field = self.eval_in_context(code, app, extra_imports) except (NameError, AttributeError, AssertionError, KeyError): # It might rely on other models being around. Add it to the # model for the second pass. failed_fields[fname] = (code, extra_imports) else: fields[fname] = field # Find the app in the Django core, and get its module more_kwds = {"Meta": meta} try: app_module = models.get_app(app) more_kwds["__module__"] = app_module.__name__ except ImproperlyConfigured: # the app this belonged to has vanished, but thankfully we can still # make a mock model, so ignore the error. more_kwds["__module__"] = "_south_mock" # Make our model fields.update(more_kwds) model = type(str(name), tuple(bases), fields) # If this is a stub model, change Objects to a whiny class if stub: model.objects = WhinyManager() # Also, make sure they can't instantiate it model.__init__ = whiny_method else: model.objects = NoDryRunManager(model.objects) if failed_fields: model._failed_fields = failed_fields return model
def really_send_create_signal(self, app_label, model_names, verbosity=0, interactive=False): """ Sends a post_syncdb signal for the model specified. If the model is not found (perhaps it's been deleted?), no signal is sent. TODO: The behavior of django.contrib.* apps seems flawed in that they don't respect created_models. Rather, they blindly execute over all models within the app sending the signal. This is a patch we should push Django to make For now, this should work. """ if self.debug: print(" - Sending post_syncdb signal for %s: %s" % (app_label, model_names)) app = models.get_app(app_label) if not app: return created_models = [] for model_name in model_names: model = models.get_model(app_label, model_name) if model: created_models.append(model) if created_models: if hasattr(dispatcher, "send"): # Older djangos dispatcher.send(signal=models.signals.post_syncdb, sender=app, app=app, created_models=created_models, verbosity=verbosity, interactive=interactive) else: if self._is_multidb(): # Django 1.2+ models.signals.post_syncdb.send( sender=app, app=app, created_models=created_models, verbosity=verbosity, interactive=interactive, db=self.db_alias, ) else: # Django 1.1 - 1.0 models.signals.post_syncdb.send( sender=app, app=app, created_models=created_models, verbosity=verbosity, interactive=interactive, )
def model_detail(request, app_label, model_name): if not utils.docutils_is_available: return missing_docutils_page(request) # Get the model class. try: app_mod = models.get_app(app_label) except ImproperlyConfigured: raise Http404, _("App %r not found") % app_label model = None for m in models.get_models(app_mod): if m._meta.object_name.lower() == model_name: model = m break if model is None: raise Http404, _("Model %(model_name)r not found in app %(app_label)r") % {'model_name': model_name, 'app_label': app_label} opts = model._meta # Gather fields/field descriptions. fields = [] for field in opts.fields: # ForeignKey is a special case since the field will actually be a # descriptor that returns the other object if isinstance(field, models.ForeignKey): data_type = related_object_name = field.rel.to.__name__ app_label = field.rel.to._meta.app_label verbose = utils.parse_rst((_("the related `%(app_label)s.%(data_type)s` object") % {'app_label': app_label, 'data_type': data_type}), 'model', _('model:') + data_type) else: data_type = get_readable_field_data_type(field) verbose = field.verbose_name fields.append({ 'name': field.name, 'data_type': data_type, 'verbose': verbose, 'help_text': field.help_text, }) # Gather many-to-many fields. for field in opts.many_to_many: data_type = related_object_name = field.rel.to.__name__ app_label = field.rel.to._meta.app_label verbose = _("related `%(app_label)s.%(object_name)s` objects") % {'app_label': app_label, 'object_name': data_type} fields.append({ 'name': "%s.all" % field.name, "data_type": 'List', 'verbose': utils.parse_rst(_("all %s") % verbose , 'model', _('model:') + opts.module_name), }) fields.append({ 'name' : "%s.count" % field.name, 'data_type' : 'Integer', 'verbose' : utils.parse_rst(_("number of %s") % verbose , 'model', _('model:') + opts.module_name), }) # Gather model methods. for func_name, func in model.__dict__.items(): if (inspect.isfunction(func) and len(inspect.getargspec(func)[0]) == 1): try: for exclude in MODEL_METHODS_EXCLUDE: if func_name.startswith(exclude): raise StopIteration except StopIteration: continue verbose = func.__doc__ if verbose: verbose = utils.parse_rst(utils.trim_docstring(verbose), 'model', _('model:') + opts.module_name) fields.append({ 'name': func_name, 'data_type': get_return_data_type(func_name), 'verbose': verbose, }) # Gather related objects for rel in opts.get_all_related_objects() + opts.get_all_related_many_to_many_objects(): verbose = _("related `%(app_label)s.%(object_name)s` objects") % {'app_label': rel.opts.app_label, 'object_name': rel.opts.object_name} accessor = rel.get_accessor_name() fields.append({ 'name' : "%s.all" % accessor, 'data_type' : 'List', 'verbose' : utils.parse_rst(_("all %s") % verbose , 'model', _('model:') + opts.module_name), }) fields.append({ 'name' : "%s.count" % accessor, 'data_type' : 'Integer', 'verbose' : utils.parse_rst(_("number of %s") % verbose , 'model', _('model:') + opts.module_name), }) return render_to_response('admin_doc/model_detail.html', { 'root_path': get_root_path(), 'name': '%s.%s' % (opts.app_label, opts.object_name), 'summary': _("Fields on %s objects") % opts.object_name, 'description': model.__doc__, 'fields': fields, }, context_instance=RequestContext(request))
def handle(self, *args, **options): kwargs = {} # @UnusedVariable include_fields = [] exclude_fields = [] inline_admins = [] # # qc # if not options['model_name']: raise CommandError( 'You need to provide a --model-name and --model-form-name') # # main # content_type_model = ContentType.objects.filter( model__iexact=options['model_name']) if content_type_model.exists() and content_type_model.count() == 1: model_name = content_type_model[0].model app_label = content_type_model[0].app_label # # the content_type.model_name is different than model.model_name # so we need content_type.model_name to get model class # and then we override it with model.object_name # logger.info("applabel %s, model_name %s " % (app_label, model_name)) model = get_model(app_label, model_name) logger.info("[ MODEL ]: %s" % model) module_name = model._meta.module_name ctype_model_name = model_name # save off the content_type name model_name = model._meta.object_name # # # get the app name # in weird nested situations # where apps are inside other apps # we don't know full context path # we we finagle it by getting app # and looking the name to understand # where it's imported from # where is something like <module>.app_name.models # full_app_label = get_app(app_label).__name__ opts = getattr(model, '_meta', None) if not opts: raise CommandError('Cannot find _meta attribute on model') for f in opts.get_all_field_names(): field, model, direct, m2m = opts.get_field_by_name( f) # @UnusedVariable name = field.name logger.info("[ FIELD ]: %s " % name) if not direct: # relation or many field from another model name = field.get_accessor_name() field = field.field if field.rel.multiple: # m2m or fk to this model #logger.debug( "m2m or fkey TO this model") #exclude_fields.append( name ) pass else: # o2o #logger.debug( "o2o TO this model") #exclude_fields.append( name ) pass else: # relation, many or field from this model if field.rel: # relation or many field if hasattr(field.rel, 'through'): # m2m #logger.debug( "m2m FROM this model") exclude_fields.append(name) else: #o2o or fkey #logger.debug( "o2o or fkey FROM this model") if not hasattr( field, 'parent_model' ): # skips fkeys that are <model>_set() exclude_fields.append(name) else: # standard field# #logger.debug( "standard field") if name != 'id': include_fields.append(name) # # # render templates # # logger.info(include_fields) mod_context = { 'inline_admins': inline_admins, 'show_fields': include_fields + exclude_fields, 'search_fields': [i for i in include_fields if i not in exclude_fields], 'order_fields': [include_fields[0]], 'filter_fields': [i for i in include_fields if i not in exclude_fields], 'app_label': full_app_label, 'model_name': model_name, 'module_name': module_name, 'include_fields': include_fields, 'exclude_fields': exclude_fields, } context = Context(mod_context) t = Template(open(BASE_TEMPLATE_PATH, "r").read()) model_form_class = t.render(context) # # # check for existence of app subdirs # # logger.info(app_label) logger.info(get_app(app_label)) create_or_replace_app_subdirs( os.path.dirname(get_app(app_label).__path__[0]), 'admin') # # # write out the model_class # # write_model_form( os.path.join(os.path.dirname(get_app(app_label).__path__[0]), 'admin', "%s_model_admin.py" % ctype_model_name), model_form_class) logger.debug('\n\n\n') logger.debug('---------------------------------------------------') logger.debug('------- COPY THESE TO YOUR MODEL ADMINS ------') logger.debug( '---------------------------------------------------\n') logger.info(model_form_class) logger.debug('\n\n\n') logger.debug('---------------------------------------------------') logger.debug('------- __INIT__.py ------') logger.debug( '---------------------------------------------------\n') # # # write out the __init__.py # # write_to_init( os.path.join(os.path.dirname(get_app(app_label).__path__[0]), "admin", "__init__.py"), "from %s_model_admin import %sModelAdmin\n" % (module_name, model_name)) logger.info("from %s_model_admin import %sModelAdmin" % (module_name, model_name)) logger.debug('\n\n\n') else: raise CommandError( 'That model name does not exist yet in content_types table. Have you added it to settings.INSTALLED_APPS and ran syncdb?' )
from django.db.models import signals, get_app from django.core.exceptions import ImproperlyConfigured from django.utils.translation import ugettext_noop as _ try: notification = get_app('notification') def create_notice_types(app, created_models, verbosity, **kwargs): notification.create_notice_type( "course_teacher_invitation", _("Invitation to Teach"), _("someone has invited you to help teach a course")) notification.create_notice_type( "course_teacher_acceptance", _("Accepted Offer to Teach"), _("someone has accepted your offer to help teach a course")) notification.create_notice_type( "course_teacher_rejection", _("Declined Offer to Teach"), _("someone has declined your offer to help teach a course")) notification.create_notice_type( "course_student_request", _("Request to Enroll"), _("someone has requested to enroll in a course that you teach")) notification.create_notice_type( "course_student_acceptance", _("Accepted Request to Enroll"), _("your request to enroll in a course has been accepted")) notification.create_notice_type( "course_student_rejection", _("Declined Request to Enroll"), _("your request to enroll in a course has been declined")) signals.post_syncdb.connect(create_notice_types, sender=notification) except ImproperlyConfigured: print "Skipping creation of NoticeTypes as notification app not found"
#-*- coding: utf-8 -*- from django.db.models import get_models, get_app, get_model from development.imagem import gerador_criar_miniaturas app = get_app(__name__.split(".")[-1]) models = get_models(app) for model in models: gerador_criar_miniaturas(model)
from django.contrib import admin from django.db.models import get_models, get_app class ModAdmin(admin.ModelAdmin): list_display = ('id', 'data', 'meta') for model in get_models(get_app('motion_app')): admin.site.register(model, ModAdmin)
def generate_dot(app_labels, **kwargs): disable_fields = kwargs.get('disable_fields', False) include_models = parse_file_or_list(kwargs.get('include_models', "")) all_applications = kwargs.get('all_applications', False) use_subgraph = kwargs.get('group_models', False) verbose_names = kwargs.get('verbose_names', False) inheritance = kwargs.get('inheritance', False) language = kwargs.get('language', None) if language is not None: activate_language(language) exclude_columns = parse_file_or_list(kwargs.get('exclude_columns', "")) exclude_models = parse_file_or_list(kwargs.get('exclude_models', "")) def skip_field(field): if exclude_columns: if verbose_names and field.verbose_name: if field.verbose_name in exclude_columns: return True if field.name in exclude_columns: return True return False t = loader.get_template_from_string(""" digraph name { fontname = "Helvetica" fontsize = 8 node [ fontname = "Helvetica" fontsize = 8 shape = "plaintext" ] edge [ fontname = "Helvetica" fontsize = 8 ] """) c = Context({}) dot = t.render(c) apps = [] if all_applications: apps = models.get_apps() for app_label in app_labels: app = models.get_app(app_label) if app not in apps: apps.append(app) graphs = [] for app in apps: graph = Context({ 'name': '"%s"' % app.__name__, 'app_name': "%s" % '.'.join(app.__name__.split('.')[:-1]), 'cluster_app_name': "cluster_%s" % app.__name__.replace(".", "_"), 'disable_fields': disable_fields, 'use_subgraph': use_subgraph, 'models': [] }) appmodels = get_models(app) abstract_models = [] for appmodel in appmodels: abstract_models = abstract_models + [ abstract_model for abstract_model in appmodel.__bases__ if hasattr( abstract_model, '_meta') and abstract_model._meta.abstract ] abstract_models = list(set(abstract_models)) # remove duplicates appmodels = abstract_models + appmodels for appmodel in appmodels: appmodel_abstracts = [ abstract_model.__name__ for abstract_model in appmodel.__bases__ if hasattr(abstract_model, '_meta') and abstract_model._meta.abstract ] # collect all attribs of abstract superclasses def getBasesAbstractFields(c): _abstract_fields = [] for e in c.__bases__: if hasattr(e, '_meta') and e._meta.abstract: _abstract_fields.extend(e._meta.fields) _abstract_fields.extend(getBasesAbstractFields(e)) return _abstract_fields abstract_fields = getBasesAbstractFields(appmodel) model = { 'app_name': appmodel.__module__.replace(".", "_"), 'name': appmodel.__name__, 'abstracts': appmodel_abstracts, 'fields': [], 'relations': [] } # consider given model name ? def consider(model_name): if exclude_models and model_name in exclude_models: return False return not include_models or model_name in include_models if not consider(appmodel._meta.object_name): continue if verbose_names and appmodel._meta.verbose_name: model['label'] = appmodel._meta.verbose_name else: model['label'] = model['name'] # model attributes def add_attributes(field): if verbose_names and field.verbose_name: label = field.verbose_name else: label = field.name t = type(field).__name__ if isinstance(field, (OneToOneField, ForeignKey)): t += " ({0})".format(field.rel.field_name) # TODO: ManyToManyField, GenericRelation model['fields'].append({ 'name': field.name, 'label': label, 'type': t, 'blank': field.blank, 'abstract': field in abstract_fields, }) # Find all the real attributes. Relations are depicted as graph # edges instead of attributes attributes = [ field for field in appmodel._meta.local_fields if not isinstance(field, RelatedField) ] # find primary key and print it first, ignoring implicit id if # other pk exists pk = appmodel._meta.pk if not appmodel._meta.abstract and pk in attributes: add_attributes(pk) for field in attributes: if skip_field(field): continue if not field.primary_key: add_attributes(field) # FIXME: actually many_to_many fields aren't saved in this model's db table, so why should we add an attribute-line for them in the resulting graph? # if appmodel._meta.many_to_many: # for field in appmodel._meta.many_to_many: # if skip_field(field): # continue # add_attributes(field) # relations def add_relation(field, extras=""): if verbose_names and field.verbose_name: label = field.verbose_name else: label = field.name # show related field name if hasattr(field, 'related_query_name'): label += ' (%s)' % field.related_query_name() # handle self-relationships if field.rel.to == 'self': target_model = field.model else: target_model = field.rel.to _rel = { 'target_app': target_model.__module__.replace('.', '_'), 'target': target_model.__name__, 'type': type(field).__name__, 'name': field.name, 'label': label, 'arrows': extras, 'needs_node': True } if _rel not in model['relations'] and consider(_rel['target']): model['relations'].append(_rel) for field in appmodel._meta.local_fields: # excluding field redundant with inheritance relation if field.attname.endswith('_ptr_id'): continue # excluding fields inherited from abstract classes. they too # show as local_fields if field in abstract_fields: continue if skip_field(field): continue if isinstance(field, OneToOneField): add_relation(field, '[arrowhead=none, arrowtail=none]') elif isinstance(field, ForeignKey): add_relation(field, '[arrowhead=none, arrowtail=dot]') for field in appmodel._meta.local_many_to_many: if skip_field(field): continue if isinstance(field, ManyToManyField): if (getattr(field, 'creates_table', False) or # django 1.1. (hasattr(field.rel.through, '_meta') and field.rel.through._meta.auto_created)): # django 1.2 add_relation( field, '[arrowhead=dot arrowtail=dot, dir=both]') elif isinstance(field, GenericRelation): add_relation( field, mark_safe( '[style="dotted", arrowhead=normal, arrowtail=normal, dir=both]' )) if inheritance: # add inheritance arrows for parent in appmodel.__bases__: if hasattr(parent, "_meta"): # parent is a model l = "multi-table" if parent._meta.abstract: l = "abstract" if appmodel._meta.proxy: l = "proxy" l += r"\ninheritance" _rel = { 'target_app': parent.__module__.replace(".", "_"), 'target': parent.__name__, 'type': "inheritance", 'name': "inheritance", 'label': l, 'arrows': '[arrowhead=empty, arrowtail=none]', 'needs_node': True } # TODO: seems as if abstract models aren't part of # models.getModels, which is why they are printed by # this without any attributes. if _rel not in model['relations'] and consider( _rel['target']): model['relations'].append(_rel) graph['models'].append(model) graphs.append(graph) nodes = [] for graph in graphs: nodes.extend([e['name'] for e in graph['models']]) for graph in graphs: # don't draw duplication nodes because of relations for model in graph['models']: for relation in model['relations']: if relation['target'] in nodes: relation['needs_node'] = False # render templates t = loader.get_template_from_string("""{% if use_subgraph %} subgraph {{ cluster_app_name }} { label=< <TABLE BORDER="0" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" ><FONT FACE="Helvetica Bold" COLOR="Black" POINT-SIZE="12" >{{ app_name }}</FONT></TD></TR> </TABLE> > color=olivedrab4 style="rounded" {% endif %} {% for model in models %} {{ model.app_name }}_{{ model.name }} [label=< <TABLE BGCOLOR="palegoldenrod" BORDER="0" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="olivedrab4" ><FONT FACE="Helvetica Bold" COLOR="white" >{{ model.label }}{% if model.abstracts %}<BR/><<FONT FACE="Helvetica Italic">{{ model.abstracts|join:"," }}</FONT>>{% endif %}</FONT></TD></TR> {% if not disable_fields %} {% for field in model.fields %} <TR><TD ALIGN="LEFT" BORDER="0" ><FONT {% if field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Helvetica {% if field.abstract %}Italic{% else %}Bold{% endif %}">{{ field.label }}</FONT ></TD> <TD ALIGN="LEFT" ><FONT {% if field.blank %}COLOR="#7B7B7B" {% endif %}FACE="Helvetica {% if field.abstract %}Italic{% else %}Bold{% endif %}">{{ field.type }}</FONT ></TD></TR> {% endfor %} {% endif %} </TABLE> >] {% endfor %} {% if use_subgraph %} } {% endif %}""") dot += '\n' + t.render(graph) for graph in graphs: t = loader.get_template_from_string("""{% for model in models %} {% for relation in model.relations %} {% if relation.needs_node %} {{ relation.target_app }}_{{ relation.target }} [label=< <TABLE BGCOLOR="palegoldenrod" BORDER="0" CELLBORDER="0" CELLSPACING="0"> <TR><TD COLSPAN="2" CELLPADDING="4" ALIGN="CENTER" BGCOLOR="olivedrab4" ><FONT FACE="Helvetica Bold" COLOR="white" >{{ relation.target }}</FONT></TD></TR> </TABLE> >] {% endif %} {{ model.app_name }}_{{ model.name }} -> {{ relation.target_app }}_{{ relation.target }} [label="{{ relation.label }}"] {{ relation.arrows }}; {% endfor %} {% endfor %}""") dot += '\n' + t.render(graph) t = loader.get_template_from_string("}") c = Context({}) dot += '\n' + t.render(c) return dot
def get_sql_for_new_models(apps=None): """ Unashamedly copied and tweaked from django.core.management.commands.syncdb """ connection = connections[DEFAULT_DB_ALIAS] # Get a list of already installed *models* so that references work right. tables = connection.introspection.table_names() seen_models = connection.introspection.installed_models(tables) created_models = set() pending_references = {} if apps: apps = [models.get_app(a) for a in apps] else: apps = models.get_apps() # Build the manifest of apps and models that are to be synchronized all_models = [(app.__name__.split('.')[-2], [ m for m in models.get_models(app, include_auto_created=True) if router.allow_syncdb(DEFAULT_DB_ALIAS, m) ]) for app in apps] def model_installed(model): opts = model._meta converter = connection.introspection.table_name_converter db_table_in = (converter(opts.db_table) in tables) auto_create_in = ( opts.auto_created and \ converter(opts.auto_created._meta.db_table) in tables ) return not (db_table_in or auto_create_in) manifest = SortedDict((app_name, filter(model_installed, model_list)) for app_name, model_list in all_models) statements = [] sql = None for app_name, model_list in manifest.items(): for model in model_list: # Create the model's database table, if it doesn't already exist. sql, references = connection.creation.sql_create_model( model, no_style(), seen_models) seen_models.add(model) created_models.add(model) statements.append( "### New Model: %s.%s" % (app_name, str(model).replace("'>", "").split(".")[-1])) for refto, refs in references.items(): pending_references.setdefault(refto, []).extend(refs) if refto in seen_models: sql.extend( connection.creation.sql_for_pending_references( refto, no_style(), pending_references)) sql.extend( connection.creation.sql_for_pending_references( model, no_style(), pending_references)) statements.extend(sql) custom_sql = None for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: custom_sql = custom_sql_for_model(model, no_style(), connection) if custom_sql: statements.extend(custom_sql) index_sql = None for app_name, model_list in manifest.items(): for model in model_list: if model in created_models: index_sql = connection.creation.sql_indexes_for_model( model, no_style()) if index_sql: statements.extend(index_sql) return statements
def fake_permissions(self): ''' Add fake app missing content types and permissions''' app = get_app('fake') update_contenttypes(app, None, 0) create_permissions(app, None, 0)
def forwards(self, orm): print "Running reversion.management.commands.createinitialrevisions (this could take several minutes)..." app = models.get_app('qsd') model_class = models.get_model('qsd', 'QuasiStaticData') createinitialrevisions_command = createinitialrevisions.Command() createinitialrevisions_command.create_initial_revisions( app, model_class, "QSD initial revisions.", 500) print "Finished running reversion.management.commands.createinitialrevisions." print "Creating initial revisions for QSD objects (this may take a while)..." # go through list of QuasiStaticData ordered by URL and # create a new object for each group with the same URL # (and delete old objects) qsdObjects = QuasiStaticData.objects.order_by('url', 'create_date') lastQsd = None counter = 0 for qsd in qsdObjects: if lastQsd and lastQsd.url == qsd.url: try: with reversion.create_revision(): lastQsd.author = qsd.author lastQsd.content = qsd.content lastQsd.title = qsd.title lastQsd.description = qsd.description lastQsd.nav_category = qsd.nav_category lastQsd.keywords = qsd.keywords lastQsd.disabled = qsd.disabled lastQsd.create_date = qsd.create_date if lastQsd.disabled is None: lastQsd.disabled = False lastQsd.save() except (ESPUser.DoesNotExist, NavBarCategory.DoesNotExist): print "... Warning: skipping " + str(qsd.url) + "/" + str( qsd.id) + " due to DoesNotExist error" else: with reversion.create_revision(): lastQsd = qsd.copy() if lastQsd.disabled is None: lastQsd.disabled = False lastQsd.save() # here we want to delete both the QSD objects itself # but also any revisions that we already have filed for it qsdRevisions = reversion.get_for_object(qsd) qsd.delete() for qsdRevision in qsdRevisions: qsdRevision.delete() counter += 1 if counter % 1000 == 0: print "... " + str(counter) + "/" + str(len(qsdObjects)) print "Finished creating initial revisions for QSD objects." print "Updating dates for newly created revisions" for version in Version.objects.filter(content_type=ContentType.objects. get_for_model(QuasiStaticData)): revision = version.revision revision.date_created = version.field_dict['create_date'] revision.save() print "Finished updating revision dates."
def create_permissions_compat(app, **kwargs): from django.db.models import get_app from django.contrib.auth.management import create_permissions create_permissions(get_app(app), (), 0)
def handle(self, *app_labels, **options): import sys, traceback from django.db.models import get_app, get_apps, get_models, get_model format = options.get('format','json') exclude = options.get('exclude',[]) show_traceback = options.get('traceback', False) stop_on_errors = options.get('stop_on_errors', False) database = options.get('database','default') excluded_apps = [get_app(app_label) for app_label in exclude] if len(app_labels) == 0: app_list = SortedDict([(app, None) for app in get_apps() if app not in excluded_apps]) else: app_list = SortedDict() for label in app_labels: try: app_label, model_label = label.split('.') try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) model = get_model(app_label, model_label) if model is None: raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) if app in app_list.keys(): if app_list[app] and model not in app_list[app]: app_list[app].append(model) else: app_list[app] = [model] except ValueError: # This is just an app - no model qualifier app_label = label try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) app_list[app] = None # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. if format not in serializers.get_public_serializer_formats(): raise CommandError("Unknown serialization format: %s" % format) try: serializers.get_serializer(format) except KeyError: raise CommandError("Unknown serialization format: %s" % format) objects = [] for app, model_list in app_list.items(): if model_list is None: model_list = get_models(app) for model in model_list: if not model._meta.proxy: #TODO: self.stdout, self.stderr #leon sys.stderr.write('Searching %s\n' % model) try: # Try serializing the whole lot first, before going one by one serializers.serialize(format, model._default_manager.using(database).all()) #sys.stdout.write('Successfully serialized %s\n' % model) except: sys.stderr.write('Error serializing %s\n' % model) for instance in model._default_manager.using(database).all(): #sys.stdout.write('Serializing %s\n' % instance) try: serializers.serialize(format, model._default_manager.using(database).filter(pk=instance.pk)) #sys.stderr.write('Succesfully serialized %s\n' % instance.__dict__) except Exception, e: sys.stderr.write('\nERROR IN %s instance: %s\n' % (model, `instance`)) sys.stdout.write('%s\n' % instance.__dict__) if stop_on_errors: raise else: sys.stderr.write(traceback.format_exc())
from django.db.models.fields.files import FieldFile, FileField from django.db.models.fields.related import ManyToManyField import django.db.models.fields.related as related #import django.db.models.fields.related.ManyRelatedManager as ManyRelatedManager import django.db.models.fields as fields from types import * from django.db.models import get_app #NOTE: the following can be defined in a 'jsrecord.settings.py'--just like #how settings.py lets you define which db table to access, here were can define #which app models to expose. # #02-07-11 Follow up: we should have router take in a different param called #app! THEN jsrecord will truly be plug and play for any project! MODELS = get_app('datacollection') #dynamically build their forms and map them #NOTE: we use _MODELFORMS to validate the fields in Save, but that's alot of #lines for something that is easy...maybe i should redo this _MODELFORMS = {} MODEL_TYPES = [] #a trick to get the current module _modname = globals()['__name__'] _this_mod = sys.modules[_modname] #timeout = 15 mins _timeout = 60 * 15 try:
def forwards(self, orm): "Create custom model permissions." create_permissions(models.get_app('api'), models.get_models(), 2 if settings.DEBUG else 0)
def handle(self, *app_labels, **options): from django.db.models import get_app, get_apps, get_models, get_model format = options.get('format','json') indent = options.get('indent',None) using = options.get('database', DEFAULT_DB_ALIAS) connection = connections[using] exclude = options.get('exclude',[]) show_traceback = options.get('traceback', False) use_natural_keys = options.get('use_natural_keys', False) excluded_apps = set(get_app(app_label) for app_label in exclude) if len(app_labels) == 0: app_list = SortedDict((app, None) for app in get_apps() if app not in excluded_apps) else: app_list = SortedDict() for label in app_labels: try: app_label, model_label = label.split('.') try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) model = get_model(app_label, model_label) if model is None: raise CommandError("Unknown model: %s.%s" % (app_label, model_label)) if app in app_list.keys(): if app_list[app] and model not in app_list[app]: app_list[app].append(model) else: app_list[app] = [model] except ValueError: # This is just an app - no model qualifier app_label = label try: app = get_app(app_label) except ImproperlyConfigured: raise CommandError("Unknown application: %s" % app_label) app_list[app] = None # Check that the serialization format exists; this is a shortcut to # avoid collating all the objects and _then_ failing. if format not in serializers.get_public_serializer_formats(): raise CommandError("Unknown serialization format: %s" % format) try: serializers.get_serializer(format) except KeyError: raise CommandError("Unknown serialization format: %s" % format) # Now collate the objects to be serialized. objects = [] for model in sort_dependencies(app_list.items()): if not model._meta.proxy and router.allow_syncdb(using, model): objects.extend(model._default_manager.using(using).all()) try: return serializers.serialize(format, objects, indent=indent, use_natural_keys=use_natural_keys) except Exception, e: if show_traceback: raise raise CommandError("Unable to serialize database: %s" % e)
from django.contrib import admin from django.contrib.auth.models import User from django.db.models import get_models, get_app for model in get_models(get_app('retrospective')): admin.site.register(model)
def build_test(label): """Construct a test case with the specified label. Label should be of the form model.TestClass or model.TestClass.test_method. Returns an instantiated test or test suite corresponding to the label provided. """ parts = label.split('.') if len(parts) < 2 or len(parts) > 3: raise ValueError("Test label '%s' should be of the form app.TestCase or app.TestCase.test_method" % label) # # First, look for TestCase instances with a name that matches # app_module = get_app(parts[0]) test_module = get_tests(app_module) TestClass = getattr(app_module, parts[1], None) # Couldn't find the test class in models.py; look in tests.py if TestClass is None: if test_module: TestClass = getattr(test_module, parts[1], None) try: if issubclass(TestClass, (unittest.TestCase, real_unittest.TestCase)): if len(parts) == 2: # label is app.TestClass try: return unittest.TestLoader().loadTestsFromTestCase(TestClass) except TypeError: raise ValueError("Test label '%s' does not refer to a test class" % label) else: # label is app.TestClass.test_method return TestClass(parts[2]) except TypeError: # TestClass isn't a TestClass - it must be a method or normal class pass # # If there isn't a TestCase, look for a doctest that matches # tests = [] for module in app_module, test_module: try: doctests = doctest.DocTestSuite(module, checker=doctestOutputChecker, runner=DocTestRunner) # Now iterate over the suite, looking for doctests whose name # matches the pattern that was given for test in doctests: if test._dt_test.name in ( '%s.%s' % (module.__name__, '.'.join(parts[1:])), '%s.__test__.%s' % (module.__name__, '.'.join(parts[1:]))): tests.append(test) except ValueError: # No doctests found. pass # If no tests were found, then we were given a bad test label. if not tests: raise ValueError("Test label '%s' does not refer to a test" % label) # Construct a suite out of the tests that matched. return unittest.TestSuite(tests)
# # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. #------------------------------------------------------------------------------- from django.utils.text import wrap from django.utils.translation import ugettext_lazy as _ from django.contrib.sites.models import Site from django.template import Context, loader from django.template.loader import render_to_string from django.conf import settings from django.db.models import get_app from django.core.exceptions import ImproperlyConfigured # favour django-mailer but fall back to django.core.mail try: mailer = get_app("mailer") from mailer import send_mail except ImproperlyConfigured: from django.core.mail import send_mail def format_quote(text): """ Wraps text at 55 chars and prepends each line with `> `. Used for quoting messages in replies. """ lines = wrap(text, 55).split('\n') for i, line in enumerate(lines): lines[i] = "> %s" % line return '\n'.join(lines)