annotate pylons_app/controllers/repos.py @ 247:51434007e21d

proper sorting fix
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 03 Jun 2010 16:01:47 +0200
parents ca80f8c00562
children fb7f066126cc
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
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
5 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
6 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
7 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
8 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
9 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
10 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
11 import shutil
246
ca80f8c00562 Fixed bug in repos, added dependencies and bumped version
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
12 from operator import itemgetter
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
13 log = logging.getLogger(__name__)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
14
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
15 class ReposController(BaseController):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
16 """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
17 # 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
18 # file has a resource setup:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
19 # map.resource('repo', 'repos')
235
fcab58c43ea1 Fixed access to repos and users.
Marcin Kuzminski <marcin@python-works.com>
parents: 220
diff changeset
20 @LoginRequired()
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
21 def __before__(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
22 c.admin_user = session.get('admin_user')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
23 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
24 super(ReposController, self).__before__()
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents: 48
diff changeset
25
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
26 def index(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
27 """GET /repos: All items in the collection"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
28 # url('repos')
246
ca80f8c00562 Fixed bug in repos, added dependencies and bumped version
Marcin Kuzminski <marcin@python-works.com>
parents: 235
diff changeset
29 cached_repo_list = HgModel().get_repos()
247
51434007e21d proper sorting fix
Marcin Kuzminski <marcin@python-works.com>
parents: 246
diff changeset
30 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
31 return render('admin/repos/repos.html')
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
32
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
33 def create(self):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34 """POST /repos: Create a new item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
35 # 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
36 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
37
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 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
39 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
40 #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
41 invalidate_cache('cached_repo_list')
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 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
43 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
44
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 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
46
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 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
48 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
49 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
50 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
51 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
52 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
53
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
54
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
55 def new(self, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
56 """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
57 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
58 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
59
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 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
61
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
62 def update(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
63 """PUT /repos/id: Update an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
64 # 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
65 # <input type="hidden" name="_method" value="PUT" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
66 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
67 # h.form(url('repo', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
68 # method='put')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
69 # url('repo', id=ID)
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
70
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
71 def delete(self, id):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
72 """DELETE /repos/id: Delete an existing item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
73 # 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
74 # <input type="hidden" name="_method" value="DELETE" />
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
75 # Or using helpers:
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
76 # h.form(url('repo', id=ID),
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
77 # method='delete')
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
78 # 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
79 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
80 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
81 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
82 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
83 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
84 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
85
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
86 #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
87 invalidate_cache('cached_repo_list')
140
b5e59e2b5cfe moved cache invalidating to utils, as seperate function. Implemented invalidating in
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
88
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
89 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
90
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
91
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
92 def show(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
93 """GET /repos/id: Show a specific item"""
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
94 # url('repo', id=ID)
220
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
95
47
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
96 def edit(self, id, format='html'):
f6ac79182600 Added rest controllers for repos and users,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
97 """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
98 # url('edit_repo', id=ID)
220
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
99 c.new_repo = id
e6c802a8dede repo edit
Marcin Kuzminski <marcin@python-works.com>
parents: 215
diff changeset
100 return render('admin/repos/repo_edit.html')