Ben Bangert avatar Ben Bangert committed d611e4e

Adding registration page. Fixing up highlighting of snippets

Comments (0)

Files changed (9)

kai/controllers/accounts.py

         c.active_sub = True
 
     def login(self):
-        return render('/accounts/login.mako')
+        return render('/accounts/login.mako')
+    
+    def register(self):
+        return render('/accounts/register.mako')

kai/controllers/snippets.py

-import logging, datetime, re
-import sphinx.directives
+import logging
+import re
+
+from couchdb import ResourceConflict
+from docutils.core import publish_parts
+from formencode import htmlfill
 from pylons import request, response, session, tmpl_context as c
 from pylons.controllers.util import abort, redirect_to
 from pylons.decorators import validate
-from docutils.core import publish_parts
-from couchdb import ResourceConflict
+
+import kai.lib.pygmentsupport
 from kai.lib.base import BaseController, render
-from formencode import htmlfill
-from kai.model import Snippet
-from kai.model import forms
+from kai.model import Snippet, forms
+
 log = logging.getLogger(__name__)
 
 class SnippetsController(BaseController):
-
     def __before__(self):
         c.active_tab = 'Tools'
         c.active_sub = 'Snippets'
         snippets = Snippet.by_date(descending=True, count=20)
         c.snippets = [Snippet.wrap(row.value) for row in snippets]
         return render('snippets/index.mako')
-        
-        
+    
     @validate(forms.AddSnippet(), form='add')
     def add(self):
-        
         """ Simply add a code snippet to the database. """
         
         c.exists = False
         added = False
         
         if hasattr(self, 'form_result'):
-            snippet = Snippet()
-            snippet.title = self.form_result.get('title')
-            snippet.description = self.form_result.get('description')
-            snippet.content = self.form_result.get('content')
-            snippet.type = 'Snippet'
+            snippet = Snippet(**self.form_result)
             snippet.human_id = 1
-            snippet.created = datetime.datetime.now()
             snippet.username = 'Andy-Test - Username'
             
             ## generate the slug
             slug = re.sub('[^A-Za-z0-9_]+', '', slug)
             
             snippet.slug = slug
-            snippet.tags = self.form_result.get('tags')
-            
+            snippet.tags = self.form_result.get('tags')            
             
             if Snippet.exists(snippet.title):
                 c.exists = True
                 return redirect_to('snippet_home')
         
         return render('snippets/add.mako')
-        
+    
     def view(self, id):
         slug = id.lower().strip()
         snippet = Snippet.wrap(Snippet.fetch_snippet(slug))
             'raw_enabled': 0,
             'input_encoding': 'unicode',
         }
-        string = publish_parts(snippet.content, writer_name="html", \
-            settings_overrides=defaults)['html_body']
+        string = publish_parts(snippet.content, writer_name='html',
+                               settings_overrides=defaults)['html_body']
             
-        snippet.content = string
+        c.snippet_content = string
         c.snippet = snippet
-        
         return render('snippets/view.mako')
 import simplejson as json
 import pylons
 
+from kai.model import Human
+
 class BaseController(WSGIController):
+    def _setup(self):
+        """Do basic request setup"""
+        if pylons.session.get('logged_in', False):
+            user = Human.load(pylons.session.get('user_id'))
+            if not user or user.session_id != pylons.session.id:
+                user = 'Anonymous'
+                pylons.session['logged_in'] = False
+        else:
+            user = 'Anonymous'
+        pylons.c.user = user
+    
     def __call__(self, environ, start_response):
         """Invoke the Controller"""
+        self._setup()
+        
         pylons.c.use_minified_assets = asbool(
             pylons.config.get('use_minified_assets', 'false'))
         pylons.c.db = self.db = Database(pylons.config['couchdb_uri'])

kai/lib/pygmentsupport.py

+import docutils.parsers.rst
+from pygments import highlight
+from pygments.lexers import get_lexer_by_name, get_all_lexers
+from pygments.formatters import HtmlFormatter
+
+def code_block( name, arguments, options, content, lineno,
+             content_offset, block_text, state, state_machine ):
+    """
+    The code-block directive provides syntax highlighting for blocks
+    of code.  It is used with the the following syntax::
+
+    .. code-block:: Python
+ 
+        class Test(object):
+            pass
+    
+    The code will be highlighted with the pygments syntax highlighter. It's
+    recommended that you include the appropriate stylesheets when using this
+    highlighter.
+    """
+
+    try:
+        language = arguments[0]
+    except IndexError:
+        language = options.get('language', '')
+    try:
+        linenos = arguments[1]
+    except IndexError:
+        linenos = options.get('linenos', False)
+
+    language = language.lower()
+    if language == 'hypertext':
+        language = 'html'
+    if language == 'pasteini':
+        language = 'ini'
+    content = '\n'.join(content)
+    if not language:
+        if content.strip().startswith('>>>'):
+            language = 'pycon'
+        else:
+            language = 'python'
+    lexer = get_lexer_by_name(language, stripall=True)
+    formatter = HtmlFormatter(linenos=linenos, cssclass="syntax", encoding='utf-8')
+    html = highlight(unicode(content), lexer, formatter).decode('utf-8')
+    raw = docutils.nodes.raw('',html, format = 'html')
+    return [raw]
+
+#code_block.arguments = (1,0,0)
+code_block.arguments = (1,0,0)
+code_block.options = {'linenos':docutils.parsers.rst.directives, 
+                      'language':docutils.parsers.rst.directives.unchanged}
+code_block.content = 1
+  
+# Simply importing this module will make the directive available.
+docutils.parsers.rst.directives.register_directive( 'code-block', code_block )

kai/model/__init__.py

     last_login = DateTimeField(default=datetime.now)
     blog = TextField()
     openid = ListField(TextField())
+    session_id = TextField()
 
 
 class Paste(Document):

kai/model/forms.py

     allow_extra_fields  = False
     filter_extra_fields = True
     
-    title               = validators.UnicodeString(not_empty=True)
-    description         = validators.UnicodeString(not_empty=True)
-    content             = validators.UnicodeString(not_empty=True)
-    tags                = validators.UnicodeString()
+    title = validators.UnicodeString(not_empty=True)
+    description = validators.UnicodeString(not_empty=True)
+    content = validators.UnicodeString(not_empty=True)
+    tags = validators.UnicodeString()

kai/templates/snippets/layout.mako

-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
-    <title>${self.title()}</title>
-    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
-    ${self.styles()}
-</head>
-<body>
-    <div id="doc4"${self.yui_class()}>
-        <div id="hd">
-            <h1 id="logo" class="replace">PylonsHQ<span>.</span></h1>
-            ${nav(getattr(c, 'active_tab', None), getattr(c, 'active_sub', None))}
-        </div>
-        <div id="bd">
-            % if request.environ['pylons.routes_dict']['controller'] != 'accounts':
-            <div id="loginbar">
-                ${h.link_to('Login', url=url('account_login'))} or 
-                ${h.link_to('Register', url=url('account_register'))}
-            </div>
-            % endif
-            ${next.body()}
-            ## Load Javascripts and such at the end
-            ${self.more_body()}
-        </div>
-        <div id="ft">
-            <p>Powered by Pylons - <a href="#">Contact Administrators</a></p>
-        </div>
-    </div>
-    ${self.javascript()}
-</body>
-</html>
-<%def name="yui_class()"></%def>
-<%def name="more_body()"></%def>
-<%def name="title()">PylonsHQ</%def>
-<%def name="styles()">
-% if c.use_minified_assets:
-    ${h.stylesheet_link('/css/phq-06202008.1-min.css')}
-% else:
-    ${h.stylesheet_link(*h.load_stylesheet_assets())}
-%endif
-</%def>
-<%def name="javascript()">
-<script type="text/javascript" src="/javascripts/jquery-1.2.6.min.js" charset="utf-8"></script>
-<script type="text/javascript" src="/javascripts/jquery.blockUI.js" charset="utf-8"></script>
-<script type="text/javascript" src="/javascripts/behavior.js" charset="utf-8"></script>
-</%def>
-<%def name="nav(tab, sub)">
-<div id="nav">\
-<%
-    active_tab = {}
-    active_tab[tab or 'Home'] = ' class="selected"'
-    active_sub = {}
-    active_sub[sub or 'Overview'] = ' class="selected"'
-%>\
-  <ul class="clearfix">
-    <li id="nav-1"${active_tab.get('Home', '') | n}>${h.link_to('Home', url=url('home'))}
-      <ul>
-        <li${active_sub.get('Overview', '') | n}>${h.link_to('Overview', url=url('home'))}</li>
-        <li${active_sub.get('Features', '') | n}><a href="#">Features</a></li>
-        <li${active_sub.get('History', '') | n}><a href="#">History</a></li>
-        <li${active_sub.get('The Team', '') | n}><a href="#">The Team</a></li>
-      </ul>
-    </li>
-    <li id="nav-2"${active_tab.get('Documentation', '') | n}>${h.link_to('Documentation', url=url('doc_home'))}
-      <ul>
-        <li${active_sub.get('Reference', '') | n}>${h.link_to('Reference', url=url('doc_home'))}</li>
-        <li${active_sub.get('FAQ', '') | n}><a href="#">FAQ</a></li>
-        <li${active_sub.get('Modules', '') | n}>${h.link_to('Modules', url=url('doc_view', url='modules/'))}</li>
-        <li${active_sub.get('Glossary', '') | n}>${h.link_to('Glossary', url=url('doc_view', url='glossary/'))}</li>
-        <li${active_sub.get('Index', '') | n}><a href="#">${h.link_to('Index', url=url('doc_view', url='index/'))}</a></li>
-      </ul>
-    </li>
-    <li id="nav-3"${active_tab.get('Community', '') | n}><a href="#">Community</a>
-      <ul>
-        <li${active_sub.get('Blog', '') | n}><a href="#">Blog</a></li>
-        <li${active_sub.get('Wiki', '') | n}>${h.link_to('Wiki', url=url('wiki'))}</li>
-        <li${active_sub.get('Jobs', '') | n}><a href="#">Jobs</a></li>
-        <li${active_sub.get('Sites Using Pylons', '') | n}><a href="#">Sites Using Pylons</a></li>
-        <li${active_sub.get('Aggregator', '') | n}><a href="#">Aggregator</a></li>
-      </ul>
-    </li>
-    <li id="nav-4"${active_tab.get('Tools', '') | n}><a href="#">Tools</a>
-      <ul>
-        <li${active_sub.get('Pastebin', '') | n}><a href="#"> Pastebin</a></li>
-        <li${active_sub.get('Tracebacks', '') | n}>${h.link_to('Tracebacks', url=url('tracebacks'))}</li>
-        <li${active_sub.get('Snippets', '') | n}><a href="#">Snippets</a></li>
-      </ul>
-    </li>
-    <li id="nav-5"${active_tab.get('Code', '') | n}><a href="#">Code</a>
-      <ul>
-        <li${active_sub.get('Contributing', '') | n}><a href="#">Contributing</a></li>
-        <li${active_sub.get('Milestones', '') | n}><a href="#">Milestones</a></li>
-        <li${active_sub.get('View Tickets', '') | n}><a href="#">View Tickets</a></li>
-        <li${active_sub.get('Source', '') | n}><a href="#">Source</a></li>
-        <li${active_sub.get('Releases', '') | n}><a href="#">Releases</a></li>
-        <li${active_sub.get('Buildbots', '') | n}>${h.link_to('Buildbots', url=url('buildbot', action='index'))}</li>
-      </ul>
-    </li>
-  </ul>
-</div>
-</%def>

kai/templates/snippets/view.mako

 </%def> 
 
 <div class="yui-b content">
-<div class="yui-b">
     <h1>${c.snippet.title}</h1>
     <p>${c.snippet.description|n}</p>
-	<p>${c.snippet.content|n}</p>
-
-</div>
+	<p>${c.snippet_content|n}</p>
 </div>
 
 
     author='Ben Bangert',
     author_email='ben@groovie.org',
     install_requires=[
-        "Pylons>=0.9.6",
-        "Mako", "CouchDB>=0.4",
+        "Pylons>=0.9.7rc2", "Mako", "CouchDB>=0.4", "python-openid>=2.2.1",
     ],
     setup_requires=["PasteScript==dev,>=1.6.3dev-r7326"],
     packages=find_packages(exclude=['ez_setup']),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.