1. Blaise Laflamme
  2. kai

Commits

Ben Bangert  committed d960ef9

Better snippet setup with layout

  • Participants
  • Parent commits c563a29
  • Branches default

Comments (0)

Files changed (10)

File kai/config/routing.py

View file
     map.connect('snippet_add', '/snippets/add', controller='snippets', action='add')
     map.connect('snippet_view', '/snippets/view/{id}', controller='snippets', action='view')
     map.connect('snippet_author', '/snippets/by_author/{id}', controller='snippets', action='by_author')
-    map.connect('/snippets/by_tag/{tag}', controller='snippets', action='by_tag')
+    map.connect('snippet_tag', '/snippets/by_tag/{tag}', controller='snippets', action='by_tag')
     
     # Resources
     map.resource('traceback', 'tracebacks', member={'reown':'GET'})

File kai/controllers/snippets.py

View file
     @validate(forms.AddSnippet(), form='add')
     def add(self):
         """ Simply add a code snippet to the database. """
+        if not c.user:
+            abort(401)
         
         c.exists = False
         c.add_error = None

File kai/model/snippet.py

View file
     reported = BooleanField(default=False)
     reported_by = TextField() # human_id
     
+    by_tag = View('snippets', '''
+        function(doc) {
+          if (doc.type == 'Snippet' && doc.tags) {
+            for (var idx in doc.tags) {
+              emit(doc.tags[idx], null)
+            }
+          }
+        }''', include_docs=True)
+    
     by_title = View('snippets', '''
         function(doc) {
           if (doc.type == 'Snippet' && doc.title) {

File kai/templates/snippets/add.mako

View file
-<%def name="styles()">
-${parent.styles()}
-${h.stylesheet_link('/css/sphinx.css')}
-</%def>
-<div class="yui-b content">
-	<div class="relnav" id="relnav">
-		<a href="#">View By Rating</a> | <a href="${url(controller='snippets', action='by_author')}">View By Author</a> | <a href="#">Search</a>
-	</div>
-    <h1>Snippet Repository</h1>
-    <p>Pylons snippet repository is a place for pylites to share their code snippets with the community. Please only share fully functioning and tested snippets and include instructions so that novice and experienced Pylons users can benefit.</p>
-	
-	% if c.exists:
-		<h3><span class="error-message">A snippet with that title already exists, please choose another title.</span></h3>
-	% endif
-	
-	% if c.add_error:
-		<h3><span class="error-message">An unknown error occurred when adding your snippet. ${c.error}</span></h3>
-	% endif
-	
-	<form action="${url('snippet_add')}" method="POST">
-	<p>Title</p>
-	<input type="text" name="title" value="" id="title" /><br />
-	<br />
-	<p>Description</p>
-	<textarea name="description" id="description" style="width: 50em; height: 5em"></textarea><br />
-	<br />
-	<p>Code</p>
-	<textarea name="content" id="content" style="width: 50em; height: 10em"></textarea><br />
-	<br />
-	<p>Tags</p>
-	<input type="text" name="tags" id="tags" value="" /><br />
-	<br />
-	<input type="submit" value="Add Snippet" />
-	</form>
+<h1>Snippet Repository</h1>
+<p>Pylons snippet repository is a place to share their code snippets with the community.
+    Please only share fully functioning and tested snippets and include instructions so
+    that novice and experienced Pylons users can benefit.</p>
 
-</div>
+% if c.exists:
+    <h3><span class="error-message">A snippet with that title already exists, please choose another title.</span></h3>
+% endif
 
+% if c.add_error:
+    <h3><span class="error-message">An unknown error occurred when adding your snippet. ${c.error}</span></h3>
+% endif
 
-<%def name="title()">${parent.title()} - Home</%def>
-<%inherit file="../layout.mako" />
+<form action="${url('snippet_add')}" method="POST">
+<p>Title</p>
+<input type="text" name="title" value="" id="title" /><br />
+<br />
+<p>Description</p>
+<textarea name="description" id="description" style="width: 50em; height: 5em"></textarea><br />
+<br />
+<p>Code</p>
+<textarea name="content" id="content" style="width: 50em; height: 10em"></textarea><br />
+<br />
+<p>Tags</p>
+<input type="text" name="tags" id="tags" value="" /><br />
+<br />
+<input type="submit" value="Add Snippet" />
+</form>
+<%def name="title()">${parent.title()} - ${_('Add Snippet')}</%def>
+<%inherit file="layout.mako" />

File kai/templates/snippets/byauthor.mako

View file
-<%def name="styles()">
-${parent.styles()}
-${h.stylesheet_link('/css/sphinx.css')}
-</%def>
+% if c.authors:
+<h1>Author List</h1>
+<ul>
+% for author in c.authors:
+    <li>${h.link_to(author['author'], url=url('snippet_author', id=author['id']))} - ${author['amount']} snippets</li>
+% endfor
+</ul>
+% endif 
 
-<div class="yui-b content">
-	<div class="relnav" id="relnav">
-		<a href="${url('snippet_add')}">Add Snippet</a> | <a href="#">View By Rating</a> | <a href="${url(controller='snippets', action='by_author')}">View By Author</a> | <a href="#">Search</a>
-	</div>
-	
-	% if c.authors:
-    <h1>Author List</h1>
-	<ul>
-	% for author in c.authors:
-	    <li>${h.link_to(author['author'], url=url('snippet_author', id=author['id']))} - ${author['amount']} snippets</li>
-	% endfor
-	</ul>
-	% endif 
-	
-	% if c.snippets:
-	<h1>View Snippets for ${c.username}</h1>
-	<ul>
-	% for snippet in c.snippets:
-	    <li>${h.link_to(snippet.title, url=url('snippet_view', id=snippet.slug))} - ${format.datetime(snippet.created, "medium")}</li>
-	% endfor 
-	</ul>
-	% endif
-</div>
+% if c.snippets:
+<h1>View Snippets for ${c.username}</h1>
+<ul>
+% for snippet in c.snippets:
+    <li>${h.link_to(snippet.title, url=url('snippet_view', id=snippet.slug))} - ${format.datetime(snippet.created, "medium")}</li>
+% endfor 
+</ul>
+% endif
 <%def name="title()">${parent.title()} - Home</%def>
-<%inherit file="../layout.mako" />
+<%inherit file="layout.mako" />

File kai/templates/snippets/index.mako

View file
-<%def name="styles()">
-${parent.styles()}
-${h.stylesheet_link('/css/sphinx.css')}
-</%def>
+<h1>Snippet Repository</h1>
+<p>Pylons snippet repository is a place for pylites to share their code snippets with the community. Please only share fully functioning and tested snippets and include instructions so that novice and experienced Pylons users can benefit.</p>
 
-<div class="yui-b content">
-	<div class="relnav" id="relnav">
-		<a href="${url('snippet_add')}">Add Snippet</a> | <a href="#">View By Rating</a> | <a href="${url(controller='snippets', action='by_author')}">View By Author</a> | <a href="#">Search</a>
-	</div>
-    <h1>Snippet Repository</h1>
-    <p>Pylons snippet repository is a place for pylites to share their code snippets with the community. Please only share fully functioning and tested snippets and include instructions so that novice and experienced Pylons users can benefit.</p>
-    
-	<h1>Latest Snippets</h1>
-	<ul>
-	% for snippet in c.snippets:
-	<li><a href="${url(controller='snippets', action='view', id=snippet.slug)}">${snippet.title}</a> - ${snippet.created.strftime('%m/%d/%Y %H:%M:%S')}</li>
-	% endfor
-    </ul>
+<h1>Latest Snippets</h1>
+<ul>
+% for snippet in c.snippets:
+<li><a href="${url(controller='snippets', action='view', id=snippet.slug)}">${snippet.title}</a> - ${snippet.created.strftime('%m/%d/%Y %H:%M:%S')}</li>
+% endfor
+</ul>
 
-	<h1>Latest Authors</h1>
-	<ul>
-	% for author in c.unique_authors:
-	<li><a href="${url(controller='snippets', action='by_author', id=author[1])}">${author[0]}</a></li>
-	% endfor
-    </ul>
-
-</div>
-<%def name="title()">${parent.title()} - Home</%def>
-<%inherit file="../layout.mako" />
+<h1>Latest Authors</h1>
+<ul>
+% for author in c.unique_authors:
+    <li>${h.link_to(author[0], url=url('snippet_author', id=author[1]))}</li>
+% endfor
+</ul>
+<%def name="title()">${parent.title()} - ${_('Snippet Home')}</%def>
+<%inherit file="layout.mako" />

File kai/templates/snippets/layout.mako

View file
+<div class="yui-b content">
+    <div class="relnav" id="relnav">\
+        ${h.link_to('Snippet Home', url=url('snippet_home'))} |
+        % if c.user:
+        ${h.link_to('Add Snippet', url=url('snippet_add'))} | \
+        % endif
+        ${h.link_to('View By Author', url=url(controller='snippets', action='by_author'))}
+    </div>
+    ${next.body()}
+</div>
+<%inherit file="../layout.mako" />
+<%def name="styles()">
+${parent.styles()}
+${h.stylesheet_link('/css/sphinx.css')}
+</%def>

File kai/templates/snippets/view.mako

View file
-<div class="yui-b content">
-    <div class="relnav" id="relnav">
-        <a href="${url('snippet_add')}">Add Snippet</a> | <a href="javascript:void(0);" id="report">Report Snippet</a> | <a href="#">View By Rating</a> | <a href="#">View By Author</a> | <a href="#">Search</a>
-    </div>
-    <h1>${c.snippet.title}</h1>
-    <p>${c.snippet.description|n}</p>
-    <p>${c.snippet_content|n}</p>
-    <p>
-    <h5>Author: ${h.link_to(c.snippet.displayname, url=url('snippet_author', id=c.snippet.human_id))}</h5>
-    <h5>Tags: 
-    % for tag in c.snippet.tags:
-        <a href="${url(controller='snippets', action='by_tag', tag=tag.strip())}">${tag}</a>
-    % endfor
-    </h5>
-    </p>
-</div>
-
+<h1>${c.snippet.title}</h1>
+<p>${c.snippet.description|n}</p>
+<p>${c.snippet_content|n}</p>
+<p>
+<h5>Author: ${h.link_to(c.snippet.displayname, url=url('snippet_author', id=c.snippet.human_id))}</h5>
+<h5>Tags: \
+% for tag in c.snippet.tags:
+    <a href="${url(controller='snippets', action='by_tag', tag=tag.strip())}">${tag}</a>\
+% endfor
+</h5>
+</p>
 <%def name="title()">${parent.title()} - Home</%def>
-<%inherit file="../layout.mako" />
+<%inherit file="layout.mako" />
 <%def name="styles()">
 ${parent.styles()}
 ${h.stylesheet_link('/css/sphinx.css')}

File kai/websetup.py

View file
         Rating.all_raters,
         
         Snippet.by_date, Snippet.by_author, Snippet.by_slug, Snippet.by_title,
-        Snippet.by_author_id,
+        Snippet.by_author_id, Snippet.by_tag,
     ])

File setup.py

View file
     author_email='ben@groovie.org',
     install_requires=[
         "Pylons>=0.9.7rc4", "CouchDB>=0.4", "python-openid>=2.2.1",
-        "pytz>=2008i", "Babel>=0.9.4", "tw.forms==0.9.2",
+        "pytz>=2008i", "Babel>=0.9.4", "tw.forms==0.9.2", "docutils>=0.5",
     ],
     setup_requires=["PasteScript>=1.6.3"],
     packages=find_packages(exclude=['ez_setup']),