##// END OF EJS Templates
docs update for repo model
marcink -
r787:4502ceec beta
parent child Browse files
Show More
@@ -1,247 +1,264 b''
1 1 # -*- coding: utf-8 -*-
2 2 """
3 3 package.rhodecode.model.repo
4 4 ~~~~~~~~~~~~~~
5 5
6 6 Repository model for rhodecode
7 7
8 8 :created_on: Jun 5, 2010
9 9 :author: marcink
10 10 :copyright: (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 13 # This program is free software; you can redistribute it and/or
14 14 # modify it under the terms of the GNU General Public License
15 15 # as published by the Free Software Foundation; version 2
16 16 # of the License or (at your opinion) any later version of the license.
17 17 #
18 18 # This program is distributed in the hope that it will be useful,
19 19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 21 # GNU General Public License for more details.
22 22 #
23 23 # You should have received a copy of the GNU General Public License
24 24 # along with this program; if not, write to the Free Software
25 25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 26 # MA 02110-1301, USA.
27 27 import os
28 28 import shutil
29 29 import logging
30 30 import traceback
31 31 from datetime import datetime
32 32
33 33 from pylons import app_globals as g
34 34
35 35 from rhodecode.model import BaseModel
36 36 from rhodecode.model.caching_query import FromCache
37 37 from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \
38 38 Statistics
39 39 from rhodecode.model.user import UserModel
40 40
41 41 from vcs.backends import get_backend
42 42
43 43 log = logging.getLogger(__name__)
44 44
45 45 class RepoModel(BaseModel):
46 46
47 47 def get(self, repo_id, cache=False):
48 48 repo = self.sa.query(Repository)\
49 49 .filter(Repository.repo_id == repo_id)
50 50
51 51 if cache:
52 52 repo = repo.options(FromCache("sql_cache_short",
53 53 "get_repo_%s" % repo_id))
54 54 return repo.scalar()
55 55
56 56
57 57 def get_by_repo_name(self, repo_name, cache=False):
58 58 repo = self.sa.query(Repository)\
59 59 .filter(Repository.repo_name == repo_name)
60 60
61 61 if cache:
62 62 repo = repo.options(FromCache("sql_cache_short",
63 63 "get_repo_%s" % repo_name))
64 64 return repo.scalar()
65 65
66 66 def get_users_js(self):
67 67
68 68 users = self.sa.query(User).filter(User.active == True).all()
69 69 u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},'''
70 70 users_array = '[%s];' % '\n'.join([u_tmpl % (u.user_id, u.name,
71 71 u.lastname, u.username)
72 72 for u in users])
73 73 return users_array
74 74
75 75
76 76 def update(self, repo_name, form_data):
77 77 try:
78 78
79 79 #update permissions
80 80 for username, perm in form_data['perms_updates']:
81 81 r2p = self.sa.query(RepoToPerm)\
82 82 .filter(RepoToPerm.user == UserModel(self.sa)\
83 83 .get_by_username(username, cache=False))\
84 84 .filter(RepoToPerm.repository == \
85 85 self.get_by_repo_name(repo_name))\
86 86 .one()
87 87
88 88 r2p.permission_id = self.sa.query(Permission).filter(
89 89 Permission.permission_name ==
90 90 perm).one().permission_id
91 91 self.sa.add(r2p)
92 92
93 93 #set new permissions
94 94 for username, perm in form_data['perms_new']:
95 95 r2p = RepoToPerm()
96 96 r2p.repository = self.get_by_repo_name(repo_name)
97 97 r2p.user = UserModel(self.sa).get_by_username(username, cache=False)
98 98
99 99 r2p.permission_id = self.sa.query(Permission).filter(
100 100 Permission.permission_name == perm)\
101 101 .one().permission_id
102 102 self.sa.add(r2p)
103 103
104 104 #update current repo
105 105 cur_repo = self.get_by_repo_name(repo_name, cache=False)
106 106
107 107 for k, v in form_data.items():
108 108 if k == 'user':
109 109 cur_repo.user_id = v
110 110 else:
111 111 setattr(cur_repo, k, v)
112 112
113 113 self.sa.add(cur_repo)
114 114
115 115 if repo_name != form_data['repo_name']:
116 116 #rename our data
117 117 self.__rename_repo(repo_name, form_data['repo_name'])
118 118
119 119 self.sa.commit()
120 120 except:
121 121 log.error(traceback.format_exc())
122 122 self.sa.rollback()
123 123 raise
124 124
125 125 def create(self, form_data, cur_user, just_db=False, fork=False):
126 126 try:
127 127 if fork:
128 128 #force str since hg doesn't go with unicode
129 129 repo_name = str(form_data['fork_name'])
130 130 org_name = str(form_data['repo_name'])
131 131
132 132 else:
133 133 org_name = repo_name = str(form_data['repo_name'])
134 134 new_repo = Repository()
135 135 for k, v in form_data.items():
136 136 if k == 'repo_name':
137 137 v = repo_name
138 138 setattr(new_repo, k, v)
139 139
140 140 if fork:
141 141 parent_repo = self.sa.query(Repository)\
142 142 .filter(Repository.repo_name == org_name).scalar()
143 143 new_repo.fork = parent_repo
144 144
145 145 new_repo.user_id = cur_user.user_id
146 146 self.sa.add(new_repo)
147 147
148 148 #create default permission
149 149 repo_to_perm = RepoToPerm()
150 150 default = 'repository.read'
151 151 for p in UserModel(self.sa).get_by_username('default', cache=False).user_perms:
152 152 if p.permission.permission_name.startswith('repository.'):
153 153 default = p.permission.permission_name
154 154 break
155 155
156 156 default_perm = 'repository.none' if form_data['private'] else default
157 157
158 158 repo_to_perm.permission_id = self.sa.query(Permission)\
159 159 .filter(Permission.permission_name == default_perm)\
160 160 .one().permission_id
161 161
162 162 repo_to_perm.repository_id = new_repo.repo_id
163 163 repo_to_perm.user_id = UserModel(self.sa)\
164 164 .get_by_username('default', cache=False).user_id
165 165
166 166 self.sa.add(repo_to_perm)
167 167 self.sa.commit()
168 168
169 169
170 170 #now automatically start following this repository as owner
171 171 from rhodecode.model.scm import ScmModel
172 172 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
173 173 cur_user.user_id)
174 174
175 175 if not just_db:
176 176 self.__create_repo(repo_name, form_data['repo_type'])
177 177 except:
178 178 log.error(traceback.format_exc())
179 179 self.sa.rollback()
180 180 raise
181 181
182 182 def create_fork(self, form_data, cur_user):
183 183 from rhodecode.lib.celerylib import tasks, run_task
184 184 run_task(tasks.create_repo_fork, form_data, cur_user)
185 185
186 186 def delete(self, repo):
187 187 try:
188 188 self.sa.delete(repo)
189 189 self.__delete_repo(repo)
190 190 self.sa.commit()
191 191 except:
192 192 log.error(traceback.format_exc())
193 193 self.sa.rollback()
194 194 raise
195 195
196 196 def delete_perm_user(self, form_data, repo_name):
197 197 try:
198 198 self.sa.query(RepoToPerm)\
199 199 .filter(RepoToPerm.repository \
200 200 == self.get_by_repo_name(repo_name))\
201 201 .filter(RepoToPerm.user_id == form_data['user_id']).delete()
202 202 self.sa.commit()
203 203 except:
204 204 log.error(traceback.format_exc())
205 205 self.sa.rollback()
206 206 raise
207 207
208 208 def delete_stats(self, repo_name):
209 209 try:
210 210 self.sa.query(Statistics)\
211 211 .filter(Statistics.repository == \
212 212 self.get_by_repo_name(repo_name)).delete()
213 213 self.sa.commit()
214 214 except:
215 215 log.error(traceback.format_exc())
216 216 self.sa.rollback()
217 217 raise
218 218
219 219
220 220 def __create_repo(self, repo_name, alias):
221 """
222 makes repository on filesystem
223 :param repo_name:
224 :param alias:
225 """
221 226 from rhodecode.lib.utils import check_repo
222 227 repo_path = os.path.join(g.base_path, repo_name)
223 228 if check_repo(repo_name, g.base_path):
224 229 log.info('creating repo %s in %s', repo_name, repo_path)
225 230 backend = get_backend(alias)
226 231 backend(repo_path, create=True)
227 232
228 233 def __rename_repo(self, old, new):
234 """
235 renames repository on filesystem
236 :param old: old name
237 :param new: new name
238 """
229 239 log.info('renaming repo from %s to %s', old, new)
230 240
231 241 old_path = os.path.join(g.base_path, old)
232 242 new_path = os.path.join(g.base_path, new)
233 243 if os.path.isdir(new_path):
234 244 raise Exception('Was trying to rename to already existing dir %s',
235 245 new_path)
236 246 shutil.move(old_path, new_path)
237 247
238 248 def __delete_repo(self, repo):
249 """
250 removes repo from filesystem, the removal is acctually made by
251 added rm__ prefix into dir, and rename internat .hg/.git dirs so this
252 repository is no longer valid for rhodecode, can be undeleted later on
253 by reverting the renames on this repository
254 :param repo: repo object
255 """
239 256 rm_path = os.path.join(g.base_path, repo.repo_name)
240 257 log.info("Removing %s", rm_path)
241 258 #disable hg/git
242 259 alias = repo.repo_type
243 260 shutil.move(os.path.join(rm_path, '.%s' % alias),
244 261 os.path.join(rm_path, 'rm__.%s' % alias))
245 262 #disable repo
246 263 shutil.move(rm_path, os.path.join(g.base_path, 'rm__%s__%s' \
247 264 % (datetime.today(), repo.repo_name)))
General Comments 0
You need to be logged in to leave comments. Login now