Permalink
Browse files

more work on cms

  • Loading branch information...
1 parent 38ed8a7 commit ddbf783971476e910ba8e13910b2d4774d3b3d56 @zbyte64 committed Nov 25, 2011
View
@@ -6,4 +6,4 @@ test_environment/src
test_environment/include
test_environment/dockit
test_environment/fieldmaker
-test_envoronment/sqlite.db
+test_environment/sqlite.db
@@ -0,0 +1 @@
+import view_points
View
@@ -1,5 +1,10 @@
import dockit
+
+import re
+
from django import forms
+from django.conf.urls.defaults import patterns
+from django.core.urlresolvers import RegexURLResolver
FORM_FIELD_TO_DOCKIT_FIELD = [
(forms.BooleanField, dockit.BooleanField),
@@ -17,21 +22,52 @@
(forms.TimeField, dockit.TimeField),
]
+REGISTERED_VIEW_POINTS = dict()
+
+def register_view_point_class(key, cls):
+ REGISTERED_VIEW_POINTS[key] = cls()
+
def dockit_field_for_form_field(form_field):
df_kwargs = {'blank': not form_field.required,
'help_text': form_field.help_text,}
for ff, df in FORM_FIELD_TO_DOCKIT_FIELD:
if isinstance(form_field, ff):
return df(**df_kwargs)
-class ViewPoint(object):
+class CMSURLResolver(RegexURLResolver):
+ def __init__(self, regex, url_patterns, default_kwargs=None, app_name=None, namespace=None):
+ # regex is a string representing a regular expression.
+ # urlconf_name is a string representing the module containing URLconfs.
+ self.regex = re.compile(regex, re.UNICODE)
+ self._url_patterns = url_patterns
+ self.callback = None
+ self.default_kwargs = default_kwargs or {}
+ self.namespace = namespace
+ self.app_name = app_name
+ self._reverse_dict = None
+ self._namespace_dict = None
+ self._app_dict = None
+
+ def _get_url_patterns(self):
+ return self._url_patterns
+ url_patterns = property(_get_url_patterns)
+
+ def __repr__(self):
+ return '<%s (%s:%s) %s>' % (self.__class__.__name__, self.app_name, self.namespace, self.regex.pattern)
+
+class BaseViewPointClass(object):
form_class = None
- view_class = None
def register_view_point(self, view_point_doc):
pass
#here it would ensure all neceassry indexes are created
- def dispatch(self, request):
- return self.view_class(request)
+ def get_urls(self, view_point_doc):
+ return patterns('')
+
+ def dispatch(self, request, view_point_doc):
+ urls = self.get_urls(view_point_doc)
+ resolver = CMSURLResolver(r'^'+view_point_doc.url, urls)
+ view_match = resolver.resolve(request.path)
+ return view_match.func(request, *view_match.args, **view_match.kwargs)
@@ -0,0 +1,44 @@
+from django.http import Http404
+from django.conf import settings
+from django.core.urlresolvers import get_script_prefix
+from django.template.response import TemplateResponse
+
+from models import ViewPoint
+
+class DockitCMSMiddleware(object):
+ ignore_script_prefix = False
+
+ def process_response(self, request, response):
+ if response.status_code != 404:
+ return response # No need to check for a flashpage for non-404 responses.
+ url = request.path_info
+ chomped_url = url
+ if self.ignore_script_prefix:
+ prefix = None
+ else:
+ prefix = get_script_prefix()
+
+ if prefix and chomped_url.startswith(prefix):
+ chomped_url = chomped_url[len(prefix)-1:]
+ try:
+ #TODO find a more efficient way to do this
+ for view_point in ViewPoint.objects.all():
+ if chomped_url.startswith(view_point.url):
+ try:
+ response = view_point.dispatch(request)
+ except Http404:
+ pass
+ else:
+ break
+ if isinstance(response, TemplateResponse):
+ response.render()
+ return response
+ # Return the original response if any errors happened. Because this
+ # is a middleware, we can't assume the errors will be caught elsewhere.
+ except Http404:
+ return response
+ except:
+ if settings.DEBUG:
+ raise
+ return response
+
View
@@ -1,8 +1,9 @@
import dockit
-from dockit.backends import get_document_backend
-from dockit.schema import create_document
+from dockit.schema import create_document, get_schema
from fieldmaker.resource import field_registry
+from common import REGISTERED_VIEW_POINTS
+
class SchemaDefinition(dockit.Document):
title = dockit.CharField()
data = dockit.ListField()
@@ -35,15 +36,21 @@ def save(self, *args, **kwargs):
def register_collection(self):
from common import dockit_field_for_form_field
- backend = get_document_backend()
name = str(self.title) #TODO make sure it is a safe name
form_fields = self.schema_definition.get_schema_fields()
fields = dict()
for key, form_field in form_fields.iteritems():
field = dockit_field_for_form_field(form_field)
fields[key] = field
document = create_document(name, fields, module='dockitcms.models')
- backend.register_document(document)
+ return document
+
+ def get_document(self):
+ key = 'dockitcms.%s' % self.title.lower()
+ try:
+ return get_schema(key)
+ except KeyError:
+ return self.register_collection()
def __unicode__(self):
if self.title:
@@ -52,10 +59,14 @@ def __unicode__(self):
return self.__repr__()
class ViewPoint(dockit.Document):
- url = dockit.CharField() #TODO middleware to hand this off
+ url = dockit.CharField()
collection = dockit.ReferenceField(Collection)
view_class = dockit.TextField()
- view_config = dockit.DictField()
+ view_config = dockit.DictField(blank=True)
+
+ def dispatch(self, request):
+ view_instance = REGISTERED_VIEW_POINTS[self.view_class]
+ return view_instance.dispatch(request, self)
def __unicode__(self):
if self.url:
@@ -0,0 +1,29 @@
+from common import BaseViewPointClass, register_view_point_class
+
+from dockit.views import ListView, DetailView
+
+from django.conf.urls.defaults import patterns
+from django import forms
+
+class ListViewPointForm(forms.Form):
+ list_template_name = forms.CharField()
+ detail_template_name = forms.CharField()
+ paginate_by = forms.IntegerField(required=False)
+
+class ListViewPointClass(BaseViewPointClass):
+ form_class = ListViewPointForm
+
+ def get_urls(self, view_point_doc):
+ document = view_point_doc.collection.get_document()
+ params = view_point_doc.view_config
+ return patterns('',
+ (r'$', ListView.as_view(model=document,
+ template_name=params['list_template_name'],
+ paginate_by=params.get('paginate_by', None)),
+ ),
+ (r'^(?P<pk>.+)/$', DetailView.as_view(model=document,
+ template_name=params['detail_template_name']),
+ ),
+ )
+
+register_view_point_class('list', ListViewPointClass)
@@ -100,6 +100,7 @@
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
+ 'dockitcms.middleware.DockitCMSMiddleware',
)
ROOT_URLCONF = 'urls'
Binary file not shown.

0 comments on commit ddbf783

Please sign in to comment.