Пример #1
0
def tag_unknown_locations_and_publish():
    import sys, os
    sys.path.append(os.path.realpath(os.sep.join([os.path.dirname(__file__), os.pardir, os.pardir])))
    from django.core.management import setup_environ
    import settings
    setup_environ(settings)

    from django.db.models.loading import cache as model_cache
    model_cache._populate()

    from django.core.cache import cache

    from entity_items.models.location import Location
    classified = Location.objects.filter(classification__isnull=False)

    decision_tree = LocationClassifier.train(classified)
    log.info("Decision tree: %s" % decision_tree)
    if decision_tree:
        cache.set(LocationClassifier.cache_key, decision_tree.copy())    # Have to copy it so it's a normal dict again

    unclassified = Location.objects.filter(classification__isnull=True)
    for loc in unclassified:
        classification = LocationClassifier.classify(loc)
        if classification:
            log.info("Saving location")
            loc.classification = classification
            loc.save()
Пример #2
0
def setup_django():
    from django.conf import settings
    if not settings.configured:
        settings.configure(
            DEBUG=True,
            DATABASES={
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    'NAME': ':memory:',
                }
            },
            INSTALLED_APPS=(
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django_ftpserver',
            ),
            MIDDLEWARE_CLASSES=(),
            FTPSERVER_ROOT='ftproot/',
        )
    from django import VERSION as version
    # In Django 1.7 or later, using "django.apps" module.
    if version[0] == 1 and version[1] >= 7:
        from django.apps import apps
        if not apps.ready:
            apps.populate(settings.INSTALLED_APPS)
            from django.core.management import call_command
            call_command('syncdb', interactive=False)
    else:
        from django.db.models.loading import cache as model_cache
        if not model_cache.loaded:
            model_cache._populate()
            from django.core.management import call_command
            call_command('syncdb', interactive=False)
Пример #3
0
def tag_unknown_locations_and_publish():
    import sys, os
    sys.path.append(
        os.path.realpath(
            os.sep.join([os.path.dirname(__file__), os.pardir, os.pardir])))
    from django.core.management import setup_environ
    import settings
    setup_environ(settings)

    from django.db.models.loading import cache as model_cache
    model_cache._populate()

    from django.core.cache import cache

    from entity_items.models.location import Location
    classified = Location.objects.filter(classification__isnull=False)

    decision_tree = LocationClassifier.train(classified)
    log.info("Decision tree: %s" % decision_tree)
    if decision_tree:
        cache.set(LocationClassifier.cache_key, decision_tree.copy()
                  )  # Have to copy it so it's a normal dict again

    unclassified = Location.objects.filter(classification__isnull=True)
    for loc in unclassified:
        classification = LocationClassifier.classify(loc)
        if classification:
            log.info("Saving location")
            loc.classification = classification
            loc.save()
Пример #4
0
def setup_django():
    from django.conf import settings
    if not settings.configured:
        settings.configure(
            DEBUG=True,
            DATABASES={
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    'NAME': ':memory:',
                }
            },
            INSTALLED_APPS=(
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django_ftpserver',
            ),
            MIDDLEWARE_CLASSES=(),
        )
    from django import VERSION as version
    # In Django 1.7 or later, using "django.apps" module.
    if version[0] == 1 and version[1] >= 7:
        from django.apps import apps
        if not apps.ready:
            apps.populate(settings.INSTALLED_APPS)
            from django.core.management import call_command
            call_command('migrate', interactive=False)
    else:
        from django.db.models.loading import cache as model_cache
        if not model_cache.loaded:
            model_cache._populate()
            from django.core.management import call_command
            call_command('syncdb', interactive=False)
Пример #5
0
 def repopulate_app_cache(self):
     """
     Rebuilds AppCache with the real model definitions.
     """
     if DJANGO_17:
         apps.clear_cache()
     else:
         cache._populate()
Пример #6
0
 def repopulate_app_cache(self):
     """
     Rebuilds AppCache with the real model definitions.
     """
     if DJANGO_17:
         apps.clear_cache()
     else:
         cache._populate()
Пример #7
0
    def setUp(self):
        self.client = Client()
        self.mock_distributed_lock()

        #workaround on model loading issue
        #http://stackoverflow.com/questions/14386536/instantiating-django-model-raises-typeerror-isinstance-arg-2-must-be-a-class
        if not model_cache.loaded: model_cache._populate()

        self.ip = Ip(oct1 = 192, oct2 = 168, oct3 = 0, oct4 = 15)
        self.vip = RequisicaoVips(id = 1, ip = self.ip)
Пример #8
0
 def _redo_app_cache(self):
     """
     Used to repopulate AppCache after fiddling with INSTALLED_APPS.
     """
     cache.loaded = False
     cache.handled = set() if django.VERSION >= (1, 6) else {}
     cache.postponed = []
     cache.app_store = SortedDict()
     cache.app_models = SortedDict()
     cache.app_errors = {}
     cache._populate()
Пример #9
0
 def _redo_app_cache(self):
     """
     Used to repopulate AppCache after fiddling with INSTALLED_APPS.
     """
     cache.loaded = False
     cache.handled = set() if django.VERSION >= (1, 6) else {}
     cache.postponed = []
     cache.app_store = SortedDict()
     cache.app_models = SortedDict()
     cache.app_errors = {}
     cache._populate()
Пример #10
0
    def tearDownClass(cls):
        # Restore sys path
        sys.path = cls.old_sys_path

        # Restore settings
        cls.override.disable()

        # Clean app cache
        cache._get_models_cache.clear()
        cache.handled.clear()
        cache.loaded = False
        cache._populate()
Пример #11
0
    def tearDownClass(cls):
        # Restore sys path
        sys.path = cls.old_sys_path

        # Restore settings
        settings.INSTALLED_APPS = cls.old_installed_apps

        # Reload app cache
        cache._get_models_cache.clear()
        cache.handled.clear()
        cache.loaded = False
        cache._populate()
Пример #12
0
    def handle(self, *args, **options):
        # For django to work, we need to populate apps and models as soon as possible
        # If the apps have not yet been populated by now (as is the case when using uwsgi),
        # populate.
        from django.db.models.loading import cache
        cache._populate()

        # Due to the use of LaxOptionParser ``args`` now contains all
        # unparsed options, and ``options`` those that the Django command
        # has declared.

        # Create log
        log = logging.getLogger('django-assets')
        log.setLevel({0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}[int(options.get('verbosity', 1))])
        log.addHandler(logging.StreamHandler())

        # If the user requested it, search for bundles defined in templates
        if options.get('parse_templates'):
            log.info('Searching templates...')
            # Note that we exclude container bundles. By their very nature,
            # they are guaranteed to have been created by solely referencing
            # other bundles which are already registered.
            get_env().add(*[b for b in self.load_from_templates()
                            if not b.is_container])

        if len(get_env()) == 0:
            raise CommandError('No asset bundles were found. '
                'If you are defining assets directly within your '
                'templates, you want to use the --parse-templates '
                'option.')

        prog = "%s assets" % path.basename(sys.argv[0])
        impl = GenericArgparseImplementation(
            env=get_env(), log=log, no_global_options=True, prog=prog)
        try:
            # The webassets script runner may either return None on success (so
            # map that to zero) or a return code on build failure (so raise
            # a Django CommandError exception when that happens)
            retval = impl.run_with_argv(args) or 0
            if retval != 0:
                raise CommandError('The webassets build script exited with '
                                   'a non-zero exit code (%d).' % retval)
        except AssetCommandError as e:
            raise CommandError(e)
Пример #13
0
def _cleanup(*models):
    """ Function to delete models from the AppCache and remove them from
    the models module in which they're defined.
    """
    from django.db.models.loading import cache
    deleted = []

    # Note that we want to use the import lock here - the app loading is
    # in many cases initiated implicitly by importing, and thus it is
    # possible to end up in deadlock when one thread initiates loading
    # without holding the importer lock and another thread then tries to
    # import something which also launches the app loading. For details of
    # this situation see Django bug #18251.
    imp.acquire_lock()
    try:
        for app_label, model_dict in cache.app_models.items():
            for django_name, klass in model_dict.items():
                name = '{}.{}'.format(klass.__module__, klass.__name__)

                if name in models:
                    module = sys.modules[klass.__module__]
                    delattr(module, klass.__name__)
                    del model_dict[django_name]
                    deleted.append(name)

        if sorted(deleted) != sorted(models):
            expected = ', '.join(models)
            expected = expected if expected else '(none)'
            actual = ', '.join(deleted)
            actual = actual if actual else '(none)'
            raise AssertionError(
                'Expected to delete {}, actually deleted {}'.format(
                    expected,
                    actual))

        # Reset a load of state variables in the app cache
        cache.loaded = False
        cache._get_models_cache = {}
        cache.handled = {}
        cache.postponed = []
        cache.nesting_level = 0
        cache._populate()
    finally:
        imp.release_lock()
Пример #14
0
def get_class_by_model_or_name(cls):
    collection_class = None
    if isinstance(cls, basestring):
        app_label, model_name = cls.split('.')
        try:
            collection_class = get_model(app_label, model_name)
        except ImportError:
            # This is a Django internal thing. If all the models are not yet loaded,
            # you can't get one out of the cache so it will try to do an import and then
            # you get circular dependencies. This just prepopulates the cache with all the models
            from django.db.models.loading import cache as app_cache
            app_cache._populate()
            # If it fails here you actually didn't specify a valid model class
            # remember it's "users.User" not just "User"
            collection_class = get_model(app_label, model_name)

    else:
        collection_class = cls
    return collection_class
Пример #15
0
def _cleanup(*models):
    """ Function to delete models from the AppCache and remove them from
    the models module in which they're defined.
    """
    from django.db.models.loading import cache
    deleted = []

    # Note that we want to use the import lock here - the app loading is
    # in many cases initiated implicitly by importing, and thus it is
    # possible to end up in deadlock when one thread initiates loading
    # without holding the importer lock and another thread then tries to
    # import something which also launches the app loading. For details of
    # this situation see Django bug #18251.
    imp.acquire_lock()
    try:
        for app_label, model_dict in cache.app_models.items():
            for django_name, klass in model_dict.items():
                name = '{}.{}'.format(klass.__module__, klass.__name__)

                if name in models:
                    module = sys.modules[klass.__module__]
                    delattr(module, klass.__name__)
                    del model_dict[django_name]
                    deleted.append(name)

        if sorted(deleted) != sorted(models):
            expected = ', '.join(models)
            expected = expected if expected else '(none)'
            actual = ', '.join(deleted)
            actual = actual if actual else '(none)'
            raise AssertionError(
                'Expected to delete {}, actually deleted {}'.format(
                    expected, actual))

        # Reset a load of state variables in the app cache
        cache.loaded = False
        cache._get_models_cache = {}
        cache.handled = {}
        cache.postponed = []
        cache.nesting_level = 0
        cache._populate()
    finally:
        imp.release_lock()
Пример #16
0
def get_class_by_model_or_name(cls):
    collection_class = None
    if isinstance(cls, basestring):
        app_label, model_name = cls.split(".")
        try:
            collection_class = get_model(app_label, model_name)
        except ImportError:
            # This is a Django internal thing. If all the models are not yet loaded,
            # you can't get one out of the cache so it will try to do an import and then
            # you get circular dependencies. This just prepopulates the cache with all the models
            from django.db.models.loading import cache as app_cache

            app_cache._populate()
            # If it fails here you actually didn't specify a valid model class
            # remember it's "users.User" not just "User"
            collection_class = get_model(app_label, model_name)

    else:
        collection_class = cls
    return collection_class
Пример #17
0
def setup_django():
    from django.conf import settings
    if not settings.configured:
        settings.configure(
            DEBUG=True,
            DATABASES={
                'default': {
                    'ENGINE': 'django.db.backends.sqlite3',
                    'NAME': ':memory:',
                }
            },
            INSTALLED_APPS=(
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django_ftpserver',
            )
        )
    from django.db.models.loading import cache as model_cache
    if not model_cache.loaded:
        model_cache._populate()
Пример #18
0
def reload_models_cache():
    """
    Reload Django internal cache for all models in installed apps.
    This includes model field mapping, related fields in _meta object, etc.
    """
    installed_app_names = [app.split('.')[-1] for app in settings.INSTALLED_APPS]
    for app_label in cache.app_models:
        if app_label not in installed_app_names:
            del cache.app_models[app_label]
    cache.loaded = False
    cache.handled = {}
    cache.app_store.clear()
    cache._populate()
    cache._get_models_cache.clear()
    for model in get_models():
        opts = model._meta
        if hasattr(opts, '_related_many_to_many_cache'):
            # we remove related m2m and fk fields cache for all models
            del opts._related_many_to_many_cache
            del opts._related_objects_cache
        opts.init_name_map()
Пример #19
0
    def begin(self):
        from django.conf import settings
        from django.core.handlers.wsgi import WSGIHandler
        from django.db.models.loading import cache

        from django.test.simple import TEST_MODULE

        from tddspry.django.settings import IP, PORT

        from twill import add_wsgi_intercept

        # swappable user will break unless AppCache is prepopulated, WTF
        cache._populate()

        log.debug('DjangoPlugin start')

        # Find to Django models in tests modules for each of ``INSTALLED_APPS``
        for label in settings.INSTALLED_APPS:
            tests = label + '.' + TEST_MODULE

            try:
                self.load_tests(tests)
            except (AttributeError, ImportError):
                pass

        # Setup Django test environment and test database
        self.setup_django()

        # Setup Twill for testing with Django
        try:
            from django.contrib.staticfiles.handlers import StaticFilesHandler
            if 'django.contrib.staticfiles' in settings.INSTALLED_APPS:
                app = StaticFilesHandler(WSGIHandler())
            else:
                app = WSGIHandler()
        except ImportError:  # django < 1.5
            from django.core.servers.basehttp import AdminMediaHandler
            app = AdminMediaHandler(WSGIHandler())
        add_wsgi_intercept(IP, PORT, lambda: app)
Пример #20
0
def reload_models_cache():
    """
    Reload Django internal cache for all models in installed apps.
    This includes model field mapping, related fields in _meta object, etc.
    """
    installed_app_names = [
        app.split('.')[-1] for app in settings.INSTALLED_APPS
    ]
    for app_label in cache.app_models:
        if app_label not in installed_app_names:
            del cache.app_models[app_label]
    cache.loaded = False
    cache.handled = {}
    cache.app_store.clear()
    cache._populate()
    cache._get_models_cache.clear()
    for model in get_models():
        opts = model._meta
        if hasattr(opts, '_related_many_to_many_cache'):
            # we remove related m2m and fk fields cache for all models
            del opts._related_many_to_many_cache
            del opts._related_objects_cache
        opts.init_name_map()
Пример #21
0
#!/usr/bin/env python
# django environment setup

import sys, os
sys.path.append(
    os.path.realpath(os.sep.join([os.path.dirname(__file__), os.pardir])))

from django.core.management import setup_environ
import settings
setup_environ(settings)

# Have to do this cache._populate bit or get_model calls will try to do an import
# and cause circular import hell
from django.db.models.loading import cache
cache._populate()

import codecs
from django.core import serializers
from django.contrib.auth.models import User as AuthUser

from issue.models import Issue
from users.models import User
from org.models import Org
from users.models import Location
from donation.models import *
from message.models import Publication, Subscription, Message

donations = list(Donation.objects.all()[:5])

payments = Payment.objects.filter(donation__in=donations)
Пример #22
0
 def repopulate_app_cache(self):
     """
     Rebuilds AppCache with the real model definitions.
     """
     cache._populate()
Пример #23
0
#!/usr/bin/env python

from django.core.management import setup_environ
import settings
setup_environ(settings)

from django.db.models.loading import cache as model_cache
model_cache._populate()

import facebook

from django.db import connection
from django.db.models import Q

import settings
import urllib2
import urlparse

from fabric.api import local
from org.models import Org
from miner.web.crawler import WebCrawler
from miner.jobs.org_social_media import OrgSocialMediaMapper, reducer


class FacebookMapper(OrgSocialMediaMapper):
    last_updated_column = 'facebook_last_fetched'

    def get_api(self):
        self.api = facebook.GraphAPI(settings.FACEBOOK_ACCESS_TOKEN)

    def has_links(self, post):
Пример #24
0
#!/usr/bin/env python2.6

import tempfile
import subprocess
import string
import sys
import time
import os

import settings # Assumed to be in the same directory.
from django.core.management import setup_environ
setup_environ(settings)

from django.db.models.loading import cache
cache._populate()

EDITOR = 'vim'

def pick(lst, name=None):
    dct = {}
    for i,item in lst:
        dct[str(i)] = item
        print '%s) %s' % (i, item)
    while True:
        inp = raw_input('pick one: ')
        if inp in dct:
            return dct[inp]
        elif not inp:
            return None

def pick_app():
Пример #25
0
from django import VERSION as DJANGO_VERSION

from parliament.models.committee import *
from parliament.models.party import *
from parliament.models.session import *
from parliament.models.document import Keyword
from parliament.models.base import UpdatableModel

import datetime

# Django bug workaround (only in version < 1.7)
if DJANGO_VERSION[0:2] < (1, 7):
    from django.db.models.loading import cache as model_cache

    if not model_cache.loaded:
        model_cache._populate()

# Helper to filter Association objects
class AssociationQuerySet(models.query.QuerySet):
    def current(self):
        return self.filter(end__isnull=True)

class AssociationManager(models.Manager):
    def current(self):
        return self.get_query_set().current()
    def get_query_set(self):
        return AssociationQuerySet(self.model, using=self._db)


class MemberManager(models.Manager):
    def current(self):