Files @ 9dae92a65e40
Branch filter:

Location: kallithea/rhodecode/model/repo.py - annotation

Marcin Kuzminski
fixes #288
- added group management into non admin repo settings form
- fixes also issues with groups after API codes merge
- all tests passes ok.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
2889a4446960
2889a4446960
bb35ad076e2f
bb35ad076e2f
2889a4446960
2889a4446960
6832ef664673
2889a4446960
2889a4446960
6832ef664673
2889a4446960
2889a4446960
a671db5bdd58
a671db5bdd58
a671db5bdd58
a671db5bdd58
6832ef664673
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
6832ef664673
7e536d1af60d
a671db5bdd58
2889a4446960
2889a4446960
2889a4446960
2889a4446960
7e536d1af60d
2889a4446960
3fc9183e05dd
405b80e4ccd5
405b80e4ccd5
405b80e4ccd5
405b80e4ccd5
b7563ad4e7ee
b7563ad4e7ee
2889a4446960
2889a4446960
b9bbc0d6e9f3
a7a772ea7b95
7e536d1af60d
2889a4446960
7e536d1af60d
7e536d1af60d
aa7e45ad0cea
89b9037d68b7
7e536d1af60d
405b80e4ccd5
405b80e4ccd5
405b80e4ccd5
405b80e4ccd5
ade3414a8b61
405b80e4ccd5
405b80e4ccd5
d2a840b29858
7e536d1af60d
7e536d1af60d
dbec976d9975
7e536d1af60d
7e536d1af60d
7e536d1af60d
dbec976d9975
7e536d1af60d
7e536d1af60d
dbec976d9975
dbec976d9975
dbec976d9975
dbec976d9975
dbec976d9975
dbec976d9975
dbec976d9975
89b9037d68b7
dbec976d9975
405b80e4ccd5
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
d2a840b29858
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
d2a840b29858
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
9dae92a65e40
7e536d1af60d
7e536d1af60d
d5a71348ee4d
7e536d1af60d
bd604cf75c5a
6fdc3ff65fce
6fdc3ff65fce
6fdc3ff65fce
04027bdb876c
6fdc3ff65fce
6fdc3ff65fce
7e536d1af60d
6fdc3ff65fce
aa7e45ad0cea
aa7e45ad0cea
6fdc3ff65fce
6fdc3ff65fce
aa7e45ad0cea
aa7e45ad0cea
aa7e45ad0cea
aa7e45ad0cea
aa7e45ad0cea
6fdc3ff65fce
6fdc3ff65fce
aa7e45ad0cea
aa7e45ad0cea
6fdc3ff65fce
7e536d1af60d
bd604cf75c5a
6fdc3ff65fce
6fdc3ff65fce
6fdc3ff65fce
6fdc3ff65fce
04027bdb876c
7e536d1af60d
6fdc3ff65fce
aa7e45ad0cea
aa7e45ad0cea
aa7e45ad0cea
6fdc3ff65fce
6fdc3ff65fce
aa7e45ad0cea
6fdc3ff65fce
aa7e45ad0cea
6fdc3ff65fce
aa7e45ad0cea
aa7e45ad0cea
aa7e45ad0cea
6fdc3ff65fce
7e536d1af60d
bd604cf75c5a
7e536d1af60d
7e536d1af60d
04027bdb876c
a7a772ea7b95
bd604cf75c5a
7ca0b584c912
9dae92a65e40
a7a772ea7b95
7e536d1af60d
7e536d1af60d
7e536d1af60d
bd604cf75c5a
bd604cf75c5a
bd604cf75c5a
7e536d1af60d
7e536d1af60d
bd604cf75c5a
a7a772ea7b95
bd604cf75c5a
7e536d1af60d
7e536d1af60d
bd604cf75c5a
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
9c0f5d558789
7e536d1af60d
7e536d1af60d
b7563ad4e7ee
b7563ad4e7ee
b7563ad4e7ee
7e536d1af60d
7e536d1af60d
b7563ad4e7ee
87ca17540603
87ca17540603
7e536d1af60d
f726a939d2d4
7e536d1af60d
7e536d1af60d
9c0f5d558789
9c0f5d558789
9c0f5d558789
9c0f5d558789
eb489bf9d7c7
eb489bf9d7c7
eb489bf9d7c7
142a05597cba
142a05597cba
142a05597cba
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
9c0f5d558789
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
04027bdb876c
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
f42ee60e673b
04027bdb876c
7e536d1af60d
7e536d1af60d
f42ee60e673b
f42ee60e673b
6d0a7284949d
eb489bf9d7c7
6d0a7284949d
f42ee60e673b
7e536d1af60d
2889a4446960
2889a4446960
2889a4446960
2889a4446960
2889a4446960
bd604cf75c5a
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
dff6d5cb8bba
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
dbec976d9975
dbec976d9975
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
7e536d1af60d
65129c332d37
65129c332d37
aa7e45ad0cea
aa7e45ad0cea
65129c332d37
aa7e45ad0cea
65129c332d37
65129c332d37
65129c332d37
65129c332d37
65129c332d37
65129c332d37
65129c332d37
b9bbc0d6e9f3
b9bbc0d6e9f3
b9bbc0d6e9f3
dbec976d9975
dbec976d9975
b9bbc0d6e9f3
b9bbc0d6e9f3
b9bbc0d6e9f3
b9bbc0d6e9f3
b9bbc0d6e9f3
b9bbc0d6e9f3
eb489bf9d7c7
4502ceec31be
87ca17540603
87ca17540603
87ca17540603
6832ef664673
4502ceec31be
4502ceec31be
eb489bf9d7c7
eb489bf9d7c7
4502ceec31be
9dae92a65e40
142a05597cba
eb489bf9d7c7
eb489bf9d7c7
eb489bf9d7c7
eb489bf9d7c7
eb489bf9d7c7
eb489bf9d7c7
b7563ad4e7ee
b7563ad4e7ee
eb489bf9d7c7
9dae92a65e40
6f468ba37650
6f468ba37650
6f468ba37650
b7563ad4e7ee
6f468ba37650
6f468ba37650
6f468ba37650
9dae92a65e40
6f468ba37650
6f468ba37650
6f468ba37650
6f468ba37650
6f468ba37650
7e536d1af60d
b7563ad4e7ee
7e536d1af60d
4502ceec31be
4502ceec31be
6832ef664673
4502ceec31be
4502ceec31be
4502ceec31be
7e536d1af60d
7e536d1af60d
405b80e4ccd5
405b80e4ccd5
7e536d1af60d
7592484e84d3
7592484e84d3
7e536d1af60d
7e536d1af60d
dff6d5cb8bba
4502ceec31be
4502ceec31be
4502ceec31be
4502ceec31be
4502ceec31be
6832ef664673
4502ceec31be
4502ceec31be
405b80e4ccd5
7e536d1af60d
dff6d5cb8bba
dff6d5cb8bba
dff6d5cb8bba
dff6d5cb8bba
7e536d1af60d
405b80e4ccd5
143b37b7b4aa
143b37b7b4aa
143b37b7b4aa
9dae92a65e40
# -*- coding: utf-8 -*-
"""
    rhodecode.model.repo
    ~~~~~~~~~~~~~~~~~~~~

    Repository model for rhodecode

    :created_on: Jun 5, 2010
    :author: marcink
    :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com>
    :license: GPLv3, see COPYING for more details.
"""
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
import logging
import traceback
from datetime import datetime

from sqlalchemy.orm import joinedload, make_transient

from vcs.utils.lazy import LazyProperty
from vcs.backends import get_backend

from rhodecode.lib import safe_str

from rhodecode.model import BaseModel
from rhodecode.model.caching_query import FromCache
from rhodecode.model.db import Repository, RepoToPerm, User, Permission, \
    Statistics, UsersGroup, UsersGroupRepoToPerm, RhodeCodeUi, Group
from rhodecode.model.user import UserModel

log = logging.getLogger(__name__)


class RepoModel(BaseModel):

    @LazyProperty
    def repos_path(self):
        """Get's the repositories root path from database
        """

        q = self.sa.query(RhodeCodeUi).filter(RhodeCodeUi.ui_key == '/').one()
        return q.ui_value

    def get(self, repo_id, cache=False):
        repo = self.sa.query(Repository)\
            .filter(Repository.repo_id == repo_id)

        if cache:
            repo = repo.options(FromCache("sql_cache_short",
                                          "get_repo_%s" % repo_id))
        return repo.scalar()

    def get_by_repo_name(self, repo_name, cache=False):
        repo = self.sa.query(Repository)\
            .filter(Repository.repo_name == repo_name)

        if cache:
            repo = repo.options(FromCache("sql_cache_short",
                                          "get_repo_%s" % repo_name))
        return repo.scalar()


    def get_users_js(self):

        users = self.sa.query(User).filter(User.active == True).all()
        u_tmpl = '''{id:%s, fname:"%s", lname:"%s", nname:"%s"},'''
        users_array = '[%s]' % '\n'.join([u_tmpl % (u.user_id, u.name,
                                                    u.lastname, u.username)
                                        for u in users])
        return users_array

    def get_users_groups_js(self):
        users_groups = self.sa.query(UsersGroup)\
            .filter(UsersGroup.users_group_active == True).all()

        g_tmpl = '''{id:%s, grname:"%s",grmembers:"%s"},'''

        users_groups_array = '[%s]' % '\n'.join([g_tmpl % \
                                    (gr.users_group_id, gr.users_group_name,
                                     len(gr.members))
                                        for gr in users_groups])
        return users_groups_array

    def _get_defaults(self, repo_name):
        """
        Get's information about repository, and returns a dict for 
        usage in forms
        
        :param repo_name:
        """

        repo_info = Repository.get_by_repo_name(repo_name)

        if repo_info is None:
            return None

        defaults = repo_info.get_dict()
        group, repo_name = repo_info.groups_and_repo
        defaults['repo_name'] = repo_name
        defaults['repo_group'] = getattr(group[-1] if group else None,
                                         'group_id', None)

        # fill owner
        if repo_info.user:
            defaults.update({'user': repo_info.user.username})
        else:
            replacement_user = User.query().filter(User.admin ==
                                                   True).first().username
            defaults.update({'user': replacement_user})

        # fill repository users
        for p in repo_info.repo_to_perm:
            defaults.update({'u_perm_%s' % p.user.username:
                             p.permission.permission_name})

        # fill repository groups
        for p in repo_info.users_group_to_perm:
            defaults.update({'g_perm_%s' % p.users_group.users_group_name:
                             p.permission.permission_name})

        return defaults


    def update(self, repo_name, form_data):
        try:
            cur_repo = self.get_by_repo_name(repo_name, cache=False)

            # update permissions
            for member, perm, member_type in form_data['perms_updates']:
                if member_type == 'user':
                    r2p = self.sa.query(RepoToPerm)\
                            .filter(RepoToPerm.user == User.get_by_username(member))\
                            .filter(RepoToPerm.repository == cur_repo)\
                            .one()

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(r2p)
                else:
                    g2p = self.sa.query(UsersGroupRepoToPerm)\
                            .filter(UsersGroupRepoToPerm.users_group ==
                                    UsersGroup.get_by_group_name(member))\
                            .filter(UsersGroupRepoToPerm.repository ==
                                    cur_repo).one()

                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.permission_name ==
                                                perm).scalar()
                    self.sa.add(g2p)

            # set new permissions
            for member, perm, member_type in form_data['perms_new']:
                if member_type == 'user':
                    r2p = RepoToPerm()
                    r2p.repository = cur_repo
                    r2p.user = User.get_by_username(member)

                    r2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(r2p)
                else:
                    g2p = UsersGroupRepoToPerm()
                    g2p.repository = cur_repo
                    g2p.users_group = UsersGroup.get_by_group_name(member)
                    g2p.permission = self.sa.query(Permission)\
                                        .filter(Permission.
                                                permission_name == perm)\
                                                .scalar()
                    self.sa.add(g2p)

            # update current repo
            for k, v in form_data.items():
                if k == 'user':
                    cur_repo.user = User.get_by_username(v)
                elif k == 'repo_name':
                    pass
                elif k == 'repo_group':
                    cur_repo.group = Group.get(v)

                else:
                    setattr(cur_repo, k, v)

            new_name = cur_repo.get_new_name(form_data['repo_name'])
            cur_repo.repo_name = new_name

            self.sa.add(cur_repo)

            if repo_name != new_name:
                # rename repository
                self.__rename_repo(old=repo_name, new=new_name)

            self.sa.commit()
            return cur_repo
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def create(self, form_data, cur_user, just_db=False, fork=False):

        try:
            if fork:
                repo_name = form_data['fork_name']
                org_name = form_data['repo_name']
                org_full_name = org_name

            else:
                org_name = repo_name = form_data['repo_name']
                repo_name_full = form_data['repo_name_full']

            new_repo = Repository()
            new_repo.enable_statistics = False
            for k, v in form_data.items():
                if k == 'repo_name':
                    if fork:
                        v = repo_name
                    else:
                        v = repo_name_full
                if k == 'repo_group':
                    k = 'group_id'

                if k == 'description':
                    v = v or repo_name

                setattr(new_repo, k, v)

            if fork:
                parent_repo = self.sa.query(Repository)\
                        .filter(Repository.repo_name == org_full_name).one()
                new_repo.fork = parent_repo

            new_repo.user_id = cur_user.user_id
            self.sa.add(new_repo)

            #create default permission
            repo_to_perm = RepoToPerm()
            default = 'repository.read'
            for p in User.get_by_username('default').user_perms:
                if p.permission.permission_name.startswith('repository.'):
                    default = p.permission.permission_name
                    break

            default_perm = 'repository.none' if form_data['private'] else default

            repo_to_perm.permission_id = self.sa.query(Permission)\
                    .filter(Permission.permission_name == default_perm)\
                    .one().permission_id

            repo_to_perm.repository = new_repo
            repo_to_perm.user_id = User.get_by_username('default').user_id

            self.sa.add(repo_to_perm)

            if not just_db:
                self.__create_repo(repo_name, form_data['repo_type'],
                                   form_data['repo_group'],
                                   form_data['clone_uri'])

            self.sa.commit()

            #now automatically start following this repository as owner
            from rhodecode.model.scm import ScmModel
            ScmModel(self.sa).toggle_following_repo(new_repo.repo_id,
                                             cur_user.user_id)
            return new_repo
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def create_fork(self, form_data, cur_user):
        from rhodecode.lib.celerylib import tasks, run_task
        run_task(tasks.create_repo_fork, form_data, cur_user)

    def delete(self, repo):
        try:
            self.sa.delete(repo)
            self.__delete_repo(repo)
            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def delete_perm_user(self, form_data, repo_name):
        try:
            self.sa.query(RepoToPerm)\
                .filter(RepoToPerm.repository \
                        == self.get_by_repo_name(repo_name))\
                .filter(RepoToPerm.user_id == form_data['user_id']).delete()
            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def delete_perm_users_group(self, form_data, repo_name):
        try:
            self.sa.query(UsersGroupRepoToPerm)\
                .filter(UsersGroupRepoToPerm.repository \
                        == self.get_by_repo_name(repo_name))\
                .filter(UsersGroupRepoToPerm.users_group_id \
                        == form_data['users_group_id']).delete()
            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def delete_stats(self, repo_name):
        try:
            self.sa.query(Statistics)\
                .filter(Statistics.repository == \
                        self.get_by_repo_name(repo_name)).delete()
            self.sa.commit()
        except:
            log.error(traceback.format_exc())
            self.sa.rollback()
            raise

    def __create_repo(self, repo_name, alias, new_parent_id, clone_uri=False):
        """
        makes repository on filesystem. It's group aware means it'll create
        a repository within a group, and alter the paths accordingly of
        group location

        :param repo_name:
        :param alias:
        :param parent_id:
        :param clone_uri:
        """
        from rhodecode.lib.utils import is_valid_repo, is_valid_repos_group

        if new_parent_id:
            paths = Group.get(new_parent_id).full_path.split(Group.url_sep())
            new_parent_path = os.sep.join(paths)
        else:
            new_parent_path = ''

        repo_path = os.path.join(*map(lambda x:safe_str(x),
                                [self.repos_path, new_parent_path, repo_name]))


        # check if this path is not a repository
        if is_valid_repo(repo_path, self.repos_path):
            raise Exception('This path %s is a valid repository' % repo_path)

        # check if this path is a group
        if is_valid_repos_group(repo_path, self.repos_path):
            raise Exception('This path %s is a valid group' % repo_path)

        log.info('creating repo %s in %s @ %s', repo_name, repo_path,
                 clone_uri)
        backend = get_backend(alias)

        backend(repo_path, create=True, src_url=clone_uri)


    def __rename_repo(self, old, new):
        """
        renames repository on filesystem

        :param old: old name
        :param new: new name
        """
        log.info('renaming repo from %s to %s', old, new)

        old_path = os.path.join(self.repos_path, old)
        new_path = os.path.join(self.repos_path, new)
        if os.path.isdir(new_path):
            raise Exception('Was trying to rename to already existing dir %s' \
            		     % new_path)
        shutil.move(old_path, new_path)

    def __delete_repo(self, repo):
        """
        removes repo from filesystem, the removal is acctually made by
        added rm__ prefix into dir, and rename internat .hg/.git dirs so this
        repository is no longer valid for rhodecode, can be undeleted later on
        by reverting the renames on this repository

        :param repo: repo object
        """
        rm_path = os.path.join(self.repos_path, repo.repo_name)
        log.info("Removing %s", rm_path)
        #disable hg/git
        alias = repo.repo_type
        shutil.move(os.path.join(rm_path, '.%s' % alias),
                    os.path.join(rm_path, 'rm__.%s' % alias))
        #disable repo
        shutil.move(rm_path, os.path.join(self.repos_path, 'rm__%s__%s' \
                                          % (datetime.today()\
                                             .strftime('%Y%m%d_%H%M%S_%f'),
                                            repo.repo_name)))