changeset 58:8fb1abd4178a

Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
author Marcin Kuzminski <marcin@python-blog.com>
date Fri, 09 Apr 2010 03:00:20 +0200
parents e96bc5a01490
children 736078908f37
files pylons_app/controllers/hg.py pylons_app/controllers/repos.py pylons_app/dbmodel/__init__.py pylons_app/model/hg_model.py pylons_app/templates/repos.html
diffstat 5 files changed, 104 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/pylons_app/controllers/hg.py	Fri Apr 09 01:42:48 2010 +0200
+++ b/pylons_app/controllers/hg.py	Fri Apr 09 03:00:20 2010 +0200
@@ -8,14 +8,9 @@
 from mako.template import Template
 from pylons.controllers.util import abort
 from pylons_app.lib.base import BaseController, render
-try:
-    from vcs.backends.hg import get_repositories
-except ImportError:
-    print 'You have to import vcs module'
-from mercurial.util import matchdate, Abort, makedate
-from mercurial.hgweb.common import get_contact
-from mercurial.templatefilters import age
 from operator import itemgetter
+
+from pylons_app.model.hg_model import HgModel
 log = logging.getLogger(__name__)
 
 class HgController(BaseController):
@@ -25,43 +20,10 @@
         c.staticurl = g.statics
 
     def index(self):
-        c.repos_list = []
+        hg_model = HgModel()
+        c.repos_list = list(hg_model.get_repos())
         c.current_sort = request.GET.get('sort', 'name')
         
-        def get_mtime(spath):
-            cl_path = os.path.join(spath, "00changelog.i")
-            if os.path.exists(cl_path):
-                return os.stat(cl_path).st_mtime
-            else:
-                return os.stat(spath).st_mtime   
-        
-        def archivelist(ui, nodeid, url):
-            allowed = g.baseui.configlist("web", "allow_archive", untrusted=True)
-            for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
-                if i[0] in allowed or ui.configbool("web", "allow" + i[0],
-                                                    untrusted=True):
-                    yield {"type" : i[0], "extension": i[1],
-                           "node": nodeid, "url": url}
-
-        for name, r in get_repositories(g.paths[0][0], g.paths[0][1]).items():
-            last_change = (get_mtime(r.spath), makedate()[1])
-            tip = r.changectx('tip')
-            tmp_d = {}
-            tmp_d['name'] = name
-            tmp_d['name_sort'] = tmp_d['name']
-            tmp_d['description'] = r.ui.config('web', 'description', 'Unknown', untrusted=True)
-            tmp_d['description_sort'] = tmp_d['description']
-            tmp_d['last_change'] = age(last_change)
-            tmp_d['last_change_sort'] = last_change[1] - last_change[0]
-            tmp_d['tip'] = str(tip)
-            tmp_d['tip_sort'] = tip.rev()
-            tmp_d['rev'] = tip.rev()
-            tmp_d['contact'] = get_contact(r.ui.config)
-            tmp_d['contact_sort'] = get_contact(r.ui.config)
-            tmp_d['repo_archives'] = archivelist(r.ui, "tip", 'sa')
-            
-            c.repos_list.append(tmp_d)
-        
         cs = c.current_sort
         c.cs_slug = cs.replace('-', '')
         sortables = ['name', 'description', 'last_change', 'tip', 'contact']
--- a/pylons_app/controllers/repos.py	Fri Apr 09 01:42:48 2010 +0200
+++ b/pylons_app/controllers/repos.py	Fri Apr 09 03:00:20 2010 +0200
@@ -1,5 +1,5 @@
 import logging
-
+import os
 from pylons import request, response, session, tmpl_context as c, url, app_globals as g
 from pylons.controllers.util import abort, redirect
 from pylons_app.lib import auth
@@ -7,7 +7,9 @@
 from pylons_app.model import meta
 from pylons_app.model.db import Users, UserLogs
 from pylons_app.lib.auth import authenticate
-
+from pylons_app.model.hg_model import HgModel
+from operator import itemgetter
+import shutil
 log = logging.getLogger(__name__)
 
 class ReposController(BaseController):
@@ -26,6 +28,9 @@
     def index(self, format='html'):
         """GET /repos: All items in the collection"""
         # url('repos')
+        hg_model = HgModel()
+        c.repos_list = list(hg_model.get_repos())
+        c.repos_list.sort(key=itemgetter('name'))
         return render('/repos.html')
     
     def create(self):
@@ -53,6 +58,14 @@
         #    h.form(url('repo', id=ID),
         #           method='delete')
         # url('repo', id=ID)
+        from datetime import datetime
+        path = g.paths[0][1].replace('*', '')
+        rm_path = os.path.join(path, id)
+        log.info("Removing %s", rm_path)
+        shutil.move(os.path.join(rm_path, '.hg'), os.path.join(rm_path, 'rm__.hg'))
+        shutil.move(rm_path, os.path.join(path, 'rm__%s-%s' % (datetime.today(), id)))
+        return redirect(url('repos'))
+        
 
     def show(self, id, format='html'):
         """GET /repos/id: Show a specific item"""
--- a/pylons_app/dbmodel/__init__.py	Fri Apr 09 01:42:48 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy.orm import relation, backref
-from sqlalchemy import ForeignKey, Column, Table, Sequence
-from sqlalchemy.types import *
-from sqlalchemy.databases.mysql import *
-from sqlalchemy.databases.postgres import *
-from pylons_app.model.meta import Base
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pylons_app/model/hg_model.py	Fri Apr 09 03:00:20 2010 +0200
@@ -0,0 +1,72 @@
+#!/usr/bin/env python
+# encoding: utf-8
+#
+# Copyright (c) 2010 marcink.  All rights reserved.
+#
+'''
+Created on Apr 9, 2010
+
+@author: marcink
+'''
+import os
+from pylons_app.lib.base import BaseController
+from pylons import tmpl_context as c, app_globals as g, session, request, config
+from pylons_app.lib import helpers as h
+from mako.template import Template
+from pylons.controllers.util import abort
+from pylons_app.lib.base import BaseController, render
+try:
+    from vcs.backends.hg import get_repositories
+except ImportError:
+    print 'You have to import vcs module'
+from mercurial.util import matchdate, Abort, makedate
+from mercurial.hgweb.common import get_contact
+from mercurial.templatefilters import age
+from operator import itemgetter
+
+class HgModel(object):
+    """
+    Mercurial Model
+    """
+
+
+    def __init__(self):
+        """
+        Constructor
+        """
+        
+
+    def get_mtime(self, spath):
+        cl_path = os.path.join(spath, "00changelog.i")
+        if os.path.exists(cl_path):
+            return os.stat(cl_path).st_mtime
+        else:
+            return os.stat(spath).st_mtime   
+    
+    def archivelist(self, ui, nodeid, url):
+        allowed = g.baseui.configlist("web", "allow_archive", untrusted=True)
+        for i in [('zip', '.zip'), ('gz', '.tar.gz'), ('bz2', '.tar.bz2')]:
+            if i[0] in allowed or ui.configbool("web", "allow" + i[0],
+                                                untrusted=True):
+                yield {"type" : i[0], "extension": i[1],
+                       "node": nodeid, "url": url}
+
+    def get_repos(self):
+        for name, r in get_repositories(g.paths[0][0], g.paths[0][1]).items():
+            last_change = (self.get_mtime(r.spath), makedate()[1])
+            tip = r.changectx('tip')
+            tmp_d = {}
+            tmp_d['name'] = name
+            tmp_d['name_sort'] = tmp_d['name']
+            tmp_d['description'] = r.ui.config('web', 'description', 'Unknown', untrusted=True)
+            tmp_d['description_sort'] = tmp_d['description']
+            tmp_d['last_change'] = age(last_change)
+            tmp_d['last_change_sort'] = last_change[1] - last_change[0]
+            tmp_d['tip'] = str(tip)
+            tmp_d['tip_sort'] = tip.rev()
+            tmp_d['rev'] = tip.rev()
+            tmp_d['contact'] = get_contact(r.ui.config)
+            tmp_d['contact_sort'] = get_contact(r.ui.config)
+            tmp_d['repo_archives'] = self.archivelist(r.ui, "tip", 'sa')
+            
+            yield tmp_d
--- a/pylons_app/templates/repos.html	Fri Apr 09 01:42:48 2010 +0200
+++ b/pylons_app/templates/repos.html	Fri Apr 09 03:00:20 2010 +0200
@@ -24,5 +24,18 @@
     </ul>
 	<div>
         <h2>${_('Mercurial repos')}</h2>
+        <table>
+	        %for cnt,repo in enumerate(c.repos_list):
+	 		<tr class="parity${cnt%2}">
+			    <td><a href="/${repo['name']}">${repo['name']}</a></td>
+		        <td>r${repo['rev']}:${repo['tip']}</td>
+                <td>
+                  ${h.form(url('repo', id=repo['name']),method='delete')}
+                  	${h.submit('remove','remove',class_="submit",onclick="return confirm('Confirm to delete this repository');")}
+                  ${h.end_form()}
+     			</td>
+			</tr>
+			%endfor
+		</table>
     </div>
 </%def>    
\ No newline at end of file