changeset 58:173d6fa4069f

Added sections.py, which makes site navigation code simpler and more consistent.
author Atul Varma <varmaa@toolness.com>
date Thu, 06 Mar 2008 16:33:10 -0600
parents 7381ef782c44
children 67fd2e3f8eca
files PersonasBackend/personas/sections.py PersonasBackend/personas/templates/personas/base.html PersonasBackend/personas/templates/personas/edit.html PersonasBackend/personas/templates/personas/list.html PersonasBackend/personas/templates/personas/logged_out.html PersonasBackend/personas/templates/personas/login.html PersonasBackend/personas/urls.py PersonasBackend/personas/views.py PersonasBackend/settings.py
diffstat 9 files changed, 93 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PersonasBackend/personas/sections.py	Thu Mar 06 16:33:10 2008 -0600
@@ -0,0 +1,66 @@
+from django.core import urlresolvers
+from django.contrib.sites.models import Site
+
+class Sections( object ):
+    def __init__( self, *sections ):
+        self.sections = sections
+        self.sidebar_entries = []
+        self.views = {}
+        self.names = {}
+        for section in self.sections:
+            if section.in_sidebar:
+                self.sidebar_entries.append( section )
+            assert section.name not in self.names
+            self.names[section.name] = section
+            assert section.view not in self.views
+            self.views[section.view] = section
+
+            curr_section = section
+            while curr_section.parent_name:
+                curr_parent = self.names[curr_section.parent_name]
+                section.parents.append( curr_parent )
+                curr_section = curr_parent
+
+class Section( object ):
+    def __init__( self, name, view, in_sidebar = False,
+                  parent_name = None ):
+        self.name = name
+        self.view = view
+        self.in_sidebar = in_sidebar
+        self.parent_name = parent_name
+
+        # This will be filled out by the parent Sections object.
+        self.parents = []
+
+    def url( self ):
+        return urlresolvers.reverse( self.view )
+
+sections = Sections(
+    Section( "Home",
+             "PersonasBackend.personas.views.home_view",
+             in_sidebar = True ),
+    Section( "Browse",
+             "PersonasBackend.personas.views.list_view",
+             parent_name = "Home",
+             in_sidebar = True ),
+    Section( "Create",
+             "PersonasBackend.personas.views.new_view",
+             parent_name = "Home",
+             in_sidebar = True ),
+    Section( "Log in",
+             "django.contrib.auth.views.login",
+             parent_name = "Home" ),
+    Section( "Log out",
+             "django.contrib.auth.views.logout",
+             parent_name = "Home" )
+    )
+
+def context( request ):
+    viewfunc, args, kwargs = urlresolvers.resolve( request.path )
+    viewname = ".".join( [viewfunc.__module__,
+                          viewfunc.__name__] )
+    return {
+        "site_name" : Site.objects.get_current().name,
+        "sidebar_entries" : sections.sidebar_entries,
+        "section" : sections.views.get( viewname, None )
+        }
--- a/PersonasBackend/personas/templates/personas/base.html	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/templates/personas/base.html	Thu Mar 06 16:33:10 2008 -0600
@@ -7,8 +7,7 @@
           href="{{ MEDIA_URL }}css/rustico.css" />  
     <link rel="shortcut icon" href="{{ MEDIA_URL }}img/favicon.ico"
           type="image/x-icon" />
-    <title>{% block title %}{{ title }} ::{% endblock %}Firefox
-    Personas</title>
+    <title>{{ section.name }} :: {{ site_name }}</title>
   </head>
   <body>
     <div id="header">
@@ -25,12 +24,11 @@
     <div id="breadcrumbs">
       <div id="breadcrumbs_container">
         <div style="float: left;">
-          <a href="{% url personas-home %}" >Personas Home</a>
-          {% block breadcrumb %}
-          &raquo; {{ title }}
-          {% endblock %}
+          {% for parent in section.parents reversed %}
+          <a href="{{ parent.url }}">{{ parent.name }}</a> &raquo; 
+          {% endfor %}
+          {{ section.name }}
         </div>
-
         <div style="text-align: right; padding-right: 4px;">            
           <a href="https://services.mozilla.com/" >Register</a> | 
           <a href="{% url login %}">Log in</a>
@@ -44,8 +42,7 @@
         <h2>
           <img src="{{ MEDIA_URL }}img/app-icons/personas.png"
                alt="Personas" />
-          Firefox Personas{% block pagetitle %}: 
-          <span>{{ title }}</span>{% endblock %}
+          {{ site_name }}: <span>{{ section.name }}</span>
         </h2>
       </div>
     </div>
@@ -54,9 +51,15 @@
       <div id="sidebar">
         <div class="menu-box">
           <ul>
-            <li><a href="{% url personas-home %}" >Home</a></li>
-            <li><a href="{% url list-personas %}">Browse</a></li>
-            <li><a href="{% url new-persona %}">Create</a></li>
+            {% for entry in sidebar_entries %}
+            {% ifequal entry.name section.name %}
+            <li class="selected">
+              <a href="{{ entry.url }}">{{entry.name}}</a>
+            </li>
+            {% else %}
+            <li><a href="{{ entry.url }}">{{ entry.name }}</a></li>
+            {% endifequal %}
+            {% endfor %}
           </ul>
         </div>
       </div>
--- a/PersonasBackend/personas/templates/personas/edit.html	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/templates/personas/edit.html	Thu Mar 06 16:33:10 2008 -0600
@@ -1,7 +1,5 @@
 {% extends "personas/base.html" %}
 
-{% block title %}Edit a Persona{% endblock %}
-
 {% block content %}
 <form enctype="multipart/form-data" method="post" action=".">
   <table>
--- a/PersonasBackend/personas/templates/personas/list.html	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/templates/personas/list.html	Thu Mar 06 16:33:10 2008 -0600
@@ -1,7 +1,5 @@
 {% extends "personas/base.html" %}
 
-{% block title %}Persona List{% endblock %}
-
 {% block content %}
 {% for persona in personas %}
 <p>Here's a list of all available Personas.  Enjoy!</p>
--- a/PersonasBackend/personas/templates/personas/logged_out.html	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/templates/personas/logged_out.html	Thu Mar 06 16:33:10 2008 -0600
@@ -1,7 +1,5 @@
 {% extends "personas/base.html" %}
 
-{% block title %}{{ title }}{% endblock %}
-
 {% block content %}
 <p>{{ title }}</p>
 {% endblock %}
--- a/PersonasBackend/personas/templates/personas/login.html	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/templates/personas/login.html	Thu Mar 06 16:33:10 2008 -0600
@@ -1,9 +1,5 @@
 {% extends "personas/base.html" %}
 
-{% block title %}Log in ::{% endblock %}
-{% block breadcrumb %}&raquo; Log in{% endblock %}
-{% block pagetitle %}: <span>Log in</span>{% endblock %}
-
 {% block content %}
 {% if form.has_errors %}
 <p>Your username and password didn't match. Please try again.</p>
--- a/PersonasBackend/personas/urls.py	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/urls.py	Thu Mar 06 16:33:10 2008 -0600
@@ -17,7 +17,7 @@
 
     # Personas UI
     url(r'^new/$',
-        'PersonasBackend.personas.views.edit_view',
+        'PersonasBackend.personas.views.new_view',
         name='new-persona'),
     url(r'^(?P<persona_id>\d+)/edit/$',
         'PersonasBackend.personas.views.edit_view',
--- a/PersonasBackend/personas/views.py	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/personas/views.py	Thu Mar 06 16:33:10 2008 -0600
@@ -63,6 +63,9 @@
         filename = time.strftime( filename )
     filedict["filename"] = filename
 
+def new_view( request ):
+    return edit_view( request )
+
 @login_required
 def edit_view( request, persona_id=None ):
     # TODO: Perform permissions check to see if user has
--- a/PersonasBackend/settings.py	Thu Mar 06 09:18:50 2008 -0600
+++ b/PersonasBackend/settings.py	Thu Mar 06 16:33:10 2008 -0600
@@ -87,3 +87,11 @@
     'django.contrib.admin',
     'PersonasBackend.personas',
 )
+
+TEMPLATE_CONTEXT_PROCESSORS = (
+    "django.core.context_processors.auth",
+    "django.core.context_processors.debug",
+    "django.core.context_processors.i18n",
+    "django.core.context_processors.media",
+    "PersonasBackend.personas.sections.context",
+)