Show More
@@ -1,71 +1,71 b'' | |||
|
1 | 1 | from pylons_app.model.meta import Base |
|
2 | 2 | from sqlalchemy.orm import relation, backref |
|
3 | 3 | from sqlalchemy import * |
|
4 | 4 | from vcs.utils.lazy import LazyProperty |
|
5 | 5 | |
|
6 | 6 | class User(Base): |
|
7 | 7 | __tablename__ = 'users' |
|
8 | 8 | __table_args__ = {'useexisting':True} |
|
9 | 9 | user_id = Column("user_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
10 | 10 | username = Column("username", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
11 | 11 | password = Column("password", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
12 | 12 | active = Column("active", BOOLEAN(), nullable=True, unique=None, default=None) |
|
13 |
admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default= |
|
|
13 | admin = Column("admin", BOOLEAN(), nullable=True, unique=None, default=False) | |
|
14 | 14 | name = Column("name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
15 | 15 | lastname = Column("lastname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
16 | 16 | email = Column("email", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
17 | 17 | last_login = Column("last_login", DATETIME(timezone=False), nullable=True, unique=None, default=None) |
|
18 | 18 | |
|
19 | 19 | user_log = relation('UserLog') |
|
20 | 20 | |
|
21 | 21 | @LazyProperty |
|
22 | 22 | def full_contact(self): |
|
23 | 23 | return '%s %s <%s>' % (self.name, self.lastname, self.email) |
|
24 | 24 | |
|
25 | 25 | def __repr__(self): |
|
26 | 26 | return "<User('%s:%s')>" % (self.user_id, self.username) |
|
27 | 27 | |
|
28 | 28 | class UserLog(Base): |
|
29 | 29 | __tablename__ = 'user_logs' |
|
30 | 30 | __table_args__ = {'useexisting':True} |
|
31 | 31 | user_log_id = Column("user_log_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
32 | 32 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
|
33 | 33 | user_ip = Column("user_ip", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
34 | 34 | repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_name'), nullable=False, unique=None, default=None) |
|
35 | 35 | action = Column("action", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
36 | 36 | action_date = Column("action_date", DATETIME(timezone=False), nullable=True, unique=None, default=None) |
|
37 | 37 | |
|
38 | 38 | user = relation('User') |
|
39 | 39 | |
|
40 | 40 | class Repository(Base): |
|
41 | 41 | __tablename__ = 'repositories' |
|
42 | 42 | __table_args__ = {'useexisting':True} |
|
43 | 43 | repo_name = Column("repo_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None, primary_key=True) |
|
44 | 44 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=False, default=None) |
|
45 | 45 | private = Column("private", BOOLEAN(), nullable=True, unique=None, default=None) |
|
46 | 46 | description = Column("description", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
47 | 47 | |
|
48 | 48 | user = relation('User') |
|
49 | 49 | repo2perm = relation('Repo2Perm', cascade='all') |
|
50 | 50 | |
|
51 | 51 | class Permission(Base): |
|
52 | 52 | __tablename__ = 'permissions' |
|
53 | 53 | __table_args__ = {'useexisting':True} |
|
54 | 54 | permission_id = Column("permission_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
55 | 55 | permission_name = Column("permission_name", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
56 | 56 | permission_longname = Column("permission_longname", TEXT(length=None, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None) |
|
57 | 57 | |
|
58 | 58 | def __repr__(self): |
|
59 | 59 | return "<Permission('%s:%s')>" % (self.permission_id, self.permission_name) |
|
60 | 60 | |
|
61 | 61 | class Repo2Perm(Base): |
|
62 | 62 | __tablename__ = 'repo_to_perm' |
|
63 | 63 | __table_args__ = (UniqueConstraint('user_id', 'permission_id', 'repository'), {'useexisting':True}) |
|
64 | 64 | repo2perm_id = Column("repo2perm_id", INTEGER(), nullable=False, unique=True, default=None, primary_key=True) |
|
65 | 65 | user_id = Column("user_id", INTEGER(), ForeignKey(u'users.user_id'), nullable=False, unique=None, default=None) |
|
66 | 66 | permission_id = Column("permission_id", INTEGER(), ForeignKey(u'permissions.permission_id'), nullable=False, unique=None, default=None) |
|
67 | 67 | repository = Column("repository", TEXT(length=None, convert_unicode=False, assert_unicode=None), ForeignKey(u'repositories.repo_name'), nullable=False, unique=None, default=None) |
|
68 | 68 | |
|
69 | 69 | user = relation('User') |
|
70 | 70 | permission = relation('Permission') |
|
71 | 71 |
@@ -1,156 +1,158 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # encoding: utf-8 |
|
3 | 3 | # Model for hg app |
|
4 | 4 | # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com> |
|
5 | from sqlalchemy.orm import joinedload | |
|
5 | 6 | |
|
6 | 7 | # This program is free software; you can redistribute it and/or |
|
7 | 8 | # modify it under the terms of the GNU General Public License |
|
8 | 9 | # as published by the Free Software Foundation; version 2 |
|
9 | 10 | # of the License or (at your opinion) any later version of the license. |
|
10 | 11 | # |
|
11 | 12 | # This program is distributed in the hope that it will be useful, |
|
12 | 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 | 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 15 | # GNU General Public License for more details. |
|
15 | 16 | # |
|
16 | 17 | # You should have received a copy of the GNU General Public License |
|
17 | 18 | # along with this program; if not, write to the Free Software |
|
18 | 19 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 | 20 | # MA 02110-1301, USA. |
|
20 | 21 | |
|
21 | 22 | """ |
|
22 | 23 | Created on April 9, 2010 |
|
23 | 24 | Model for hg app |
|
24 | 25 | @author: marcink |
|
25 | 26 | """ |
|
26 | 27 | |
|
27 | 28 | from beaker.cache import cache_region |
|
28 | 29 | from mercurial import ui |
|
29 | 30 | from mercurial.hgweb.hgwebdir_mod import findrepos |
|
30 | 31 | from vcs.exceptions import RepositoryError, VCSError |
|
31 | 32 | from pylons_app.model.meta import Session |
|
32 | 33 | from pylons_app.model.db import Repository |
|
33 | 34 | import logging |
|
34 | 35 | import os |
|
35 | 36 | import sys |
|
36 | 37 | log = logging.getLogger(__name__) |
|
37 | 38 | |
|
38 | 39 | try: |
|
39 | 40 | from vcs.backends.hg import MercurialRepository |
|
40 | 41 | except ImportError: |
|
41 | 42 | sys.stderr.write('You have to import vcs module') |
|
42 | 43 | raise Exception('Unable to import vcs') |
|
43 | 44 | |
|
44 | 45 | def _get_repos_cached_initial(app_globals): |
|
45 | 46 | """ |
|
46 | 47 | return cached dict with repos |
|
47 | 48 | """ |
|
48 | 49 | g = app_globals |
|
49 | 50 | return HgModel.repo_scan(g.paths[0][0], g.paths[0][1], g.baseui) |
|
50 | 51 | |
|
51 | 52 | @cache_region('long_term', 'cached_repo_list') |
|
52 | 53 | def _get_repos_cached(): |
|
53 | 54 | """ |
|
54 | 55 | return cached dict with repos |
|
55 | 56 | """ |
|
56 | 57 | log.info('getting all repositories list') |
|
57 | 58 | from pylons import app_globals as g |
|
58 | 59 | return HgModel.repo_scan(g.paths[0][0], g.paths[0][1], g.baseui) |
|
59 | 60 | |
|
60 | 61 | @cache_region('long_term', 'full_changelog') |
|
61 | 62 | def _full_changelog_cached(repo_name): |
|
62 | 63 | log.info('getting full changelog for %s', repo_name) |
|
63 | 64 | return list(reversed(list(HgModel().get_repo(repo_name)))) |
|
64 | 65 | |
|
65 | 66 | class HgModel(object): |
|
66 | 67 | """ |
|
67 | 68 | Mercurial Model |
|
68 | 69 | """ |
|
69 | 70 | |
|
70 | 71 | def __init__(self): |
|
71 | 72 | """ |
|
72 | 73 | Constructor |
|
73 | 74 | """ |
|
74 | 75 | |
|
75 | 76 | @staticmethod |
|
76 | 77 | def repo_scan(repos_prefix, repos_path, baseui): |
|
77 | 78 | """ |
|
78 | 79 | Listing of repositories in given path. This path should not be a |
|
79 | 80 | repository itself. Return a dictionary of repository objects |
|
80 | 81 | :param repos_path: path to directory it could take syntax with |
|
81 | 82 | * or ** for deep recursive displaying repositories |
|
82 | 83 | """ |
|
83 | 84 | sa = Session() |
|
84 | 85 | def check_repo_dir(path): |
|
85 | 86 | """ |
|
86 | 87 | Checks the repository |
|
87 | 88 | :param path: |
|
88 | 89 | """ |
|
89 | 90 | repos_path = path.split('/') |
|
90 | 91 | if repos_path[-1] in ['*', '**']: |
|
91 | 92 | repos_path = repos_path[:-1] |
|
92 | 93 | if repos_path[0] != '/': |
|
93 | 94 | repos_path[0] = '/' |
|
94 | 95 | if not os.path.isdir(os.path.join(*repos_path)): |
|
95 | 96 | raise RepositoryError('Not a valid repository in %s' % path[0][1]) |
|
96 | 97 | if not repos_path.endswith('*'): |
|
97 | 98 | raise VCSError('You need to specify * or ** at the end of path ' |
|
98 | 99 | 'for recursive scanning') |
|
99 | 100 | |
|
100 | 101 | check_repo_dir(repos_path) |
|
101 | 102 | log.info('scanning for repositories in %s', repos_path) |
|
102 | 103 | repos = findrepos([(repos_prefix, repos_path)]) |
|
103 | 104 | if not isinstance(baseui, ui.ui): |
|
104 | 105 | baseui = ui.ui() |
|
105 | 106 | |
|
106 | 107 | repos_list = {} |
|
107 | 108 | for name, path in repos: |
|
108 | 109 | try: |
|
109 | 110 | #name = name.split('/')[-1] |
|
110 | 111 | if repos_list.has_key(name): |
|
111 | 112 | raise RepositoryError('Duplicate repository name %s found in' |
|
112 | 113 | ' %s' % (name, path)) |
|
113 | 114 | else: |
|
114 | 115 | |
|
115 | 116 | repos_list[name] = MercurialRepository(path, baseui=baseui) |
|
116 | 117 | repos_list[name].name = name |
|
117 | 118 | dbrepo = sa.query(Repository).get(name) |
|
118 | 119 | if dbrepo: |
|
120 | repos_list[name].dbrepo = dbrepo | |
|
119 | 121 | repos_list[name].description = dbrepo.description |
|
120 | 122 | repos_list[name].contact = dbrepo.user.full_contact |
|
121 | 123 | except OSError: |
|
122 | 124 | continue |
|
123 | 125 | return repos_list |
|
124 | 126 | |
|
125 | 127 | def get_repos(self): |
|
126 | 128 | for name, repo in _get_repos_cached().items(): |
|
127 | 129 | if repo._get_hidden(): |
|
128 | 130 | #skip hidden web repository |
|
129 | 131 | continue |
|
130 | 132 | |
|
131 | 133 | last_change = repo.last_change |
|
132 | 134 | try: |
|
133 | 135 | tip = repo.get_changeset('tip') |
|
134 | 136 | except RepositoryError: |
|
135 | 137 | from pylons_app.lib.utils import EmptyChangeset |
|
136 | 138 | tip = EmptyChangeset() |
|
137 | 139 | |
|
138 | 140 | tmp_d = {} |
|
139 | 141 | tmp_d['name'] = repo.name |
|
140 | 142 | tmp_d['name_sort'] = tmp_d['name'].lower() |
|
141 | 143 | tmp_d['description'] = repo.description |
|
142 | 144 | tmp_d['description_sort'] = tmp_d['description'] |
|
143 | 145 | tmp_d['last_change'] = last_change |
|
144 | 146 | tmp_d['last_change_sort'] = last_change[1] - last_change[0] |
|
145 | 147 | tmp_d['tip'] = tip.raw_id |
|
146 | 148 | tmp_d['tip_sort'] = tip.revision |
|
147 | 149 | tmp_d['rev'] = tip.revision |
|
148 | 150 | tmp_d['contact'] = repo.contact |
|
149 | 151 | tmp_d['contact_sort'] = tmp_d['contact'] |
|
150 | 152 | tmp_d['repo_archives'] = list(repo._get_archives()) |
|
151 | 153 | tmp_d['last_msg'] = tip.message |
|
152 | ||
|
154 | tmp_d['repo'] = repo | |
|
153 | 155 | yield tmp_d |
|
154 | 156 | |
|
155 | 157 | def get_repo(self, repo_name): |
|
156 | 158 | return _get_repos_cached()[repo_name] |
General Comments 0
You need to be logged in to leave comments.
Login now