From baaef6860f6ef950d8fce0e346d4f922aba0fb91 Mon Sep 17 00:00:00 2001
From: Marti Raudsepp <marti@juffo.org>
Date: Sat, 26 May 2012 03:29:02 +0300
Subject: [PATCH 2/2] Add basic version navigation support for documentation

Also refactored "devel" version display logic into
DocPage.display_version function.

Requires database modification:
alter table docs add unique (file, version);
---
 media/css/docs.css           |    2 +-
 pgweb/docs/models.py         |    9 +++++++++
 pgweb/docs/views.py          |    8 +++++---
 templates/docs/docspage.html |   17 ++++++++++++++---
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/media/css/docs.css b/media/css/docs.css
index 24857b0..3b272f6 100644
--- a/media/css/docs.css
+++ b/media/css/docs.css
@@ -51,7 +51,7 @@ div.NAVHEADER table {
   padding-bottom: 2px;
 }
 
-#docNav {
+#docNav, #docVersions {
   position: relative;
   text-align: left;
   margin-left: 10px;
diff --git a/pgweb/docs/models.py b/pgweb/docs/models.py
index 4aafde4..5ea1497 100644
--- a/pgweb/docs/models.py
+++ b/pgweb/docs/models.py
@@ -12,8 +12,17 @@ class DocPage(models.Model):
 	title = models.CharField(max_length=256, null=True, blank=True)
 	content = models.TextField(null=True, blank=True)
 
+	def display_version(self):
+		"""Version as used for displaying and in URLs"""
+		if self.version == 0:
+			return 'devel'
+		else:
+			return str(self.version)
+
 	class Meta:
 		db_table = 'docs'
+		# Index file first, because we want to list versions by file
+		unique_together = [('file', 'version')]
 
 class DocComment(PgModel, models.Model):
 	version = models.DecimalField(max_digits=3, decimal_places=1, null=False)
diff --git a/pgweb/docs/views.py b/pgweb/docs/views.py
index d5969a8..aaafd1d 100644
--- a/pgweb/docs/views.py
+++ b/pgweb/docs/views.py
@@ -41,17 +41,19 @@ def docpage(request, version, typ, filename):
 	else:
 		indexname = "index.html"
 
-	page = get_object_or_404(DocPage, version=ver, file="%s.%s" % (filename, extension))
+	fullname = "%s.%s" % (filename, extension)
+	page = get_object_or_404(DocPage, version=ver, file=fullname)
+	versions = DocPage.objects.filter(file=fullname).order_by('-version').only('version', 'file')
 
 	if typ=="interactive":
-		comments = DocComment.objects.filter(version=ver, file="%s.%s" % (filename, extension), approved=True).order_by('posted_at')
+		comments = DocComment.objects.filter(version=ver, file=fullname, approved=True).order_by('posted_at')
 	else:
 		comments = None
 
 	return render_to_response('docs/docspage.html', {
 		'page': page,
+		'versions': versions,
 		'title': page.title,
-		'doc_nav_version': ver > 0 and ver or "devel",
 		'doc_type': typ,
 		'comments': comments,
 		'can_comment': (typ=="interactive" and ver==currver),
diff --git a/templates/docs/docspage.html b/templates/docs/docspage.html
index d68dafe..2727562 100644
--- a/templates/docs/docspage.html
+++ b/templates/docs/docspage.html
@@ -1,7 +1,7 @@
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" dir="ltr">
 <head>
-    <title>PostgreSQL: Documentation: {{doc_nav_version}}: {{page.title}}</title>
+    <title>PostgreSQL: Documentation: {{page.display_version}}: {{page.title}}</title>
     <style type="text/css" media="screen" title="Normal Text">@import url("/media/css/docs.css");</style>
     <link rel="alternate stylesheet" media="screen" href="/media/css/docs_large.css" type="text/css" title="Large Text" />
     <script type="text/javascript" src="/media/js/styleswitcher.js"></script>
@@ -30,14 +30,25 @@
 <div id="docSearch">
 	<form action="/search/" method="get">
 	<div>
-	<input type="hidden" name="u" value="/docs/{{doc_nav_version}}/">
+	<input type="hidden" name="u" value="/docs/{{page.display_version}}/">
 	<label for="q">Search Documentation:&nbsp;</label><input type="text" id="q" name="q" size="20" onfocus="if( this.value==this.defaultValue ) this.value='';" value="Search" /><input id="submit" type="submit" value="Search" />
 	</div>
 	</form>
         <div id="docTextSize">Text Size: <a href="#" onclick="setActiveStyleSheet('Normal Text'); return false;" onkeypress="return false;" title="Normal Text Size">Normal</a> / <a href="#" onclick="setActiveStyleSheet('Large Text'); return false;" onkeypress="return false;" title="Large Text Size">Large</a></div>
 </div>
 <div id="docNav">
-<a href="/" title="Home">Home</a> &rarr; <a href="/docs" title="Documentation">Documentation</a> &rarr; <a href="/docs/manuals" title="Manuals">Manuals</a> &rarr; <a href="/docs/{{doc_nav_version}}/{{doc_type}}/{{doc_index_filename}}">PostgreSQL {{doc_nav_version}}</a>{%if loaddate%} ({{loaddate|date:"Y-m-d H:i:s"}}){%endif%}
+<a href="/" title="Home">Home</a> &rarr; <a href="/docs" title="Documentation">Documentation</a> &rarr; <a href="/docs/manuals" title="Manuals">Manuals</a> &rarr; <a href="/docs/{{page.display_version}}/{{doc_type}}/{{doc_index_filename}}">PostgreSQL {{page.display_version}}</a>{%if loaddate%} ({{loaddate|date:"Y-m-d H:i:s"}}){%endif%}
+</div>
+<div id="docVersions">
+This page in other versions:
+{% for ver in versions %}
+	{% if not forloop.first %}/{% endif %}
+	{% if ver.version == page.version %}
+	<b>{{ver.display_version}}</b>
+	{% else %}
+	<a href="/docs/{{ver.display_version}}/{{doc_type}}/{{ver.file}}" title="This page in version {{ver.display_version}}">{{ver.display_version}}</a>
+	{% endif %}
+{% endfor %}
 </div>
 </div>
 
-- 
1.7.10.2

