[cig-commits] r7816 - cs/pythia/trunk/opal/components

leif at geodynamics.org leif at geodynamics.org
Tue Aug 14 14:57:01 PDT 2007


Author: leif
Date: 2007-08-14 14:57:01 -0700 (Tue, 14 Aug 2007)
New Revision: 7816

Added:
   cs/pythia/trunk/opal/components/Editor.py
   cs/pythia/trunk/opal/components/ListEditor.py
   cs/pythia/trunk/opal/components/ObjectEditor.py
Modified:
   cs/pythia/trunk/opal/components/WebComponent.py
Log:
Unearthed some Django-related code I wrote last year, but never
checked-in.


Added: cs/pythia/trunk/opal/components/Editor.py
===================================================================
--- cs/pythia/trunk/opal/components/Editor.py	2007-08-14 20:18:43 UTC (rev 7815)
+++ cs/pythia/trunk/opal/components/Editor.py	2007-08-14 21:57:01 UTC (rev 7816)
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+#                      California Institute of Technology
+#                        (C) 2007  All Rights Reserved
+#
+# {LicenseText}
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+
+
+from WebComponent import UnearthedComponent
+
+
+class Action(object):
+    def __init__(self, slug, name):
+        self.slug = slug
+        self.name = name
+
+
+class Editor(UnearthedComponent):
+
+    def __init__(self, **attrs):
+        super(Editor, self).__init__(**attrs)
+        self.Model = attrs['Model']
+        self.manager = self.Model.objects
+        self.context['actions'] = self.getActions()
+
+
+    def querySet(self):
+        return self.manager.all()
+
+
+    def createSubcomponent(self, slug, **attrs):
+        attrs['slug'] = slug
+        attrs['url'] = self.url
+        attrs['Model'] = self.Model
+        model = attrs['model']
+        attrs['title'] = model.name
+        c = self.SubEditor(**attrs)
+        return c
+
+
+    def subcomponent(self, slug, **attrs):
+        from django.http import Http404
+        try:
+            model = self.manager.get(slug = slug)
+        except self.Model.DoesNotExist:
+            raise Http404
+        return self.createSubcomponent(slug, model=model, **attrs)
+
+
+    def expand(self, **attrs):
+        if self.subcomponents is None:
+            self.subcomponents = {}
+            for model in self.querySet():
+                slug = model.slug
+                self.subcomponents[slug] =  self.createSubcomponent(slug, model=model, **attrs)
+        return self.subcomponents
+
+
+    def getFormContext(self, request):
+        context = dict(self.context)
+        context['actionURL'] = request.get_full_path()
+        return context
+
+
+    def getActions(self):
+        return []
+
+
+    def unknownAction(self, action, request):
+        self.postMessage("Unknown action '%s'." % action, request)
+        return
+
+
+# end of file

Added: cs/pythia/trunk/opal/components/ListEditor.py
===================================================================
--- cs/pythia/trunk/opal/components/ListEditor.py	2007-08-14 20:18:43 UTC (rev 7815)
+++ cs/pythia/trunk/opal/components/ListEditor.py	2007-08-14 21:57:01 UTC (rev 7816)
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+#                      California Institute of Technology
+#                        (C) 2007  All Rights Reserved
+#
+# {LicenseText}
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+
+
+from Editor import Editor
+
+
+class ListEditor(Editor):
+
+
+    def __init__(self, **attrs):
+        super(ListEditor, self).__init__(**attrs)
+        self.allowEmpty = attrs.get('allowEmpty', True)
+        self.SubEditor = attrs.get('SubEditor', ObjectEditor)
+
+
+    def getView(self, request):
+        from django.views.generic.create_update import create_object
+        action = request.GET.get('action')
+        if action is None:
+            response = self.getListView(request)
+        elif action == 'new':
+            response = create_object(request, self.Model,
+                                     extra_context = self.getFormContext(request),
+                                     post_save_redirect = self.parentURL,
+                                     )
+        else:
+            self.unknownAction(action, request)
+            response = self.getListView(request)
+        return response
+
+
+    def getListView(self, request):
+        from django.views.generic.list_detail import object_list
+        return object_list(request, self.querySet(),
+                           allow_empty = self.allowEmpty,
+                           extra_context = self.context,
+                           )
+
+    def getActions(self):
+        return [Action('new', "New")]
+
+
+# end of file

Added: cs/pythia/trunk/opal/components/ObjectEditor.py
===================================================================
--- cs/pythia/trunk/opal/components/ObjectEditor.py	2007-08-14 20:18:43 UTC (rev 7815)
+++ cs/pythia/trunk/opal/components/ObjectEditor.py	2007-08-14 21:57:01 UTC (rev 7816)
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+#                      California Institute of Technology
+#                        (C) 2007  All Rights Reserved
+#
+# {LicenseText}
+#
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+
+
+from Editor import Editor
+
+
+class ObjectEditor(Editor):
+
+
+    def __init__(self, **attrs):
+        super(ObjectEditor, self).__init__(**attrs)
+        self.model = attrs['model']
+
+
+    def getView(self, request):
+        from django.views.generic.create_update import delete_object, update_object
+        id = self.model.id
+        action = request.GET.get('action')
+        if action is None:
+            response = self.getDetailView(request)
+        elif action == 'edit':
+            response = update_object(request, self.Model,
+                                     object_id = id,
+                                     extra_context = self.getFormContext(request),
+                                     )
+        elif action == 'delete':
+            response = delete_object(request, self.Model, self.parentURL,
+                                     object_id = id,
+                                     extra_context = self.getFormContext(request),
+                                     )
+        else:
+            self.unknownAction(action, request)
+            response = self.getDetailView(request)
+        return response
+
+
+    def getDetailView(self, request):
+        from django.views.generic.list_detail import object_detail
+        return object_detail(request, self.querySet(),
+                             object_id = self.model.id,
+                             extra_context = self.context,
+                             )
+
+
+    def getActions(self):
+        return [Action('edit', "Edit"), Action('delete', "Delete")]
+
+
+# end of file

Modified: cs/pythia/trunk/opal/components/WebComponent.py
===================================================================
--- cs/pythia/trunk/opal/components/WebComponent.py	2007-08-14 20:18:43 UTC (rev 7815)
+++ cs/pythia/trunk/opal/components/WebComponent.py	2007-08-14 21:57:01 UTC (rev 7816)
@@ -123,4 +123,135 @@
         return http.HttpResponseServerError(t.render(Context({})))
 
 
+#------------------------------------------------------------------------
+# unearthed stuff
+
+
+class Property(object):
+    def __init__(self, **attrs):
+        self.attrs = attrs
+
+
+component = Property
+
+
+class ComponentClass(type):
+
+
+    def __init__(Class, name, bases, dct):
+        type.__init__(name, bases, dct)
+
+        subcomponentRegistry = {}
+
+        for name, prop in [kv for kv in dct.iteritems()
+                           if isinstance(kv[1], Property)]:
+            slug = prop.attrs.setdefault('slug', name)
+            subcomponentRegistry[slug] = prop
+
+        Class.subcomponentRegistry = subcomponentRegistry
+
+        return
+
+
+class UnearthedComponent(object):
+
+
+    __metaclass__ = ComponentClass
+
+
+    from cig.web.views import View
+
+
+    def __init__(self, **attrs):
+        #super(object, self).__init__()
+        self.slug = attrs.get('slug')
+        self.context = attrs.get('context', {})
+        self.parentURL = attrs.get('url')
+        self.subcomponents = None
+        self._initTreeNode(**attrs)
+
+
+    def createSubcomponent(self, slug, **attrs):
+        prop = self.subcomponentRegistry[slug]
+        attrs.update(prop.attrs)
+        attrs['slug'] = slug
+        attrs['url'] = self.url
+        Class = attrs['Class']
+        c = Class(**attrs)
+        return c
+
+
+    def subcomponent(self, slug, **attrs):
+        from django.http import Http404
+        self.expand(**attrs)
+        try:
+            c = self.subcomponents[slug]
+        except KeyError:
+            raise Http404
+        return c
+
+
+    def iterSubcomponents(self):
+        self.expand()
+        for c in self.subcomponents.itervalues():
+            yield c
+        return
+
+
+    def expand(self, **attrs):
+        if self.subcomponents is None:
+            self.subcomponents = {}
+            for slug in self.subcomponentRegistry.iterkeys():
+                self.subcomponents[slug] =  self.createSubcomponent(slug, **attrs)
+        return self.subcomponents
+
+
+    def resolve(self, path, **attrs):
+        from django.http import Http404
+        context = attrs.setdefault('context', {})
+        context.setdefault('root', self)
+        item = path[0]
+        path = path[1:]
+        if item:
+            subcomponent = self.subcomponent(item, **attrs)
+            self.selectedChild = subcomponent
+            return subcomponent.resolve(path, **attrs)
+        if path:
+            raise Http404
+        self.isExpanded = True
+        args = ()
+        kwargs = {}
+        return self.View(component=self), args, kwargs
+
+
+    # TreeNode protocol
+
+    def _initTreeNode(self, **attrs):
+        # this node
+        self.name = attrs.get('name', self.slug)
+        self.title = attrs.get('title', self.name.title())
+        self.url = attrs['url'] + '/' + self.slug
+
+        # children
+        self.isLeaf = False
+        self.isExpanded = False
+        self.selectedChild = None
+
+
+    def _getChildren(self):
+        return [child for child in self.iterChildren()]
+    children = property(_getChildren)
+
+
+    def iterChildren(self):
+        for component in self.iterSubcomponents():
+            yield component
+        return
+
+
+    def postMessage(self, message, request):
+        request.user.message_set.create(message = message)
+        return
+
+
 # end of file



More information about the cig-commits mailing list