Commits

Anonymous committed 20c3611

fixes to support changing templates from a dropdown.

Comments (0)

Files changed (6)

c5t/core/controllers/page.py

 
 from tg.controllers import RestController
 from c5t.core.model.content import Page
+from c5t.core.model.session import main_orm_session
 
 from pymongo.objectid import ObjectId
 
     return render_mako(template, template_vars)
 
 class PageController(RestController):
-    
 
     default_custom_template_path = 'c5t.core.templates.page.custom'
     default_template_name        = 'c5t.core.templates.page.default'
     def get_all(self):
         # required_roles = ['admin', 'writer', 'editor']
         user = tmpl_context.user
-                
+
         # if user and user.role in required_roles + self.page.acl:
         d = dict(page=self.page, action='Add')
         return render(self.page.template, d)
         #         status, reason = 'error', u'Your account is not authorized to view this resource.'
         #     flash(reason, status='error')
         #     abort(response.status_int, reason)
-        
-        
+
+
 
     @require(HasAnyRole('admin', 'writer'))
     @expose('c5t.core.templates.page.new')
         # if the user has selected "boilerplate" then copy a blank template
         # into the custom templates location for future editing
         new_template_name = kw.get('template', self.default_template_name)
-        if new_template_name == 'custom':
+        if new_template_name == 'other_template_value':
+            new_template_name = kw.get('other_template_value', self.default_template_name)
+        elif new_template_name == 'custom':
             new_template_name  = '.'.join((self.default_custom_template_path, slug))
             boilerplate_template_filename = template_finder.get_dotted_filename(self.boilerplate_template_path, '.mak')
             new_template_filename = template_finder.get_dotted_filename(new_template_name, '.mak')
         parent_id = raw_parent_id and ObjectId(raw_parent_id) or None
 
         page = Page(slug=slug, title=kw['title'], template=new_template_name, text=kw['text'], parent_id=parent_id)
-        
+
+        main_orm_session.flush_all()
+        main_orm_session.close_all()
+
         flash('The page, "%s," has been created' % page.title)
         redirect('/%s' % page.path())
 
         page_id = kw.get('page_id')
         page = Page.query.get(_id=ObjectId(page_id))
         del kw['page_id']
+
+        #override the template with the "other template" selection
+        if kw['template'] == 'other_template_value':
+            kw['template'] = kw.get('other_template_value', self.default_template_name)
+
         # if user and user.role in required_roles + page.acl:
         for k,v in kw.iteritems():
             setattr(page, k, v)
+        main_orm_session.flush_all()
+        main_orm_session.close_all()
         redirect('/%s' % page.path())
         # else:
         #     if response.status_int == 401:
     def post_delete(self, **kw):
         required_roles = ['admin', 'writer']
         user = tmpl_context.user
-        
+
         page_id = kw.get('page_id')
         page = Page.query.get(_id=ObjectId(page_id))
         # if user and user.role in required_roles + page.acl:
     def publish(self, **kw):
         required_roles = ['admin', 'writer']
         user = tmpl_context.user
-        
+
         page_id = request.params.get('page_id')
         page = Page.query.get(_id=ObjectId(page_id))
         # if user and user.role in required_roles + page.acl:
     def edit(self):
         required_roles = ['admin', 'writer', 'editor']
         user = tmpl_context.user
-        
+
         parent_id = tmpl_context.parent_id
         page = Page.query.get(_id=parent_id)
         # if user and user.role in required_roles + page.acl:
     def get_all(self):
         # pages = Page.query.find()
         # return dict(page='pages', pages=pages)
-        
+
         required_roles = ['admin', 'editor']
         user = tmpl_context.user
-        
+
         pages = []
         if user:
             pages = [page for page in Page.query.find() if user.role in required_roles + page.acl]

c5t/core/controllers/root.py

         parent_page = None
         parent_id = None
         current_slug = None
-        
+
         while remainder:
             if len(remainder)==2:
                 next_slug, verb_slug = remainder
         #also true if there is not a page found, but we are instead in the process of creating one
         if request.method == 'POST' and not remainder:
             new_controller = self.defaultPageController(current_slug, current_page)
-            return self._dispatch_controller(current_slug, new_controller, state, [])
+            r = self._dispatch_controller(current_slug, new_controller, state, [])
+            return r
 
         if request.url.endswith('new'):
             abort(404, detail='Cannot add page to a page that does not exist.')

c5t/core/lib/helpers.py

 import os
 from tg import config
 from tg.util import odict
-def find_local_templates():
-    package = config['pylons.package']
-    return find_templates_in_package(package)
+import pkg_resources
 
 def find_templates_in_package(package):
-    m = __import__(package)
-    start_dir = os.path.dirname(m.__file__)
+    start_dir = pkg_resources.resource_filename(package, '')
     r = []
     for (path, dirs, files) in os.walk(start_dir):
         #if this directory is not a module, the template may not be used
 
 def make_template_list():
     r = odict()
-    if config['pylons.package'] != 'c5t.core':
-        r['local'] = find_local_templates()
+    package = config['pylons.package']
+    if package != 'c5t.core':
+        r[package] = find_templates_in_package(package)
     r['c5t'] = find_templates_in_package('c5t.core')
     return r

c5t/core/model/content.py

     acl = FieldProperty(s.Array(s.String), if_missing=[])
     parent_id = FieldProperty(s.ObjectId, if_missing=None)
     status = FieldProperty(s.OneOf('draft', 'reviewed', 'published', 'removed'), if_missing='published')
-    
+
     @property
     def parent(self):
         return Page.query.get(_id=self.parent_id)
-    
+
     @property
     def children(self):
         return self.query.find_by(parent_id=self._id)
     slug = FieldProperty(s.String)
     docstring = FieldProperty(s.String) # What is this for? If you know, please tell me. -chbrown
     template = FieldProperty(s.String) # This is a "python dotted notation path" that is package.templates.template_name
-    
+
     def path(self):
         path = []
         current = self
     @property
     def files(self):
         return File.query.find_by(parent_id=self._id)
-        
+
     @property
     def news(self):
         return News.query.find_by(parent_id=self._id)
     @property
     def slug_or_id(self):
         return self.slug or str(self._id)
-        
-    
+
+
     # @classmethod
     # def by_slug(cls, slug):
     #     return cls.query.get(slug=slug)
     class __mongometa__:
         session = main_orm_session
         name = 'file'
-    
+
     # file_name = FieldProperty(s.String) -> title
     # file_type = FieldProperty(s.String) -> format
     description = FieldProperty(s.String)
-    # :attr:`public` may look like it's a duplicate of Page.status, 
+    # :attr:`public` may look like it's a duplicate of Page.status,
     # but it actually controls where the file is put in the file system
     public = FieldProperty(s.Bool, if_missing=True)
-    
+
     def path(self):
         return (self.public and '/files/%s/%s' or '/%s/files/%s') % (self.parent.path(), self.title)
-            
+
     def filepath(self):
         short_path = '%s/%s' % (self.parent.path(), self.title)
-        
+
         if self.public:
             return os.path.join(config['files.public.path'], short_path)
         else:
             return os.path.join(config['files.private.path'], short_path)
-            
+
 class Image(File):
     class __mongometa__:
         session = main_orm_session

c5t/core/templates/page/edit.mak

 <%inherit file="local:templates.master"/>
 <%namespace file="local:templates.util.editor" import="head"/>
 
+<%
+from c5t.core.lib.helpers import make_template_list
+%>
+
 <%def name="title()">Editing: ${page.title}</%def>
 
 <form action="." method="POST" id="main">
         <tr><td><label>Template</label></td>
         <td>
             <input type="radio" name="template" checked="checked" value="${page.template}" />${page.template}<br/>
+            <input type="radio" name="template" value="other_template_value">
+                <select name="other_template_value">
+                    %for group, items in make_template_list().iteritems():
+                        <optgroup label="${group}">
+                        %for item in items:
+                            <option value="${item}">${item}</option>
+                        %endfor
+                        </optgroup>
+                    %endfor
+
+                </select><br/>
             <input type="radio" name="template" value="c5t.core.templates.page.default" />Use Default Template<br/>
+
             <input type="radio" name="template" value="custom" />Create Custom Template<br/>
         </td></tr>
         <tr>
 %for file_ in page.files:
 <form method="POST" action="${file_.path()}" id="files">
     <input type="hidden" name="_method" value="DELETE" />
-    <input class="delete" value="delete" type="submit" 
+    <input class="delete" value="delete" type="submit"
         onclick="return confirm('Are you sure you want to delete ${file_.title}?');" />
     <a href="${file_.path()}">${file_.text or file_.title}</a>
 </form>

c5t/core/templates/page/new.mak

             <td><label>Template</label></td>
             <td>
                 <input type="radio" checked="checked" name="template" value="${default_template_name}"/>Use Default Template<br/>
-                <input type="radio" checked="checked" name="template" value="other_template_value">
+                <input type="radio" name="template" value="other_template_value">
                 <select name="other_template_value">
                     %for group, items in make_template_list().iteritems():
                         <optgroup label="${group}">