In pygit, add view to display last 50 commits in repo.

portnov [2008-06-13 12:11:39]
In pygit, add view to display last 50 commits in repo.
Some fixes.
Filename
pygit/chart.py
pygit/pygit.py
pygit/urls.py
pygit/views.py
templates/repo.html
diff --git a/pygit/chart.py b/pygit/chart.py
index 9b51665..855defd 100644
--- a/pygit/chart.py
+++ b/pygit/chart.py
@@ -42,7 +42,6 @@ class Chart(object):
     self.height = height
     self.surf = cairo.ImageSurface(cairo.FORMAT_ARGB32,width,height)
     self.cx = cairo.Context(self.surf)
-    self.hash = 0
     place_height = float(self.height)/n
     self.places = []
     self.cx.set_source_rgb(1,1,1)
@@ -66,7 +65,6 @@ class Chart(object):
     for i in range(len(lst)):
       pc.line_to(i,lst[i])
     pc.cx.stroke()
-    self.hash += hash(tuple(lst))

   def labels(self,lst):
     step_x = float(self.width-pl-pr)/(len(lst)-1)
@@ -89,12 +87,9 @@ class Chart(object):
       self.cx.move_to(cur_x, int(self.height-py/2))
       self.cx.show_text(label)
       cur_x += step_x
-    self.hash += hash(tuple(lst))

-  def finish(self):
-    path = "/media/charts/chart_%s.png" % self.hash
-    root = "/home/portnov/www/projects"
-    if not exists(root+path):
-      self.surf.write_to_png(root+path)
-    print root+path
+  def finish(self,cache_id):
+    path = "/media/charts/chart_%s.png" % cache_id
+    if not exists(settings.ROOT+path):
+      self.surf.write_to_png(settings.ROOT+path)
     return "<img src='%s'/>" % path
diff --git a/pygit/pygit.py b/pygit/pygit.py
index 5ebec17..ce2a1c5 100644
--- a/pygit/pygit.py
+++ b/pygit/pygit.py
@@ -12,8 +12,8 @@ curr_path = None
 def open_repo(rid):
   global curr_path,rp
   path = settings.REPOS[int(rid)]
-#   if rp and curr_path==path:
-#     return rp
+  if rp and curr_path==path:
+    return rp
   curr_path = path
   rp = Repo(path)
   return rp
@@ -57,9 +57,9 @@ def tarball(rid,branch):
     f.close()
   return path

-def commits(rid,branch='master'):
+def commits(rid,branch='master',n=10):
   r = open_repo(rid)
-  cs = r.commits(branch)
+  cs = r.commits(branch,max_count=n)
   return [(c.id_abbrev,date(c.authored_date),c.author.name,c.message) for c in cs]

 def root_tree(rid,branch='master'):
@@ -120,11 +120,11 @@ def group_by_date(lst):
     if dt == prev:
       group.append(cm)
     else:
-      r.append((dt,group))
+      r.append((prev,group))
       del group
       group = []
     prev = dt
-#   r.append((prev,group))
+  r.append((prev,group))
   return r

 def stats_sum(lst):
diff --git a/pygit/urls.py b/pygit/urls.py
index ef81ac3..d11a535 100644
--- a/pygit/urls.py
+++ b/pygit/urls.py
@@ -5,6 +5,7 @@ urlpatterns = patterns('',
     (r'^repo/(\d+)/$', 'pygit.views.one_repo'),
     (r'^repo/(\d+)/([a-zA-Z0-9_-]+)/$', 'pygit.views.one_repo'),
     (r'^repo/(\d+)/([a-zA-Z0-9_-]+)/tree/(\w+)/(.*)/$', 'pygit.views.tree'),
+    (r'^repo/(\d+)/([a-zA-Z0-9_-]+)/commits/$', 'pygit.views.commits'),
     (r'^blame/(\d+)/([a-zA-Z0-9_-]+)/(.*)/$','pygit.views.blame'),
     (r'^diff/(\d+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$', 'pygit.views.diff_all'),
     (r'^diff/(\d+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)/$', 'pygit.views.diff_file'),
diff --git a/pygit/views.py b/pygit/views.py
index c127c36..79b4080 100644
--- a/pygit/views.py
+++ b/pygit/views.py
@@ -5,6 +5,7 @@ from os.path import dirname,join

 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
+from django.core.cache import cache

 import pygit
 from chart import Chart
@@ -26,6 +27,16 @@ def all_repos(request):
       {'repos': pygit.repos()},
       request)

+
+def cached_stats(rid,branch,cid):
+  r = cache.get('git_stats_%s' % cid)
+  if r:
+    return r
+  else:
+    r = pygit.commits_stats(rid,branch)
+    cache.set('git_stats_%s' % cid, r, 60*60)
+    return r
+
 def one_repo(request,rid,branch='master'):
   if not branch:
     branch = 'master'
@@ -33,18 +44,20 @@ def one_repo(request,rid,branch='master'):
   brs = pygit.branches(rid)
   tags = pygit.tags(rid)
   cs  = pygit.commits(rid,branch)
+  cid = cs[0][0]
   dirs,files = pygit.root_tree(rid,branch)
-  dates,n_commits,files_s,lines,inss,dels = pygit.commits_stats(rid,branch)
+  dates,n_commits,files_s,lines,inss,dels = cached_stats(rid,branch,cid)
+
   cht = Chart(900,200,n=2)
-#   cht.set_scale(0,len(n_commits)-1,max(max(n_commits),max(files_s)))
-  cht.set_scale(1,len(inss)-1,max(inss))
+  cht.set_scale(1,len(inss)-1,max(max(inss),max(dels)))
   cht.draw(n_commits,place=0,color=(0.9,0.9,0.2))
   cht.draw(files_s,place=0,color=(0.3,0.3,0.3))
   cht.draw(inss,place=1,color=(0.2,0.2,0.9))
   cht.draw(dels,place=1,color=(0.9,0.2,0.2))
 #   cht.draw(lines,place=1,color=(0.2,0.9,0.2))
   cht.labels(dates)
-  chart = cht.finish()
+  chart = cht.finish(cid)
+
   return render_it('repo.html',
       {'description': dsc,
        'rid': rid,
@@ -57,6 +70,13 @@ def one_repo(request,rid,branch='master'):
        'files': files},
       request)

+def commits(request,rid,branch='master'):
+  cs = pygit.commits(rid,branch,50)
+  return render_it('git_commits.html',
+      {'repo': repo_rid(rid),
+       'commits': cs},
+      request)
+
 def tree(request,rid,branch,tid,path):
   dirs,files = pygit.tree(rid,tid)
   repo = repo_rid(rid)
diff --git a/templates/repo.html b/templates/repo.html
index e888993..de8b5e7 100644
--- a/templates/repo.html
+++ b/templates/repo.html
@@ -20,16 +20,8 @@
 <h3>Активность:</h3>
 {{activity_chart|safe}}

-<h3>Последние коммиты:</h3>
-
-<table>
-  {% for id,dt,author,message in commits %}
-    <tr>
-      <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>
+<h3><a href='{% url pygit.views.commits rid,current_branch %}'>Последние коммиты:</a></h3>
+{% include "commits_table.html" %}

 {% if tags %}
 <div class='git-tags'>
ViewGit