New app: DBURLs. Now admin can adjust URL mappings via Django admin pages, and developers could add modules, intercepting specific URLs.

portnov [2008-06-18 07:26:05]
New app: DBURLs. Now admin can adjust URL mappings via Django admin pages, and developers could add modules, intercepting specific URLs.
Filename
dburls/__init__.py
dburls/models.py
dburls/views.py
hms/hooks.py
hms/modules/about.py
hms/modules/test1.py
hms/modules/test2.py
hms/modules/testblock.py
mgmt/views.py
settings.py
urls.py
diff --git a/dburls/__init__.py b/dburls/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/dburls/models.py b/dburls/models.py
new file mode 100644
index 0000000..94daed3
--- /dev/null
+++ b/dburls/models.py
@@ -0,0 +1,19 @@
+from django.db import models
+from django.utils.translation import gettext_lazy as _
+
+class URLMap(models.Model):
+  source = models.CharField(_("Source"),max_length=128)
+  destination = models.CharField(_("Destination"),max_length=128)
+  weight = models.SmallIntegerField(_("Weight"),null=False,default=0)
+
+  def __unicode__(self):
+    return self.source + " => " + self.destination
+
+  class Meta:
+    ordering = ("weight",)
+    verbose_name = _("URL mapping")
+    verbose_name_plural = _("URL mappings")
+
+  class Admin:
+    pass
+
diff --git a/dburls/views.py b/dburls/views.py
new file mode 100644
index 0000000..887eb9d
--- /dev/null
+++ b/dburls/views.py
@@ -0,0 +1,29 @@
+import re
+from django.http import HttpRequest,HttpResponse,HttpResponseRedirect,Http404
+from models import URLMap
+from hms import hms
+hms.modules.init()
+
+def generic(request):
+  maps = URLMap.objects.all()
+  path = request.path
+  for map in maps:
+    cre = re.compile(map.source,re.L|re.U)
+    if cre.match(path):
+      url = cre.sub(map.destination,path)
+      return HttpResponseRedirect(url)
+  return strange(request)
+
+@hms.hook
+def map_url(request):
+  if isinstance(request,HttpResponse):
+    return request
+  elif isinstance(request,HttpRequest):
+    raise Http404
+
+def strange(request):
+  ret = hms.call_hooks('map_url',request)
+  if isinstance(ret,HttpResponse):
+    return ret
+  else:
+    raise Http404
diff --git a/hms/hooks.py b/hms/hooks.py
index f991423..d4bd142 100755
--- a/hms/hooks.py
+++ b/hms/hooks.py
@@ -36,6 +36,7 @@ def hook(func):
     else:
       return func(*r)
   wrapper.__name__ = name
+  wrapper.__doc__ = func.__doc__
   return wrapper


diff --git a/hms/modules/about.py b/hms/modules/about.py
new file mode 100644
index 0000000..cd9cfda
--- /dev/null
+++ b/hms/modules/about.py
@@ -0,0 +1,14 @@
+
+weight = 1
+
+def init():
+
+  from django.http import HttpResponse
+
+  @intercept('map_url')
+  def about_page(request):
+    if isinstance(request,HttpResponse):
+      return request
+    if request.path == '/about/':
+      return HttpResponse("<h1>This page is about Projects system.</h1>")
+    return HttpResponse("<h1>This is %s page</h1>" % request.path)
diff --git a/hms/modules/test1.py b/hms/modules/test1.py
deleted file mode 100644
index caecc35..0000000
--- a/hms/modules/test1.py
+++ /dev/null
@@ -1,14 +0,0 @@
-
-def init():
-
-  @intercept('do_something')
-  def func(s):
-    print "Intercepted."
-    return s
-
-  @hook
-  def my_hook():
-    print "Some event."
-
-
-  my_hook()
diff --git a/hms/modules/test2.py b/hms/modules/test2.py
deleted file mode 100644
index f5d3524..0000000
--- a/hms/modules/test2.py
+++ /dev/null
@@ -1,7 +0,0 @@
-
-def init():
-  print "Test 2 loaded"
-
-  @intercept('my_hook')
-  def on_hook():
-    print "<myhook> intercepted."
diff --git a/hms/modules/testblock.py b/hms/modules/testblock.py
deleted file mode 100644
index be49e4f..0000000
--- a/hms/modules/testblock.py
+++ /dev/null
@@ -1,10 +0,0 @@
-
-def init():
-
-  @block
-  def testblk():
-    b = Block()
-    b.name = "testblk"
-    b.title = "Test block"
-    b.content = "Content of test block."
-    return b
diff --git a/mgmt/views.py b/mgmt/views.py
index 5687a42..8a78b7c 100644
--- a/mgmt/views.py
+++ b/mgmt/views.py
@@ -3,13 +3,13 @@

 from datetime import datetime

-from django.http import HttpResponse,Http404,HttpResponseForbidden,HttpResponseRedirect
+from django.http import HttpRequest,HttpResponse,Http404,HttpResponseForbidden,HttpResponseRedirect
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse

 from django.conf import settings
-from django.utils.translation import activate
-activate(settings.LANGUAGE_CODE)
+# from django.utils.translation import activate
+# activate(settings.LANGUAGE_CODE)

 import debug
 from models import *
@@ -116,5 +116,3 @@ def message_preview(request):
     return {'text': text}
   raise Http404

-def strange(request):
-  raise Http404
diff --git a/settings.py b/settings.py
index b0f4d59..a6a02d3 100644
--- a/settings.py
+++ b/settings.py
@@ -88,6 +88,7 @@ INSTALLED_APPS = (
     'django.contrib.sites',
     'mgmt',
     'kb',
+    'dburls',
 )

 SHOW_BLOCKS = dict(
diff --git a/urls.py b/urls.py
index 7b538dd..2a9ae84 100644
--- a/urls.py
+++ b/urls.py
@@ -43,5 +43,5 @@ urlpatterns = patterns('',

     (r'^admin/', include('django.contrib.admin.urls')),

-    (r'^.*/$', 'mgmt.views.strange'),
+    (r'^.*/$', 'dburls.views.generic'),
 )
ViewGit