Ejemplo n.º 1
0
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."
Ejemplo n.º 2
0
 def _redo_app_cache(self):
     """
     Used to repopulate AppCache after fiddling with INSTALLED_APPS.
     """
     a = AppCache()
     a.loaded = False
     a._populate()
Ejemplo n.º 3
0
def get_columns(request,tables):
    # Strip leading/trailing whitespace and punctuation (so we only have
    # comma-delimited list)
    tables = tables.strip(string.whitespace + string.punctuation)
    if not tables:
        raise Http404
    
    tables = tables.split(",")
    
    app_loader = AppCache()
    
    tables2 = []
    real_tables = []
    columns = []
    for table in tables:
        try:
            real_table = APP_MAP[table]
            
            model = app_loader.get_model(*real_table.split("."))
            
            tables2.append(table)
            real_tables.append(real_table)
            columns.append(
                model.objects.all().values()[0].keys()
            )
        except:
            pass
    
    return HttpResponse(
        json.dumps({
            "tables":tables,
            "real_tables":real_tables,
            "columns":columns
        }, indent=4),
        mimetype="text/javascript")
Ejemplo n.º 4
0
 def _redo_app_cache(self):
     """
     Used to repopulate AppCache after fiddling with INSTALLED_APPS.
     """
     a = AppCache()
     a.loaded = False
     a._populate()
Ejemplo n.º 5
0
 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()
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
 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()
Ejemplo n.º 8
0
    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, ))
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    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 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)
Ejemplo n.º 14
0
class Models(object):
    def __init__(self):
        self.cache = AppCache()

    def __getattr__(self, name):
        """
        If the package doesn't exist, then locate it, attach it and return it
        """
        try:
            package = ModelPackage(self.cache.get_app(name), name, self)
            setattr(self, name, package)
            return package
        except:
            raise AttributeError("object 'Models' has no attribute '{}'".format(name))

    def _get_models(self, pkg):
        """
        get the models for a given package. used by Package children
        """
        return self.cache.get_models(pkg)
Ejemplo n.º 15
0
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 render_to_response("scaffold/section.html", {'section': section}, context_instance=RequestContext(request))
    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."
Ejemplo n.º 16
0
def clean_cache():
    from django.db.models.loading import AppCache
    cache = AppCache()
    from django.utils.datastructures import SortedDict
    cache.app_store = SortedDict()
    cache.app_models = SortedDict()
    cache.app_errors = {}
    cache.handled = {}
    cache.loaded = False
Ejemplo n.º 17
0
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."
Ejemplo n.º 18
0
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
Ejemplo n.º 19
0
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
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
# 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)
Ejemplo n.º 22
0
 def redo_app_cache(self):
     from django.db.models.loading import AppCache
     a = AppCache()
     a.loaded = False
     a._populate()
Ejemplo n.º 23
0
def reload_modules():
    """
    Reload modules that don't have any variables that can be reset.
    Note that python reloading is a tricky art and strange things have
    been known to happen if debugging and reloading a lot. A server 
    cold reboot is often needed eventually.

    """
    # We protect e.g. src/ from reload since reloading it in a running
    # server can create unexpected results (and besides, non-evennia devs 
    # should never need to do that anyway). Updating src requires a server
    # reboot. Modules in except_dirs are considered ok to reload despite being
    # inside src/
    protected_dirs = ('src.', 'django.', 'twisted.')     # note that these MUST be tuples!
    except_dirs = ('src.commands.default.',) #                          "  

    # flag 'dangerous' typeclasses (those which retain a memory
    # reference, notably Scripts with a timer component) for
    # non-reload, since these cannot be safely cleaned from memory
    # without causing havoc. A server reboot is required for updating
    # these (or killing all running, timed scripts).
    unsafe_modules = []
    for scriptobj in ScriptDB.objects.get_all_scripts():
        if (scriptobj.interval > -1) and scriptobj.typeclass_path:
            unsafe_modules.append(scriptobj.typeclass_path)            
    unsafe_modules = list(set(unsafe_modules))

    def safe_dir_to_reload(modpath):
        "Check so modpath is not a subdir of a protected dir, and not an ok exception"
        return not any(modpath.startswith(pdir) and not any(modpath.startswith(edir) for edir in except_dirs) for pdir in protected_dirs)
    def safe_mod_to_reload(modpath):
        "Check so modpath is not in an unsafe module"
        return not any(mpath.startswith(modpath) for mpath in unsafe_modules)
                                               
    #cemit_info(" Cleaning module caches ...")

    # clean as much of the caches as we can
    cache = AppCache()
    cache.app_store = SortedDict()
    #cache.app_models = SortedDict() # cannot clean this, it resets ContentTypes!
    cache.app_errors = {}
    cache.handled = {}
    cache.loaded = False
 
    # find modified modules 
    modified = reimport.modified()
    safe_dir_modified = [mod for mod in modified if safe_dir_to_reload(mod)]
    unsafe_dir_modified = [mod for mod in modified if mod not in safe_dir_modified]
    safe_modified = [mod for mod in safe_dir_modified if safe_mod_to_reload(mod)]
    unsafe_mod_modified = [mod for mod in safe_dir_modified if mod not in safe_modified]

    string = ""
    if unsafe_dir_modified or unsafe_mod_modified:

        if unsafe_mod_modified:
            string += "\n {rModules containing Script classes with a timer component{n" 
            string += "\n {rand which has already spawned instances cannot be reloaded safely.{n"             
        string += "\n {rThese module(s) can only be reloaded by server reboot:{n\n  %s\n"
        string = string % ", ".join(unsafe_dir_modified + unsafe_mod_modified)

    if string:
        cemit_info(string) 

    if safe_modified:
        cemit_info(" Reloading safe module(s):{n\n  %s" % "\n  ".join(safe_modified))
        reimport.reimport(*safe_modified)
        wait_time = 5
        cemit_info(" Waiting %s secs to give modules time to re-cache ..." % wait_time)
        time.sleep(wait_time)        
        cemit_info(" ... all safe modules reloaded.") 
    else:
        cemit_info(" No modules reloaded.")

    # clean out cache dictionary of typeclasses, exits and channels    
    typeclassmodels.reset()    
    channelhandler.CHANNELHANDLER.update()
Ejemplo n.º 24
0
#! /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),
Ejemplo n.º 25
0
def get_django_models():
    ac = AppCache()
    return ac.get_models()
Ejemplo n.º 26
0
import json 

from django.http import HttpResponseBadRequest, HttpResponse
from lfs.addresses.models import Address

from django.utils import simplejson
from django.conf import settings

from django.db.models.loading import AppCache
from django.core.serializers.json import DjangoJSONEncoder
from django.utils import simplejson

if hasattr(settings, "LFS_ADDRESS_MODEL"):
    path, app_name, models, address_model = settings.LFS_ADDRESS_MODEL.rsplit('.',3)
    cache = AppCache()
    address_model = cache.get_model(app_name, address_model)
else:
    address_model = Address

def retrieve_address(request):
    """ This function is used to retrieve the address information of an
        address, given address id via ajax call
    """

    if not request.is_ajax():
        return HttpResponseBadRequest("XMLHttpRequest expected")

    address_id = request.POST.get('address_id')
    address_info = address_model.objects.filter(id=address_id).values()

    if address_info is None:
Ejemplo n.º 27
0
from app.detective import parser, utils
from app.detective.modelrules import ModelRules
from app.detective.models import Topic
from django.conf.urls import url, include, patterns
from django.conf import settings
from django.core.cache import cache
from django.core.urlresolvers import clear_url_caches
from django.db.models.loading import AppCache
from tastypie.api import NamespacedApi

import importlib
import sys
import imp

appcache = AppCache()


class TopicRegistor(object):
    __instance = None

    def __is_topic(self, topic):
        return isinstance(topic, Topic)

    def __get_topic_key(self, topic):
        key = topic
        if self.__is_topic(topic):
            key = topic.ontology_as_mod
        return key

    def __topic_models(self, topic):
        key = self.__get_topic_key(topic)
Ejemplo n.º 28
0
def restart_cache():
    import os
    from django.db.models.loading import AppCache
    cache = AppCache()

    cache.loaded = False
Ejemplo n.º 29
0
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
Ejemplo n.º 30
0
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
Ejemplo n.º 31
0
def get_searchable_models():
    """
    Returns a list of all models in the Django project which implement ISearchable
    """
    app = AppCache();
    return filter(lambda klass: implements(klass, ISearchable), app.get_models())
Ejemplo n.º 32
0
    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)
 def __init__(self):
     app = AppCache()
     models = app.get_models()
     for m in models:
         setattr(self, m.__name__, m)
Ejemplo n.º 34
0
def get_django_models():
    ac = AppCache()
    return ac.get_models()
Ejemplo n.º 35
0
 def __init__(self):
     self.cache = AppCache()
Ejemplo n.º 36
0
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
Ejemplo n.º 37
0
 def redo_app_cache(self):
     from django.db.models.loading import AppCache
     a = AppCache()
     a.loaded = False
     a._populate()