annotate pylons_app/controllers/repos.py @ 248:fb7f066126cc

Added support for repository located in subdirectories.
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 03 Jun 2010 20:28:46 +0200
parents 51434007e21d
children 3782a6d698af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
1 from pylons import request, response, session, tmpl_context as c, url, \
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
2 app_globals as g
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
3 from pylons.controllers.util import abort, redirect
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 220
diff changeset
4 from pylons_app.lib.auth import LoginRequired
248
fb7f066126cc Added support for repository located in subdirectories.
Marcin Kuzminski <marcin@python-works.com>
parents: 247
diff changeset
5 from pylons.i18n.translation import _
fb7f066126cc Added support for repository located in subdirectories.
Marcin Kuzminski <marcin@python-works.com>
parents: 247
diff changeset
6 from pylons_app.lib import helpers as h
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
7 from pylons_app.lib.base import BaseController, render
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 220
diff changeset
8 from pylons_app.lib.filters import clean_repo
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
9 from pylons_app.lib.utils import check_repo, invalidate_cache
246
ca80f8c00562 Fixed bug in repos, added dependencies and bumped version
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
10 from pylons_app.model.hg_model import HgModel
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
11 import logging
58
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
12 import os
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
13 import shutil
246
ca80f8c00562 Fixed bug in repos, added dependencies and bumped version
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
14 from operator import itemgetter
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
15 log = logging.getLogger(__name__)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
16
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
17 class ReposController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
18 """REST Controller styled on the Atom Publishing Protocol"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
19 # To properly map this controller, ensure your config/routing.py
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
20 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
21 # map.resource('repo', 'repos')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 220
diff changeset
22 @LoginRequired()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
23 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
24 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
25 c.admin_username = session.get('admin_username')
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 169
diff changeset
26 super(ReposController, self).__before__()
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
27
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
28 def index(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
29 """GET /repos: All items in the collection"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
30 # url('repos')
246
ca80f8c00562 Fixed bug in repos, added dependencies and bumped version
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
31 cached_repo_list = HgModel().get_repos()
247
51434007e21d proper sorting fix
Marcin Kuzminski <marcin@python-works.com>
parents: 246
diff changeset
32 c.repos_list = sorted(cached_repo_list, key=itemgetter('name_sort'))
127
20dc7a5eb748 Html changes and cleanups, made folders for html templates, implemented tags and branches pages
Marcin Kuzminski <marcin@python-works.com>
parents: 101
diff changeset
33 return render('admin/repos/repos.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
35 def create(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
36 """POST /repos: Create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
37 # url('repos')
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
38 name = request.POST.get('name')
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
39
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
40 try:
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
41 self._create_repo(name)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
42 #clear our cached list for refresh with new repo
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
43 invalidate_cache('cached_repo_list')
248
fb7f066126cc Added support for repository located in subdirectories.
Marcin Kuzminski <marcin@python-works.com>
parents: 247
diff changeset
44 h.flash(_('created repository %s') % name, category='success')
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
45 except Exception as e:
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
46 log.error(e)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
47
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
48 return redirect('repos')
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
49
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
50 def _create_repo(self, repo_name):
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
51 repo_path = os.path.join(g.base_path, repo_name)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
52 if check_repo(repo_name, g.base_path):
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
53 log.info('creating repo %s in %s', repo_name, repo_path)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
54 from vcs.backends.hg import MercurialRepository
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
55 MercurialRepository(repo_path, create=True)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
56
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
57
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
58 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
59 """GET /repos/new: Form to create a new item"""
215
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
60 new_repo = request.GET.get('repo', '')
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
61 c.new_repo = clean_repo(new_repo)
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
62
70f645fa97cc Moved repo creation to admin/repos, as part of crud controller. Now repo creation is based on a form, which can be auto filled with data from 404 page. Fixed the error controller to properly give the repo name.
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
63 return render('admin/repos/repo_add.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
64
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
65 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
66 """PUT /repos/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
67 # Forms posted to this method should contain a hidden field:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
68 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
69 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
70 # h.form(url('repo', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
71 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
72 # url('repo', id=ID)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
73
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
74 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
75 """DELETE /repos/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
76 # Forms posted to this method should contain a hidden field:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
77 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
78 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
79 # h.form(url('repo', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
80 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
81 # url('repo', id=ID)
58
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
82 from datetime import datetime
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
83 path = g.paths[0][1].replace('*', '')
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
84 rm_path = os.path.join(path, id)
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
85 log.info("Removing %s", rm_path)
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
86 shutil.move(os.path.join(rm_path, '.hg'), os.path.join(rm_path, 'rm__.hg'))
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
87 shutil.move(rm_path, os.path.join(path, 'rm__%s-%s' % (datetime.today(), id)))
140
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
88
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
89 #clear our cached list for refresh with new repo
169
8e01265fb586 added long term caching of repo_list to the base controller. changed hg and repos to use that cached list.
Marcin Kuzminski <marcin@python-works.com>
parents: 151
diff changeset
90 invalidate_cache('cached_repo_list')
248
fb7f066126cc Added support for repository located in subdirectories.
Marcin Kuzminski <marcin@python-works.com>
parents: 247
diff changeset
91 h.flash(_('deleted repository %s') % rm_path, category='success')
58
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
92 return redirect(url('repos'))
8fb1abd4178a Added hg model,implemented removal of repos, added HgModel for fetching repos(with generator)
Marcin Kuzminski <marcin@python-blog.com>
parents: 52
diff changeset
93
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
94
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
95 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
96 """GET /repos/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
97 # url('repo', id=ID)
220
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
98
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
99 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
100 """GET /repos/id/edit: Form to edit an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
101 # url('edit_repo', id=ID)
220
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
102 c.new_repo = id
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
103 return render('admin/repos/repo_edit.html')