From 748acb8f89915e5be49287562be381ad08b9c194 Mon Sep 17 00:00:00 2001 From: Israel Fruchter Date: Thu, 8 Mar 2012 18:34:06 +0200 Subject: [PATCH 1/2] - adding star import support - first try on using django ORM --- djmicro.py | 12 ++++++++- example/db_index.html | 19 ++++++++++++++ example/djmicro.py | 52 ++++++++++++++++++++++++++++++++++++- example/web.py | 6 ++--- example/web_db.py | 53 ++++++++++++++++++++++++++++++++++++++ example/web_star_import.py | 10 +++++++ 6 files changed, 146 insertions(+), 6 deletions(-) create mode 100644 example/db_index.html mode change 120000 => 100644 example/djmicro.py create mode 100644 example/web_db.py create mode 100644 example/web_star_import.py diff --git a/djmicro.py b/djmicro.py index 9db7907..d779cea 100644 --- a/djmicro.py +++ b/djmicro.py @@ -1,6 +1,7 @@ import os _base_module = None +__all__ = ["configure", "route", "run", "render", "redirect"] def configure(options={}, module=None): if not module: @@ -38,4 +39,13 @@ def add_route(view): def run(): from django.core.management import execute_from_command_line - execute_from_command_line() \ No newline at end of file + execute_from_command_line() + +def render(*args, **kwargs): + from django.shortcuts import render as orig_render + return orig_render(*args, **kwargs) + +def redirect(*args, **kwargs): + from django.shortcuts import redirect as orig_redirect + return orig_redirect(*args, **kwargs) + diff --git a/example/db_index.html b/example/db_index.html new file mode 100644 index 0000000..1ff844b --- /dev/null +++ b/example/db_index.html @@ -0,0 +1,19 @@ + + + + djmicro db example + + + +
{% csrf_token %} + {{ form.as_p }} + +
    + {% for task in tasks %} +
  1. {{ task.title|escape }}
  2. + {% endfor %} +
+
+ + \ No newline at end of file diff --git a/example/djmicro.py b/example/djmicro.py deleted file mode 120000 index 40084bb..0000000 --- a/example/djmicro.py +++ /dev/null @@ -1 +0,0 @@ -../djmicro.py \ No newline at end of file diff --git a/example/djmicro.py b/example/djmicro.py new file mode 100644 index 0000000..d779cea --- /dev/null +++ b/example/djmicro.py @@ -0,0 +1,51 @@ +import os + +_base_module = None +__all__ = ["configure", "route", "run", "render", "redirect"] + +def configure(options={}, module=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__']] + + # 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 = [] + ) + opts.update(options) + settings.configure(**opts) + + # urls + from django.conf.urls.defaults import patterns + module.urlpatterns = patterns('') + + global _base_module + _base_module = module + +def route(*args, **kwargs): + def add_route(view): + from django.conf.urls.defaults import patterns, url + _base_module.urlpatterns += patterns('', + url(args[0], view, *args[1:], **kwargs) + ) + return view + return add_route + +def run(): + from django.core.management import execute_from_command_line + execute_from_command_line() + +def render(*args, **kwargs): + from django.shortcuts import render as orig_render + return orig_render(*args, **kwargs) + +def redirect(*args, **kwargs): + from django.shortcuts import redirect as orig_redirect + return orig_redirect(*args, **kwargs) + diff --git a/example/web.py b/example/web.py index 54a43a1..25e351b 100644 --- a/example/web.py +++ b/example/web.py @@ -1,15 +1,13 @@ import djmicro djmicro.configure() -from django.shortcuts import render - @djmicro.route(r'^$') def hello(request): - return render(request, 'index.html', {}) + return djmicro.render(request, 'index.html', {}) @djmicro.route(r'^test/(\d+)/$') def test(request, id): - return render(request, 'test.html', {'id': id}) + return djmicro.render(request, 'test.html', {'id': id}) if __name__ == '__main__': djmicro.run() \ No newline at end of file diff --git a/example/web_db.py b/example/web_db.py new file mode 100644 index 0000000..dfdce44 --- /dev/null +++ b/example/web_db.py @@ -0,0 +1,53 @@ +''' demoing db usage on djmicro ''' +from djmicro import * +import os.path +configure(dict( + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join( os.path.dirname(__file__), 'djmicro.db3') + } + }, + + INSTALLED_APPS= ('django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.sites', + 'django.contrib.messages', + 'web_db') + +)) + +from django.db import models +from django.forms import ModelForm +import sys +# TODO: find a way to hide this in djmicro +# and actually make this work.. +# currently the DB for Task isn't been created +sys.modules['web_db.web'] = sys.modules[__name__] +sys.modules[__name__].__name__ = 'web_db.web' + +class Task(models.Model): + title = models.CharField(max_length=255) + +class TaskForm(ModelForm): + class Meta: + model = Task + +@route(r'^$') +def index(request): + context = dict( + form = TaskForm(), + tasks = Task.objects.all() + ) + return render(request, 'db_index.html', context) + +@route(r'^add$') +def add(request): + if request.method == 'POST': + f = TaskForm(request.POST) + f.save() + return redirect("/") + +if __name__ == 'web_db.web': + run() \ No newline at end of file diff --git a/example/web_star_import.py b/example/web_star_import.py new file mode 100644 index 0000000..3ef0307 --- /dev/null +++ b/example/web_star_import.py @@ -0,0 +1,10 @@ +''' demoing star import on djmicro ''' +from djmicro import * +configure() + +@route(r'^$') +def hello(request): + return render(request, 'index.html', {}) + +if __name__ == '__main__': + run() \ No newline at end of file From 035506475898ed65157a2e5031b57728db2e44f5 Mon Sep 17 00:00:00 2001 From: Israel Fruchter Date: Thu, 8 Mar 2012 20:04:43 +0200 Subject: [PATCH 2/2] now a working example with the ORM --- example/web_db.py | 53 --------------------------- {example => example_db}/db_index.html | 0 example_db/tasks/__init__.py | 0 example_db/tasks/models.py | 9 +++++ example_db/web_db.py | 34 +++++++++++++++++ 5 files changed, 43 insertions(+), 53 deletions(-) delete mode 100644 example/web_db.py rename {example => example_db}/db_index.html (100%) create mode 100644 example_db/tasks/__init__.py create mode 100644 example_db/tasks/models.py create mode 100644 example_db/web_db.py diff --git a/example/web_db.py b/example/web_db.py deleted file mode 100644 index dfdce44..0000000 --- a/example/web_db.py +++ /dev/null @@ -1,53 +0,0 @@ -''' demoing db usage on djmicro ''' -from djmicro import * -import os.path -configure(dict( - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join( os.path.dirname(__file__), 'djmicro.db3') - } - }, - - INSTALLED_APPS= ('django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.sites', - 'django.contrib.messages', - 'web_db') - -)) - -from django.db import models -from django.forms import ModelForm -import sys -# TODO: find a way to hide this in djmicro -# and actually make this work.. -# currently the DB for Task isn't been created -sys.modules['web_db.web'] = sys.modules[__name__] -sys.modules[__name__].__name__ = 'web_db.web' - -class Task(models.Model): - title = models.CharField(max_length=255) - -class TaskForm(ModelForm): - class Meta: - model = Task - -@route(r'^$') -def index(request): - context = dict( - form = TaskForm(), - tasks = Task.objects.all() - ) - return render(request, 'db_index.html', context) - -@route(r'^add$') -def add(request): - if request.method == 'POST': - f = TaskForm(request.POST) - f.save() - return redirect("/") - -if __name__ == 'web_db.web': - run() \ No newline at end of file diff --git a/example/db_index.html b/example_db/db_index.html similarity index 100% rename from example/db_index.html rename to example_db/db_index.html diff --git a/example_db/tasks/__init__.py b/example_db/tasks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/example_db/tasks/models.py b/example_db/tasks/models.py new file mode 100644 index 0000000..5f91ecd --- /dev/null +++ b/example_db/tasks/models.py @@ -0,0 +1,9 @@ +from django.db import models +from django.forms import ModelForm + +class Task(models.Model): + title = models.CharField(max_length=255) + +class TaskForm(ModelForm): + class Meta: + model = Task diff --git a/example_db/web_db.py b/example_db/web_db.py new file mode 100644 index 0000000..420b1bc --- /dev/null +++ b/example_db/web_db.py @@ -0,0 +1,34 @@ +''' demoing db usage on djmicro ''' +from djmicro import * +import os.path +configure(dict( + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join( os.path.dirname(__file__), 'djmicro.db3') + } + }, + + INSTALLED_APPS= ('tasks',) + +)) + +from tasks.models import Task, TaskForm + +@route(r'^$') +def index(request): + context = dict( + form = TaskForm(), + tasks = Task.objects.all() + ) + return render(request, 'db_index.html', context) + +@route(r'^add/$') +def add(request): + if request.method == 'POST': + f = TaskForm(request.POST) + f.save() + return redirect("/") + +if __name__ == '__main__': + run() \ No newline at end of file