Move pygit urlconf into pygit directory. Switch to using {% url %} tag in templates.

portnov [2008-06-11 07:01:13]
Move pygit urlconf into pygit directory. Switch to using {% url %} tag in templates.
Filename
pygit/urls.py
pygit/views.py
templates/all_bugs.html
templates/bug_body.html
templates/document.html
templates/edit_bug.html
templates/edit_document.html
templates/git.html
templates/git_blame.html
templates/git_diff.html
templates/git_tree.html
templates/project_body.html
templates/project_bugs.html
templates/project_docs.html
templates/repo.html
templates/repos.html
templates/user_page.html
urls.py
diff --git a/pygit/urls.py b/pygit/urls.py
new file mode 100644
index 0000000..566d3e1
--- /dev/null
+++ b/pygit/urls.py
@@ -0,0 +1,12 @@
+from django.conf.urls.defaults import *
+
+urlpatterns = patterns('',
+    (r'^$', "pygit.views.all_repos"),
+    (r'^repo/(\d+)/$', 'pygit.views.one_repo'),
+    (r'^repo/(\d+)/(\w+)/$', 'pygit.views.one_repo'),
+    (r'^repo/(\d+)/(\w+)/tree/(\w+)/(.*)/$', 'pygit.views.tree'),
+    (r'^blame/(\d+)/(\w+)/(.*)/$','pygit.views.blame'),
+    (r'^diff/(\d+)/(\w+)/(\w+)/$', 'pygit.views.diff_all'),
+    (r'^diff/(\d+)/(\w+)/(\w+)/(.*)/$', 'pygit.views.diff_file'),
+  )
+
diff --git a/pygit/views.py b/pygit/views.py
index 1801f58..2e137ca 100644
--- a/pygit/views.py
+++ b/pygit/views.py
@@ -71,36 +71,31 @@ def escape(s):
   s1 = s.replace('<','&lt;')
   return s1.replace('>','&gt;')

-def diff(request,rid,branch,cid,path=None):
-  def format_diff(diff):
-    data = diff[1]
-    def format_line(line):
-      span=None
-      if not line:
-        return line
-      if line[0]=='+':
-        span='diff-add'
-      if line[0]=='-':
-        span='diff-del'
-      if line[:2]=='@@':
-        span='diff-at'
-      if span:
-        return "<span class='%s'>%s</span>" % (span,line)
-      else:
-        return line
-    lines = map(escape,data.split('\n'))
-    return diff[0], '\n'.join(map(format_line,lines)), diff[2]
+def format_diff(diff):
+  data = diff[1]
+  def format_line(line):
+    span=None
+    if not line:
+      return line
+    if line[0]=='+':
+      span='diff-add'
+    if line[0]=='-':
+      span='diff-del'
+    if line[:2]=='@@':
+      span='diff-at'
+    if span:
+      return "<span class='%s'>%s</span>" % (span,line)
+    else:
+      return line
+  lines = map(escape,data.split('\n'))
+  return diff[0], '\n'.join(map(format_line,lines)), diff[2]

+def diff_file(request,rid,branch,cid,path):
   message = pygit.commit_message(rid,cid)
   total,files = pygit.commit_stats(rid,cid)
-  if not path:
-    ds = pygit.diff_all(rid,cid)
-    if ds:
-      ds = [(d[0],d[1], files[d[0]]) for d in ds]
-  else:
-    ds = pygit.diff_path(rid,path,cid)
-    if ds:
-      ds[0] = (ds[0][0], ds[0][1], files[path])
+  ds = pygit.diff_path(rid,path,cid)
+  if ds:
+    ds[0] = (ds[0][0], ds[0][1], files[path])
   repo = repo_rid(rid)
   ds = map(format_diff,ds)
   return render_it('git_diff.html',
@@ -112,3 +107,19 @@ def diff(request,rid,branch,cid,path=None):
      'total_stats': total,
      'diffs': ds},
     request)
+
+def diff_all(request,rid,branch,cid):
+  ds = pygit.diff_all(rid,cid)
+  if ds:
+    ds = [(d[0],d[1], files[d[0]]) for d in ds]
+  repo = repo_rid(rid)
+  ds = map(format_diff,ds)
+  return render_it('git_diff.html',
+    {'repo': repo,
+     'message': message,
+     'branch': branch,
+     'path': None,
+     'cid': cid,
+     'total_stats': total,
+     'diffs': ds},
+    request)
diff --git a/templates/all_bugs.html b/templates/all_bugs.html
index 8a52102..135cc39 100644
--- a/templates/all_bugs.html
+++ b/templates/all_bugs.html
@@ -10,7 +10,7 @@
 <div class='bugs'>
 {% for bug in bugs %}
     <div class='bug bug-state-{{bug.status.id}}'>
-      <h3><a href='/bugs/{{bug.id}}/'>{{bug.name}}</a></h3>
+      <h3><a href='{% url mgmt.views.one_bug bug.id %}'>{{bug.name}}</a></h3>
       <div class='project-link'>{{bug.project|link|safe}}</div>
         {% if edit_link %}
           <div class='links'><a href='{{edit_link}}'>Изменить</a></div>
diff --git a/templates/bug_body.html b/templates/bug_body.html
index 5c8fc89..432f8cf 100644
--- a/templates/bug_body.html
+++ b/templates/bug_body.html
@@ -1,7 +1,7 @@
     {% load prj_filters %}
       <div class='bug bug-state-{{bug.status.id}}'>
         <div class='project-link'>{{project|link|safe}}</div>
-        <h3><a href='/bugs/{{bug.id}}/'>Баг #{{bug.id}}: {{bug.name}}</a></h3>
+        <h3><a href='{% url mgmt.views.one_bug bug.id %}'>Баг #{{bug.id}}: {{bug.name}}</a></h3>
         {% if edit_link %}
           <div class='links'><a href='{{edit_link}}'>Изменить</a></div>
         {% endif %}
diff --git a/templates/document.html b/templates/document.html
index 7f39bf6..a3f13cd 100644
--- a/templates/document.html
+++ b/templates/document.html
@@ -11,7 +11,7 @@
 {% endif %}
 <h2>{{document.name}}</h2>

-<p>Проект: <a href='/projects/{{document.project.id}}/'>{{document.project.name}}</a></p>
+<p>Проект: <a href='{% url mgmt.views.one_project document.project.id %}'>{{document.project.name}}</a></p>
 <div class='document'>
   {{document.text}}
 </div>
diff --git a/templates/edit_bug.html b/templates/edit_bug.html
index 1e6fe87..2441571 100644
--- a/templates/edit_bug.html
+++ b/templates/edit_bug.html
@@ -5,7 +5,7 @@
 {% block main %}

 <h2>Редактировать баг #{{bug.id}}</h2>
-<div class='links'><a href='/bugs/{{bug.id}}/'>Посмотреть</a></div>
+<div class='links'><a href='{% url mgmt.views.one_bug bug.id %}'>Посмотреть</a></div>

 <form action='.' method='post'>
   {{form.as_p}}
diff --git a/templates/edit_document.html b/templates/edit_document.html
index 36870b3..7ca3c3b 100644
--- a/templates/edit_document.html
+++ b/templates/edit_document.html
@@ -6,7 +6,7 @@

 <h2>Редактировать документ</h2>
 <div class='links'>
-  <a href='/docs/{{document.id}}'>Посмотреть</a>
+  <a href='{% url mgmt.views.one_document document.id %}'>Посмотреть</a>
 </div>

 <form method='post' action='.'>
diff --git a/templates/git.html b/templates/git.html
index f293572..35898d8 100644
--- a/templates/git.html
+++ b/templates/git.html
@@ -1,7 +1,7 @@
 {% extends "base.html" %}

 {% block main %}
-<div class='links'>Репозиторий: <a href='/git/repo/{{repo.rid}}/'>{{repo.description}}</a></div>
+<div class='links'>Репозиторий: <a href='{% url pygit.views.one_repo repo.rid %}'>{{repo.description}}</a></div>

   {% block git %}
   {% endblock %}
diff --git a/templates/git_blame.html b/templates/git_blame.html
index b434271..f26dbd3 100644
--- a/templates/git_blame.html
+++ b/templates/git_blame.html
@@ -8,7 +8,7 @@
 <table>
   {% for cid,author,dt,message,line in blame %}
   <tr>
-    <td><a href='/git/diff/{{repo.rid}}/{{branch}}/{{cid}}/{{path}}/'>{{cid}}</a></td>
+    <td><a href='{% url pygit.views.diff_file repo.rid,branch,cid,path %}'>{{cid}}</a></td>
     <td>{{author}}</td><td>{{dt}}</td><td>{{message}}</td><td>{{forloop.counter}}</td><td>{{line}}</td>
   </tr>
   {% endfor %}
diff --git a/templates/git_diff.html b/templates/git_diff.html
index de1f8d0..58fb4ed 100644
--- a/templates/git_diff.html
+++ b/templates/git_diff.html
@@ -5,12 +5,12 @@
 {% block git %}

 <h2>Diff: #{{cid}}{% if path %} {{path}}{% endif %}</h2>
-<p><a href='/git/diff/{{repo.rid}}/{{branch}}/{{cid}}/'>Коммит: {{message}}</a></p>
+<p><a href='{% url pygit.views.diff_all repo.rid,branch,cid %}'>Коммит: {{message}}</a></p>
 <p>Всего файлов: {{total_stats.files}}, всего строк: {{total_stats.lines}},
 всего новых строк: {{total_stats.insertions}}, всего удалено строк: {{total_stats.deletions}}</p>
 {% autoescape off %}
 {% for file,diff,stats in diffs %}
-  <h3><a href='/git/blame/{{repo.rid}}/{{branch}}/{{file}}/'>{{file}}</a></h3>
+  <h3><a href='{% url pygit.views.blame repo.rid,branch,file %}'>{{file}}</a></h3>
   <p>Новых строк: {{stats.insertions}}, удалено строк: {{stats.deletions}}</p>
   <code><pre>
   {{diff}}
diff --git a/templates/git_tree.html b/templates/git_tree.html
index 5544958..3338a35 100644
--- a/templates/git_tree.html
+++ b/templates/git_tree.html
@@ -7,10 +7,10 @@

 <ul>
   {% for id,name in dirs %}
-  <li><a href='/git/repo/{{repo.rid}}/{{branch}}/tree/{{id}}/{{path}}/{{name}}/'>{{name}}</a></li>
+  <li><a href='{% url pygit.views.tree repo.rid,branch,id,path %}{{name}}/'>{{name}}</a></li>
   {% endfor %}
   {% for id,name in files %}
-  <li><a href='/git/blame/{{repo.rid}}/{{branch}}/{{path}}/{{name}}/'>{{name}}</a></li>
+  <li><a href='{% url pygit.views.blame repo.rid,branch,path %}{{name}}/'>{{name}}</a></li>
   {% endfor %}
 </ul>

diff --git a/templates/project_body.html b/templates/project_body.html
index 65cf4a2..0ce5dac 100644
--- a/templates/project_body.html
+++ b/templates/project_body.html
@@ -1,10 +1,9 @@
-
     <div class='project'>
-      <p><strong><a href='/projects/{{ project.id }}'>{{ project.name }}</a></strong></p>
+      <p><strong><a href='{% url mgmt.views.one_project project.id %}'>{{ project.name }}</a></strong></p>
       <p><strong>Репозиторий: </strong> {{ project.repo }}</p>
       <p>{{ project.text }}</p>
       <p class='links'>
-        <a href='/projects/{{project.id}}/docs/'>Документация</a>
-        <a href='/projects/{{project.id}}/bugs/'>Баги</a>
+        <a href='{% url mgmt.views.project_documents project.id %}'>Документация</a>
+        <a href='{% url mgmt.views.project_bugs project.id %}'>Баги</a>
       </p>
     </div>
diff --git a/templates/project_bugs.html b/templates/project_bugs.html
index ff481c0..902938e 100644
--- a/templates/project_bugs.html
+++ b/templates/project_bugs.html
@@ -6,7 +6,7 @@

 {% include "project_body.html" %}

-<h3><a href='/bugs/'>Баги:</a></h3>
+<h3><a href='{% url mgmt.views.all_bugs %}'>Баги:</a></h3>

 {% if bugs %}
   <div class='bugs'>
diff --git a/templates/project_docs.html b/templates/project_docs.html
index 6bb1821..332dcb0 100644
--- a/templates/project_docs.html
+++ b/templates/project_docs.html
@@ -9,13 +9,13 @@
   {%if docs%}
     <ul class='documents'>
     {% for doc in docs %}
-      <li><a href='/docs/{{doc.id}}/'>{{doc.name}}</a></li>
+      <li><a href='{% url mgmt.views.one_document doc.id %}'>{{doc.name}}</a></li>
     {% endfor %}
     </ul>
   {%else%}
     <p>Документации пока нет.</p>
   {%endif%}
-  <p><a href='/create/doc/{{project.id}}/'>Создать страницу документации</a></p>
+  <p><a href='{% url mgmt.views.create_document project.id %}'>Создать страницу документации</a></p>

 {% endblock %}

diff --git a/templates/repo.html b/templates/repo.html
index 7fbb919..75193d2 100644
--- a/templates/repo.html
+++ b/templates/repo.html
@@ -9,7 +9,7 @@
 <div class='links'>
   Ветви:
   {% for branch in branches %}
-  <a href='/git/repo/{{rid}}/{{branch}}/'>{{branch}}</a>
+  <a href='{% url pygit.views.one_repo rid %}{{branch}}/'>{{branch}}</a>
   {% endfor %}
 </div>

@@ -21,7 +21,7 @@
 <table>
   {% for id,dt,author,message in commits %}
     <tr>
-      <td><a href='/git/diff/{{rid}}/{{current_branch}}/{{id}}/'>{{id}}</a></td><td>{{dt}}</td><td>{{author}}</td><td>{{message}}</td>
+      <td><a href='{% url pygit.views.diff_all rid,current_branch,id %}'>{{id}}</a></td><td>{{dt}}</td><td>{{author}}</td><td>{{message}}</td>
     </tr>
   {% endfor %}
 </table>
@@ -31,7 +31,7 @@
   <h3>Теги:</h3>
   <ul>
   {% for tag in tags %}
-    <li><a href='/git/diff/{{rid}}/{{current_branch}}/{{tag}}/'>{{tag}}</a></li>
+    <li><a href='{% url pygit.views.diff_all rid,current_branch,tag %}'>{{tag}}</a></li>
   {% endfor %}
   </ul>
 </div>
@@ -41,10 +41,10 @@

 <ul>
   {% for id,name in dirs %}
-  <li><a href='/git/repo/{{rid}}/{{current_branch}}/tree/{{id}}/{{name}}/'>{{name}}</a></li>
+  <li><a href='{% url pygit.views.tree rid,current_branch,id,name %}'>{{name}}</a></li>
   {% endfor %}
   {% for id,name in files %}
-  <li><a href='/git/blame/{{rid}}/{{current_branch}}/{{name}}/'>{{name}}</a></li>
+  <li><a href='{% url pygit.views.blame rid,current_branch,name %}'>{{name}}</a></li>
   {% endfor %}
 </ul>

diff --git a/templates/repos.html b/templates/repos.html
index c4e278f..0457d89 100644
--- a/templates/repos.html
+++ b/templates/repos.html
@@ -8,7 +8,7 @@

 <ul>
   {% for id,name in repos %}
-  <li><a href='/git/repo/{{id}}/'>{{name}}</a></li>
+  <li><a href='{% url pygit.views.one_repo id %}'>{{name}}</a></li>
   {% endfor %}
 </ul>

diff --git a/templates/user_page.html b/templates/user_page.html
index 2506634..8c3ac22 100644
--- a/templates/user_page.html
+++ b/templates/user_page.html
@@ -10,7 +10,7 @@
   <h3>Администрирует проекты:</h3>
   <ul>
   {% for project in projects_admin %}
-    <li><a href='/projects/{{project.id}}/'>{{project.name}}</a></li>
+    <li><a href='{% url mgmt.views.one_project project.id %}'>{{project.name}}</a></li>
   {% endfor %}
   </ul>
 {% endif %}
@@ -19,7 +19,7 @@
   <h3>В команде проектов:</h3>
   <ul>
   {% for project in projects_team %}
-    <li><a href='/projects/{{project.id}}/'>{{project.name}}</a></li>
+    <li><a href='{% url mgmt.views.one_project project.id %}'>{{project.name}}</a></li>
   {% endfor %}
   </ul>
 {% endif %}
@@ -30,7 +30,7 @@
   {% for bug in bugs_resp %}
       <div class='bug bug-state-{{bug.status.id}}'>
         <div class='project-link'>{{bug.project|link|safe}}</div>
-        <h3><a href='/bugs/{{bug.id}}/'>Баг #{{bug.id}}: {{bug.name}}</a></h3>
+        <h3><a href='{% url mgmt.views.one_bug bug.id %}'>Баг #{{bug.id}}: {{bug.name}}</a></h3>
         {% if edit_link %}
           <div class='links'><a href='{{edit_link}}'>Изменить</a></div>
         {% endif %}
diff --git a/urls.py b/urls.py
index 621bb24..437b276 100644
--- a/urls.py
+++ b/urls.py
@@ -15,13 +15,7 @@ urlpatterns = patterns('',
     (r'^create/doc/(\d+)/$', "mgmt.views.create_document"),
     (r'^users/(\w+)/$', 'mgmt.views.user_page'),

-    (r'^git/$', "pygit.views.all_repos"),
-    (r'^git/repo/(\d+)/$', 'pygit.views.one_repo'),
-    (r'^git/repo/(\d+)/(\w+)/$', 'pygit.views.one_repo'),
-    (r'^git/repo/(\d+)/(\w+)/tree/(\w+)/(.*)/$', 'pygit.views.tree'),
-    (r'^git/blame/(\d+)/(\w+)/(.*)/$','pygit.views.blame'),
-    (r'^git/diff/(\d+)/(\w+)/(\w+)/$', 'pygit.views.diff'),
-    (r'^git/diff/(\d+)/(\w+)/(\w+)/(.*)/$', 'pygit.views.diff'),
+    (r'git/', include('pygit.urls')),

     (r'^login/', login),
     (r'^logout/', logout),
ViewGit