-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdjmicro.py
98 lines (79 loc) · 3.07 KB
/
djmicro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os, sys, types
_base_module = None
_app_name = None
def _extend_opts(initial, to_add):
if type(to_add) is dict:
initial.update(to_add)
elif type(to_add) is types.ModuleType:
module_opts = {k: v for k, v in to_add.__dict__.iteritems() if not k.startswith('__')}
initial.update(module_opts)
elif type(to_add) in (tuple, list):
for opt_set in to_add:
_extend_opts(initial, opt_set)
elif to_add is None:
pass
else:
raise ValueError("Options must be a dict, module, list, tuple, or None.")
def configure(options={}, module=None, app_name=None):
if not module:
# hack to figure out where we were called from
import sys, inspect
module = sys.modules[inspect.stack()[1][0].f_locals['__name__']]
# djmicro makes an implicit app that you can install models into, add modules from, etc.
if not app_name:
app_name = "djmicro"
global _app_name
_app_name = app_name
if app_name != __name__:
sys.modules[app_name] = sys.modules[__name__]
# settings
from django.conf import settings
if not settings.configured:
opts = dict(
DEBUG = True,
ROOT_URLCONF = module.__name__,
TEMPLATE_DIRS = [os.path.dirname(module.__file__)],
INSTALLED_APPS = [],
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware',)
)
_extend_opts(opts, options)
if 'djmicro' not in opts['INSTALLED_APPS']:
opts['INSTALLED_APPS'] += (app_name,)
settings.configure(**opts)
# urls
from django.conf.urls import patterns
module.urlpatterns = patterns('')
# wsgi application
from django.core.wsgi import get_wsgi_application
module.application = get_wsgi_application()
global _base_module
_base_module = module
def route(*args, **kwargs):
def add_route(view):
# if it's a class-based view, take .as_view() of it
from django.views.generic import View
target = view.as_view() if isinstance(view, types.TypeType) and issubclass(view, View) else view
from django.conf.urls import patterns, url
_base_module.urlpatterns += patterns('',
url(args[0], target, *args[1:], **kwargs)
)
return view
return add_route
def add_module_to_app(module, name=None):
if name is None:
name = module.__name__
# make it available as a module
sys.modules["%s.%s" % (_app_name, name)] = module
if _app_name != __name__:
sys.modules["%s.%s" % (__name__, name)] = module
# make it available with a from import
globals()[name] = module
# if it's a models module, special-case handle it because model detection has already occurred
if name == "models":
from django.apps import apps
new_config = apps.get_app_config(_app_name)
new_config.import_models(apps.all_models[_app_name])
apps.app_configs[_app_name] = new_config
def run():
from django.core.management import execute_from_command_line
execute_from_command_line()