def build_suite(self, test_labels, extra_tests=None, **kwargs): cache = AppCache() get_app = cache.get_app get_apps = cache.get_apps suite = unittest.TestSuite() #get all features for given apps if test_labels: for label in test_labels: if '.' in label: print 'Ignoring label with dot in: ' % label continue app = get_app(label) features_dir = get_features(app) if features_dir is not None: suite.addTest(self.make_bdd_test_suite(features_dir)) else: for app in get_apps(): features_dir = get_features(app) if features_dir is not None: suite.addTest(self.make_bdd_test_suite(features_dir)) return reorder_suite(suite, (LiveServerTestCase, ))
def _redo_app_cache(self): """ Used to repopulate AppCache after fiddling with INSTALLED_APPS. """ a = AppCache() a.loaded = False a._populate()
def _reload_models_module(self, app_name): """ Reload Django models Based on http://stackoverflow.com/questions/890924/how-do-you-reload-a-django-model-module-using-the-interactive-interpreter-via-m """ curdir = os.getcwd() cache = AppCache() for app in cache.get_apps(): f = app.__file__ if f.startswith(curdir) and f.endswith('.pyc'): try: os.remove(f) except Exception: pass __import__(app.__name__) reload(app) cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False # Reset app's models in global scope # Using a dictionary here instead of cache.get_models(app_name) # The latter does not seem to work (look into that) reimported_app = importlib.import_module("{}.models".format(app_name)) model_names = self.model_globals[app_name] for model_name in model_names: self.shell_globals[model_name] = getattr(reimported_app, model_name) self._update_class_instances(reimported_app, model_name)
def _redo_app_cache(self): """ Used to repopulate AppCache after fiddling with INSTALLED_APPS. """ a = AppCache() a.loaded = False a.handled = {} a.postponed = [] a.app_store = SortedDict() a.app_models = SortedDict() a.app_errors = {} a._populate()
def reload_django_appcache(): cache = AppCache() cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False for app in cache.get_apps(): __import__(app.__name__) reload(app)
def test_missing_app(self): """ Test that repeated app loading doesn't succeed in case there is an error. Refs #17667. """ # AppCache is a Borg, so we can instantiate one and change its # loaded to False to force the following code to actually try to # populate the cache. a = AppCache() a.loaded = False try: with override_settings(INSTALLED_APPS=('notexists',)): self.assertRaises(ImportError, get_model, 'notexists', 'nomodel', seed_cache=True) self.assertRaises(ImportError, get_model, 'notexists', 'nomodel', seed_cache=True) finally: a.loaded = True
def handle(self, *args, **options): apps = AppCache() check = [] for module in apps.get_apps(): for d in module.__dict__: ref = getattr(module, d) if isinstance(ref, simpledb.models.ModelMetaclass): domain = ref.Meta.domain.name if domain not in check: check.append(domain) sdb = simpledb.SimpleDB(settings.AWS_KEY, settings.AWS_SECRET) domains = [d.name for d in list(sdb)] for c in check: if c not in domains: sdb.create_domain(c) print "Creating domain %s ..." % c
def reload_models(): import os from django.db.models.loading import AppCache cache = AppCache() curdir = os.getcwd() for app in cache.get_apps(): f = app.__file__ if f.startswith(curdir) and f.endswith('.pyc'): os.remove(f) __import__(app.__name__) reload(app) from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
def create_node_model(name, fields=None, app_label='', module='', options=None): """ Create specified model """ from app.detective.models import update_topic_cache, delete_entity from neo4django.db import models from django.db.models.loading import AppCache # Django use a cache by model cache = AppCache() # If we already create a model for this app if app_label in cache.app_models and name.lower( ) in cache.app_models[app_label]: # We just delete it quietly del cache.app_models[app_label][name.lower()] class Meta: # Using type('Meta', ...) gives a dictproxy error during model creation pass if app_label: # app_label must be set using the Meta inner class setattr(Meta, 'app_label', app_label) # Update Meta with any options that were provided if options is not None: for key, value in options.iteritems(): setattr(Meta, key, value) # Set up a dictionary to simulate declarations within a class attrs = {'__module__': module, 'Meta': Meta} # Add in any fields that were provided if fields: attrs.update(fields) # Create the class, which automatically triggers ModelBase processing cls = type(name, (FluidNodeModel, ), attrs) signals.post_save.connect(update_topic_cache, sender=cls) signals.post_delete.connect(delete_entity, sender=cls) return cls
def section(request, section_path=None, id_override=None): """ A view of a section. """ try: section = get_current_section() except MiddlewareNotUsed: lookup_from = id_override or request section = lookup_section(lookup_from) if section: return simple.direct_to_template(request, template="scaffold/section.html", extra_context={'section': section}) else: app_cache = AppCache() try: app_cache.get_app('flatpages') try: return flatpage(request, request.path_info) except Http404: pass except ImproperlyConfigured: pass raise Http404, "Section does not exist."
#! /usr/bin/env python from sqlalchemy import types, Column, Table import django from django.conf import settings try: from django.apps import apps as django_apps except ImportError: from django.db.models.loading import AppCache django_apps = AppCache() from aldjemy.types import simple, foreign_key, varchar DATA_TYPES = { 'AutoField': simple(types.Integer), 'BigAutoField': simple(types.BigInteger), 'BooleanField': simple(types.Boolean), 'CharField': varchar, 'CommaSeparatedIntegerField': varchar, 'DateField': simple(types.Date), 'DateTimeField': simple(types.DateTime), 'DecimalField': lambda x: types.Numeric(scale=x.decimal_places, precision=x.max_digits),
def render(self, request): from django.db.models.query import QuerySet seria = self.construct() title = "table" heads = "" if isinstance(self.data, QuerySet): model = self.data.model fields = [('|' in f) and f.split("|")[0] or f for f in self.fields] heads = [get_field_verbosename(model, f, ".") for f in fields] title = model._meta.verbose_name self.cells = [] for a in seria: cells_list = [] for f in fields: if f in a: f_check = '.' in f and f.split('.')[0] or f field = "" try: field = model._meta.get_field(f_check) except: pass if field: if isinstance( field, models.fields.related.ManyToManyField): m2m_field_display = hasattr( model, 'Admin' ) and model.Admin.api_m2m_display[ f] or "" #m2m要显示字段,多对多字段要导出必须在api_fields中配置要显示的字段(一个或多个,多个时用“ ”隔开显示) m2m_field_verbose = [] for record in a[f]: d_list = [] for d in m2m_field_display.split('.'): d_list.append(record[d]) m2m_field_verbose.append(' '.join(d_list)) cells_list.append(','.join(m2m_field_verbose)) elif type(a[f]) == dict and isinstance( field, models.fields.related.ForeignKey): if u"%s" % a[f]['id'] == 'None': cells_list.append('') else: if hasattr(field.rel.to, "export_unicode"): instance = field.rel.to.objects.get( id=a[f]['id']) if instance: cells_list.append( instance.export_unicode()) else: cells_list.append(a[f]) else: if a[f] == None: cells_list.append('') else: if type(a[f]) == dict and u"%s" % ( a[f]['verbose']) == 'None': cells_list.append('') else: cells_list.append(a[f]) else: if a[f] == None: cells_list.append('') else: if type(a[f]) == dict and u"%s" % ( a[f]['verbose']) == 'None': cells_list.append('') else: cells_list.append(a[f]) self.cells.append(tuple(cells_list)) else: try: from django.db.models.loading import AppCache http_app = request.META["HTTP_REFERER"].split("/")[-3] http_model = request.META["HTTP_REFERER"].split("/")[-2] modelname = AppCache().app_models.get(http_app).get( http_model.lower()) title = modelname._meta.verbose_name except: title = "table" fields = self.fields or (self.data and self.data[0].keys() or []) self.cells = [ tuple([a[f] or '' for f in fields if f in a]) for a in seria ] self.head = heads if hasattr(request, "special_head"): self.head = request.special_head rn = request.REQUEST.get("reportname", '') title = rn and rn or title #print "---rn=%s---title=%s"%(rn,title) self.title = title self.file_name = "%s_%s" % ( title, datetime.datetime.now().strftime("%Y%m%d%H%M%S")) return self.render_data(request)
from django.db.models.loading import AppCache cache = AppCache() for app in cache.get_apps(): __import__(app.__name__) reload(app) from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
# from https://github.com/dgreisen/django-traversal/blob/master/appring.py # Apache 2.0 licensed try: from django.db.models.loading import AppCache apps = AppCache() except: from django.apps import apps from django.conf import settings from importlib import import_module from types import ModuleType class Apps(object): """ Return a ModuleWrapper-wrapped instance of the app module. The ModuleWrapper makes it easy to get to nested submodules through a dot notation. No need to do further importing. """ def __init__(self): self.app_paths = {x.split('.')[-1]: x for x in settings.INSTALLED_APPS} def __getattr__(self, name): """ If the package doesn't exist, then locate it, attach it and return it """ try: packageString = self.app_paths[name] package = import_module(packageString) package = ModuleWrapper(package, packageString) setattr(self, name, package)
from django.db import models from django.db.models.loading import AppCache app_cache = AppCache() class DefaultTracker(models.Model): built = models.BooleanField(default=False) needs_rebuild = models.BooleanField(default=False) class Meta: abstract = True class AppDefaultTracker(DefaultTracker): app_name = models.CharField(max_length=100, unique=True) def get_or_create_model_tracker(self, model_name): created = False try: m = self.model_trackers.get(model_name=model_name) except ModelDefaultTracker.DoesNotExist: m = self.model_trackers.create(model_name=model_name) if created: self.built = False self.save() return m def check_models(self): built = self.built needs_rebuild = False
def get_django_models(): ac = AppCache() return ac.get_models()
def redo_app_cache(self): from django.db.models.loading import AppCache a = AppCache() a.loaded = False a._populate()