##// END OF EJS Templates
New repo model create function...
marcink -
r2524:9d4b8074 beta
parent child Browse files
Show More
@@ -146,7 +146,7 b' class ReposController(BaseController):'
146 form_result = RepoForm(repo_groups=c.repo_groups_choices,
146 form_result = RepoForm(repo_groups=c.repo_groups_choices,
147 landing_revs=c.landing_revs_choices)()\
147 landing_revs=c.landing_revs_choices)()\
148 .to_python(dict(request.POST))
148 .to_python(dict(request.POST))
149 RepoModel().create(form_result, self.rhodecode_user)
149 RepoModel().create(form_result, self.rhodecode_user.user_id)
150 if form_result['clone_uri']:
150 if form_result['clone_uri']:
151 h.flash(_('created repository %s from %s') \
151 h.flash(_('created repository %s from %s') \
152 % (form_result['repo_name'], form_result['clone_uri']),
152 % (form_result['repo_name'], form_result['clone_uri']),
@@ -112,7 +112,7 b' def get_session():'
112 if CELERY_ON:
112 if CELERY_ON:
113 engine = engine_from_config(config, 'sqlalchemy.db1.')
113 engine = engine_from_config(config, 'sqlalchemy.db1.')
114 init_model(engine)
114 init_model(engine)
115 sa = meta.Session
115 sa = meta.Session()
116 return sa
116 return sa
117
117
118
118
@@ -373,7 +373,7 b' def create_repo_fork(form_data, cur_user'
373
373
374 base_path = Repository.base_path()
374 base_path = Repository.base_path()
375
375
376 fork_repo = RepoModel(DBS).create(form_data, cur_user,
376 fork_repo = RepoModel(DBS).create(form_data, cur_user.user_id,
377 just_db=True, fork=True)
377 just_db=True, fork=True)
378
378
379 alias = form_data['repo_type']
379 alias = form_data['repo_type']
@@ -49,6 +49,7 b' log = logging.getLogger(__name__)'
49 class RepoModel(BaseModel):
49 class RepoModel(BaseModel):
50
50
51 cls = Repository
51 cls = Repository
52 URL_SEPARATOR = Repository.url_sep()
52
53
53 def __get_users_group(self, users_group):
54 def __get_users_group(self, users_group):
54 return self._get_instance(UsersGroup, users_group,
55 return self._get_instance(UsersGroup, users_group,
@@ -203,37 +204,38 b' class RepoModel(BaseModel):'
203 log.error(traceback.format_exc())
204 log.error(traceback.format_exc())
204 raise
205 raise
205
206
206 def create(self, form_data, cur_user, just_db=False, fork=False):
207 def create_repo(self, repo_name, repo_type, description, owner,
208 private=False, clone_uri=None, repos_group=None,
209 landing_rev='tip', just_db=False, fork_of=None,
210 copy_fork_permissions=False):
207 from rhodecode.model.scm import ScmModel
211 from rhodecode.model.scm import ScmModel
208
212
213 owner = self._get_user(owner)
214 fork_of = self._get_repo(fork_of)
215 repo_group = self.__get_repos_group(repos_group)
209 try:
216 try:
210 if fork:
211 fork_parent_id = form_data['fork_parent_id']
212
217
213 # repo name is just a name of repository
218 # repo name is just a name of repository
214 # while repo_name_full is a full qualified name that is combined
219 # while repo_name_full is a full qualified name that is combined
215 # with name and path of group
220 # with name and path of group
216 repo_name = form_data['repo_name']
221 repo_name = repo_name.split(self.URL_SEPARATOR)[-1]
217 repo_name_full = form_data['repo_name_full']
222 repo_name_full = repo_name
218
223
219 new_repo = Repository()
224 new_repo = Repository()
220 new_repo.enable_statistics = False
225 new_repo.enable_statistics = False
226 new_repo.repo_name = repo_name_full
227 new_repo.repo_type = repo_type
228 new_repo.user = owner
229 new_repo.group = repo_group
230 new_repo.description = description or repo_name
231 new_repo.private = private
232 new_repo.clone_uri = clone_uri
233 new_repo.landing_rev = landing_rev
221
234
222 for k, v in form_data.items():
235 if fork_of:
223 if k == 'repo_name':
236 parent_repo = fork_of
224 v = repo_name_full
225 if k == 'repo_group':
226 k = 'group_id'
227 if k == 'description':
228 v = v or repo_name
229
230 setattr(new_repo, k, v)
231
232 if fork:
233 parent_repo = Repository.get(fork_parent_id)
234 new_repo.fork = parent_repo
237 new_repo.fork = parent_repo
235
238
236 new_repo.user_id = cur_user.user_id
237 self.sa.add(new_repo)
239 self.sa.add(new_repo)
238
240
239 def _create_default_perms():
241 def _create_default_perms():
@@ -245,7 +247,7 b' class RepoModel(BaseModel):'
245 default = p.permission.permission_name
247 default = p.permission.permission_name
246 break
248 break
247
249
248 default_perm = 'repository.none' if form_data['private'] else default
250 default_perm = 'repository.none' if private else default
249
251
250 repo_to_perm.permission_id = self.sa.query(Permission)\
252 repo_to_perm.permission_id = self.sa.query(Permission)\
251 .filter(Permission.permission_name == default_perm)\
253 .filter(Permission.permission_name == default_perm)\
@@ -256,9 +258,9 b' class RepoModel(BaseModel):'
256
258
257 self.sa.add(repo_to_perm)
259 self.sa.add(repo_to_perm)
258
260
259 if fork:
261 if fork_of:
260 if form_data.get('copy_permissions'):
262 if copy_fork_permissions:
261 repo = Repository.get(fork_parent_id)
263 repo = fork_of
262 user_perms = UserRepoToPerm.query()\
264 user_perms = UserRepoToPerm.query()\
263 .filter(UserRepoToPerm.repository == repo).all()
265 .filter(UserRepoToPerm.repository == repo).all()
264 group_perms = UsersGroupRepoToPerm.query()\
266 group_perms = UsersGroupRepoToPerm.query()\
@@ -277,20 +279,37 b' class RepoModel(BaseModel):'
277 _create_default_perms()
279 _create_default_perms()
278
280
279 if not just_db:
281 if not just_db:
280 self.__create_repo(repo_name, form_data['repo_type'],
282 self.__create_repo(repo_name, repo_type,
281 form_data['repo_group'],
283 repo_group,
282 form_data['clone_uri'])
284 clone_uri)
283 log_create_repository(new_repo.get_dict(),
285 log_create_repository(new_repo.get_dict(),
284 created_by=cur_user.username)
286 created_by=owner.username)
285
287
286 # now automatically start following this repository as owner
288 # now automatically start following this repository as owner
287 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
289 ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
288 cur_user.user_id)
290 owner.user_id)
289 return new_repo
291 return new_repo
290 except:
292 except:
291 log.error(traceback.format_exc())
293 log.error(traceback.format_exc())
292 raise
294 raise
293
295
296 def create(self, form_data, cur_user, just_db=False, fork=None):
297
298 repo_name = form_data['repo_name_full']
299 repo_type = form_data['repo_type']
300 description = form_data['description']
301 owner = cur_user
302 private = form_data['private']
303 clone_uri = form_data.get('clone_uri')
304 repos_group = form_data['repo_group']
305 landing_rev = form_data['landing_rev']
306 copy_fork_permissions = form_data.get('copy_permissions')
307 fork_of = form_data.get('fork_parent_id')
308 return self.create_repo(
309 repo_name, repo_type, description, owner, private, clone_uri,
310 repos_group, landing_rev, just_db, fork_of, copy_fork_permissions
311 )
312
294 def create_fork(self, form_data, cur_user):
313 def create_fork(self, form_data, cur_user):
295 """
314 """
296 Simple wrapper into executing celery task for fork creation
315 Simple wrapper into executing celery task for fork creation
@@ -303,12 +322,13 b' class RepoModel(BaseModel):'
303
322
304 def delete(self, repo):
323 def delete(self, repo):
305 repo = self._get_repo(repo)
324 repo = self._get_repo(repo)
306 try:
325 if repo:
307 self.sa.delete(repo)
326 try:
308 self.__delete_repo(repo)
327 self.sa.delete(repo)
309 except:
328 self.__delete_repo(repo)
310 log.error(traceback.format_exc())
329 except:
311 raise
330 log.error(traceback.format_exc())
331 raise
312
332
313 def grant_user_permission(self, repo, user, perm):
333 def grant_user_permission(self, repo, user, perm):
314 """
334 """
@@ -37,6 +37,8 b' log = logging.getLogger(__name__)'
37
37
38 class UsersGroupModel(BaseModel):
38 class UsersGroupModel(BaseModel):
39
39
40 cls = UsersGroup
41
40 def __get_users_group(self, users_group):
42 def __get_users_group(self, users_group):
41 return self._get_instance(UsersGroup, users_group,
43 return self._get_instance(UsersGroup, users_group,
42 callback=UsersGroup.get_by_group_name)
44 callback=UsersGroup.get_by_group_name)
General Comments 0
You need to be logged in to leave comments. Login now